CConsoleHandler.cpp 6.3 KB

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