VirtualBox

source: vbox/trunk/src/VBox/Main/glue/errorprint.cpp@ 72944

Last change on this file since 72944 was 72944, checked in by vboxsync, 7 years ago

GluePrintErrorContext: In fact, don't make any copy of anything here. RTMsgError does an excellent job deal without out-of-memory and similar mishaps, Utf8Str doesn't.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: errorprint.cpp 72944 2018-07-07 15:27:11Z vboxsync $ */
2
3/** @file
4 * MS COM / XPCOM Abstraction Layer:
5 * Error info print helpers. This implements the shared code from the macros from errorprint.h.
6 */
7
8/*
9 * Copyright (C) 2009-2017 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21#include <VBox/com/ErrorInfo.h>
22#include <VBox/com/errorprint.h>
23#include <VBox/log.h>
24
25#include <ProgressImpl.h>
26
27#include <iprt/stream.h>
28#include <iprt/message.h>
29#include <iprt/path.h>
30
31namespace com
32{
33
34void GluePrintErrorInfo(const com::ErrorInfo &info)
35{
36 bool haveResultCode = false;
37#if defined (RT_OS_WIN)
38 haveResultCode = info.isFullAvailable();
39 bool haveComponent = true;
40 bool haveInterfaceID = true;
41#else /* defined (RT_OS_WIN) */
42 haveResultCode = true;
43 bool haveComponent = info.isFullAvailable();
44 bool haveInterfaceID = info.isFullAvailable();
45#endif
46
47 Utf8Str str;
48 RTCList<Utf8Str> comp;
49
50 Bstr bstrDetailsText = info.getText();
51 if (!bstrDetailsText.isEmpty())
52 str = Utf8StrFmt("%ls\n",
53 bstrDetailsText.raw());
54 if (haveResultCode)
55 comp.append(Utf8StrFmt("code %Rhrc (0x%RX32)",
56 info.getResultCode(),
57 info.getResultCode()));
58 if (haveComponent)
59 comp.append(Utf8StrFmt("component %ls",
60 info.getComponent().raw()));
61 if (haveInterfaceID)
62 comp.append(Utf8StrFmt("interface %ls",
63 info.getInterfaceName().raw()));
64 if (!info.getCalleeName().isEmpty())
65 comp.append(Utf8StrFmt("callee %ls",
66 info.getCalleeName().raw()));
67
68 if (comp.size() > 0)
69 {
70 str += "Details: ";
71 for (size_t i = 0; i < comp.size() - 1; ++i)
72 str += comp.at(i) + ", ";
73 str += comp.last();
74 str += "\n";
75 }
76
77 // print and log
78 RTMsgError("%s", str.c_str());
79 Log(("ERROR: %s", str.c_str()));
80}
81
82void GluePrintErrorContext(const char *pcszContext, const char *pcszSourceFile, uint32_t ulLine)
83{
84 // pcszSourceFile comes from __FILE__ macro, which always contains the full path,
85 // which we don't want to see printed:
86 // print and log
87 const char *pszFilenameOnly = RTPathFilename(pcszSourceFile);
88 RTMsgError("Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly);
89 Log(("Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly));
90}
91
92void GluePrintRCMessage(HRESULT rc)
93{
94 Utf8Str str = Utf8StrFmt("Code %Rhra (extended info not available)\n", rc);
95 // print and log
96 RTMsgError("%s", str.c_str());
97 Log(("ERROR: %s", str.c_str()));
98}
99
100static void glueHandleComErrorInternal(com::ErrorInfo &info,
101 const char *pcszContext,
102 HRESULT rc,
103 const char *pcszSourceFile,
104 uint32_t ulLine)
105{
106 if (info.isFullAvailable() || info.isBasicAvailable())
107 {
108 const com::ErrorInfo *pInfo = &info;
109 do
110 {
111 GluePrintErrorInfo(*pInfo);
112
113 pInfo = pInfo->getNext();
114 /* If there is more than one error, separate them visually. */
115 if (pInfo)
116 {
117 /* If there are several errors then at least basic error
118 * information must be available, otherwise something went
119 * horribly wrong. */
120 Assert(pInfo->isFullAvailable() || pInfo->isBasicAvailable());
121
122 RTMsgError("--------\n");
123 }
124 }
125 while (pInfo);
126 }
127 else
128 GluePrintRCMessage(rc);
129
130 GluePrintErrorContext(pcszContext, pcszSourceFile, ulLine);
131}
132
133void GlueHandleComError(ComPtr<IUnknown> iface,
134 const char *pcszContext,
135 HRESULT rc,
136 const char *pcszSourceFile,
137 uint32_t ulLine)
138{
139 /* If we have full error info, print something nice, and start with the
140 * actual error message. */
141 com::ErrorInfo info(iface, COM_IIDOF(IUnknown));
142
143 glueHandleComErrorInternal(info,
144 pcszContext,
145 rc,
146 pcszSourceFile,
147 ulLine);
148
149}
150
151void GlueHandleComErrorProgress(ComPtr<IProgress> progress,
152 const char *pcszContext,
153 HRESULT rc,
154 const char *pcszSourceFile,
155 uint32_t ulLine)
156{
157 /* Get the error info out of the progress object. */
158 ProgressErrorInfo ei(progress);
159
160 glueHandleComErrorInternal(ei,
161 pcszContext,
162 rc,
163 pcszSourceFile,
164 ulLine);
165}
166
167} /* namespace com */
168
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette