CConsoleHandler.cpp 6.5 KB

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