1 | /* $Id: errinfolog.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Error Info, Setters with logging.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-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/errcore.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/log.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | RTDECL(int) RTErrInfoLogAndSet(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszMsg)
|
---|
50 | {
|
---|
51 | /* The logging: */
|
---|
52 | if (fFlags & RTERRINFO_LOG_F_RELEASE)
|
---|
53 | {
|
---|
54 | PRTLOGGER pLogger = RTLogRelGetDefaultInstanceExWeak(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
|
---|
55 | if (pLogger)
|
---|
56 | RTLogLoggerExWeak(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoSet(%Rrc): %s\n", rc, pszMsg);
|
---|
57 | }
|
---|
58 |
|
---|
59 | PRTLOGGER pLogger = RTLogGetDefaultInstanceExWeak(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
|
---|
60 | if (pLogger)
|
---|
61 | RTLogLoggerExWeak(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoSet(%Rrc): %s\n", rc, pszMsg);
|
---|
62 |
|
---|
63 | /* The setting: */
|
---|
64 | if (pErrInfo)
|
---|
65 | {
|
---|
66 | AssertPtr(pErrInfo);
|
---|
67 | Assert((pErrInfo->fFlags & RTERRINFO_FLAGS_MAGIC_MASK) == RTERRINFO_FLAGS_MAGIC);
|
---|
68 |
|
---|
69 | RTStrCopy(pErrInfo->pszMsg, pErrInfo->cbMsg, pszMsg);
|
---|
70 | pErrInfo->rc = rc;
|
---|
71 | pErrInfo->fFlags |= RTERRINFO_FLAGS_SET;
|
---|
72 | }
|
---|
73 | return rc;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | RTDECL(int) RTErrInfoLogAndSetF(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszFormat, ...)
|
---|
78 | {
|
---|
79 | va_list va;
|
---|
80 | va_start(va, pszFormat);
|
---|
81 | RTErrInfoLogAndSetV(pErrInfo, rc, iLogGroup, fFlags, pszFormat, va);
|
---|
82 | va_end(va);
|
---|
83 | return rc;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | RTDECL(int) RTErrInfoLogAndSetV(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszFormat, va_list va)
|
---|
88 | {
|
---|
89 | /* The logging: */
|
---|
90 | if (fFlags & RTERRINFO_LOG_F_RELEASE)
|
---|
91 | {
|
---|
92 | PRTLOGGER pLogger = RTLogRelGetDefaultInstanceExWeak(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
|
---|
93 | if (pLogger)
|
---|
94 | {
|
---|
95 | va_list va2;
|
---|
96 | va_copy(va2, va);
|
---|
97 | RTLogLoggerExWeak(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoSet(%Rrc): %N\n", rc, pszFormat, &va2);
|
---|
98 | va_end(va2);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | PRTLOGGER pLogger = RTLogGetDefaultInstanceExWeak(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
|
---|
103 | if (pLogger)
|
---|
104 | {
|
---|
105 | va_list va2;
|
---|
106 | va_copy(va2, va);
|
---|
107 | RTLogLoggerExWeak(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoSet(%Rrc): %N\n", rc, pszFormat, &va2);
|
---|
108 | va_end(va2);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* The setting: */
|
---|
112 | if (pErrInfo)
|
---|
113 | {
|
---|
114 | AssertPtr(pErrInfo);
|
---|
115 | Assert((pErrInfo->fFlags & RTERRINFO_FLAGS_MAGIC_MASK) == RTERRINFO_FLAGS_MAGIC);
|
---|
116 |
|
---|
117 | RTStrPrintfV(pErrInfo->pszMsg, pErrInfo->cbMsg, pszFormat, va);
|
---|
118 | pErrInfo->rc = rc;
|
---|
119 | pErrInfo->fFlags |= RTERRINFO_FLAGS_SET;
|
---|
120 | }
|
---|
121 | return rc;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(int) RTErrInfoLogAndAdd(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszMsg)
|
---|
126 | {
|
---|
127 | if (pErrInfo)
|
---|
128 | {
|
---|
129 | AssertPtr(pErrInfo);
|
---|
130 | if (pErrInfo->fFlags & RTERRINFO_FLAGS_SET)
|
---|
131 | RTStrCat(pErrInfo->pszMsg, pErrInfo->cbMsg, pszMsg);
|
---|
132 | else
|
---|
133 | {
|
---|
134 | while (*pszMsg == ' ')
|
---|
135 | pszMsg++;
|
---|
136 | return RTErrInfoSet(pErrInfo, rc, pszMsg);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* The logging: */
|
---|
141 | if (fFlags & RTERRINFO_LOG_F_RELEASE)
|
---|
142 | {
|
---|
143 | PRTLOGGER pLogger = RTLogRelGetDefaultInstanceExWeak(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
|
---|
144 | if (pLogger)
|
---|
145 | RTLogLoggerExWeak(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoAdd(%Rrc): %s\n", rc, pszMsg);
|
---|
146 | }
|
---|
147 |
|
---|
148 | PRTLOGGER pLogger = RTLogGetDefaultInstanceExWeak(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
|
---|
149 | if (pLogger)
|
---|
150 | RTLogLoggerExWeak(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoAdd(%Rrc): %s\n", rc, pszMsg);
|
---|
151 |
|
---|
152 | return rc;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | RTDECL(int) RTErrInfoLogAndAddF(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszFormat, ...)
|
---|
157 | {
|
---|
158 | va_list va;
|
---|
159 | va_start(va, pszFormat);
|
---|
160 | RTErrInfoLogAndAddV(pErrInfo, rc, iLogGroup, fFlags, pszFormat, va);
|
---|
161 | va_end(va);
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | RTDECL(int) RTErrInfoLogAndAddV(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszFormat, va_list va)
|
---|
167 | {
|
---|
168 | if (pErrInfo)
|
---|
169 | {
|
---|
170 | AssertPtr(pErrInfo);
|
---|
171 | Assert((pErrInfo->fFlags & RTERRINFO_FLAGS_MAGIC_MASK) == RTERRINFO_FLAGS_MAGIC);
|
---|
172 | if (pErrInfo->fFlags & RTERRINFO_FLAGS_SET)
|
---|
173 | {
|
---|
174 | char *pszOut = (char *)memchr(pErrInfo->pszMsg, '\0', pErrInfo->cbMsg - 2);
|
---|
175 | if (pszOut)
|
---|
176 | {
|
---|
177 | va_list va2;
|
---|
178 | va_copy(va2, va);
|
---|
179 | RTStrPrintfV(pszOut, &pErrInfo->pszMsg[pErrInfo->cbMsg] - pszOut, pszFormat, va2);
|
---|
180 | va_end(va2);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | else
|
---|
184 | {
|
---|
185 | while (*pszFormat == ' ')
|
---|
186 | pszFormat++;
|
---|
187 | return RTErrInfoSetV(pErrInfo, rc, pszFormat, va);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* The logging: */
|
---|
192 | if (fFlags & RTERRINFO_LOG_F_RELEASE)
|
---|
193 | {
|
---|
194 | PRTLOGGER pLogger = RTLogRelGetDefaultInstanceExWeak(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
|
---|
195 | if (pLogger)
|
---|
196 | {
|
---|
197 | va_list va2;
|
---|
198 | va_copy(va2, va);
|
---|
199 | RTLogLoggerExWeak(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoAdd(%Rrc): %N\n", rc, pszFormat, &va2);
|
---|
200 | va_end(va2);
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | PRTLOGGER pLogger = RTLogGetDefaultInstanceExWeak(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
|
---|
205 | if (pLogger)
|
---|
206 | {
|
---|
207 | va_list va2;
|
---|
208 | va_copy(va2, va);
|
---|
209 | RTLogLoggerExWeak(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoAdd(%Rrc): %N\n", rc, pszFormat, &va2);
|
---|
210 | va_end(va2);
|
---|
211 | }
|
---|
212 |
|
---|
213 | return rc;
|
---|
214 | }
|
---|
215 |
|
---|