SimpleLogger.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <time.h>
  25. #include <stdarg.h>
  26. SimpleLogger::SimpleLogger(string filename) {
  27. file = fopen(filename.c_str(), "a");
  28. }
  29. SimpleLogger::SimpleLogger(FILE* logfile) {
  30. file = logfile;
  31. }
  32. SimpleLogger::~SimpleLogger() {
  33. if(file != NULL) {
  34. fclose(file);
  35. }
  36. }
  37. void SimpleLogger::writeLog(int level, const char* msg, va_list ap, Exception* e) const
  38. {
  39. string levelStr;
  40. switch(level) {
  41. case DEBUG:
  42. levelStr = "DEBUG";
  43. break;
  44. case ERROR:
  45. levelStr = "ERROR";
  46. break;
  47. case INFO:
  48. default:
  49. levelStr = "INFO";
  50. }
  51. time_t now = time(NULL);
  52. char datestr[26];
  53. ctime_r(&now, datestr);
  54. datestr[strlen(datestr)-1] = '\0';
  55. vfprintf(file, string(string(datestr)+" - "+levelStr+" - "+Util::replace(msg, "\r", "")+"\n").c_str(), ap);
  56. if(e != NULL) {
  57. fprintf(file, string(string(datestr)+" - "+levelStr+" - exception: "+Util::replace(e->getMsg(), "\r", "")+"\n").c_str());
  58. }
  59. fflush(stdout);
  60. }
  61. void SimpleLogger::debug(const char* msg, ...) const {
  62. va_list ap;
  63. va_start(ap, msg);
  64. writeLog(DEBUG, msg, ap);
  65. va_end(ap);
  66. }
  67. void SimpleLogger::debug(const char* msg, Exception* e, ...) const {
  68. va_list ap;
  69. va_start(ap, e);
  70. writeLog(DEBUG, msg, ap, e);
  71. va_end(ap);
  72. }
  73. void SimpleLogger::info(const char* msg, ...) const {
  74. va_list ap;
  75. va_start(ap, msg);
  76. writeLog(INFO, msg, ap);
  77. va_end(ap);
  78. }
  79. void SimpleLogger::info(const char* msg, Exception* e, ...) const {
  80. va_list ap;
  81. va_start(ap, e);
  82. writeLog(INFO, msg, ap, e);
  83. va_end(ap);
  84. }
  85. void SimpleLogger::error(const char* msg, ...) const {
  86. va_list ap;
  87. va_start(ap, msg);
  88. writeLog(ERROR, msg, ap);
  89. va_end(ap);
  90. }
  91. void SimpleLogger::error(const char* msg, Exception* e, ...) const {
  92. va_list ap;
  93. va_start(ap, e);
  94. writeLog(ERROR, msg, ap, e);
  95. va_end(ap);
  96. }