CConsoleHandler.cpp 6.7 KB

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