SimpleLogger.cc 6.0 KB

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