Logger.cc 5.2 KB

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