VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/err/errinfolog.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: errinfolog.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - Error Info, Setters with logging.
4 */
5
6/*
7 * Copyright (C) 2010-2022 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
49RTDECL(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 = RTLogRelGetDefaultInstanceEx(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
55 if (pLogger)
56 RTLogLoggerEx(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoSet(%Rrc): %s\n", rc, pszMsg);
57 }
58
59 PRTLOGGER pLogger = RTLogGetDefaultInstanceEx(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
60 if (pLogger)
61 RTLogLoggerEx(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
77RTDECL(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
87RTDECL(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 = RTLogRelGetDefaultInstanceEx(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
93 if (pLogger)
94 RTLogLoggerEx(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoSet(%Rrc): %N\n", rc, pszFormat, &va);
95 }
96
97 PRTLOGGER pLogger = RTLogGetDefaultInstanceEx(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
98 if (pLogger)
99 RTLogLoggerEx(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoSet(%Rrc): %N\n", rc, pszFormat, &va);
100
101 /* The setting: */
102 if (pErrInfo)
103 {
104 AssertPtr(pErrInfo);
105 Assert((pErrInfo->fFlags & RTERRINFO_FLAGS_MAGIC_MASK) == RTERRINFO_FLAGS_MAGIC);
106
107 RTStrPrintfV(pErrInfo->pszMsg, pErrInfo->cbMsg, pszFormat, va);
108 pErrInfo->rc = rc;
109 pErrInfo->fFlags |= RTERRINFO_FLAGS_SET;
110 }
111 return rc;
112}
113
114
115RTDECL(int) RTErrInfoLogAndAdd(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszMsg)
116{
117 if (pErrInfo)
118 {
119 AssertPtr(pErrInfo);
120 if (pErrInfo->fFlags & RTERRINFO_FLAGS_SET)
121 RTStrCat(pErrInfo->pszMsg, pErrInfo->cbMsg, pszMsg);
122 else
123 {
124 while (*pszMsg == ' ')
125 pszMsg++;
126 return RTErrInfoSet(pErrInfo, rc, pszMsg);
127 }
128 }
129
130 /* The logging: */
131 if (fFlags & RTERRINFO_LOG_F_RELEASE)
132 {
133 PRTLOGGER pLogger = RTLogRelGetDefaultInstanceEx(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
134 if (pLogger)
135 RTLogLoggerEx(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoAdd(%Rrc): %s\n", rc, pszMsg);
136 }
137
138 PRTLOGGER pLogger = RTLogGetDefaultInstanceEx(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
139 if (pLogger)
140 RTLogLoggerEx(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoAdd(%Rrc): %s\n", rc, pszMsg);
141
142 return rc;
143}
144
145
146RTDECL(int) RTErrInfoLogAndAddF(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszFormat, ...)
147{
148 va_list va;
149 va_start(va, pszFormat);
150 RTErrInfoLogAndAddV(pErrInfo, rc, iLogGroup, fFlags, pszFormat, va);
151 va_end(va);
152 return rc;
153}
154
155
156RTDECL(int) RTErrInfoLogAndAddV(PRTERRINFO pErrInfo, int rc, uint32_t iLogGroup, uint32_t fFlags, const char *pszFormat, va_list va)
157{
158 if (pErrInfo)
159 {
160 AssertPtr(pErrInfo);
161 Assert((pErrInfo->fFlags & RTERRINFO_FLAGS_MAGIC_MASK) == RTERRINFO_FLAGS_MAGIC);
162 if (pErrInfo->fFlags & RTERRINFO_FLAGS_SET)
163 {
164 char *pszOut = (char *)memchr(pErrInfo->pszMsg, '\0', pErrInfo->cbMsg - 2);
165 if (pszOut)
166 {
167 va_list va2;
168 va_copy(va2, va);
169 RTStrPrintfV(pszOut, &pErrInfo->pszMsg[pErrInfo->cbMsg] - pszOut, pszFormat, va2);
170 va_end(va2);
171 }
172 }
173 else
174 {
175 while (*pszFormat == ' ')
176 pszFormat++;
177 return RTErrInfoSetV(pErrInfo, rc, pszFormat, va);
178 }
179 }
180
181 /* The logging: */
182 if (fFlags & RTERRINFO_LOG_F_RELEASE)
183 {
184 PRTLOGGER pLogger = RTLogRelGetDefaultInstanceEx(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
185 if (pLogger)
186 RTLogLoggerEx(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoAdd(%Rrc): %N\n", rc, pszFormat, &va);
187 }
188
189 PRTLOGGER pLogger = RTLogGetDefaultInstanceEx(RT_MAKE_U32(RTLOGGRPFLAGS_LEVEL_1, iLogGroup));
190 if (pLogger)
191 RTLogLoggerEx(pLogger, RTLOGGRPFLAGS_LEVEL_1, iLogGroup, "RTErrInfoAdd(%Rrc): %N\n", rc, pszFormat, &va);
192
193 return rc;
194}
195
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