1 | /* $Id: message.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Error reporting to standard error.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/message.h>
|
---|
43 |
|
---|
44 | #include <iprt/path.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/stream.h>
|
---|
47 | #include "internal/process.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /** The program name we're using. */
|
---|
54 | static const char * volatile g_pszProgName = NULL;
|
---|
55 | /** Custom program name set via RTMsgSetProgName. */
|
---|
56 | static char g_szProgName[128];
|
---|
57 |
|
---|
58 |
|
---|
59 | RTDECL(int) RTMsgSetProgName(const char *pszFormat, ...)
|
---|
60 | {
|
---|
61 | g_pszProgName = &g_szrtProcExePath[g_offrtProcName];
|
---|
62 |
|
---|
63 | va_list va;
|
---|
64 | va_start(va, pszFormat);
|
---|
65 | RTStrPrintfV(g_szProgName, sizeof(g_szProgName) - 1, pszFormat, va);
|
---|
66 | va_end(va);
|
---|
67 |
|
---|
68 | g_pszProgName = g_szProgName;
|
---|
69 |
|
---|
70 | return VINF_SUCCESS;
|
---|
71 | }
|
---|
72 | RT_EXPORT_SYMBOL(RTMsgSetProgName);
|
---|
73 |
|
---|
74 |
|
---|
75 | static int rtMsgWorker(PRTSTREAM pDst, const char *pszPrefix, const char *pszFormat, va_list va)
|
---|
76 | {
|
---|
77 | if ( !*pszFormat
|
---|
78 | || !strcmp(pszFormat, "\n"))
|
---|
79 | RTStrmPrintf(pDst, "\n");
|
---|
80 | else
|
---|
81 | {
|
---|
82 | const char *pszProgName = g_pszProgName;
|
---|
83 | if (!pszProgName)
|
---|
84 | g_pszProgName = pszProgName = &g_szrtProcExePath[g_offrtProcName];
|
---|
85 |
|
---|
86 | char *pszMsg;
|
---|
87 | ssize_t cch = RTStrAPrintfV(&pszMsg, pszFormat, va);
|
---|
88 | if (cch >= 0)
|
---|
89 | {
|
---|
90 | /* print it line by line. */
|
---|
91 | char *psz = pszMsg;
|
---|
92 | do
|
---|
93 | {
|
---|
94 | char *pszEnd = strchr(psz, '\n');
|
---|
95 | if (!pszEnd)
|
---|
96 | {
|
---|
97 | RTStrmPrintf(pDst, "%s: %s%s\n", pszProgName, pszPrefix, psz);
|
---|
98 | break;
|
---|
99 | }
|
---|
100 | if (pszEnd == psz)
|
---|
101 | RTStrmPrintf(pDst, "\n");
|
---|
102 | else
|
---|
103 | {
|
---|
104 | *pszEnd = '\0';
|
---|
105 | RTStrmPrintf(pDst, "%s: %s%s\n", pszProgName, pszPrefix, psz);
|
---|
106 | }
|
---|
107 | psz = pszEnd + 1;
|
---|
108 | } while (*psz);
|
---|
109 | RTStrFree(pszMsg);
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | /* Simple fallback for handling out-of-memory conditions. */
|
---|
114 | RTStrmPrintf(pDst, "%s: %s", pszProgName, pszPrefix);
|
---|
115 | RTStrmPrintfV(pDst, pszFormat, va);
|
---|
116 | if (!strchr(pszFormat, '\n'))
|
---|
117 | RTStrmPrintf(pDst, "\n");
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | }
|
---|
123 |
|
---|
124 | RTDECL(int) RTMsgError(const char *pszFormat, ...)
|
---|
125 | {
|
---|
126 | va_list va;
|
---|
127 | va_start(va, pszFormat);
|
---|
128 | int rc = RTMsgErrorV(pszFormat, va);
|
---|
129 | va_end(va);
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 | RT_EXPORT_SYMBOL(RTMsgError);
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(int) RTMsgErrorV(const char *pszFormat, va_list va)
|
---|
136 | {
|
---|
137 | return rtMsgWorker(g_pStdErr, "error: ", pszFormat, va);
|
---|
138 | }
|
---|
139 | RT_EXPORT_SYMBOL(RTMsgErrorV);
|
---|
140 |
|
---|
141 |
|
---|
142 | RTDECL(RTEXITCODE) RTMsgErrorExit(RTEXITCODE enmExitCode, const char *pszFormat, ...)
|
---|
143 | {
|
---|
144 | va_list va;
|
---|
145 | va_start(va, pszFormat);
|
---|
146 | RTMsgErrorV(pszFormat, va);
|
---|
147 | va_end(va);
|
---|
148 | return enmExitCode;
|
---|
149 | }
|
---|
150 | RT_EXPORT_SYMBOL(RTMsgErrorExit);
|
---|
151 |
|
---|
152 |
|
---|
153 | RTDECL(RTEXITCODE) RTMsgErrorExitV(RTEXITCODE enmExitCode, const char *pszFormat, va_list va)
|
---|
154 | {
|
---|
155 | RTMsgErrorV(pszFormat, va);
|
---|
156 | return enmExitCode;
|
---|
157 | }
|
---|
158 | RT_EXPORT_SYMBOL(RTMsgErrorExitV);
|
---|
159 |
|
---|
160 |
|
---|
161 | RTDECL(RTEXITCODE) RTMsgErrorExitFailure(const char *pszFormat, ...)
|
---|
162 | {
|
---|
163 | va_list va;
|
---|
164 | va_start(va, pszFormat);
|
---|
165 | RTMsgErrorV(pszFormat, va);
|
---|
166 | va_end(va);
|
---|
167 | return RTEXITCODE_FAILURE;
|
---|
168 | }
|
---|
169 | RT_EXPORT_SYMBOL(RTMsgErrorExitFailure);
|
---|
170 |
|
---|
171 |
|
---|
172 | RTDECL(RTEXITCODE) RTMsgErrorExitFailureV(const char *pszFormat, va_list va)
|
---|
173 | {
|
---|
174 | RTMsgErrorV(pszFormat, va);
|
---|
175 | return RTEXITCODE_FAILURE;
|
---|
176 | }
|
---|
177 | RT_EXPORT_SYMBOL(RTMsgErrorExitFailureV);
|
---|
178 |
|
---|
179 |
|
---|
180 | RTDECL(int) RTMsgErrorRc(int rcRet, const char *pszFormat, ...)
|
---|
181 | {
|
---|
182 | va_list va;
|
---|
183 | va_start(va, pszFormat);
|
---|
184 | RTMsgErrorV(pszFormat, va);
|
---|
185 | va_end(va);
|
---|
186 | return rcRet;
|
---|
187 | }
|
---|
188 | RT_EXPORT_SYMBOL(RTMsgErrorRcV);
|
---|
189 |
|
---|
190 |
|
---|
191 | RTDECL(int) RTMsgErrorRcV(int rcRet, const char *pszFormat, va_list va)
|
---|
192 | {
|
---|
193 | RTMsgErrorV(pszFormat, va);
|
---|
194 | return rcRet;
|
---|
195 | }
|
---|
196 | RT_EXPORT_SYMBOL(RTMsgErrorRcV);
|
---|
197 |
|
---|
198 |
|
---|
199 | RTDECL(RTEXITCODE) RTMsgInitFailure(int rcRTR3Init)
|
---|
200 | {
|
---|
201 | if ( g_offrtProcName
|
---|
202 | && g_offrtProcName < sizeof(g_szrtProcExePath)
|
---|
203 | && g_szrtProcExePath[0]
|
---|
204 | && g_szrtProcExePath[g_offrtProcName])
|
---|
205 | RTStrmPrintf(g_pStdErr, "%s: fatal error: RTR3Init: %Rrc\n", &g_szrtProcExePath[g_offrtProcName], rcRTR3Init);
|
---|
206 | else
|
---|
207 | RTStrmPrintf(g_pStdErr, "fatal error: RTR3Init: %Rrc\n", rcRTR3Init);
|
---|
208 | return RTEXITCODE_INIT;
|
---|
209 | }
|
---|
210 | RT_EXPORT_SYMBOL(RTMsgInitFailure);
|
---|
211 |
|
---|
212 |
|
---|
213 | RTDECL(RTEXITCODE) RTMsgSyntax(const char *pszFormat, ...)
|
---|
214 | {
|
---|
215 | va_list va;
|
---|
216 | va_start(va, pszFormat);
|
---|
217 | RTMsgSyntaxV(pszFormat, va);
|
---|
218 | va_end(va);
|
---|
219 | return RTEXITCODE_SYNTAX;
|
---|
220 | }
|
---|
221 | RT_EXPORT_SYMBOL(RTMsgSyntax);
|
---|
222 |
|
---|
223 |
|
---|
224 | RTDECL(RTEXITCODE) RTMsgSyntaxV(const char *pszFormat, va_list va)
|
---|
225 | {
|
---|
226 | rtMsgWorker(g_pStdOut, "syntax error: ", pszFormat, va);
|
---|
227 | return RTEXITCODE_SYNTAX;
|
---|
228 | }
|
---|
229 | RT_EXPORT_SYMBOL(RTMsgSyntaxV);
|
---|
230 |
|
---|
231 |
|
---|
232 | RTDECL(int) RTMsgWarning(const char *pszFormat, ...)
|
---|
233 | {
|
---|
234 | va_list va;
|
---|
235 | va_start(va, pszFormat);
|
---|
236 | int rc = RTMsgWarningV(pszFormat, va);
|
---|
237 | va_end(va);
|
---|
238 | return rc;
|
---|
239 | }
|
---|
240 | RT_EXPORT_SYMBOL(RTMsgInfo);
|
---|
241 |
|
---|
242 |
|
---|
243 | RTDECL(int) RTMsgWarningV(const char *pszFormat, va_list va)
|
---|
244 | {
|
---|
245 | return rtMsgWorker(g_pStdErr, "warning: ", pszFormat, va);
|
---|
246 | }
|
---|
247 | RT_EXPORT_SYMBOL(RTMsgWarningV);
|
---|
248 |
|
---|
249 |
|
---|
250 | RTDECL(int) RTMsgInfo(const char *pszFormat, ...)
|
---|
251 | {
|
---|
252 | va_list va;
|
---|
253 | va_start(va, pszFormat);
|
---|
254 | int rc = RTMsgInfoV(pszFormat, va);
|
---|
255 | va_end(va);
|
---|
256 | return rc;
|
---|
257 | }
|
---|
258 | RT_EXPORT_SYMBOL(RTMsgInfo);
|
---|
259 |
|
---|
260 |
|
---|
261 | RTDECL(int) RTMsgInfoV(const char *pszFormat, va_list va)
|
---|
262 | {
|
---|
263 | return rtMsgWorker(g_pStdOut, "info: ", pszFormat, va);
|
---|
264 | }
|
---|
265 | RT_EXPORT_SYMBOL(RTMsgInfoV);
|
---|
266 |
|
---|