backend.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <cstdio>
  7. #include "common/assert.h"
  8. #include "common/common_funcs.h" // snprintf compatibility define
  9. #include "common/logging/backend.h"
  10. #include "common/logging/filter.h"
  11. #include "common/logging/log.h"
  12. #include "common/logging/text_formatter.h"
  13. namespace Log {
  14. /// Macro listing all log classes. Code should define CLS and SUB as desired before invoking this.
  15. #define ALL_LOG_CLASSES() \
  16. CLS(Log) \
  17. CLS(Common) \
  18. SUB(Common, Filesystem) \
  19. SUB(Common, Memory) \
  20. CLS(Core) \
  21. SUB(Core, ARM11) \
  22. SUB(Core, Timing) \
  23. CLS(Config) \
  24. CLS(Debug) \
  25. SUB(Debug, Emulated) \
  26. SUB(Debug, GPU) \
  27. SUB(Debug, Breakpoint) \
  28. CLS(Kernel) \
  29. SUB(Kernel, SVC) \
  30. CLS(Service) \
  31. SUB(Service, SRV) \
  32. SUB(Service, FS) \
  33. SUB(Service, ERR) \
  34. SUB(Service, APT) \
  35. SUB(Service, GSP) \
  36. SUB(Service, AC) \
  37. SUB(Service, AM) \
  38. SUB(Service, PTM) \
  39. SUB(Service, LDR) \
  40. SUB(Service, NIM) \
  41. SUB(Service, NWM) \
  42. SUB(Service, CFG) \
  43. SUB(Service, DSP) \
  44. SUB(Service, HID) \
  45. SUB(Service, SOC) \
  46. SUB(Service, Y2R) \
  47. CLS(HW) \
  48. SUB(HW, Memory) \
  49. SUB(HW, LCD) \
  50. SUB(HW, GPU) \
  51. CLS(Frontend) \
  52. CLS(Render) \
  53. SUB(Render, Software) \
  54. SUB(Render, OpenGL) \
  55. CLS(Loader)
  56. // GetClassName is a macro defined by Windows.h, grrr...
  57. const char* GetLogClassName(Class log_class) {
  58. switch (log_class) {
  59. #define CLS(x) case Class::x: return #x;
  60. #define SUB(x, y) case Class::x##_##y: return #x "." #y;
  61. ALL_LOG_CLASSES()
  62. #undef CLS
  63. #undef SUB
  64. }
  65. return "Unknown";
  66. }
  67. const char* GetLevelName(Level log_level) {
  68. #define LVL(x) case Level::x: return #x
  69. switch (log_level) {
  70. LVL(Trace);
  71. LVL(Debug);
  72. LVL(Info);
  73. LVL(Warning);
  74. LVL(Error);
  75. LVL(Critical);
  76. case Level::Count:
  77. UNREACHABLE();
  78. }
  79. #undef LVL
  80. }
  81. Entry CreateEntry(Class log_class, Level log_level,
  82. const char* filename, unsigned int line_nr, const char* function,
  83. const char* format, va_list args) {
  84. using std::chrono::steady_clock;
  85. using std::chrono::duration_cast;
  86. static steady_clock::time_point time_origin = steady_clock::now();
  87. std::array<char, 4 * 1024> formatting_buffer;
  88. Entry entry;
  89. entry.timestamp = duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin);
  90. entry.log_class = log_class;
  91. entry.log_level = log_level;
  92. snprintf(formatting_buffer.data(), formatting_buffer.size(), "%s:%s:%u", filename, function, line_nr);
  93. entry.location = std::string(formatting_buffer.data());
  94. vsnprintf(formatting_buffer.data(), formatting_buffer.size(), format, args);
  95. entry.message = std::string(formatting_buffer.data());
  96. return std::move(entry);
  97. }
  98. static Filter* filter = nullptr;
  99. void SetFilter(Filter* new_filter) {
  100. filter = new_filter;
  101. }
  102. void LogMessage(Class log_class, Level log_level,
  103. const char* filename, unsigned int line_nr, const char* function,
  104. const char* format, ...) {
  105. if (filter != nullptr && !filter->CheckMessage(log_class, log_level))
  106. return;
  107. va_list args;
  108. va_start(args, format);
  109. Entry entry = CreateEntry(log_class, log_level,
  110. filename, line_nr, function, format, args);
  111. va_end(args);
  112. PrintColoredMessage(entry);
  113. }
  114. }