backend.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <utility>
  5. #include "common/assert.h"
  6. #include "common/logging/backend.h"
  7. #include "common/logging/filter.h"
  8. #include "common/logging/log.h"
  9. #include "common/logging/text_formatter.h"
  10. #include "common/string_util.h"
  11. namespace Log {
  12. /// Macro listing all log classes. Code should define CLS and SUB as desired before invoking this.
  13. #define ALL_LOG_CLASSES() \
  14. CLS(Log) \
  15. CLS(Common) \
  16. SUB(Common, Filesystem) \
  17. SUB(Common, Memory) \
  18. CLS(Core) \
  19. SUB(Core, ARM) \
  20. SUB(Core, Timing) \
  21. CLS(Config) \
  22. CLS(Debug) \
  23. SUB(Debug, Emulated) \
  24. SUB(Debug, GPU) \
  25. SUB(Debug, Breakpoint) \
  26. SUB(Debug, GDBStub) \
  27. CLS(Kernel) \
  28. SUB(Kernel, SVC) \
  29. CLS(Service) \
  30. SUB(Service, ACC) \
  31. SUB(Service, Audio) \
  32. SUB(Service, AM) \
  33. SUB(Service, AOC) \
  34. SUB(Service, APM) \
  35. SUB(Service, BCAT) \
  36. SUB(Service, Fatal) \
  37. SUB(Service, Friend) \
  38. SUB(Service, FS) \
  39. SUB(Service, HID) \
  40. SUB(Service, LM) \
  41. SUB(Service, MM) \
  42. SUB(Service, NFP) \
  43. SUB(Service, NIFM) \
  44. SUB(Service, NS) \
  45. SUB(Service, NVDRV) \
  46. SUB(Service, PCTL) \
  47. SUB(Service, PREPO) \
  48. SUB(Service, SET) \
  49. SUB(Service, SM) \
  50. SUB(Service, SPL) \
  51. SUB(Service, SSL) \
  52. SUB(Service, Time) \
  53. SUB(Service, VI) \
  54. CLS(HW) \
  55. SUB(HW, Memory) \
  56. SUB(HW, LCD) \
  57. SUB(HW, GPU) \
  58. SUB(HW, AES) \
  59. CLS(IPC) \
  60. CLS(Frontend) \
  61. CLS(Render) \
  62. SUB(Render, Software) \
  63. SUB(Render, OpenGL) \
  64. CLS(Audio) \
  65. SUB(Audio, DSP) \
  66. SUB(Audio, Sink) \
  67. CLS(Input) \
  68. CLS(Network) \
  69. CLS(Loader) \
  70. CLS(WebService)
  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, std::string message) {
  105. using std::chrono::duration_cast;
  106. using std::chrono::steady_clock;
  107. static steady_clock::time_point time_origin = steady_clock::now();
  108. Entry entry;
  109. entry.timestamp = duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin);
  110. entry.log_class = log_class;
  111. entry.log_level = log_level;
  112. entry.filename = Common::TrimSourcePath(filename);
  113. entry.line_num = line_nr;
  114. entry.function = function;
  115. entry.message = std::move(message);
  116. return entry;
  117. }
  118. static Filter* filter = nullptr;
  119. void SetFilter(Filter* new_filter) {
  120. filter = new_filter;
  121. }
  122. void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename,
  123. unsigned int line_num, const char* function, const char* format,
  124. const fmt::format_args& args) {
  125. if (filter && !filter->CheckMessage(log_class, log_level))
  126. return;
  127. Entry entry =
  128. CreateEntry(log_class, log_level, filename, line_num, function, fmt::vformat(format, args));
  129. PrintColoredMessage(entry);
  130. }
  131. } // namespace Log