SimpleLogger.cc 5.1 KB

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