|
// ###########################################################################
// #
// # file:
// # Xept.cpp
// #
// ###########################################################################
#include "stdafx.h"
#pragma warning(disable:4786)
#include "xept.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static TCHAR THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////////////
// some helpers.
std::string GetLastErrorMsg( const DWORD last_error )
{
void *msgbuf;
if ( !FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
0, last_error,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
reinterpret_cast<LPTSTR>( &msgbuf ), 0, 0 ) )
return (LPCTSTR)_T("");
else {
const CString str = static_cast<LPCSTR>( msgbuf );
LocalFree( msgbuf );
return (LPCTSTR)str;
}
}
std::string GetExceptionErrorMsg( CException *const e, const bool del_lf )
{
enum { bufsize=10000 };
TCHAR buf[bufsize];
memset( buf, 0, sizeof buf );
VERIFY( e->GetErrorMessage( buf, bufsize ) );
CString str = buf;
if ( del_lf ) { // remove any "\r" and "\n".
while ( true ) {
const int pos = str.FindOneOf( "\r\n");
if ( pos==-1 )
break;
else {
const CString tmpL = str.Left( pos );
const CString tmpR = str.Right( str.GetLength()-(pos+1) );
str = tmpL+tmpR;
}
}
}
return (LPCTSTR)str;
}
//////////////////////////////////////////////////////////////////////////////
// base exception class.
Xept::Xept() :
m_LastError(::GetLastError()),
m_Line(0)
{
}
Xept::Xept( const TCHAR *const file, const int line,
const TCHAR *const text, ... ) :
m_Line(0),
m_LastError(::GetLastError())
{
va_list lst;
va_start( lst, text );
Xept::Fmt( file, line, text, lst );
va_end( lst );
}
const char* Xept::what() const
{
// last error.
CString last_error;
const CString last_error_msg=::GetLastErrorMsg( m_LastError ).c_str();
if ( !last_error_msg.IsEmpty() )
last_error.Format( _T(" (%s)"), (LPCTSTR)last_error_msg );
last_error.Replace( _T("\r"), _T("") );
last_error.Replace( _T("\n"), _T("") );
m_ConstructWhat = m_Text.c_str()+last_error;
return m_ConstructWhat.c_str();
}
void Xept::Fmt( const TCHAR *const file, const int line,
const TCHAR *const text, va_list arg_list )
{
m_File = file;
m_Line = line;
// occurence of the error in the source file.
TCHAR name[1000];
TCHAR ext [1000];
_tsplitpath( file, 0, 0, name, ext );
CString source;
source.Format( _T("%s%s (%d) "), name, ext, line );
enum { bufsize=10000 };
TCHAR buf[bufsize];
memset( buf, 0, sizeof buf );
vsprintf( buf, text, arg_list );
va_end( arg_list );
m_Text = source+buf;
}
//////////////////////////////////////////////////////////////////////////////
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.