SimpleLogger.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "SimpleLogger.h"
  36. #include "Util.h"
  37. #include "DlAbortEx.h"
  38. #include "message.h"
  39. #include "a2io.h"
  40. #include "a2time.h"
  41. #include "StringFormat.h"
  42. #include "A2STR.h"
  43. #include <cerrno>
  44. #include <cstring>
  45. #include <iostream>
  46. #include <cstdlib>
  47. #include <cassert>
  48. namespace aria2 {
  49. #if !defined(va_copy)
  50. # if defined(__va_copy)
  51. # define va_copy(dest, src) __va_copy(dest, src)
  52. # else
  53. # define va_copy(dest, src) (dest = src)
  54. # endif
  55. #endif
  56. #define WRITE_LOG(LEVEL, MSG) \
  57. if(LEVEL >= _logLevel && (stdoutField&LEVEL || file.is_open())) { \
  58. va_list ap; \
  59. va_start(ap, MSG); \
  60. writeFile(LEVEL, MSG, ap); \
  61. flush(); \
  62. va_end(ap); \
  63. }
  64. #define WRITE_LOG_EX(LEVEL, MSG, EX) \
  65. if(LEVEL >= _logLevel && (stdoutField&LEVEL || file.is_open())) { \
  66. va_list ap; \
  67. va_start(ap, EX); \
  68. writeFile(LEVEL, MSG, ap); \
  69. writeStackTrace(LEVEL, EX); \
  70. flush(); \
  71. va_end(ap); \
  72. }
  73. const std::string SimpleLogger::DEBUG("DEBUG");
  74. const std::string SimpleLogger::NOTICE("NOTICE");
  75. const std::string SimpleLogger::WARN("WARN");
  76. const std::string SimpleLogger::ERROR("ERROR");
  77. const std::string SimpleLogger::INFO("INFO");
  78. SimpleLogger::SimpleLogger():stdoutField(0), _logLevel(Logger::DEBUG) {}
  79. SimpleLogger::~SimpleLogger() {
  80. closeFile();
  81. }
  82. void SimpleLogger::openFile(const std::string& filename) {
  83. file.open(filename.c_str(), std::ios::app|std::ios::binary);
  84. if(!file) {
  85. throw DL_ABORT_EX
  86. (StringFormat(EX_FILE_OPEN, filename.c_str(), strerror(errno)).str());
  87. }
  88. }
  89. void SimpleLogger::closeFile() {
  90. if(file.is_open()) {
  91. file.close();
  92. }
  93. }
  94. void SimpleLogger::setStdout(Logger::LEVEL level, bool enabled) {
  95. if(enabled) {
  96. stdoutField |= level;
  97. } else {
  98. stdoutField &= ~level;
  99. }
  100. }
  101. void SimpleLogger::writeHeader(std::ostream& o, const std::string& date,
  102. const std::string& level)
  103. {
  104. o << StringFormat("%s %s - ", date.c_str(), level.c_str());
  105. }
  106. void SimpleLogger::writeLog(std::ostream& o, Logger::LEVEL level,
  107. const char* msg, va_list ap,
  108. bool printHeader)
  109. {
  110. va_list apCopy;
  111. va_copy(apCopy, ap);
  112. std::string levelStr;
  113. switch(level) {
  114. case Logger::DEBUG:
  115. levelStr = DEBUG;
  116. break;
  117. case Logger::NOTICE:
  118. levelStr = NOTICE;
  119. break;
  120. case Logger::WARN:
  121. levelStr = WARN;
  122. break;
  123. case Logger::ERROR:
  124. levelStr = ERROR;
  125. break;
  126. case Logger::INFO:
  127. default:
  128. levelStr = INFO;
  129. }
  130. struct timeval tv;
  131. gettimeofday(&tv, 0);
  132. char datestr[27]; // 'YYYY-MM-DD hh:mm:ss.uuuuuu'+'\0' = 27 bytes
  133. struct tm tm;
  134. //tv.tv_sec may not be of type time_t.
  135. time_t timesec = tv.tv_sec;
  136. localtime_r(&timesec, &tm);
  137. size_t dateLength =
  138. strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
  139. assert(dateLength <= (size_t)20);
  140. snprintf(datestr+dateLength, sizeof(datestr)-dateLength,
  141. ".%06ld", tv.tv_usec);
  142. // TODO a quick hack not to print header in console
  143. if(printHeader) {
  144. writeHeader(o, datestr, levelStr);
  145. }
  146. {
  147. char buf[1024];
  148. if(vsnprintf(buf, sizeof(buf), std::string(Util::replace(msg, A2STR::CR_C, A2STR::NIL)+A2STR::LF_C).c_str(), apCopy) < 0) {
  149. o << "SimpleLogger error, failed to format message.\n";
  150. } else {
  151. o << buf;
  152. }
  153. }
  154. va_end(apCopy);
  155. }
  156. void SimpleLogger::writeFile(Logger::LEVEL level, const char* msg, va_list ap)
  157. {
  158. writeLog(file, level, msg, ap);
  159. if(stdoutField&level) {
  160. std::cout << "\n";
  161. writeLog(std::cout, level, msg, ap);
  162. }
  163. }
  164. void SimpleLogger::writeStackTrace(Logger::LEVEL level, const Exception& e)
  165. {
  166. file << e.stackTrace();
  167. if(stdoutField&level) {
  168. std::cout << e.stackTrace();
  169. }
  170. }
  171. void SimpleLogger::flush()
  172. {
  173. file << std::flush;
  174. std::cout << std::flush;
  175. }
  176. void SimpleLogger::debug(const char* msg, ...) {
  177. WRITE_LOG(Logger::DEBUG, msg);
  178. }
  179. void SimpleLogger::debug(const char* msg, const Exception& e, ...) {
  180. WRITE_LOG_EX(Logger::DEBUG, msg, e);
  181. }
  182. void SimpleLogger::info(const char* msg, ...) {
  183. WRITE_LOG(Logger::INFO, msg);
  184. }
  185. void SimpleLogger::info(const char* msg, const Exception& e, ...) {
  186. WRITE_LOG_EX(Logger::INFO, msg, e);
  187. }
  188. void SimpleLogger::notice(const char* msg, ...) {
  189. WRITE_LOG(Logger::NOTICE, msg);
  190. }
  191. void SimpleLogger::notice(const char* msg, const Exception& e, ...) {
  192. WRITE_LOG_EX(Logger::INFO, msg, e);
  193. }
  194. void SimpleLogger::warn(const char* msg, ...) {
  195. WRITE_LOG(Logger::WARN, msg);
  196. }
  197. void SimpleLogger::warn(const char* msg, const Exception& e, ...) {
  198. WRITE_LOG_EX(Logger::WARN, msg, e);
  199. }
  200. void SimpleLogger::error(const char* msg, ...) {
  201. WRITE_LOG(Logger::ERROR, msg);
  202. }
  203. void SimpleLogger::error(const char* msg, const Exception& e, ...) {
  204. WRITE_LOG_EX(Logger::ERROR, msg, e);
  205. }
  206. void SimpleLogger::setLogLevel(Logger::LEVEL level)
  207. {
  208. _logLevel = level;
  209. }
  210. } // namespace aria2