backend.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, Fatal) \
  36. SUB(Service, Friend) \
  37. SUB(Service, FS) \
  38. SUB(Service, HID) \
  39. SUB(Service, LM) \
  40. SUB(Service, NFP) \
  41. SUB(Service, NIFM) \
  42. SUB(Service, NS) \
  43. SUB(Service, NVDRV) \
  44. SUB(Service, PCTL) \
  45. SUB(Service, PREPO) \
  46. SUB(Service, SET) \
  47. SUB(Service, SM) \
  48. SUB(Service, SPL) \
  49. SUB(Service, SSL) \
  50. SUB(Service, Time) \
  51. SUB(Service, VI) \
  52. CLS(HW) \
  53. SUB(HW, Memory) \
  54. SUB(HW, LCD) \
  55. SUB(HW, GPU) \
  56. SUB(HW, AES) \
  57. CLS(IPC) \
  58. CLS(Frontend) \
  59. CLS(Render) \
  60. SUB(Render, Software) \
  61. SUB(Render, OpenGL) \
  62. CLS(Audio) \
  63. SUB(Audio, DSP) \
  64. SUB(Audio, Sink) \
  65. CLS(Input) \
  66. CLS(Network) \
  67. CLS(Loader) \
  68. CLS(WebService)
  69. // GetClassName is a macro defined by Windows.h, grrr...
  70. const char* GetLogClassName(Class log_class) {
  71. switch (log_class) {
  72. #define CLS(x) \
  73. case Class::x: \
  74. return #x;
  75. #define SUB(x, y) \
  76. case Class::x##_##y: \
  77. return #x "." #y;
  78. ALL_LOG_CLASSES()
  79. #undef CLS
  80. #undef SUB
  81. case Class::Count:
  82. UNREACHABLE();
  83. }
  84. }
  85. const char* GetLevelName(Level log_level) {
  86. #define LVL(x) \
  87. case Level::x: \
  88. return #x
  89. switch (log_level) {
  90. LVL(Trace);
  91. LVL(Debug);
  92. LVL(Info);
  93. LVL(Warning);
  94. LVL(Error);
  95. LVL(Critical);
  96. case Level::Count:
  97. UNREACHABLE();
  98. }
  99. #undef LVL
  100. }
  101. Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
  102. const char* function, std::string message) {
  103. using std::chrono::duration_cast;
  104. using std::chrono::steady_clock;
  105. static steady_clock::time_point time_origin = steady_clock::now();
  106. Entry entry;
  107. entry.timestamp = duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin);
  108. entry.log_class = log_class;
  109. entry.log_level = log_level;
  110. entry.filename = Common::TrimSourcePath(filename);
  111. entry.line_num = line_nr;
  112. entry.function = function;
  113. entry.message = std::move(message);
  114. return entry;
  115. }
  116. static Filter* filter = nullptr;
  117. void SetFilter(Filter* new_filter) {
  118. filter = new_filter;
  119. }
  120. void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename,
  121. unsigned int line_num, const char* function, const char* format,
  122. const fmt::format_args& args) {
  123. if (filter && !filter->CheckMessage(log_class, log_level))
  124. return;
  125. Entry entry =
  126. CreateEntry(log_class, log_level, filename, line_num, function, fmt::vformat(format, args));
  127. PrintColoredMessage(entry);
  128. }
  129. } // namespace Log