[16555] | 1 | /* $Id: errorprint.cpp 98297 2023-01-25 01:59:25Z vboxsync $ */
|
---|
| 2 |
|
---|
[16554] | 3 | /** @file
|
---|
| 4 | * MS COM / XPCOM Abstraction Layer:
|
---|
[20928] | 5 | * Error info print helpers. This implements the shared code from the macros from errorprint.h.
|
---|
[16554] | 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
[98103] | 9 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
[16554] | 10 | *
|
---|
[96407] | 11 | * This file is part of VirtualBox base platform packages, as
|
---|
| 12 | * available from https://www.virtualbox.org.
|
---|
| 13 | *
|
---|
| 14 | * This program is free software; you can redistribute it and/or
|
---|
| 15 | * modify it under the terms of the GNU General Public License
|
---|
| 16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 17 | * License.
|
---|
| 18 | *
|
---|
| 19 | * This program is distributed in the hope that it will be useful, but
|
---|
| 20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 22 | * General Public License for more details.
|
---|
| 23 | *
|
---|
| 24 | * You should have received a copy of the GNU General Public License
|
---|
| 25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 26 | *
|
---|
| 27 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[16554] | 28 | */
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | #include <VBox/com/ErrorInfo.h>
|
---|
[20928] | 32 | #include <VBox/com/errorprint.h>
|
---|
[16554] | 33 | #include <VBox/log.h>
|
---|
| 34 |
|
---|
[38510] | 35 | #include <ProgressImpl.h>
|
---|
| 36 |
|
---|
[16554] | 37 | #include <iprt/stream.h>
|
---|
[32718] | 38 | #include <iprt/message.h>
|
---|
[16554] | 39 | #include <iprt/path.h>
|
---|
| 40 |
|
---|
| 41 | namespace com
|
---|
| 42 | {
|
---|
| 43 |
|
---|
[38509] | 44 | void GluePrintErrorInfo(const com::ErrorInfo &info)
|
---|
[16554] | 45 | {
|
---|
[38509] | 46 | bool haveResultCode = false;
|
---|
| 47 | #if defined (RT_OS_WIN)
|
---|
| 48 | haveResultCode = info.isFullAvailable();
|
---|
| 49 | bool haveComponent = true;
|
---|
| 50 | bool haveInterfaceID = true;
|
---|
| 51 | #else /* defined (RT_OS_WIN) */
|
---|
| 52 | haveResultCode = true;
|
---|
| 53 | bool haveComponent = info.isFullAvailable();
|
---|
| 54 | bool haveInterfaceID = info.isFullAvailable();
|
---|
| 55 | #endif
|
---|
| 56 |
|
---|
[72947] | 57 | try
|
---|
| 58 | {
|
---|
[98297] | 59 | HRESULT hrc = S_OK;
|
---|
[72947] | 60 | Utf8Str str;
|
---|
| 61 | RTCList<Utf8Str> comp;
|
---|
[38509] | 62 |
|
---|
[72947] | 63 | Bstr bstrDetailsText = info.getText();
|
---|
| 64 | if (!bstrDetailsText.isEmpty())
|
---|
| 65 | str = Utf8StrFmt("%ls\n",
|
---|
| 66 | bstrDetailsText.raw());
|
---|
| 67 | if (haveResultCode)
|
---|
[89720] | 68 | {
|
---|
[98297] | 69 | hrc = info.getResultCode();
|
---|
| 70 | comp.append(Utf8StrFmt("code %Rhrc (0x%RX32)", hrc, hrc));
|
---|
[89720] | 71 | }
|
---|
[72947] | 72 | if (haveComponent)
|
---|
| 73 | comp.append(Utf8StrFmt("component %ls",
|
---|
| 74 | info.getComponent().raw()));
|
---|
| 75 | if (haveInterfaceID)
|
---|
| 76 | comp.append(Utf8StrFmt("interface %ls",
|
---|
| 77 | info.getInterfaceName().raw()));
|
---|
| 78 | if (!info.getCalleeName().isEmpty())
|
---|
| 79 | comp.append(Utf8StrFmt("callee %ls",
|
---|
| 80 | info.getCalleeName().raw()));
|
---|
[38509] | 81 |
|
---|
[72947] | 82 | if (comp.size() > 0)
|
---|
| 83 | {
|
---|
| 84 | str += "Details: ";
|
---|
| 85 | for (size_t i = 0; i < comp.size() - 1; ++i)
|
---|
| 86 | str += comp.at(i) + ", ";
|
---|
| 87 | str += comp.last();
|
---|
| 88 | str += "\n";
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | // print and log
|
---|
[98297] | 92 | if (FAILED(hrc))
|
---|
[89720] | 93 | {
|
---|
| 94 | RTMsgError("%s", str.c_str());
|
---|
| 95 | Log(("ERROR: %s", str.c_str()));
|
---|
| 96 | }
|
---|
| 97 | else
|
---|
| 98 | {
|
---|
| 99 | RTMsgWarning("%s", str.c_str());
|
---|
| 100 | Log(("WARNING: %s", str.c_str()));
|
---|
| 101 | }
|
---|
[72947] | 102 | }
|
---|
[73505] | 103 | catch (std::bad_alloc &)
|
---|
[38509] | 104 | {
|
---|
[72947] | 105 | RTMsgError("std::bad_alloc in GluePrintErrorInfo!");
|
---|
| 106 | Log(("ERROR: std::bad_alloc in GluePrintErrorInfo!\n"));
|
---|
[38509] | 107 | }
|
---|
[16554] | 108 | }
|
---|
| 109 |
|
---|
[89720] | 110 | void GluePrintErrorContext(const char *pcszContext, const char *pcszSourceFile, uint32_t ulLine, bool fWarning /* = false */)
|
---|
[16580] | 111 | {
|
---|
| 112 | // pcszSourceFile comes from __FILE__ macro, which always contains the full path,
|
---|
| 113 | // which we don't want to see printed:
|
---|
| 114 | // print and log
|
---|
[72944] | 115 | const char *pszFilenameOnly = RTPathFilename(pcszSourceFile);
|
---|
[89720] | 116 | if (!fWarning)
|
---|
| 117 | {
|
---|
| 118 | RTMsgError("Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly);
|
---|
| 119 | Log(("ERROR: Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly));
|
---|
| 120 | }
|
---|
| 121 | else
|
---|
| 122 | {
|
---|
| 123 | RTMsgWarning("Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly);
|
---|
| 124 | Log(("WARNING: Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly));
|
---|
| 125 | }
|
---|
[16580] | 126 | }
|
---|
| 127 |
|
---|
[98297] | 128 | void GluePrintRCMessage(HRESULT hrc)
|
---|
[16554] | 129 | {
|
---|
| 130 | // print and log
|
---|
[98297] | 131 | if (FAILED(hrc))
|
---|
[89720] | 132 | {
|
---|
[98297] | 133 | RTMsgError("Code %Rhra (extended info not available)\n", hrc);
|
---|
| 134 | Log(("ERROR: Code %Rhra (extended info not available)\n", hrc));
|
---|
[89720] | 135 | }
|
---|
| 136 | else
|
---|
| 137 | {
|
---|
[98297] | 138 | RTMsgWarning("Code %Rhra (extended info not available)\n", hrc);
|
---|
| 139 | Log(("WARNING: Code %Rhra (extended info not available)\n", hrc));
|
---|
[89720] | 140 | }
|
---|
[16554] | 141 | }
|
---|
| 142 |
|
---|
[38509] | 143 | static void glueHandleComErrorInternal(com::ErrorInfo &info,
|
---|
| 144 | const char *pcszContext,
|
---|
[98297] | 145 | HRESULT hrc,
|
---|
[38509] | 146 | const char *pcszSourceFile,
|
---|
| 147 | uint32_t ulLine)
|
---|
| 148 | {
|
---|
[42167] | 149 | if (info.isFullAvailable() || info.isBasicAvailable())
|
---|
[38509] | 150 | {
|
---|
[42167] | 151 | const com::ErrorInfo *pInfo = &info;
|
---|
| 152 | do
|
---|
| 153 | {
|
---|
[38509] | 154 | GluePrintErrorInfo(*pInfo);
|
---|
[42167] | 155 |
|
---|
[98297] | 156 | /* Use hrc for figuring out if there were just warnings. */
|
---|
| 157 | HRESULT hrc2 = pInfo->getResultCode();
|
---|
| 158 | if ( (SUCCEEDED_WARNING(hrc) && FAILED(hrc2))
|
---|
| 159 | || (SUCCEEDED(hrc) && (FAILED(hrc2) || SUCCEEDED_WARNING(hrc2))))
|
---|
| 160 | hrc = hrc2;
|
---|
[89720] | 161 |
|
---|
[42167] | 162 | pInfo = pInfo->getNext();
|
---|
| 163 | /* If there is more than one error, separate them visually. */
|
---|
| 164 | if (pInfo)
|
---|
| 165 | {
|
---|
| 166 | /* If there are several errors then at least basic error
|
---|
| 167 | * information must be available, otherwise something went
|
---|
| 168 | * horribly wrong. */
|
---|
| 169 | Assert(pInfo->isFullAvailable() || pInfo->isBasicAvailable());
|
---|
| 170 |
|
---|
| 171 | RTMsgError("--------\n");
|
---|
| 172 | }
|
---|
[98297] | 173 | } while (pInfo);
|
---|
[38509] | 174 | }
|
---|
[42167] | 175 | else
|
---|
[98297] | 176 | GluePrintRCMessage(hrc);
|
---|
[38509] | 177 |
|
---|
[86141] | 178 | if (pcszContext != NULL || pcszSourceFile != NULL)
|
---|
[98297] | 179 | GluePrintErrorContext(pcszContext, pcszSourceFile, ulLine, SUCCEEDED_WARNING(hrc));
|
---|
[38509] | 180 | }
|
---|
| 181 |
|
---|
[16554] | 182 | void GlueHandleComError(ComPtr<IUnknown> iface,
|
---|
| 183 | const char *pcszContext,
|
---|
[98297] | 184 | HRESULT hrc,
|
---|
[16554] | 185 | const char *pcszSourceFile,
|
---|
| 186 | uint32_t ulLine)
|
---|
| 187 | {
|
---|
[38509] | 188 | /* If we have full error info, print something nice, and start with the
|
---|
| 189 | * actual error message. */
|
---|
[30681] | 190 | com::ErrorInfo info(iface, COM_IIDOF(IUnknown));
|
---|
[38509] | 191 |
|
---|
| 192 | glueHandleComErrorInternal(info,
|
---|
| 193 | pcszContext,
|
---|
[98297] | 194 | hrc,
|
---|
[38509] | 195 | pcszSourceFile,
|
---|
| 196 | ulLine);
|
---|
| 197 |
|
---|
[16554] | 198 | }
|
---|
| 199 |
|
---|
[98297] | 200 | void GlueHandleComErrorNoCtx(ComPtr<IUnknown> iface, HRESULT hrc)
|
---|
[86141] | 201 | {
|
---|
[98297] | 202 | GlueHandleComError(iface, NULL, hrc, NULL, 0);
|
---|
[86141] | 203 | }
|
---|
| 204 |
|
---|
[38509] | 205 | void GlueHandleComErrorProgress(ComPtr<IProgress> progress,
|
---|
| 206 | const char *pcszContext,
|
---|
[98297] | 207 | HRESULT hrc,
|
---|
[38509] | 208 | const char *pcszSourceFile,
|
---|
| 209 | uint32_t ulLine)
|
---|
| 210 | {
|
---|
| 211 | /* Get the error info out of the progress object. */
|
---|
| 212 | ProgressErrorInfo ei(progress);
|
---|
[16554] | 213 |
|
---|
[38509] | 214 | glueHandleComErrorInternal(ei,
|
---|
| 215 | pcszContext,
|
---|
[98297] | 216 | hrc,
|
---|
[38509] | 217 | pcszSourceFile,
|
---|
| 218 | ulLine);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[16554] | 221 | } /* namespace com */
|
---|
| 222 |
|
---|