backend.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, ARM) \
  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, SM) \
  33. SUB(Service, FS) \
  34. SUB(Service, GSP) \
  35. SUB(Service, CFG) \
  36. SUB(Service, DSP) \
  37. SUB(Service, HID) \
  38. CLS(HW) \
  39. SUB(HW, Memory) \
  40. SUB(HW, LCD) \
  41. SUB(HW, GPU) \
  42. SUB(HW, AES) \
  43. CLS(IPC) \
  44. CLS(Frontend) \
  45. CLS(Render) \
  46. SUB(Render, Software) \
  47. SUB(Render, OpenGL) \
  48. CLS(Audio) \
  49. SUB(Audio, DSP) \
  50. SUB(Audio, Sink) \
  51. CLS(Input) \
  52. CLS(Network) \
  53. CLS(Loader) \
  54. CLS(WebService)
  55. // GetClassName is a macro defined by Windows.h, grrr...
  56. const char* GetLogClassName(Class log_class) {
  57. switch (log_class) {
  58. #define CLS(x) \
  59. case Class::x: \
  60. return #x;
  61. #define SUB(x, y) \
  62. case Class::x##_##y: \
  63. return #x "." #y;
  64. ALL_LOG_CLASSES()
  65. #undef CLS
  66. #undef SUB
  67. case Class::Count:
  68. UNREACHABLE();
  69. }
  70. }
  71. const char* GetLevelName(Level log_level) {
  72. #define LVL(x) \
  73. case Level::x: \
  74. return #x
  75. switch (log_level) {
  76. LVL(Trace);
  77. LVL(Debug);
  78. LVL(Info);
  79. LVL(Warning);
  80. LVL(Error);
  81. LVL(Critical);
  82. case Level::Count:
  83. UNREACHABLE();
  84. }
  85. #undef LVL
  86. }
  87. Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
  88. const char* function, const char* format, va_list args) {
  89. using std::chrono::duration_cast;
  90. using std::chrono::steady_clock;
  91. static steady_clock::time_point time_origin = steady_clock::now();
  92. std::array<char, 4 * 1024> formatting_buffer;
  93. Entry entry;
  94. entry.timestamp = duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin);
  95. entry.log_class = log_class;
  96. entry.log_level = log_level;
  97. snprintf(formatting_buffer.data(), formatting_buffer.size(), "%s:%s:%u", filename, function,
  98. line_nr);
  99. entry.location = std::string(formatting_buffer.data());
  100. vsnprintf(formatting_buffer.data(), formatting_buffer.size(), format, args);
  101. entry.message = std::string(formatting_buffer.data());
  102. return entry;
  103. }
  104. static Filter* filter = nullptr;
  105. void SetFilter(Filter* new_filter) {
  106. filter = new_filter;
  107. }
  108. void LogMessage(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
  109. const char* function, const char* format, ...) {
  110. if (filter != nullptr && !filter->CheckMessage(log_class, log_level))
  111. return;
  112. va_list args;
  113. va_start(args, format);
  114. Entry entry = CreateEntry(log_class, log_level, filename, line_nr, function, format, args);
  115. va_end(args);
  116. PrintColoredMessage(entry);
  117. }
  118. }