log_file.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "log_file.h"
  2. INITIALIZE_EASYLOGGINGPP
  3. LogFile::LogFile()
  4. {
  5. shrink_size_ = 1 * 1024 * 1024; // 1M
  6. }
  7. static unsigned int log_file_bk_idx_ = 0;
  8. static bool check_log_file_exist(const char* filename)
  9. {
  10. std::stringstream stream;
  11. stream << filename << "." << ++log_file_bk_idx_;
  12. std::fstream file;
  13. file.open(stream.str(), std::ios::in);
  14. if(file)
  15. {
  16. return check_log_file_exist(filename);
  17. }
  18. return false;
  19. }
  20. static void roll_out_handler(const char* filename, std::size_t size)
  21. {
  22. std::stringstream stream;
  23. if(!check_log_file_exist(filename))
  24. {
  25. stream << filename << "." << log_file_bk_idx_;
  26. rename(filename, stream.str().c_str());
  27. }
  28. }
  29. bool LogFile::init(const char* conf_file, const char* app_name)
  30. {
  31. el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
  32. el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
  33. el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);
  34. el::Helpers::installPreRollOutCallback(roll_out_handler);
  35. size_t file_len = disk_file::get_file_size(conf_file);
  36. if(file_len == 0)
  37. {
  38. ASSERT(false);
  39. return false;
  40. }
  41. char* buffer = (char*)malloc(file_len + 1);
  42. size_t read_len = disk_file::file_readstring(conf_file, buffer, file_len);
  43. buffer[read_len] = 0;
  44. std::string content = buffer;
  45. free(buffer);
  46. // Ìæ»»%appname
  47. std::string replaceWhat = "%appname";
  48. size_t pos = -1;
  49. while((pos = content.find("%appname", pos + 1)) != std::string::npos)
  50. {
  51. content.replace(pos, replaceWhat.length(), app_name);
  52. }
  53. if(!conf_.parseFromText(content))
  54. {
  55. ASSERT(false);
  56. return false;
  57. }
  58. el::Loggers::reconfigureAllLoggers(conf_);
  59. running_ = true;
  60. thread_ = std::thread(&LogFile::check_output, this);
  61. return true;
  62. }
  63. void LogFile::shut()
  64. {
  65. this->running_ = false;
  66. while(msg_queue_.IsDirty())
  67. {
  68. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  69. }
  70. msg_queue_.SwapWriteBuffers();
  71. thread_.join();
  72. }
  73. LogFile::~LogFile()
  74. {
  75. }
  76. bool LogFile::trace_log(int log_type, const char* log_content)
  77. {
  78. if(log_type < E_LOG_DBG || log_type > E_LOG_FATAL)
  79. {
  80. return false;
  81. }
  82. if(!msg_queue_.GetWriteBuffer().put((void*)&log_type, sizeof(log_type), (void*)log_content, strlen(log_content) + 1)) {
  83. return false;
  84. }
  85. msg_queue_.SwapWriteBuffers();
  86. return true;
  87. }
  88. #ifdef _PLATFORM_WINDOWS_
  89. enum E_CONSOLE_COLOR_TYPE
  90. {
  91. E_CONSOLE_COLOR_RED = 0,
  92. E_CONSOLE_COLOR_BLUE,
  93. E_CONSOLE_COLOR_GREEN,
  94. E_CONSOLE_COLOR_YELLOW,
  95. E_CONSOLE_COLOR_PURPLE,
  96. E_CONSOLE_COLOR_CYAN,
  97. E_CONSOLE_COLOR_LIGHTRED,
  98. E_CONSOLE_COLOR_LIGHTBLUE,
  99. E_CONSOLE_COLOR_LIGHTGREEN,
  100. E_CONSOLE_COLOR_LIGHTYELLOW,
  101. E_CONSOLE_COLOR_LOGHTPURPLE,
  102. E_CONSOLE_COLOR_LIGHTCYAN,
  103. E_CONSOLE_COLOR_GRAY,
  104. E_CONSOLE_COLOR_WHITE,
  105. E_CONSOLE_COLOR_MAX,
  106. };
  107. static unsigned int s_console_colors_[E_CONSOLE_COLOR_MAX] =
  108. {
  109. FOREGROUND_RED,
  110. FOREGROUND_BLUE,
  111. FOREGROUND_GREEN,
  112. (FOREGROUND_RED | FOREGROUND_GREEN),
  113. (FOREGROUND_RED | FOREGROUND_BLUE),
  114. (FOREGROUND_GREEN | FOREGROUND_BLUE),
  115. (FOREGROUND_RED | FOREGROUND_INTENSITY),
  116. (FOREGROUND_BLUE | FOREGROUND_INTENSITY),
  117. (FOREGROUND_GREEN | FOREGROUND_INTENSITY),
  118. (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY),
  119. (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY),
  120. (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY),
  121. (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN),
  122. (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY),
  123. };
  124. #endif
  125. void LogFile::check_output()
  126. {
  127. while(running_ || msg_queue_.IsDirty())
  128. {
  129. if(!msg_queue_.IsDirty())
  130. {
  131. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  132. continue;
  133. }
  134. #ifdef _PLATFORM_WINDOWS_
  135. SYSTEMTIME tm;
  136. ::GetLocalTime(&tm);
  137. char nowtime[32];
  138. ::sprintf(nowtime, "[%02d-%02d %02d:%02d:%02d,%03d]",
  139. tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond, tm.wMilliseconds);
  140. #endif
  141. msg_queue_.SwapReadBuffers();
  142. auto& read_buffer = msg_queue_.Read();
  143. size_t it = 0;
  144. const void* data_ptr{nullptr};
  145. size_t data_len{};
  146. std::string out_buf;
  147. while(read_buffer.peek(it, data_ptr, data_len))
  148. {
  149. int type = *(int*)(data_ptr);
  150. const char* content = (const char*)data_ptr + sizeof(int);
  151. #ifdef _PLATFORM_WINDOWS_
  152. utf8_to_str(content, out_buf);
  153. switch(type)
  154. {
  155. case E_LOG_DBG:
  156. {
  157. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), s_console_colors_[E_CONSOLE_COLOR_LIGHTGREEN]);
  158. LOG(DEBUG) << (const char*)content;
  159. printf("%s [D] %s\n", nowtime, out_buf.c_str());
  160. }
  161. break;
  162. case E_LOG_INFORMATION:
  163. {
  164. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), s_console_colors_[E_CONSOLE_COLOR_WHITE]);
  165. LOG(INFO) << (const char*)content;
  166. printf("%s [I] %s\n", nowtime, out_buf.c_str());
  167. }
  168. break;
  169. case E_LOG_WARNING:
  170. {
  171. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), s_console_colors_[E_CONSOLE_COLOR_LIGHTYELLOW]);
  172. LOG(WARNING) << (const char*)content;
  173. printf("%s [W] %s\n", nowtime, out_buf.c_str());
  174. }
  175. break;
  176. case E_LOG_ERROR:
  177. {
  178. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), s_console_colors_[E_CONSOLE_COLOR_LIGHTRED]);
  179. LOG(ERROR) << (const char*)content;
  180. printf("%s [E] %s\n", nowtime, out_buf.c_str());
  181. }
  182. break;
  183. case E_LOG_FATAL:
  184. {
  185. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), s_console_colors_[E_CONSOLE_COLOR_LIGHTRED]);
  186. LOG(FATAL) << (const char*)content;
  187. printf("%s [F] %s\n", nowtime, out_buf.c_str());
  188. }
  189. break;
  190. default:
  191. break;
  192. }
  193. #else
  194. switch(_type)
  195. {
  196. case E_LOG_DBG:
  197. LOG(DEBUG) << (const char*)content;
  198. break;
  199. case E_LOG_INFORMATION:
  200. LOG(INFO) << (const char*)content;
  201. break;
  202. case E_LOG_WARNING:
  203. LOG(WARNING) << (const char*)content;
  204. break;
  205. case E_LOG_ERROR:
  206. LOG(ERROR) << (const char*)content;
  207. break;
  208. case E_LOG_FATAL:
  209. LOG(FATAL) << (const char*)content;
  210. break;
  211. default:
  212. break;
  213. }
  214. #endif
  215. }
  216. read_buffer.clear();
  217. read_buffer.shrink(shrink_size_);
  218. }
  219. }