Logger.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <cstring>
  37. #include <cstdio>
  38. #include "DlAbortEx.h"
  39. #include "fmt.h"
  40. #include "message.h"
  41. #include "A2STR.h"
  42. #include "a2time.h"
  43. #include "BufferedFile.h"
  44. #include "util.h"
  45. #include "console.h"
  46. namespace aria2 {
  47. namespace {
  48. static const std::string DEBUG_LABEL("DEBUG");
  49. static const std::string INFO_LABEL("INFO");
  50. static const std::string NOTICE_LABEL("NOTICE");
  51. static const std::string WARN_LABEL("WARN");
  52. static const std::string ERROR_LABEL("ERROR");
  53. } // namespace
  54. Logger::Logger()
  55. : logLevel_(Logger::A2_DEBUG),
  56. stdoutField_(0)
  57. {}
  58. Logger::~Logger()
  59. {
  60. }
  61. void Logger::openFile(const std::string& filename)
  62. {
  63. closeFile();
  64. if(filename == DEV_STDOUT) {
  65. fpp_ = global::cout;
  66. } else {
  67. fpp_.reset(new BufferedFile(filename, BufferedFile::APPEND));
  68. if(!fpp_) {
  69. throw DL_ABORT_EX(fmt(EX_FILE_OPEN, filename.c_str(), "n/a"));
  70. }
  71. }
  72. }
  73. void Logger::closeFile()
  74. {
  75. if(fpp_) {
  76. fpp_.reset();
  77. }
  78. }
  79. void Logger::setStdoutLogLevel(Logger::LEVEL level, bool enabled)
  80. {
  81. if(enabled) {
  82. stdoutField_ |= level;
  83. } else {
  84. stdoutField_ &= ~level;
  85. }
  86. }
  87. bool Logger::levelEnabled(LEVEL level)
  88. {
  89. return (level >= logLevel_ && fpp_) || stdoutField_&level;
  90. }
  91. namespace {
  92. const std::string& levelToString(Logger::LEVEL level)
  93. {
  94. switch(level) {
  95. case Logger::A2_DEBUG:
  96. return DEBUG_LABEL;
  97. case Logger::A2_INFO:
  98. return INFO_LABEL;
  99. case Logger::A2_NOTICE:
  100. return NOTICE_LABEL;
  101. case Logger::A2_WARN:
  102. return WARN_LABEL;
  103. case Logger::A2_ERROR:
  104. return ERROR_LABEL;
  105. default:
  106. return A2STR::NIL;
  107. }
  108. }
  109. } // namespace
  110. namespace {
  111. template<typename Output>
  112. void writeHeader
  113. (Output& fp, Logger::LEVEL level, const char* sourceFile, int lineNum)
  114. {
  115. struct timeval tv;
  116. gettimeofday(&tv, 0);
  117. char datestr[20]; // 'YYYY-MM-DD hh:mm:ss'+'\0' = 20 bytes
  118. struct tm tm;
  119. //tv.tv_sec may not be of type time_t.
  120. time_t timesec = tv.tv_sec;
  121. localtime_r(&timesec, &tm);
  122. size_t dateLength =
  123. strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
  124. assert(dateLength <= (size_t)20);
  125. fp.printf("%s.%06ld %s - ", datestr, tv.tv_usec,
  126. levelToString(level).c_str());
  127. if(sourceFile) {
  128. fp.printf("[%s:%d]", sourceFile, lineNum);
  129. }
  130. }
  131. } // namespace
  132. namespace {
  133. template<typename Output>
  134. void writeStackTrace(Output& fp, const std::string& stackTrace)
  135. {
  136. fp.write(stackTrace.c_str());
  137. }
  138. } // namespace
  139. void Logger::writeLog
  140. (Logger::LEVEL level,
  141. const char* sourceFile,
  142. int lineNum,
  143. const char* msg,
  144. const std::string& trace,
  145. bool toStream,
  146. bool toConsole)
  147. {
  148. if(toStream) {
  149. writeHeader(*fpp_, level, sourceFile, lineNum);
  150. fpp_->printf("%s\n", msg);
  151. writeStackTrace(*fpp_, trace);
  152. fpp_->flush();
  153. }
  154. if(toConsole) {
  155. global::cout->printf("\n");
  156. writeHeader(*global::cout, level, 0, 0);
  157. global::cout->printf("%s\n", msg);
  158. writeStackTrace(*global::cout, trace);
  159. global::cout->flush();
  160. }
  161. }
  162. void Logger::log
  163. (LEVEL level,
  164. const char* sourceFile,
  165. int lineNum,
  166. const char* msg)
  167. {
  168. writeLog(level, sourceFile, lineNum, msg, A2STR::NIL,
  169. level >= logLevel_ && fpp_,
  170. stdoutField_&level);
  171. }
  172. void Logger::log
  173. (LEVEL level,
  174. const char* sourceFile,
  175. int lineNum,
  176. const std::string& msg)
  177. {
  178. log(level, sourceFile, lineNum, msg.c_str());
  179. }
  180. void Logger::log
  181. (LEVEL level,
  182. const char* sourceFile,
  183. int lineNum,
  184. const char* msg,
  185. const Exception& ex)
  186. {
  187. writeLog(level, sourceFile, lineNum, msg, ex.stackTrace(),
  188. level >= logLevel_ && fpp_,
  189. stdoutField_&level);
  190. }
  191. void Logger::log
  192. (LEVEL level,
  193. const char* sourceFile,
  194. int lineNum,
  195. const std::string& msg,
  196. const Exception& ex)
  197. {
  198. log(level, sourceFile, lineNum, msg.c_str(), ex);
  199. }
  200. } // namespace aria2