2
0

CConsoleHandler.cpp 6.6 KB

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