VirtualBox

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

Last change on this file since 99553 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
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. */
54static const char * volatile g_pszProgName = NULL;
55/** Custom program name set via RTMsgSetProgName. */
56static char g_szProgName[128];
57
58
59RTDECL(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}
72RT_EXPORT_SYMBOL(RTMsgSetProgName);
73
74
75static 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
124RTDECL(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}
132RT_EXPORT_SYMBOL(RTMsgError);
133
134
135RTDECL(int) RTMsgErrorV(const char *pszFormat, va_list va)
136{
137 return rtMsgWorker(g_pStdErr, "error: ", pszFormat, va);
138}
139RT_EXPORT_SYMBOL(RTMsgErrorV);
140
141
142RTDECL(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}
150RT_EXPORT_SYMBOL(RTMsgErrorExit);
151
152
153RTDECL(RTEXITCODE) RTMsgErrorExitV(RTEXITCODE enmExitCode, const char *pszFormat, va_list va)
154{
155 RTMsgErrorV(pszFormat, va);
156 return enmExitCode;
157}
158RT_EXPORT_SYMBOL(RTMsgErrorExitV);
159
160
161RTDECL(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}
169RT_EXPORT_SYMBOL(RTMsgErrorExitFailure);
170
171
172RTDECL(RTEXITCODE) RTMsgErrorExitFailureV(const char *pszFormat, va_list va)
173{
174 RTMsgErrorV(pszFormat, va);
175 return RTEXITCODE_FAILURE;
176}
177RT_EXPORT_SYMBOL(RTMsgErrorExitFailureV);
178
179
180RTDECL(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}
188RT_EXPORT_SYMBOL(RTMsgErrorRcV);
189
190
191RTDECL(int) RTMsgErrorRcV(int rcRet, const char *pszFormat, va_list va)
192{
193 RTMsgErrorV(pszFormat, va);
194 return rcRet;
195}
196RT_EXPORT_SYMBOL(RTMsgErrorRcV);
197
198
199RTDECL(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}
210RT_EXPORT_SYMBOL(RTMsgInitFailure);
211
212
213RTDECL(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}
221RT_EXPORT_SYMBOL(RTMsgSyntax);
222
223
224RTDECL(RTEXITCODE) RTMsgSyntaxV(const char *pszFormat, va_list va)
225{
226 rtMsgWorker(g_pStdOut, "syntax error: ", pszFormat, va);
227 return RTEXITCODE_SYNTAX;
228}
229RT_EXPORT_SYMBOL(RTMsgSyntaxV);
230
231
232RTDECL(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}
240RT_EXPORT_SYMBOL(RTMsgInfo);
241
242
243RTDECL(int) RTMsgWarningV(const char *pszFormat, va_list va)
244{
245 return rtMsgWorker(g_pStdErr, "warning: ", pszFormat, va);
246}
247RT_EXPORT_SYMBOL(RTMsgWarningV);
248
249
250RTDECL(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}
258RT_EXPORT_SYMBOL(RTMsgInfo);
259
260
261RTDECL(int) RTMsgInfoV(const char *pszFormat, va_list va)
262{
263 return rtMsgWorker(g_pStdOut, "info: ", pszFormat, va);
264}
265RT_EXPORT_SYMBOL(RTMsgInfoV);
266
Note: See TracBrowser for help on using the repository browser.

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