2011-12-14 00:35:28 +03:00
# include "StdInc.h"
2007-08-17 20:42:21 +03:00
# include "CConsoleHandler.h"
2011-12-14 00:35:28 +03:00
2011-02-24 15:57:47 +02:00
# include "CThreadHelper.h"
2008-09-18 23:24:53 +03:00
2009-07-31 23:10:22 +03:00
/*
* CConsoleHandler . cpp , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
2009-06-23 11:14:49 +03:00
2013-04-09 17:31:36 +03:00
boost : : mutex CConsoleHandler : : smx ;
2013-04-11 18:58:01 +03:00
DLL_LINKAGE CConsoleHandler * console = nullptr ;
2009-06-23 11:14:49 +03:00
# ifndef _WIN32
typedef std : : string TColor ;
2010-12-24 17:37:48 +02:00
# define CONSOLE_GREEN "\x1b[1;32m"
2013-04-02 20:06:43 +03:00
# define CONSOLE_RED "\x1b[1;31m"
2010-12-24 17:37:48 +02:00
# define CONSOLE_MAGENTA "\x1b[1;35m"
2013-04-02 20:06:43 +03:00
# define CONSOLE_YELLOW "\x1b[1;33m"
# define CONSOLE_WHITE "\x1b[1;37m"
2010-12-24 17:37:48 +02:00
# define CONSOLE_GRAY "\x1b[1;30m"
# define CONSOLE_TEAL "\x1b[1;36m"
2009-06-23 11:14:49 +03:00
# else
2009-08-17 16:08:05 +03:00
# define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
2013-01-31 00:28:31 +03:00
# include <Windows.h>
2012-09-15 22:16:16 +03:00
# ifndef __MINGW32__
2009-07-31 23:10:22 +03:00
# include <dbghelp.h>
# pragma comment(lib, "dbghelp.lib")
2012-09-15 22:16:16 +03:00
# endif
2009-06-23 11:14:49 +03:00
typedef WORD TColor ;
HANDLE handleIn ;
HANDLE handleOut ;
# define CONSOLE_GREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
# define CONSOLE_RED FOREGROUND_RED | FOREGROUND_INTENSITY
# define CONSOLE_MAGENTA FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY
# define CONSOLE_YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
# define CONSOLE_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
# define CONSOLE_GRAY FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
2010-11-28 18:39:13 +02:00
# define CONSOLE_TEAL FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
2008-06-21 16:27:52 +03:00
# endif
2009-06-23 11:14:49 +03:00
TColor defColor ;
2009-07-31 23:10:22 +03:00
# ifdef _WIN32
void printWinError ( )
{
//Get error code
int error = GetLastError ( ) ;
if ( ! error )
{
2013-04-09 17:31:36 +03:00
logGlobal - > errorStream ( ) < < " No Win error information set. " ;
2009-07-31 23:10:22 +03:00
return ;
}
2013-04-09 17:31:36 +03:00
logGlobal - > errorStream ( ) < < " Error " < < error < < " encountered: " ;
2009-07-31 23:10:22 +03:00
//Get error description
2013-06-26 14:18:27 +03:00
char * pTemp = nullptr ;
2009-07-31 23:10:22 +03:00
FormatMessageA ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM ,
2013-06-26 14:18:27 +03:00
nullptr , error , MAKELANGID ( LANG_NEUTRAL , SUBLANG_DEFAULT ) , ( LPSTR ) & pTemp , 1 , nullptr ) ;
2013-04-09 17:31:36 +03:00
logGlobal - > errorStream ( ) < < pTemp ;
2009-07-31 23:10:22 +03:00
LocalFree ( pTemp ) ;
}
const char * exceptionName ( DWORD exc )
{
# define EXC_CASE(EXC) case EXCEPTION_##EXC : return "EXCEPTION_" #EXC
switch ( exc )
{
EXC_CASE ( ACCESS_VIOLATION ) ;
EXC_CASE ( DATATYPE_MISALIGNMENT ) ;
EXC_CASE ( BREAKPOINT ) ;
EXC_CASE ( SINGLE_STEP ) ;
EXC_CASE ( ARRAY_BOUNDS_EXCEEDED ) ;
EXC_CASE ( FLT_DENORMAL_OPERAND ) ;
EXC_CASE ( FLT_DIVIDE_BY_ZERO ) ;
EXC_CASE ( FLT_INEXACT_RESULT ) ;
EXC_CASE ( FLT_INVALID_OPERATION ) ;
EXC_CASE ( FLT_OVERFLOW ) ;
EXC_CASE ( FLT_STACK_CHECK ) ;
EXC_CASE ( FLT_UNDERFLOW ) ;
EXC_CASE ( INT_DIVIDE_BY_ZERO ) ;
EXC_CASE ( INT_OVERFLOW ) ;
EXC_CASE ( PRIV_INSTRUCTION ) ;
EXC_CASE ( IN_PAGE_ERROR ) ;
EXC_CASE ( ILLEGAL_INSTRUCTION ) ;
EXC_CASE ( NONCONTINUABLE_EXCEPTION ) ;
EXC_CASE ( STACK_OVERFLOW ) ;
EXC_CASE ( INVALID_DISPOSITION ) ;
EXC_CASE ( GUARD_PAGE ) ;
EXC_CASE ( INVALID_HANDLE ) ;
default :
return " UNKNOWN EXCEPTION " ;
}
# undef EXC_CASE
}
LONG WINAPI onUnhandledException ( EXCEPTION_POINTERS * exception )
{
2013-04-09 17:31:36 +03:00
logGlobal - > errorStream ( ) < < " Disaster happened. " ;
2009-07-31 23:10:22 +03:00
PEXCEPTION_RECORD einfo = exception - > ExceptionRecord ;
2013-04-09 17:31:36 +03:00
logGlobal - > errorStream ( ) < < " Reason: 0x " < < std : : hex < < einfo - > ExceptionCode < < " - " < < exceptionName ( einfo - > ExceptionCode )
< < " at " < < std : : setfill ( ' 0 ' ) < < std : : setw ( 4 ) < < exception - > ContextRecord - > SegCs < < " : " < < ( void * ) einfo - > ExceptionAddress ;
2009-07-31 23:10:22 +03:00
if ( einfo - > ExceptionCode = = EXCEPTION_ACCESS_VIOLATION )
{
2013-04-09 17:31:36 +03:00
logGlobal - > errorStream ( ) < < " Attempt to " < < ( einfo - > ExceptionInformation [ 0 ] = = 1 ? " write to " : " read from " )
< < " 0x " < < std : : setw ( 8 ) < < ( void * ) einfo - > ExceptionInformation [ 1 ] ;
2009-07-31 23:10:22 +03:00
}
const DWORD threadId = : : GetCurrentThreadId ( ) ;
2013-04-09 17:31:36 +03:00
logGlobal - > errorStream ( ) < < " Thread ID: " < < threadId < < " [ " < < std : : dec < < std : : setw ( 0 ) < < threadId < < " ] " ;
2009-07-31 23:10:22 +03:00
2012-09-15 22:16:16 +03:00
# ifndef __MINGW32__
2009-07-31 23:10:22 +03:00
//exception info to be placed in the dump
MINIDUMP_EXCEPTION_INFORMATION meinfo = { threadId , exception , TRUE } ;
//create file where dump will be placed
2013-06-26 14:18:27 +03:00
char * mname = nullptr ;
2009-07-31 23:10:22 +03:00
char buffer [ MAX_PATH + 1 ] ;
2013-06-26 14:18:27 +03:00
HMODULE hModule = nullptr ;
2009-07-31 23:10:22 +03:00
GetModuleFileNameA ( hModule , buffer , MAX_PATH ) ;
mname = strrchr ( buffer , ' \\ ' ) ;
if ( mname ! = 0 )
mname + + ;
else
mname = buffer ;
strcat ( mname , " _crashinfo.dmp " ) ;
HANDLE dfile = CreateFileA ( mname , GENERIC_READ | GENERIC_WRITE , FILE_SHARE_WRITE | FILE_SHARE_READ , 0 , CREATE_ALWAYS , 0 , 0 ) ;
2013-04-09 17:31:36 +03:00
logGlobal - > errorStream ( ) < < " Crash info will be put in " < < mname ;
2009-07-31 23:10:22 +03:00
MiniDumpWriteDump ( GetCurrentProcess ( ) , GetCurrentProcessId ( ) , dfile , MiniDumpWithDataSegs , & meinfo , 0 , 0 ) ;
2012-09-15 22:16:16 +03:00
# endif
2009-07-31 23:10:22 +03:00
MessageBoxA ( 0 , " VCMI has crashed. We are sorry. File with information about encountered problem has been created. " , " VCMI Crashhandler " , MB_OK | MB_ICONERROR ) ;
return EXCEPTION_EXECUTE_HANDLER ;
}
# endif
2008-06-21 16:27:52 +03:00
2009-04-15 17:03:31 +03:00
2013-04-05 13:29:46 +03:00
void CConsoleHandler : : setColor ( EConsoleTextColor : : EConsoleTextColor color )
2008-09-17 13:18:22 +03:00
{
2013-04-05 13:29:46 +03:00
TColor colorCode ;
switch ( color )
2008-09-17 13:18:22 +03:00
{
2013-04-05 13:29:46 +03:00
case EConsoleTextColor : : DEFAULT :
colorCode = defColor ;
2008-09-17 13:18:22 +03:00
break ;
2013-04-05 13:29:46 +03:00
case EConsoleTextColor : : GREEN :
colorCode = CONSOLE_GREEN ;
2008-09-17 13:18:22 +03:00
break ;
2013-04-05 13:29:46 +03:00
case EConsoleTextColor : : RED :
colorCode = CONSOLE_RED ;
2008-09-17 13:18:22 +03:00
break ;
2013-04-05 13:29:46 +03:00
case EConsoleTextColor : : MAGENTA :
colorCode = CONSOLE_MAGENTA ;
2008-09-17 13:18:22 +03:00
break ;
2013-04-05 13:29:46 +03:00
case EConsoleTextColor : : YELLOW :
colorCode = CONSOLE_YELLOW ;
2008-09-17 13:18:22 +03:00
break ;
2013-04-05 13:29:46 +03:00
case EConsoleTextColor : : WHITE :
colorCode = CONSOLE_WHITE ;
2008-09-17 13:18:22 +03:00
break ;
2013-04-05 13:29:46 +03:00
case EConsoleTextColor : : GRAY :
colorCode = CONSOLE_GRAY ;
2008-09-17 13:18:22 +03:00
break ;
2013-04-05 13:29:46 +03:00
case EConsoleTextColor : : TEAL :
colorCode = CONSOLE_TEAL ;
2010-11-28 18:39:13 +02:00
break ;
2008-09-17 13:18:22 +03:00
default :
2013-04-05 13:29:46 +03:00
colorCode = defColor ;
2008-09-17 13:18:22 +03:00
break ;
}
2008-09-18 23:24:53 +03:00
# ifdef _WIN32
2013-04-05 13:29:46 +03:00
SetConsoleTextAttribute ( handleOut , colorCode ) ;
2008-09-18 23:24:53 +03:00
# else
2013-04-05 13:29:46 +03:00
std : : cout < < colorCode ;
2008-09-17 13:18:22 +03:00
# endif
}
2008-06-21 16:27:52 +03:00
2008-09-17 13:18:22 +03:00
int CConsoleHandler : : run ( )
2007-08-15 18:13:11 +03:00
{
2012-06-27 23:44:01 +03:00
setThreadName ( " CConsoleHandler::run " ) ;
2011-01-15 19:30:46 +02:00
//disabling sync to make in_avail() work (othervice always returns 0)
std : : ios : : sync_with_stdio ( false ) ;
std : : string buffer ;
while ( std : : cin . good ( ) )
2007-08-15 18:13:11 +03:00
{
2011-02-24 15:57:47 +02:00
# ifndef _WIN32
2011-01-15 19:30:46 +02:00
//check if we have some unreaded symbols
if ( std : : cin . rdbuf ( ) - > in_avail ( ) )
{
if ( getline ( std : : cin , buffer ) . good ( ) )
if ( cb & & * cb )
( * cb ) ( buffer ) ;
}
else
boost : : this_thread : : sleep ( boost : : posix_time : : millisec ( 100 ) ) ;
boost : : this_thread : : interruption_point ( ) ;
2011-02-24 15:57:47 +02:00
# else
std : : getline ( std : : cin , buffer ) ;
if ( cb & & * cb )
( * cb ) ( buffer ) ;
# endif
2007-08-15 18:13:11 +03:00
}
return - 1 ;
}
2013-04-05 13:29:46 +03:00
CConsoleHandler : : CConsoleHandler ( ) : thread ( nullptr )
2007-08-15 18:13:11 +03:00
{
2008-09-18 23:24:53 +03:00
# ifdef _WIN32
2008-09-17 13:18:22 +03:00
handleIn = GetStdHandle ( STD_INPUT_HANDLE ) ;
handleOut = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
CONSOLE_SCREEN_BUFFER_INFO csbi ;
GetConsoleScreenBufferInfo ( handleOut , & csbi ) ;
defColor = csbi . wAttributes ;
2009-08-02 17:21:18 +03:00
# ifndef _DEBUG
2009-07-31 23:10:22 +03:00
SetUnhandledExceptionFilter ( onUnhandledException ) ;
2009-08-02 17:21:18 +03:00
# endif
2008-09-18 23:24:53 +03:00
# else
defColor = " \x1b [0m " ;
2008-09-17 13:18:22 +03:00
# endif
2013-06-26 14:18:27 +03:00
cb = new std : : function < void ( const std : : string & ) > ;
2007-08-15 18:13:11 +03:00
}
2008-09-17 13:18:22 +03:00
CConsoleHandler : : ~ CConsoleHandler ( )
{
2013-04-09 17:31:36 +03:00
logGlobal - > infoStream ( ) < < " Killing console... " ;
2009-10-26 07:39:30 +02:00
end ( ) ;
2008-09-17 13:18:22 +03:00
delete cb ;
2013-04-09 17:31:36 +03:00
logGlobal - > infoStream ( ) < < " Killing console... done! " ;
2008-09-17 13:18:22 +03:00
}
2009-06-23 11:14:49 +03:00
void CConsoleHandler : : end ( )
2008-09-18 23:24:53 +03:00
{
2012-09-15 22:16:16 +03:00
if ( thread )
2011-02-24 15:57:47 +02:00
{
# ifndef _WIN32
2011-01-15 19:30:46 +02:00
thread - > interrupt ( ) ;
2011-02-24 15:57:47 +02:00
# else
TerminateThread ( thread - > native_handle ( ) , 0 ) ;
# endif
2009-10-26 07:39:30 +02:00
thread - > join ( ) ;
delete thread ;
2013-06-26 14:18:27 +03:00
thread = nullptr ;
2009-10-26 07:39:30 +02:00
}
2008-09-18 23:24:53 +03:00
}
2009-06-23 11:14:49 +03:00
void CConsoleHandler : : start ( )
{
2013-07-02 18:23:32 +03:00
thread = new boost : : thread ( boost : : bind ( & CConsoleHandler : : run , console ) ) ;
2009-10-26 07:39:30 +02:00
}