backend.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. SUB(Debug, GDBStub) \
  29. CLS(Kernel) \
  30. SUB(Kernel, SVC) \
  31. CLS(Service) \
  32. SUB(Service, SRV) \
  33. SUB(Service, FRD) \
  34. SUB(Service, FS) \
  35. SUB(Service, ERR) \
  36. SUB(Service, APT) \
  37. SUB(Service, BOSS) \
  38. SUB(Service, GSP) \
  39. SUB(Service, AC) \
  40. SUB(Service, AM) \
  41. SUB(Service, PTM) \
  42. SUB(Service, LDR) \
  43. SUB(Service, MIC) \
  44. SUB(Service, NDM) \
  45. SUB(Service, NFC) \
  46. SUB(Service, NIM) \
  47. SUB(Service, NWM) \
  48. SUB(Service, CAM) \
  49. SUB(Service, CECD) \
  50. SUB(Service, CFG) \
  51. SUB(Service, CSND) \
  52. SUB(Service, DSP) \
  53. SUB(Service, DLP) \
  54. SUB(Service, HID) \
  55. SUB(Service, HTTP) \
  56. SUB(Service, SOC) \
  57. SUB(Service, IR) \
  58. SUB(Service, Y2R) \
  59. CLS(HW) \
  60. SUB(HW, Memory) \
  61. SUB(HW, LCD) \
  62. SUB(HW, GPU) \
  63. CLS(Frontend) \
  64. CLS(Render) \
  65. SUB(Render, Software) \
  66. SUB(Render, OpenGL) \
  67. CLS(Audio) \
  68. SUB(Audio, DSP) \
  69. SUB(Audio, Sink) \
  70. CLS(Loader)
  71. // GetClassName is a macro defined by Windows.h, grrr...
  72. const char* GetLogClassName(Class log_class) {
  73. switch (log_class) {
  74. #define CLS(x) \
  75. case Class::x: \
  76. return #x;
  77. #define SUB(x, y) \
  78. case Class::x##_##y: \
  79. return #x "." #y;
  80. ALL_LOG_CLASSES()
  81. #undef CLS
  82. #undef SUB
  83. case Class::Count:
  84. UNREACHABLE();
  85. }
  86. }
  87. const char* GetLevelName(Level log_level) {
  88. #define LVL(x) \
  89. case Level::x: \
  90. return #x
  91. switch (log_level) {
  92. LVL(Trace);
  93. LVL(Debug);
  94. LVL(Info);
  95. LVL(Warning);
  96. LVL(Error);
  97. LVL(Critical);
  98. case Level::Count:
  99. UNREACHABLE();
  100. }
  101. #undef LVL
  102. }
  103. Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
  104. const char* function, const char* format, va_list args) {
  105. using std::chrono::steady_clock;
  106. using std::chrono::duration_cast;
  107. static steady_clock::time_point time_origin = steady_clock::now();
  108. std::array<char, 4 * 1024> formatting_buffer;
  109. Entry entry;
  110. entry.timestamp = duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin);
  111. entry.log_class = log_class;
  112. entry.log_level = log_level;
  113. snprintf(formatting_buffer.data(), formatting_buffer.size(), "%s:%s:%u", filename, function,
  114. line_nr);
  115. entry.location = std::string(formatting_buffer.data());
  116. vsnprintf(formatting_buffer.data(), formatting_buffer.size(), format, args);
  117. entry.message = std::string(formatting_buffer.data());
  118. return entry;
  119. }
  120. static Filter* filter = nullptr;
  121. void SetFilter(Filter* new_filter) {
  122. filter = new_filter;
  123. }
  124. void LogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
  125. const char* function, const char* format, ...) {
  126. if (filter != nullptr && !filter->CheckMessage(log_class, log_level))
  127. return;
  128. va_list args;
  129. va_start(args, format);
  130. Entry entry = CreateEntry(log_class, log_level, filename, line_nr, function, format, args);
  131. va_end(args);
  132. PrintColoredMessage(entry);
  133. }
  134. }