VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/message.cpp@ 27128

Last change on this file since 27128 was 27128, checked in by vboxsync, 15 years ago

IPRT: Added RTMsgWarning[V] and RTMsgInfo[V].

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/* $Id: message.cpp 27128 2010-03-05 23:49:44Z vboxsync $ */
2/** @file
3 * IPRT - Error reporting to standard error.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include "internal/iprt.h"
35#include <iprt/message.h>
36
37#include <iprt/path.h>
38#include <iprt/string.h>
39#include <iprt/stream.h>
40#include "internal/process.h"
41
42
43static int rtMsgWorker(PRTSTREAM pDst, const char *pszPrefix, const char *pszFormat, va_list va)
44{
45 if ( !*pszFormat
46 || !strcmp(pszFormat, "\n"))
47 RTStrmPrintf(pDst, "\n");
48 else
49 {
50 char *pszMsg;
51 ssize_t cch = RTStrAPrintfV(&pszMsg, pszFormat, va);
52 if (cch >= 0)
53 {
54 /* print it line by line. */
55 char *psz = pszMsg;
56 do
57 {
58 char *pszEnd = strchr(psz, '\n');
59 if (!pszEnd)
60 {
61 RTStrmPrintf(pDst, "%s: %s%s\n", pszPrefix, &g_szrtProcExePath[g_offrtProcName], psz);
62 break;
63 }
64 if (pszEnd == psz)
65 RTStrmPrintf(pDst, "\n");
66 else
67 {
68 *pszEnd = '\0';
69 RTStrmPrintf(pDst, "%s: %s%s\n", pszPrefix, &g_szrtProcExePath[g_offrtProcName], psz);
70 }
71 psz = pszEnd + 1;
72 } while (*psz);
73 RTStrFree(pszMsg);
74 }
75 else
76 {
77 /* Simple fallback for handling out-of-memory conditions. */
78 RTStrmPrintf(pDst, "%s: %s", pszPrefix, &g_szrtProcExePath[g_offrtProcName]);
79 RTStrmPrintfV(pDst, pszFormat, va);
80 if (!strchr(pszFormat, '\n'))
81 RTStrmPrintf(pDst, "\n");
82 }
83 }
84
85 return VINF_SUCCESS;
86}
87
88RTDECL(int) RTMsgError(const char *pszFormat, ...)
89{
90 va_list va;
91 va_start(va, pszFormat);
92 int rc = RTMsgErrorV(pszFormat, va);
93 va_end(va);
94 return rc;
95}
96RT_EXPORT_SYMBOL(RTMsgError);
97
98
99RTDECL(int) RTMsgErrorV(const char *pszFormat, va_list va)
100{
101 return rtMsgWorker(g_pStdErr, " error:", pszFormat, va);
102}
103RT_EXPORT_SYMBOL(RTMsgErrorV);
104
105
106RTDECL(RTEXITCODE) RTMsgErrorExit(RTEXITCODE enmExitCode, const char *pszFormat, ...)
107{
108 va_list va;
109 va_start(va, pszFormat);
110 RTMsgErrorV(pszFormat, va);
111 va_end(va);
112 return enmExitCode;
113}
114RT_EXPORT_SYMBOL(RTMsgErrorExitV);
115
116
117RTDECL(RTEXITCODE) RTMsgErrorExitV(RTEXITCODE enmExitCode, const char *pszFormat, va_list va)
118{
119 RTMsgErrorV(pszFormat, va);
120 return enmExitCode;
121}
122RT_EXPORT_SYMBOL(RTMsgErrorExitV);
123
124
125RTDECL(RTEXITCODE) RTMsgInitFailure(int rcRTR3Init)
126{
127 if ( g_offrtProcName
128 && g_offrtProcName < sizeof(g_szrtProcExePath)
129 && g_szrtProcExePath[0]
130 && g_szrtProcExePath[g_offrtProcName])
131 RTStrmPrintf(g_pStdErr, "%s: fatal error: RTR3Init: %Rrc\n", &g_szrtProcExePath[g_offrtProcName], rcRTR3Init);
132 else
133 RTStrmPrintf(g_pStdErr, "fatal error: RTR3Init: %Rrc\n", rcRTR3Init);
134 return RTEXITCODE_INIT;
135}
136RT_EXPORT_SYMBOL(RTMsgInitFailure);
137
138
139RTDECL(int) RTMsgWarning(const char *pszFormat, ...)
140{
141 va_list va;
142 va_start(va, pszFormat);
143 int rc = RTMsgWarningV(pszFormat, va);
144 va_end(va);
145 return rc;
146}
147RT_EXPORT_SYMBOL(RTMsgInfo);
148
149
150RTDECL(int) RTMsgWarningV(const char *pszFormat, va_list va)
151{
152 return rtMsgWorker(g_pStdErr, " warning:", pszFormat, va);
153}
154RT_EXPORT_SYMBOL(RTMsgWarningV);
155
156
157RTDECL(int) RTMsgInfo(const char *pszFormat, ...)
158{
159 va_list va;
160 va_start(va, pszFormat);
161 int rc = RTMsgInfoV(pszFormat, va);
162 va_end(va);
163 return rc;
164}
165RT_EXPORT_SYMBOL(RTMsgInfo);
166
167
168RTDECL(int) RTMsgInfoV(const char *pszFormat, va_list va)
169{
170 return rtMsgWorker(g_pStdOut, " info:", pszFormat, va);
171}
172RT_EXPORT_SYMBOL(RTMsgInfoV);
173
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette