Logger.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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::init()
  23. {
  24. }
  25. void Logger::log(const char *fmt, ...)
  26. {
  27. print_premessage(" [LOG]");
  28. va_list args;
  29. va_start(args, fmt);
  30. vprintf(fmt, args);
  31. va_end(args);
  32. puts("");
  33. }
  34. void Logger::debug(const char *fmt, ...)
  35. {
  36. print_premessage("[DEBUG]");
  37. va_list args;
  38. va_start(args, fmt);
  39. vprintf(fmt, args);
  40. va_end(args);
  41. puts("");
  42. }
  43. void Logger::warn(const char *fmt, ...)
  44. {
  45. print_premessage(" [WARN]");
  46. va_list args;
  47. va_start(args, fmt);
  48. vprintf(fmt, args);
  49. va_end(args);
  50. puts("");
  51. }
  52. void Logger::error(const char *fmt, ...)
  53. {
  54. print_premessage("[ERROR]");
  55. va_list args;
  56. va_start(args, fmt);
  57. vprintf(fmt, args);
  58. va_end(args);
  59. puts("");
  60. }