errorprint.cpp revision 15e9181ff43bf09ce40eaab73cee8c8492f99878
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync/* $Id$ */
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync/** @file
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * MS COM / XPCOM Abstraction Layer:
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * Error info print helpers. This implements the shared code from the macros from errorprint.h.
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync */
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync/*
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * Copyright (C) 2009 Oracle Corporation
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync *
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * available from http://www.virtualbox.org. This file is free software;
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * you can redistribute it and/or modify it under the terms of the GNU
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * General Public License (GPL) as published by the Free Software
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync */
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync#include <VBox/com/ErrorInfo.h>
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync#include <VBox/com/errorprint.h>
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync#include <VBox/log.h>
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync#include <iprt/stream.h>
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync#include <iprt/path.h>
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsyncnamespace com
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync{
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsyncvoid GluePrintErrorInfo(com::ErrorInfo &info)
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync{
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync Utf8Str str = Utf8StrFmt("ERROR: %ls\n"
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync "Details: code %Rhrc (0x%RX32), component %ls, interface %ls, callee %ls\n"
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync ,
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync info.getText().raw(),
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync info.getResultCode(),
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync info.getResultCode(),
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync info.getComponent().raw(),
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync info.getInterfaceName().raw(),
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync info.getCalleeName().raw());
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync // print and log
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync RTPrintf("%s", str.c_str());
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync Log(("%s", str.c_str()));
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync}
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsyncvoid GluePrintErrorContext(const char *pcszContext, const char *pcszSourceFile, uint32_t ulLine)
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync{
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync // pcszSourceFile comes from __FILE__ macro, which always contains the full path,
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync // which we don't want to see printed:
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync Utf8Str strFilename(RTPathFilename(pcszSourceFile));
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync Utf8Str str = Utf8StrFmt("Context: \"%s\" at line %d of file %s\n",
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync pcszContext,
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync ulLine,
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync strFilename.c_str());
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync // print and log
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync RTPrintf("%s", str.c_str());
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync Log(("%s", str.c_str()));
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync}
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsyncvoid GluePrintRCMessage(HRESULT rc)
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync{
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync Utf8Str str = Utf8StrFmt("ERROR: code %Rhra (extended info not available)\n", rc);
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync // print and log
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync RTPrintf("%s", str.c_str());
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync Log(("%s", str.c_str()));
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync}
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsyncvoid GlueHandleComError(ComPtr<IUnknown> iface,
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync const char *pcszContext,
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync HRESULT rc,
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync const char *pcszSourceFile,
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync uint32_t ulLine)
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync{
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync // if we have full error info, print something nice, and start with the actual error message
34edf41b7b7cb5219f0ce1b172e131ae7520ae83vboxsync com::ErrorInfo info(iface, COM_IIDOF(IUnknown));
if (info.isFullAvailable() || info.isBasicAvailable())
GluePrintErrorInfo(info);
else
GluePrintRCMessage(rc);
GluePrintErrorContext(pcszContext, pcszSourceFile, ulLine);
}
} /* namespace com */