VirtualBox

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

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: message.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - Error reporting to standard error.
4 */
5
6/*
7 * Copyright (C) 2009-2022 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/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/message.h>
33
34#include <iprt/path.h>
35#include <iprt/string.h>
36#include <iprt/stream.h>
37#include "internal/process.h"
38
39
40/*********************************************************************************************************************************
41* Global Variables *
42*********************************************************************************************************************************/
43/** The program name we're using. */
44static const char * volatile g_pszProgName = NULL;
45/** Custom program name set via RTMsgSetProgName. */
46static char g_szProgName[128];
47
48
49RTDECL(int) RTMsgSetProgName(const char *pszFormat, ...)
50{
51 g_pszProgName = &g_szrtProcExePath[g_offrtProcName];
52
53 va_list va;
54 va_start(va, pszFormat);
55 RTStrPrintfV(g_szProgName, sizeof(g_szProgName) - 1, pszFormat, va);
56 va_end(va);
57
58 g_pszProgName = g_szProgName;
59
60 return VINF_SUCCESS;
61}
62RT_EXPORT_SYMBOL(RTMsgSetProgName);
63
64
65static int rtMsgWorker(PRTSTREAM pDst, const char *pszPrefix, const char *pszFormat, va_list va)
66{
67 if ( !*pszFormat
68 || !strcmp(pszFormat, "\n"))
69 RTStrmPrintf(pDst, "\n");
70 else
71 {
72 const char *pszProgName = g_pszProgName;
73 if (!pszProgName)
74 g_pszProgName = pszProgName = &g_szrtProcExePath[g_offrtProcName];
75
76 char *pszMsg;
77 ssize_t cch = RTStrAPrintfV(&pszMsg, pszFormat, va);
78 if (cch >= 0)
79 {
80 /* print it line by line. */
81 char *psz = pszMsg;
82 do
83 {
84 char *pszEnd = strchr(psz, '\n');
85 if (!pszEnd)
86 {
87 RTStrmPrintf(pDst, "%s: %s%s\n", pszProgName, pszPrefix, psz);
88 break;
89 }
90 if (pszEnd == psz)
91 RTStrmPrintf(pDst, "\n");
92 else
93 {
94 *pszEnd = '\0';
95 RTStrmPrintf(pDst, "%s: %s%s\n", pszProgName, pszPrefix, psz);
96 }
97 psz = pszEnd + 1;
98 } while (*psz);
99 RTStrFree(pszMsg);
100 }
101 else
102 {
103 /* Simple fallback for handling out-of-memory conditions. */
104 RTStrmPrintf(pDst, "%s: %s", pszProgName, pszPrefix);
105 RTStrmPrintfV(pDst, pszFormat, va);
106 if (!strchr(pszFormat, '\n'))
107 RTStrmPrintf(pDst, "\n");
108 }
109 }
110
111 return VINF_SUCCESS;
112}
113
114RTDECL(int) RTMsgError(const char *pszFormat, ...)
115{
116 va_list va;
117 va_start(va, pszFormat);
118 int rc = RTMsgErrorV(pszFormat, va);
119 va_end(va);
120 return rc;
121}
122RT_EXPORT_SYMBOL(RTMsgError);
123
124
125RTDECL(int) RTMsgErrorV(const char *pszFormat, va_list va)
126{
127 return rtMsgWorker(g_pStdErr, "error: ", pszFormat, va);
128}
129RT_EXPORT_SYMBOL(RTMsgErrorV);
130
131
132RTDECL(RTEXITCODE) RTMsgErrorExit(RTEXITCODE enmExitCode, const char *pszFormat, ...)
133{
134 va_list va;
135 va_start(va, pszFormat);
136 RTMsgErrorV(pszFormat, va);
137 va_end(va);
138 return enmExitCode;
139}
140RT_EXPORT_SYMBOL(RTMsgErrorExit);
141
142
143RTDECL(RTEXITCODE) RTMsgErrorExitV(RTEXITCODE enmExitCode, const char *pszFormat, va_list va)
144{
145 RTMsgErrorV(pszFormat, va);
146 return enmExitCode;
147}
148RT_EXPORT_SYMBOL(RTMsgErrorExitV);
149
150
151RTDECL(RTEXITCODE) RTMsgErrorExitFailure(const char *pszFormat, ...)
152{
153 va_list va;
154 va_start(va, pszFormat);
155 RTMsgErrorV(pszFormat, va);
156 va_end(va);
157 return RTEXITCODE_FAILURE;
158}
159RT_EXPORT_SYMBOL(RTMsgErrorExitFailure);
160
161
162RTDECL(RTEXITCODE) RTMsgErrorExitFailureV(const char *pszFormat, va_list va)
163{
164 RTMsgErrorV(pszFormat, va);
165 return RTEXITCODE_FAILURE;
166}
167RT_EXPORT_SYMBOL(RTMsgErrorExitFailureV);
168
169
170RTDECL(int) RTMsgErrorRc(int rcRet, const char *pszFormat, ...)
171{
172 va_list va;
173 va_start(va, pszFormat);
174 RTMsgErrorV(pszFormat, va);
175 va_end(va);
176 return rcRet;
177}
178RT_EXPORT_SYMBOL(RTMsgErrorRcV);
179
180
181RTDECL(int) RTMsgErrorRcV(int rcRet, const char *pszFormat, va_list va)
182{
183 RTMsgErrorV(pszFormat, va);
184 return rcRet;
185}
186RT_EXPORT_SYMBOL(RTMsgErrorRcV);
187
188
189RTDECL(RTEXITCODE) RTMsgInitFailure(int rcRTR3Init)
190{
191 if ( g_offrtProcName
192 && g_offrtProcName < sizeof(g_szrtProcExePath)
193 && g_szrtProcExePath[0]
194 && g_szrtProcExePath[g_offrtProcName])
195 RTStrmPrintf(g_pStdErr, "%s: fatal error: RTR3Init: %Rrc\n", &g_szrtProcExePath[g_offrtProcName], rcRTR3Init);
196 else
197 RTStrmPrintf(g_pStdErr, "fatal error: RTR3Init: %Rrc\n", rcRTR3Init);
198 return RTEXITCODE_INIT;
199}
200RT_EXPORT_SYMBOL(RTMsgInitFailure);
201
202
203RTDECL(int) RTMsgWarning(const char *pszFormat, ...)
204{
205 va_list va;
206 va_start(va, pszFormat);
207 int rc = RTMsgWarningV(pszFormat, va);
208 va_end(va);
209 return rc;
210}
211RT_EXPORT_SYMBOL(RTMsgInfo);
212
213
214RTDECL(int) RTMsgWarningV(const char *pszFormat, va_list va)
215{
216 return rtMsgWorker(g_pStdErr, "warning: ", pszFormat, va);
217}
218RT_EXPORT_SYMBOL(RTMsgWarningV);
219
220
221RTDECL(int) RTMsgInfo(const char *pszFormat, ...)
222{
223 va_list va;
224 va_start(va, pszFormat);
225 int rc = RTMsgInfoV(pszFormat, va);
226 va_end(va);
227 return rc;
228}
229RT_EXPORT_SYMBOL(RTMsgInfo);
230
231
232RTDECL(int) RTMsgInfoV(const char *pszFormat, va_list va)
233{
234 return rtMsgWorker(g_pStdOut, "info: ", pszFormat, va);
235}
236RT_EXPORT_SYMBOL(RTMsgInfoV);
237
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