SimpleLogger.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <cstdarg>
  42. #include <cerrno>
  43. #include <cstring>
  44. namespace aria2 {
  45. #if !defined(va_copy)
  46. # if defined(__va_copy)
  47. # define va_copy(dest, src) __va_copy(dest, src)
  48. # else
  49. # define va_copy(dest, src) (dest = src)
  50. # endif
  51. #endif
  52. #define WRITE_LOG(LEVEL, MSG) \
  53. va_list ap;\
  54. va_start(ap, MSG);\
  55. writeFile(Logger::LEVEL, MSG, ap);\
  56. va_end(ap);
  57. #define WRITE_LOG_EX(LEVEL, MSG, EX) \
  58. va_list ap;\
  59. va_start(ap, EX);\
  60. writeFile(Logger::LEVEL, MSG, ap, EX);\
  61. va_end(ap);
  62. SimpleLogger::SimpleLogger(FILE* logfile):file(logfile), stdoutField(0) {}
  63. SimpleLogger::~SimpleLogger() {
  64. closeFile();
  65. }
  66. void SimpleLogger::openFile(const std::string& filename) {
  67. file = fopen(filename.c_str(), "ab");
  68. if(file == NULL) {
  69. throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
  70. }
  71. }
  72. void SimpleLogger::closeFile() {
  73. if(file != NULL) {
  74. fclose(file);
  75. }
  76. }
  77. void SimpleLogger::setStdout(Logger::LEVEL level, bool enabled) {
  78. if(enabled) {
  79. stdoutField |= level;
  80. } else {
  81. stdoutField &= ~level;
  82. }
  83. }
  84. void SimpleLogger::writeHeader(FILE* file,
  85. const std::string& date, const std::string& level) const
  86. {
  87. fprintf(file, "%s %s - ", date.c_str(), level.c_str());
  88. }
  89. void SimpleLogger::writeLog(FILE* file, Logger::LEVEL level, const char* msg, va_list ap, Exception* e, bool printHeader) const
  90. {
  91. va_list apCopy;
  92. va_copy(apCopy, ap);
  93. std::string levelStr;
  94. switch(level) {
  95. case Logger::DEBUG:
  96. levelStr = "DEBUG";
  97. break;
  98. case Logger::NOTICE:
  99. levelStr = "NOTICE";
  100. break;
  101. case Logger::WARN:
  102. levelStr = "WARN";
  103. break;
  104. case Logger::ERROR:
  105. levelStr = "ERROR";
  106. break;
  107. case Logger::INFO:
  108. default:
  109. levelStr = "INFO";
  110. }
  111. time_t now = time(NULL);
  112. char datestr[20];
  113. struct tm tm;
  114. localtime_r(&now, &tm);
  115. strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
  116. // TODO a quick hack not to print header in console
  117. if(printHeader) {
  118. writeHeader(file, datestr, levelStr);
  119. }
  120. vfprintf(file, std::string(Util::replace(msg, "\r", "")+"\n").c_str(), apCopy);
  121. for(Exception* nestedEx = e; nestedEx; nestedEx = nestedEx->getCause()) {
  122. // TODO a quick hack not to print header in console
  123. if(printHeader) {
  124. writeHeader(file, datestr, levelStr);
  125. }
  126. fprintf(file, "exception: %s\n", Util::replace(nestedEx->getMsg(), "\r", "").c_str());
  127. }
  128. fflush(file);
  129. va_end(apCopy);
  130. }
  131. void SimpleLogger::writeFile(Logger::LEVEL level, const char* msg, va_list ap, Exception* e) const {
  132. writeLog(file, level, msg, ap, e);
  133. if(stdoutField&level) {
  134. fprintf(stdout, "\n");
  135. writeLog(stdout, level, msg, ap, e);
  136. }
  137. }
  138. void SimpleLogger::debug(const char* msg, ...) const {
  139. WRITE_LOG(DEBUG, msg);
  140. }
  141. void SimpleLogger::debug(const char* msg, Exception* e, ...) const {
  142. WRITE_LOG_EX(DEBUG, msg, e);
  143. }
  144. void SimpleLogger::info(const char* msg, ...) const {
  145. WRITE_LOG(INFO, msg);
  146. }
  147. void SimpleLogger::info(const char* msg, Exception* e, ...) const {
  148. WRITE_LOG_EX(INFO, msg, e);
  149. }
  150. void SimpleLogger::notice(const char* msg, ...) const {
  151. WRITE_LOG(NOTICE, msg);
  152. }
  153. void SimpleLogger::notice(const char* msg, Exception* e, ...) const {
  154. WRITE_LOG_EX(INFO, msg, e);
  155. }
  156. void SimpleLogger::warn(const char* msg, ...) const {
  157. WRITE_LOG(WARN, msg);
  158. }
  159. void SimpleLogger::warn(const char* msg, Exception* e, ...) const {
  160. WRITE_LOG_EX(WARN, msg, e);
  161. }
  162. void SimpleLogger::error(const char* msg, ...) const {
  163. WRITE_LOG(ERROR, msg);
  164. }
  165. void SimpleLogger::error(const char* msg, Exception* e, ...) const {
  166. WRITE_LOG_EX(ERROR, msg, e);
  167. }
  168. } // namespace aria2