SimpleLogger.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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::writeLog(int level, const char* msg, va_list ap, Exception* e) const
  59. {
  60. string levelStr;
  61. switch(level) {
  62. case Logger::DEBUG:
  63. levelStr = "DEBUG";
  64. break;
  65. case Logger::WARN:
  66. levelStr = "WARN";
  67. break;
  68. case Logger::ERROR:
  69. levelStr = "ERROR";
  70. break;
  71. case Logger::INFO:
  72. default:
  73. levelStr = "INFO";
  74. }
  75. time_t now = time(NULL);
  76. char datestr[26];
  77. ctime_r(&now, datestr);
  78. datestr[strlen(datestr)-1] = '\0';
  79. vfprintf(file, string(string(datestr)+" - "+levelStr+" - "+Util::replace(msg, "\r", "")+"\n").c_str(), ap);
  80. if(e != NULL) {
  81. fprintf(file, string(string(datestr)+" - "+levelStr+" - exception: "+Util::replace(e->getMsg(), "\r", "")+"\n").c_str());
  82. }
  83. fflush(file);
  84. }
  85. void SimpleLogger::debug(const char* msg, ...) const {
  86. WRITE_LOG(DEBUG, msg);
  87. }
  88. void SimpleLogger::debug(const char* msg, Exception* e, ...) const {
  89. WRITE_LOG_EX(DEBUG, msg, e);
  90. }
  91. void SimpleLogger::info(const char* msg, ...) const {
  92. WRITE_LOG(INFO, msg);
  93. }
  94. void SimpleLogger::info(const char* msg, Exception* e, ...) const {
  95. WRITE_LOG_EX(INFO, msg, e);
  96. }
  97. void SimpleLogger::warn(const char* msg, ...) const {
  98. WRITE_LOG(WARN, msg);
  99. }
  100. void SimpleLogger::warn(const char* msg, Exception* e, ...) const {
  101. WRITE_LOG_EX(WARN, msg, e);
  102. }
  103. void SimpleLogger::error(const char* msg, ...) const {
  104. WRITE_LOG(ERROR, msg);
  105. }
  106. void SimpleLogger::error(const char* msg, Exception* e, ...) const {
  107. WRITE_LOG_EX(ERROR, msg, e);
  108. }