Logger.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "Logger.h"
  36. #include <unistd.h>
  37. #include <cstring>
  38. #include <cstdio>
  39. #include <cassert>
  40. #include "DlAbortEx.h"
  41. #include "fmt.h"
  42. #include "message.h"
  43. #include "A2STR.h"
  44. #include "a2time.h"
  45. #include "BufferedFile.h"
  46. #include "util.h"
  47. #include "console.h"
  48. namespace aria2 {
  49. Logger::Logger()
  50. : logLevel_(Logger::A2_DEBUG),
  51. consoleLogLevel_(Logger::A2_NOTICE),
  52. consoleOutput_(true),
  53. #ifdef __MINGW32__
  54. // Windows DOS prompt does not handle ANSI color code, so make
  55. // this false.
  56. useColor_(false)
  57. #else // !__MINGW32__
  58. useColor_(isatty(STDOUT_FILENO) == 1)
  59. #endif // !__MINGW32__
  60. {}
  61. Logger::~Logger()
  62. {
  63. }
  64. void Logger::openFile(const std::string& filename)
  65. {
  66. closeFile();
  67. if(filename == DEV_STDOUT) {
  68. fpp_ = global::cout();
  69. } else {
  70. fpp_.reset(new BufferedFile(filename.c_str(), BufferedFile::APPEND));
  71. if(!*static_cast<BufferedFile*>(fpp_.get())) {
  72. throw DL_ABORT_EX(fmt(EX_FILE_OPEN, filename.c_str(), "n/a"));
  73. }
  74. }
  75. }
  76. void Logger::closeFile()
  77. {
  78. if(fpp_) {
  79. fpp_.reset();
  80. }
  81. }
  82. void Logger::setConsoleOutput(bool enabled)
  83. {
  84. consoleOutput_ = enabled;
  85. }
  86. bool Logger::fileLogEnabled(LEVEL level)
  87. {
  88. return level >= logLevel_ && fpp_;
  89. }
  90. bool Logger::consoleLogEnabled(LEVEL level)
  91. {
  92. return consoleOutput_ && level >= consoleLogLevel_;
  93. }
  94. bool Logger::levelEnabled(LEVEL level)
  95. {
  96. return fileLogEnabled(level) || consoleLogEnabled(level);
  97. }
  98. namespace {
  99. const char* levelToString(Logger::LEVEL level)
  100. {
  101. switch(level) {
  102. case Logger::A2_DEBUG:
  103. return "DEBUG";
  104. case Logger::A2_INFO:
  105. return "INFO";
  106. case Logger::A2_NOTICE:
  107. return "NOTICE";
  108. case Logger::A2_WARN:
  109. return "WARN";
  110. case Logger::A2_ERROR:
  111. return "ERROR";
  112. default:
  113. return "";
  114. }
  115. }
  116. } // namespace
  117. namespace {
  118. template<typename Output>
  119. void writeHeader
  120. (Output& fp, Logger::LEVEL level, const char* sourceFile, int lineNum)
  121. {
  122. struct timeval tv;
  123. gettimeofday(&tv, nullptr);
  124. char datestr[20]; // 'YYYY-MM-DD hh:mm:ss'+'\0' = 20 bytes
  125. struct tm tm;
  126. //tv.tv_sec may not be of type time_t.
  127. time_t timesec = tv.tv_sec;
  128. localtime_r(&timesec, &tm);
  129. size_t dateLength =
  130. strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
  131. assert(dateLength <= (size_t)20);
  132. fp.printf("%s.%06ld [%s] [%s:%d] ", datestr, tv.tv_usec, levelToString(level),
  133. sourceFile, lineNum);
  134. }
  135. } // namespace
  136. namespace {
  137. const char* levelColor(Logger::LEVEL level)
  138. {
  139. switch(level) {
  140. case Logger::A2_DEBUG:
  141. return "\033[1;37m";
  142. case Logger::A2_INFO:
  143. return "\033[1;36m";
  144. case Logger::A2_NOTICE:
  145. return "\033[1;32m";
  146. case Logger::A2_WARN:
  147. return "\033[1;33m";
  148. case Logger::A2_ERROR:
  149. return "\033[1;31m";
  150. default:
  151. return "";
  152. }
  153. }
  154. } // namespace
  155. namespace {
  156. template<typename Output>
  157. void writeHeaderConsole(Output& fp, Logger::LEVEL level, bool useColor)
  158. {
  159. struct timeval tv;
  160. gettimeofday(&tv, nullptr);
  161. char datestr[15]; // 'MM/DD hh:mm:ss'+'\0' = 15 bytes
  162. struct tm tm;
  163. //tv.tv_sec may not be of type time_t.
  164. time_t timesec = tv.tv_sec;
  165. localtime_r(&timesec, &tm);
  166. size_t dateLength =
  167. strftime(datestr, sizeof(datestr), "%m/%d %H:%M:%S", &tm);
  168. assert(dateLength <= (size_t)15);
  169. if(useColor) {
  170. fp.printf("%s [%s%s\033[0m] ", datestr, levelColor(level),
  171. levelToString(level));
  172. } else {
  173. fp.printf("%s [%s] ", datestr, levelToString(level));
  174. }
  175. }
  176. } // namespace
  177. namespace {
  178. template<typename Output>
  179. void writeStackTrace(Output& fp, const char* stackTrace)
  180. {
  181. fp.write(stackTrace);
  182. }
  183. } // namespace
  184. void Logger::writeLog
  185. (Logger::LEVEL level,
  186. const char* sourceFile,
  187. int lineNum,
  188. const char* msg,
  189. const char* trace)
  190. {
  191. if(fileLogEnabled(level)) {
  192. writeHeader(*fpp_, level, sourceFile, lineNum);
  193. fpp_->printf("%s\n", msg);
  194. writeStackTrace(*fpp_, trace);
  195. fpp_->flush();
  196. }
  197. if(consoleLogEnabled(level)) {
  198. global::cout()->printf("\n");
  199. writeHeaderConsole(*global::cout(), level, useColor_);
  200. global::cout()->printf("%s\n", msg);
  201. writeStackTrace(*global::cout(), trace);
  202. global::cout()->flush();
  203. }
  204. }
  205. void Logger::log
  206. (LEVEL level,
  207. const char* sourceFile,
  208. int lineNum,
  209. const char* msg)
  210. {
  211. writeLog(level, sourceFile, lineNum, msg, "");
  212. }
  213. void Logger::log
  214. (LEVEL level,
  215. const char* sourceFile,
  216. int lineNum,
  217. const std::string& msg)
  218. {
  219. log(level, sourceFile, lineNum, msg.c_str());
  220. }
  221. void Logger::log
  222. (LEVEL level,
  223. const char* sourceFile,
  224. int lineNum,
  225. const char* msg,
  226. const Exception& ex)
  227. {
  228. writeLog(level, sourceFile, lineNum, msg, ex.stackTrace().c_str());
  229. }
  230. void Logger::log
  231. (LEVEL level,
  232. const char* sourceFile,
  233. int lineNum,
  234. const std::string& msg,
  235. const Exception& ex)
  236. {
  237. log(level, sourceFile, lineNum, msg.c_str(), ex);
  238. }
  239. } // namespace aria2