VirtualBox

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

Last change on this file since 44417 was 37951, checked in by vboxsync, 13 years ago

IPRT: Added RTMsgSetProgName.

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