summaryrefslogtreecommitdiff
path: root/src/VBox/Main/glue/errorprint.cpp
blob: d966d264396c92a83b86d1b87122c17113146262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* $Id$ */

/** @file
 * MS COM / XPCOM Abstraction Layer:
 * Error info print helpers. This implements the shared code from the macros from errorprint.h.
 */

/*
 * Copyright (C) 2009-2022 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


#include <VBox/com/ErrorInfo.h>
#include <VBox/com/errorprint.h>
#include <VBox/log.h>

#include <ProgressImpl.h>

#include <iprt/stream.h>
#include <iprt/message.h>
#include <iprt/path.h>

namespace com
{

void GluePrintErrorInfo(const com::ErrorInfo &info)
{
    bool haveResultCode = false;
#if defined (RT_OS_WIN)
    haveResultCode = info.isFullAvailable();
    bool haveComponent = true;
    bool haveInterfaceID = true;
#else /* defined (RT_OS_WIN) */
    haveResultCode = true;
    bool haveComponent = info.isFullAvailable();
    bool haveInterfaceID = info.isFullAvailable();
#endif

    try
    {
        HRESULT rc = S_OK;
        Utf8Str str;
        RTCList<Utf8Str> comp;

        Bstr bstrDetailsText = info.getText();
        if (!bstrDetailsText.isEmpty())
            str = Utf8StrFmt("%ls\n",
                             bstrDetailsText.raw());
        if (haveResultCode)
        {
            rc = info.getResultCode();
            comp.append(Utf8StrFmt("code %Rhrc (0x%RX32)", rc, rc));
        }
        if (haveComponent)
            comp.append(Utf8StrFmt("component %ls",
                                   info.getComponent().raw()));
        if (haveInterfaceID)
            comp.append(Utf8StrFmt("interface %ls",
                                   info.getInterfaceName().raw()));
        if (!info.getCalleeName().isEmpty())
            comp.append(Utf8StrFmt("callee %ls",
                                   info.getCalleeName().raw()));

        if (comp.size() > 0)
        {
            str += "Details: ";
            for (size_t i = 0; i < comp.size() - 1; ++i)
                str += comp.at(i) + ", ";
            str += comp.last();
            str += "\n";
        }

        // print and log
        if (FAILED(rc))
        {
            RTMsgError("%s", str.c_str());
            Log(("ERROR: %s", str.c_str()));
        }
        else
        {
            RTMsgWarning("%s", str.c_str());
            Log(("WARNING: %s", str.c_str()));
        }
    }
    catch (std::bad_alloc &)
    {
        RTMsgError("std::bad_alloc in GluePrintErrorInfo!");
        Log(("ERROR: std::bad_alloc in GluePrintErrorInfo!\n"));
    }
}

void GluePrintErrorContext(const char *pcszContext, const char *pcszSourceFile, uint32_t ulLine, bool fWarning /* = false */)
{
    // pcszSourceFile comes from __FILE__ macro, which always contains the full path,
    // which we don't want to see printed:
    // print and log
    const char *pszFilenameOnly = RTPathFilename(pcszSourceFile);
    if (!fWarning)
    {
        RTMsgError("Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly);
        Log(("ERROR: Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly));
    }
    else
    {
        RTMsgWarning("Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly);
        Log(("WARNING: Context: \"%s\" at line %d of file %s\n", pcszContext, ulLine, pszFilenameOnly));
    }
}

void GluePrintRCMessage(HRESULT rc)
{
    // print and log
    if (FAILED(rc))
    {
        RTMsgError("Code %Rhra (extended info not available)\n", rc);
        Log(("ERROR: Code %Rhra (extended info not available)\n", rc));
    }
    else
    {
        RTMsgWarning("Code %Rhra (extended info not available)\n", rc);
        Log(("WARNING: Code %Rhra (extended info not available)\n", rc));
    }
}

static void glueHandleComErrorInternal(com::ErrorInfo &info,
                                       const char *pcszContext,
                                       HRESULT rc,
                                       const char *pcszSourceFile,
                                       uint32_t ulLine)
{
    if (info.isFullAvailable() || info.isBasicAvailable())
    {
        const com::ErrorInfo *pInfo = &info;
        do
        {
            GluePrintErrorInfo(*pInfo);

            /* Use rc for figuring out if there were just warnings. */
            HRESULT rc2 = pInfo->getResultCode();
            if (   (SUCCEEDED_WARNING(rc) && FAILED(rc2))
                || (SUCCEEDED(rc) && (FAILED(rc2) || SUCCEEDED_WARNING(rc2))))
                rc = rc2;

            pInfo = pInfo->getNext();
            /* If there is more than one error, separate them visually. */
            if (pInfo)
            {
                /* If there are several errors then at least basic error
                 * information must be available, otherwise something went
                 * horribly wrong. */
                Assert(pInfo->isFullAvailable() || pInfo->isBasicAvailable());

                RTMsgError("--------\n");
            }
        }
        while (pInfo);
    }
    else
        GluePrintRCMessage(rc);

    if (pcszContext != NULL || pcszSourceFile != NULL)
        GluePrintErrorContext(pcszContext, pcszSourceFile, ulLine, SUCCEEDED_WARNING(rc));
}

void GlueHandleComError(ComPtr<IUnknown> iface,
                        const char *pcszContext,
                        HRESULT rc,
                        const char *pcszSourceFile,
                        uint32_t ulLine)
{
    /* If we have full error info, print something nice, and start with the
     * actual error message. */
    com::ErrorInfo info(iface, COM_IIDOF(IUnknown));

    glueHandleComErrorInternal(info,
                               pcszContext,
                               rc,
                               pcszSourceFile,
                               ulLine);

}

void GlueHandleComErrorNoCtx(ComPtr<IUnknown> iface, HRESULT rc)
{
    GlueHandleComError(iface, NULL, rc, NULL, 0);
}

void GlueHandleComErrorProgress(ComPtr<IProgress> progress,
                                const char *pcszContext,
                                HRESULT rc,
                                const char *pcszSourceFile,
                                uint32_t ulLine)
{
    /* Get the error info out of the progress object. */
    ProgressErrorInfo ei(progress);

    glueHandleComErrorInternal(ei,
                               pcszContext,
                               rc,
                               pcszSourceFile,
                               ulLine);
}

} /* namespace com */