Logger.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "Logger.h"
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <time.h>
  5. using namespace WalrusRPG;
  6. namespace
  7. {
  8. // TODO : Find a better name
  9. /**
  10. * Prints the timestamp and the message category/type.
  11. */
  12. void print_premessage(const char *type)
  13. {
  14. char date_buffer[256];
  15. time_t now = time(0);
  16. strftime(date_buffer, 256, "%Y-%m-%d %H:%M:%S", localtime(&now));
  17. printf("%s %5s : ", date_buffer, type);
  18. }
  19. }
  20. // NOTE : I really wish there would be a better way to handle these stupid va_lists. So
  21. // much redundant code...
  22. void Logger::log(const char *fmt, ...)
  23. {
  24. print_premessage(" [LOG]");
  25. va_list args;
  26. va_start(args, fmt);
  27. vprintf(fmt, args);
  28. va_end(args);
  29. puts("");
  30. }
  31. void Logger::debug(const char *fmt, ...)
  32. {
  33. print_premessage("[DEBUG]");
  34. va_list args;
  35. va_start(args, fmt);
  36. vprintf(fmt, args);
  37. va_end(args);
  38. puts("");
  39. }
  40. void Logger::warn(const char *fmt, ...)
  41. {
  42. print_premessage(" [WARN]");
  43. va_list args;
  44. va_start(args, fmt);
  45. vprintf(fmt, args);
  46. va_end(args);
  47. puts("");
  48. }
  49. void Logger::error(const char *fmt, ...)
  50. {
  51. print_premessage("[ERROR]");
  52. va_list args;
  53. va_start(args, fmt);
  54. vprintf(fmt, args);
  55. va_end(args);
  56. puts("");
  57. }