summaryrefslogtreecommitdiff
path: root/src/VBox/Main/glue/errorprint.cpp
blob: fbd1fbb41a0e0bf4b7497209b44fd63ee8dc8545 (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
/* $Id: errorprint.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */

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

/*
 * Copyright (C) 2009 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */


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

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

namespace com
{


void GluePrintErrorInfo(com::ErrorInfo &info)
{
    Utf8Str str = Utf8StrFmt("ERROR: %ls\n"
                             "Details: code %Rhrc (0x%RX32), component %ls, interface %ls, callee %ls\n"
                             ,
                             info.getText().raw(),
                             info.getResultCode(),
                             info.getResultCode(),
                             info.getComponent().raw(),
                             info.getInterfaceName().raw(),
                             info.getCalleeName().raw());
    // print and log
    RTPrintf("%s", str.c_str());
    Log(("%s", str.c_str()));
}

void GluePrintErrorContext(const char *pcszContext, const char *pcszSourceFile, uint32_t ulLine)
{
    // pcszSourceFile comes from __FILE__ macro, which always contains the full path,
    // which we don't want to see printed:
    Utf8Str strFilename(RTPathFilename(pcszSourceFile));
    Utf8Str str = Utf8StrFmt("Context: \"%s\" at line %d of file %s\n",
                                pcszContext,
                                ulLine,
                                strFilename.c_str());
    // print and log
    RTPrintf("%s", str.c_str());
    Log(("%s", str.c_str()));
}

void GluePrintRCMessage(HRESULT rc)
{
    Utf8Str str = Utf8StrFmt("ERROR: code %Rhra (extended info not available)\n", rc);
    // print and log
    RTPrintf("%s", str.c_str());
    Log(("%s", str.c_str()));
}

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);
    if (info.isFullAvailable() || info.isBasicAvailable())
        GluePrintErrorInfo(info);
    else
        GluePrintRCMessage(rc);
    GluePrintErrorContext(pcszContext, pcszSourceFile, ulLine);
}


} /* namespace com */