Logger.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. colorOutput_(global::cout()->supportsColor())
  54. {
  55. }
  56. Logger::~Logger() {}
  57. void Logger::openFile(const std::string& filename)
  58. {
  59. closeFile();
  60. if (filename == DEV_STDOUT) {
  61. fpp_ = global::cout();
  62. }
  63. else {
  64. fpp_ =
  65. std::make_shared<BufferedFile>(filename.c_str(), BufferedFile::APPEND);
  66. if (!*static_cast<BufferedFile*>(fpp_.get())) {
  67. throw DL_ABORT_EX(fmt(EX_FILE_OPEN, filename.c_str(), "n/a"));
  68. }
  69. }
  70. }
  71. void Logger::closeFile()
  72. {
  73. if (fpp_) {
  74. fpp_.reset();
  75. }
  76. }
  77. void Logger::setConsoleOutput(bool enabled) { consoleOutput_ = enabled; }
  78. void Logger::setColorOutput(bool enabled) { colorOutput_ = enabled; }
  79. bool Logger::fileLogEnabled(LEVEL level) { return level >= logLevel_ && fpp_; }
  80. bool Logger::consoleLogEnabled(LEVEL level)
  81. {
  82. return consoleOutput_ && level >= consoleLogLevel_;
  83. }
  84. bool Logger::levelEnabled(LEVEL level)
  85. {
  86. return fileLogEnabled(level) || consoleLogEnabled(level);
  87. }
  88. namespace {
  89. const char* levelToString(Logger::LEVEL level)
  90. {
  91. switch (level) {
  92. case Logger::A2_DEBUG:
  93. return "DEBUG";
  94. case Logger::A2_INFO:
  95. return "INFO";
  96. case Logger::A2_NOTICE:
  97. return "NOTICE";
  98. case Logger::A2_WARN:
  99. return "WARN";
  100. case Logger::A2_ERROR:
  101. return "ERROR";
  102. default:
  103. return "";
  104. }
  105. }
  106. } // namespace
  107. namespace {
  108. template <typename Output>
  109. void writeHeader(Output& fp, Logger::LEVEL level, const char* sourceFile,
  110. int lineNum)
  111. {
  112. struct timeval tv;
  113. gettimeofday(&tv, nullptr);
  114. char datestr[20]; // 'YYYY-MM-DD hh:mm:ss'+'\0' = 20 bytes
  115. struct tm tm;
  116. // tv.tv_sec may not be of type time_t.
  117. time_t timesec = tv.tv_sec;
  118. localtime_r(&timesec, &tm);
  119. size_t dateLength =
  120. strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
  121. assert(dateLength <= (size_t)20);
  122. fp.printf("%s.%06ld [%s] [%s:%d] ", datestr, tv.tv_usec, levelToString(level),
  123. sourceFile, lineNum);
  124. }
  125. } // namespace
  126. namespace {
  127. const char* levelColor(Logger::LEVEL level)
  128. {
  129. switch (level) {
  130. case Logger::A2_DEBUG:
  131. return "\033[1;37m";
  132. case Logger::A2_INFO:
  133. return "\033[1;36m";
  134. case Logger::A2_NOTICE:
  135. return "\033[1;32m";
  136. case Logger::A2_WARN:
  137. return "\033[1;33m";
  138. case Logger::A2_ERROR:
  139. return "\033[1;31m";
  140. default:
  141. return "";
  142. }
  143. }
  144. } // namespace
  145. namespace {
  146. template <typename Output>
  147. void writeHeaderConsole(Output& fp, Logger::LEVEL level, bool useColor)
  148. {
  149. struct timeval tv;
  150. gettimeofday(&tv, nullptr);
  151. char datestr[15]; // 'MM/DD hh:mm:ss'+'\0' = 15 bytes
  152. struct tm tm;
  153. // tv.tv_sec may not be of type time_t.
  154. time_t timesec = tv.tv_sec;
  155. localtime_r(&timesec, &tm);
  156. size_t dateLength = strftime(datestr, sizeof(datestr), "%m/%d %H:%M:%S", &tm);
  157. assert(dateLength <= (size_t)15);
  158. if (useColor) {
  159. fp.printf("%s [%s%s\033[0m] ", datestr, levelColor(level),
  160. levelToString(level));
  161. }
  162. else {
  163. fp.printf("%s [%s] ", datestr, levelToString(level));
  164. }
  165. }
  166. } // namespace
  167. namespace {
  168. template <typename Output>
  169. void writeStackTrace(Output& fp, const char* stackTrace)
  170. {
  171. fp.write(stackTrace);
  172. }
  173. } // namespace
  174. void Logger::writeLog(Logger::LEVEL level, const char* sourceFile, int lineNum,
  175. const char* msg, const char* trace)
  176. {
  177. if (fileLogEnabled(level)) {
  178. writeHeader(*fpp_, level, sourceFile, lineNum);
  179. fpp_->printf("%s\n", msg);
  180. writeStackTrace(*fpp_, trace);
  181. fpp_->flush();
  182. }
  183. if (consoleLogEnabled(level)) {
  184. global::cout()->printf("\n");
  185. writeHeaderConsole(*global::cout(), level, colorOutput_);
  186. global::cout()->printf("%s\n", msg);
  187. writeStackTrace(*global::cout(), trace);
  188. global::cout()->flush();
  189. }
  190. }
  191. void Logger::log(LEVEL level, const char* sourceFile, int lineNum,
  192. const char* msg)
  193. {
  194. writeLog(level, sourceFile, lineNum, msg, "");
  195. }
  196. void Logger::log(LEVEL level, const char* sourceFile, int lineNum,
  197. const std::string& msg)
  198. {
  199. log(level, sourceFile, lineNum, msg.c_str());
  200. }
  201. void Logger::log(LEVEL level, const char* sourceFile, int lineNum,
  202. const char* msg, const Exception& ex)
  203. {
  204. writeLog(level, sourceFile, lineNum, msg, ex.stackTrace().c_str());
  205. }
  206. void Logger::log(LEVEL level, const char* sourceFile, int lineNum,
  207. const std::string& msg, const Exception& ex)
  208. {
  209. log(level, sourceFile, lineNum, msg.c_str(), ex);
  210. }
  211. } // namespace aria2