CConsoleHandler.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #define VCMI_DLL
  2. #include "stdafx.h"
  3. #include "CConsoleHandler.h"
  4. #include <boost/function.hpp>
  5. #include <boost/thread.hpp>
  6. #include <iomanip>
  7. /*
  8. * CConsoleHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. #ifndef _WIN32
  17. typedef std::string TColor;
  18. #define _kill_thread(a) pthread_cancel(a)
  19. typedef pthread_t ThreadHandle;
  20. #define CONSOLE_GREEN "\x1b[1;40;32m"
  21. #define CONSOLE_RED "\x1b[1;40;32m"
  22. #define CONSOLE_MAGENTA "\x1b[1;40;35m"
  23. #define CONSOLE_YELLOW "\x1b[1;40;32m"
  24. #define CONSOLE_WHITE "\x1b[1;40;39m"
  25. #define CONSOLE_GRAY "\x1b[0;40;39m"
  26. #else
  27. #define WIN32_LEAN_AND_MEAN //excludes rarely used stuff from windows headers - delete this line if something is missing
  28. #include <windows.h>
  29. #include <dbghelp.h>
  30. #pragma comment(lib, "dbghelp.lib")
  31. typedef WORD TColor;
  32. #define _kill_thread(a) TerminateThread(a,0)
  33. HANDLE handleIn;
  34. HANDLE handleOut;
  35. typedef void* ThreadHandle;
  36. #define CONSOLE_GREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
  37. #define CONSOLE_RED FOREGROUND_RED | FOREGROUND_INTENSITY
  38. #define CONSOLE_MAGENTA FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY
  39. #define CONSOLE_YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
  40. #define CONSOLE_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
  41. #define CONSOLE_GRAY FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
  42. #endif
  43. TColor defColor;
  44. #ifdef _WIN32
  45. void printWinError()
  46. {
  47. //Get error code
  48. int error = GetLastError();
  49. if(!error)
  50. {
  51. tlog0 << "No Win error information set.\n";
  52. return;
  53. }
  54. tlog1 << "Error " << error << " encountered:\n";
  55. //Get error description
  56. char* pTemp = NULL;
  57. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  58. NULL, error, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)&pTemp, 1, NULL);
  59. tlog1 << pTemp << std::endl;
  60. LocalFree( pTemp );
  61. }
  62. const char* exceptionName(DWORD exc)
  63. {
  64. #define EXC_CASE(EXC) case EXCEPTION_##EXC : return "EXCEPTION_" #EXC
  65. switch (exc)
  66. {
  67. EXC_CASE(ACCESS_VIOLATION);
  68. EXC_CASE(DATATYPE_MISALIGNMENT);
  69. EXC_CASE(BREAKPOINT);
  70. EXC_CASE(SINGLE_STEP);
  71. EXC_CASE(ARRAY_BOUNDS_EXCEEDED);
  72. EXC_CASE(FLT_DENORMAL_OPERAND);
  73. EXC_CASE(FLT_DIVIDE_BY_ZERO);
  74. EXC_CASE(FLT_INEXACT_RESULT);
  75. EXC_CASE(FLT_INVALID_OPERATION);
  76. EXC_CASE(FLT_OVERFLOW);
  77. EXC_CASE(FLT_STACK_CHECK);
  78. EXC_CASE(FLT_UNDERFLOW);
  79. EXC_CASE(INT_DIVIDE_BY_ZERO);
  80. EXC_CASE(INT_OVERFLOW);
  81. EXC_CASE(PRIV_INSTRUCTION);
  82. EXC_CASE(IN_PAGE_ERROR);
  83. EXC_CASE(ILLEGAL_INSTRUCTION);
  84. EXC_CASE(NONCONTINUABLE_EXCEPTION);
  85. EXC_CASE(STACK_OVERFLOW);
  86. EXC_CASE(INVALID_DISPOSITION);
  87. EXC_CASE(GUARD_PAGE);
  88. EXC_CASE(INVALID_HANDLE);
  89. default:
  90. return "UNKNOWN EXCEPTION";
  91. }
  92. #undef EXC_CASE
  93. }
  94. LONG WINAPI onUnhandledException(EXCEPTION_POINTERS* exception)
  95. {
  96. tlog1 << "Disaster happened.\n";
  97. PEXCEPTION_RECORD einfo = exception->ExceptionRecord;
  98. tlog1 << "Reason: 0x" << std::hex << einfo->ExceptionCode << " - " << exceptionName(einfo->ExceptionCode);
  99. tlog1 << " at " << std::setfill('0') << std::setw(4) << exception->ContextRecord->SegCs << ":" << (void*)einfo->ExceptionAddress << std::endl;;
  100. if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
  101. {
  102. tlog1 << "Attempt to " << (einfo->ExceptionInformation[0] == 1 ? "write to " : "read from ")
  103. << "0x" << std::setw(8) << (void*)einfo->ExceptionInformation[1] << std::endl;;
  104. }
  105. const DWORD threadId = ::GetCurrentThreadId();
  106. tlog1 << "Thread ID: " << threadId << " [" << std::dec << std::setw(0) << threadId << "]\n";
  107. //exception info to be placed in the dump
  108. MINIDUMP_EXCEPTION_INFORMATION meinfo = {threadId, exception, TRUE};
  109. //create file where dump will be placed
  110. char *mname = NULL;
  111. char buffer[MAX_PATH + 1];
  112. HMODULE hModule = NULL;
  113. GetModuleFileNameA(hModule, buffer, MAX_PATH);
  114. mname = strrchr(buffer, '\\');
  115. if (mname != 0)
  116. mname++;
  117. else
  118. mname = buffer;
  119. strcat(mname, "_crashinfo.dmp");
  120. HANDLE dfile = CreateFileA(mname, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
  121. tlog1 << "Crash info will be put in " << mname << std::endl;
  122. MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), dfile, MiniDumpWithDataSegs, &meinfo, 0, 0);
  123. MessageBoxA(0, "VCMI has crashed. We are sorry. File with information about encountered problem has been created.", "VCMI Crashhandler", MB_OK | MB_ICONERROR);
  124. return EXCEPTION_EXECUTE_HANDLER;
  125. }
  126. #endif
  127. void CConsoleHandler::setColor(int level)
  128. {
  129. TColor color;
  130. switch(level)
  131. {
  132. case -1:
  133. color = defColor;
  134. break;
  135. case 0:
  136. color = CONSOLE_GREEN;
  137. break;
  138. case 1:
  139. color = CONSOLE_RED;
  140. break;
  141. case 2:
  142. color = CONSOLE_MAGENTA;
  143. break;
  144. case 3:
  145. color = CONSOLE_YELLOW;
  146. break;
  147. case 4:
  148. color = CONSOLE_WHITE;
  149. break;
  150. case 5:
  151. color = CONSOLE_GRAY;
  152. break;
  153. default:
  154. color = defColor;
  155. break;
  156. }
  157. #ifdef _WIN32
  158. SetConsoleTextAttribute(handleOut,color);
  159. #else
  160. std::cout << color;
  161. #endif
  162. }
  163. int CConsoleHandler::run()
  164. {
  165. char buffer[5000];
  166. while(true)
  167. {
  168. std::cin.getline(buffer, 5000);
  169. if(cb && *cb)
  170. (*cb)(buffer);
  171. }
  172. return -1;
  173. }
  174. CConsoleHandler::CConsoleHandler()
  175. {
  176. #ifdef _WIN32
  177. handleIn = GetStdHandle(STD_INPUT_HANDLE);
  178. handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
  179. CONSOLE_SCREEN_BUFFER_INFO csbi;
  180. GetConsoleScreenBufferInfo(handleOut,&csbi);
  181. defColor = csbi.wAttributes;
  182. #ifndef _DEBUG
  183. SetUnhandledExceptionFilter(onUnhandledException);
  184. #endif
  185. #else
  186. defColor = "\x1b[0m";
  187. #endif
  188. cb = new boost::function<void(const std::string &)>;
  189. thread = NULL;
  190. }
  191. CConsoleHandler::~CConsoleHandler()
  192. {
  193. delete cb;
  194. delete thread;
  195. }
  196. void CConsoleHandler::end()
  197. {
  198. tlog3 << "Killing console... ";
  199. ThreadHandle th = (ThreadHandle)thread->native_handle();
  200. _kill_thread(th);
  201. delete thread;
  202. thread = NULL;
  203. tlog3 << "done!\n";
  204. }
  205. void CConsoleHandler::start()
  206. {
  207. thread = new boost::thread(boost::bind(&CConsoleHandler::run,console));
  208. }