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