1 | /* $Id: message.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Error reporting to standard error.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include "internal/iprt.h"
|
---|
31 | #include <iprt/message.h>
|
---|
32 |
|
---|
33 | #include <iprt/path.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include "internal/process.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | static int rtMsgWorker(PRTSTREAM pDst, const char *pszPrefix, const char *pszFormat, va_list va)
|
---|
40 | {
|
---|
41 | if ( !*pszFormat
|
---|
42 | || !strcmp(pszFormat, "\n"))
|
---|
43 | RTStrmPrintf(pDst, "\n");
|
---|
44 | else
|
---|
45 | {
|
---|
46 | char *pszMsg;
|
---|
47 | ssize_t cch = RTStrAPrintfV(&pszMsg, pszFormat, va);
|
---|
48 | if (cch >= 0)
|
---|
49 | {
|
---|
50 | /* print it line by line. */
|
---|
51 | char *psz = pszMsg;
|
---|
52 | do
|
---|
53 | {
|
---|
54 | char *pszEnd = strchr(psz, '\n');
|
---|
55 | if (!pszEnd)
|
---|
56 | {
|
---|
57 | RTStrmPrintf(pDst, "%s: %s%s\n", &g_szrtProcExePath[g_offrtProcName], pszPrefix, psz);
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | if (pszEnd == psz)
|
---|
61 | RTStrmPrintf(pDst, "\n");
|
---|
62 | else
|
---|
63 | {
|
---|
64 | *pszEnd = '\0';
|
---|
65 | RTStrmPrintf(pDst, "%s: %s%s\n", &g_szrtProcExePath[g_offrtProcName], pszPrefix, psz);
|
---|
66 | }
|
---|
67 | psz = pszEnd + 1;
|
---|
68 | } while (*psz);
|
---|
69 | RTStrFree(pszMsg);
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | /* Simple fallback for handling out-of-memory conditions. */
|
---|
74 | RTStrmPrintf(pDst, "%s: %s", &g_szrtProcExePath[g_offrtProcName], pszPrefix);
|
---|
75 | RTStrmPrintfV(pDst, pszFormat, va);
|
---|
76 | if (!strchr(pszFormat, '\n'))
|
---|
77 | RTStrmPrintf(pDst, "\n");
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | }
|
---|
83 |
|
---|
84 | RTDECL(int) RTMsgError(const char *pszFormat, ...)
|
---|
85 | {
|
---|
86 | va_list va;
|
---|
87 | va_start(va, pszFormat);
|
---|
88 | int rc = RTMsgErrorV(pszFormat, va);
|
---|
89 | va_end(va);
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 | RT_EXPORT_SYMBOL(RTMsgError);
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(int) RTMsgErrorV(const char *pszFormat, va_list va)
|
---|
96 | {
|
---|
97 | return rtMsgWorker(g_pStdErr, "error: ", pszFormat, va);
|
---|
98 | }
|
---|
99 | RT_EXPORT_SYMBOL(RTMsgErrorV);
|
---|
100 |
|
---|
101 |
|
---|
102 | RTDECL(RTEXITCODE) RTMsgErrorExit(RTEXITCODE enmExitCode, const char *pszFormat, ...)
|
---|
103 | {
|
---|
104 | va_list va;
|
---|
105 | va_start(va, pszFormat);
|
---|
106 | RTMsgErrorV(pszFormat, va);
|
---|
107 | va_end(va);
|
---|
108 | return enmExitCode;
|
---|
109 | }
|
---|
110 | RT_EXPORT_SYMBOL(RTMsgErrorExitV);
|
---|
111 |
|
---|
112 |
|
---|
113 | RTDECL(RTEXITCODE) RTMsgErrorExitV(RTEXITCODE enmExitCode, const char *pszFormat, va_list va)
|
---|
114 | {
|
---|
115 | RTMsgErrorV(pszFormat, va);
|
---|
116 | return enmExitCode;
|
---|
117 | }
|
---|
118 | RT_EXPORT_SYMBOL(RTMsgErrorExitV);
|
---|
119 |
|
---|
120 |
|
---|
121 | RTDECL(int) RTMsgErrorRc(int rcRet, const char *pszFormat, ...)
|
---|
122 | {
|
---|
123 | va_list va;
|
---|
124 | va_start(va, pszFormat);
|
---|
125 | RTMsgErrorV(pszFormat, va);
|
---|
126 | va_end(va);
|
---|
127 | return rcRet;
|
---|
128 | }
|
---|
129 | RT_EXPORT_SYMBOL(RTMsgErrorRcV);
|
---|
130 |
|
---|
131 |
|
---|
132 | RTDECL(int) RTMsgErrorRcV(int rcRet, const char *pszFormat, va_list va)
|
---|
133 | {
|
---|
134 | RTMsgErrorV(pszFormat, va);
|
---|
135 | return rcRet;
|
---|
136 | }
|
---|
137 | RT_EXPORT_SYMBOL(RTMsgErrorRcV);
|
---|
138 |
|
---|
139 |
|
---|
140 | RTDECL(RTEXITCODE) RTMsgInitFailure(int rcRTR3Init)
|
---|
141 | {
|
---|
142 | if ( g_offrtProcName
|
---|
143 | && g_offrtProcName < sizeof(g_szrtProcExePath)
|
---|
144 | && g_szrtProcExePath[0]
|
---|
145 | && g_szrtProcExePath[g_offrtProcName])
|
---|
146 | RTStrmPrintf(g_pStdErr, "%s: fatal error: RTR3Init: %Rrc\n", &g_szrtProcExePath[g_offrtProcName], rcRTR3Init);
|
---|
147 | else
|
---|
148 | RTStrmPrintf(g_pStdErr, "fatal error: RTR3Init: %Rrc\n", rcRTR3Init);
|
---|
149 | return RTEXITCODE_INIT;
|
---|
150 | }
|
---|
151 | RT_EXPORT_SYMBOL(RTMsgInitFailure);
|
---|
152 |
|
---|
153 |
|
---|
154 | RTDECL(int) RTMsgWarning(const char *pszFormat, ...)
|
---|
155 | {
|
---|
156 | va_list va;
|
---|
157 | va_start(va, pszFormat);
|
---|
158 | int rc = RTMsgWarningV(pszFormat, va);
|
---|
159 | va_end(va);
|
---|
160 | return rc;
|
---|
161 | }
|
---|
162 | RT_EXPORT_SYMBOL(RTMsgInfo);
|
---|
163 |
|
---|
164 |
|
---|
165 | RTDECL(int) RTMsgWarningV(const char *pszFormat, va_list va)
|
---|
166 | {
|
---|
167 | return rtMsgWorker(g_pStdErr, "warning: ", pszFormat, va);
|
---|
168 | }
|
---|
169 | RT_EXPORT_SYMBOL(RTMsgWarningV);
|
---|
170 |
|
---|
171 |
|
---|
172 | RTDECL(int) RTMsgInfo(const char *pszFormat, ...)
|
---|
173 | {
|
---|
174 | va_list va;
|
---|
175 | va_start(va, pszFormat);
|
---|
176 | int rc = RTMsgInfoV(pszFormat, va);
|
---|
177 | va_end(va);
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 | RT_EXPORT_SYMBOL(RTMsgInfo);
|
---|
181 |
|
---|
182 |
|
---|
183 | RTDECL(int) RTMsgInfoV(const char *pszFormat, va_list va)
|
---|
184 | {
|
---|
185 | return rtMsgWorker(g_pStdOut, "info: ", pszFormat, va);
|
---|
186 | }
|
---|
187 | RT_EXPORT_SYMBOL(RTMsgInfoV);
|
---|
188 |
|
---|