SimpleLogger.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "SimpleLogger.h"
  23. #include "Util.h"
  24. #include "DlAbortEx.h"
  25. #include "message.h"
  26. #include <time.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #define WRITE_LOG(LEVEL, MSG) \
  31. va_list ap;\
  32. va_start(ap, MSG);\
  33. writeLog(Logger::LEVEL, MSG, ap);\
  34. va_end(ap);
  35. #define WRITE_LOG_EX(LEVEL, MSG, EX) \
  36. va_list ap;\
  37. va_start(ap, EX);\
  38. writeLog(Logger::LEVEL, MSG, ap, EX);\
  39. va_end(ap);
  40. SimpleLogger::SimpleLogger():file(NULL) {}
  41. SimpleLogger::SimpleLogger(FILE* logfile) {
  42. file = logfile;
  43. }
  44. SimpleLogger::~SimpleLogger() {
  45. closeFile();
  46. }
  47. void SimpleLogger::openFile(const string& filename) {
  48. file = fopen(filename.c_str(), "a");
  49. if(file == NULL) {
  50. throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
  51. }
  52. }
  53. void SimpleLogger::closeFile() {
  54. if(file != NULL) {
  55. fclose(file);
  56. }
  57. }
  58. void SimpleLogger::writeHeader(string date, string level) const {
  59. fprintf(file, "%s - %s - ", date.c_str(), level.c_str());
  60. }
  61. void SimpleLogger::writeLog(int level, const char* msg, va_list ap, Exception* e) const
  62. {
  63. string levelStr;
  64. switch(level) {
  65. case Logger::DEBUG:
  66. levelStr = "DEBUG";
  67. break;
  68. case Logger::WARN:
  69. levelStr = "WARN";
  70. break;
  71. case Logger::ERROR:
  72. levelStr = "ERROR";
  73. break;
  74. case Logger::INFO:
  75. default:
  76. levelStr = "INFO";
  77. }
  78. time_t now = time(NULL);
  79. char datestr[26];
  80. ctime_r(&now, datestr);
  81. datestr[strlen(datestr)-1] = '\0';
  82. writeHeader(datestr, levelStr);
  83. vfprintf(file, string(Util::replace(msg, "\r", "")+"\n").c_str(), ap);
  84. if(e != NULL) {
  85. writeHeader(datestr, levelStr);
  86. fprintf(file, "exception: %s\n", Util::replace(e->getMsg(), "\r", "").c_str());
  87. }
  88. fflush(file);
  89. }
  90. void SimpleLogger::debug(const char* msg, ...) const {
  91. WRITE_LOG(DEBUG, msg);
  92. }
  93. void SimpleLogger::debug(const char* msg, Exception* e, ...) const {
  94. WRITE_LOG_EX(DEBUG, msg, e);
  95. }
  96. void SimpleLogger::info(const char* msg, ...) const {
  97. WRITE_LOG(INFO, msg);
  98. }
  99. void SimpleLogger::info(const char* msg, Exception* e, ...) const {
  100. WRITE_LOG_EX(INFO, msg, e);
  101. }
  102. void SimpleLogger::warn(const char* msg, ...) const {
  103. WRITE_LOG(WARN, msg);
  104. }
  105. void SimpleLogger::warn(const char* msg, Exception* e, ...) const {
  106. WRITE_LOG_EX(WARN, msg, e);
  107. }
  108. void SimpleLogger::error(const char* msg, ...) const {
  109. WRITE_LOG(ERROR, msg);
  110. }
  111. void SimpleLogger::error(const char* msg, Exception* e, ...) const {
  112. WRITE_LOG_EX(ERROR, msg, e);
  113. }