1 | /* $Id: errinfo.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Error Info, Setters.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2020 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/errcore.h>
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | RTDECL(int) RTErrInfoSet(PRTERRINFO pErrInfo, int rc, const char *pszMsg)
|
---|
39 | {
|
---|
40 | if (pErrInfo)
|
---|
41 | {
|
---|
42 | AssertPtr(pErrInfo);
|
---|
43 | Assert((pErrInfo->fFlags & RTERRINFO_FLAGS_MAGIC_MASK) == RTERRINFO_FLAGS_MAGIC);
|
---|
44 |
|
---|
45 | RTStrCopy(pErrInfo->pszMsg, pErrInfo->cbMsg, pszMsg);
|
---|
46 | pErrInfo->rc = rc;
|
---|
47 | pErrInfo->fFlags |= RTERRINFO_FLAGS_SET;
|
---|
48 | }
|
---|
49 | return rc;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | RTDECL(int) RTErrInfoSetF(PRTERRINFO pErrInfo, int rc, const char *pszFormat, ...)
|
---|
54 | {
|
---|
55 | va_list va;
|
---|
56 | va_start(va, pszFormat);
|
---|
57 | RTErrInfoSetV(pErrInfo, rc, pszFormat, va);
|
---|
58 | va_end(va);
|
---|
59 | return rc;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | RTDECL(int) RTErrInfoSetV(PRTERRINFO pErrInfo, int rc, const char *pszFormat, va_list va)
|
---|
64 | {
|
---|
65 | if (pErrInfo)
|
---|
66 | {
|
---|
67 | AssertPtr(pErrInfo);
|
---|
68 | Assert((pErrInfo->fFlags & RTERRINFO_FLAGS_MAGIC_MASK) == RTERRINFO_FLAGS_MAGIC);
|
---|
69 |
|
---|
70 | RTStrPrintfV(pErrInfo->pszMsg, pErrInfo->cbMsg, pszFormat, va);
|
---|
71 | pErrInfo->rc = rc;
|
---|
72 | pErrInfo->fFlags |= RTERRINFO_FLAGS_SET;
|
---|
73 | }
|
---|
74 | return rc;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | RTDECL(int) RTErrInfoAdd(PRTERRINFO pErrInfo, int rc, const char *pszMsg)
|
---|
79 | {
|
---|
80 | if (pErrInfo)
|
---|
81 | {
|
---|
82 | AssertPtr(pErrInfo);
|
---|
83 | if (pErrInfo->fFlags & RTERRINFO_FLAGS_SET)
|
---|
84 | RTStrCat(pErrInfo->pszMsg, pErrInfo->cbMsg, pszMsg);
|
---|
85 | else
|
---|
86 | {
|
---|
87 | while (*pszMsg == ' ')
|
---|
88 | pszMsg++;
|
---|
89 | return RTErrInfoSet(pErrInfo, rc, pszMsg);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return rc;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | RTDECL(int) RTErrInfoAddF(PRTERRINFO pErrInfo, int rc, const char *pszFormat, ...)
|
---|
97 | {
|
---|
98 | va_list va;
|
---|
99 | va_start(va, pszFormat);
|
---|
100 | RTErrInfoAddV(pErrInfo, rc, pszFormat, va);
|
---|
101 | va_end(va);
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | RTDECL(int) RTErrInfoAddV(PRTERRINFO pErrInfo, int rc, const char *pszFormat, va_list va)
|
---|
107 | {
|
---|
108 | if (pErrInfo)
|
---|
109 | {
|
---|
110 | AssertPtr(pErrInfo);
|
---|
111 | Assert((pErrInfo->fFlags & RTERRINFO_FLAGS_MAGIC_MASK) == RTERRINFO_FLAGS_MAGIC);
|
---|
112 | if (pErrInfo->fFlags & RTERRINFO_FLAGS_SET)
|
---|
113 | {
|
---|
114 | char *pszOut = (char *)memchr(pErrInfo->pszMsg, '\0', pErrInfo->cbMsg - 2);
|
---|
115 | if (pszOut)
|
---|
116 | RTStrPrintfV(pszOut, &pErrInfo->pszMsg[pErrInfo->cbMsg] - pszOut, pszFormat, va);
|
---|
117 | }
|
---|
118 | else
|
---|
119 | {
|
---|
120 | while (*pszFormat == ' ')
|
---|
121 | pszFormat++;
|
---|
122 | return RTErrInfoSetV(pErrInfo, rc, pszFormat, va);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return rc;
|
---|
126 | }
|
---|
127 |
|
---|