Logger.cc 5.2 KB

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