VirtualBox

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

Last change on this file since 28142 was 27639, checked in by vboxsync, 15 years ago

iprt: added RTMsgErrorRc[V].

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