filter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "common/logging/filter.h"
  6. #include "common/string_util.h"
  7. namespace Common::Log {
  8. namespace {
  9. template <typename It>
  10. Level GetLevelByName(const It begin, const It end) {
  11. for (u8 i = 0; i < static_cast<u8>(Level::Count); ++i) {
  12. const char* level_name = GetLevelName(static_cast<Level>(i));
  13. if (Common::ComparePartialString(begin, end, level_name)) {
  14. return static_cast<Level>(i);
  15. }
  16. }
  17. return Level::Count;
  18. }
  19. template <typename It>
  20. Class GetClassByName(const It begin, const It end) {
  21. for (u8 i = 0; i < static_cast<u8>(Class::Count); ++i) {
  22. const char* level_name = GetLogClassName(static_cast<Class>(i));
  23. if (Common::ComparePartialString(begin, end, level_name)) {
  24. return static_cast<Class>(i);
  25. }
  26. }
  27. return Class::Count;
  28. }
  29. template <typename Iterator>
  30. bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) {
  31. auto level_separator = std::find(begin, end, ':');
  32. if (level_separator == end) {
  33. LOG_ERROR(Log, "Invalid log filter. Must specify a log level after `:`: {}",
  34. std::string(begin, end));
  35. return false;
  36. }
  37. const Level level = GetLevelByName(level_separator + 1, end);
  38. if (level == Level::Count) {
  39. LOG_ERROR(Log, "Unknown log level in filter: {}", std::string(begin, end));
  40. return false;
  41. }
  42. if (Common::ComparePartialString(begin, level_separator, "*")) {
  43. instance.ResetAll(level);
  44. return true;
  45. }
  46. const Class log_class = GetClassByName(begin, level_separator);
  47. if (log_class == Class::Count) {
  48. LOG_ERROR(Log, "Unknown log class in filter: {}", std::string(begin, end));
  49. return false;
  50. }
  51. instance.SetClassLevel(log_class, level);
  52. return true;
  53. }
  54. } // Anonymous namespace
  55. /// Macro listing all log classes. Code should define CLS and SUB as desired before invoking this.
  56. #define ALL_LOG_CLASSES() \
  57. CLS(Log) \
  58. CLS(Common) \
  59. SUB(Common, Filesystem) \
  60. SUB(Common, Memory) \
  61. CLS(Core) \
  62. SUB(Core, ARM) \
  63. SUB(Core, Timing) \
  64. CLS(Config) \
  65. CLS(Debug) \
  66. SUB(Debug, Emulated) \
  67. SUB(Debug, GPU) \
  68. SUB(Debug, Breakpoint) \
  69. SUB(Debug, GDBStub) \
  70. CLS(Kernel) \
  71. SUB(Kernel, SVC) \
  72. CLS(Service) \
  73. SUB(Service, ACC) \
  74. SUB(Service, Audio) \
  75. SUB(Service, AM) \
  76. SUB(Service, AOC) \
  77. SUB(Service, APM) \
  78. SUB(Service, ARP) \
  79. SUB(Service, BCAT) \
  80. SUB(Service, BPC) \
  81. SUB(Service, BGTC) \
  82. SUB(Service, BTDRV) \
  83. SUB(Service, BTM) \
  84. SUB(Service, Capture) \
  85. SUB(Service, ERPT) \
  86. SUB(Service, ETicket) \
  87. SUB(Service, EUPLD) \
  88. SUB(Service, Fatal) \
  89. SUB(Service, FGM) \
  90. SUB(Service, Friend) \
  91. SUB(Service, FS) \
  92. SUB(Service, GRC) \
  93. SUB(Service, HID) \
  94. SUB(Service, IRS) \
  95. SUB(Service, LBL) \
  96. SUB(Service, LDN) \
  97. SUB(Service, LDR) \
  98. SUB(Service, LM) \
  99. SUB(Service, Migration) \
  100. SUB(Service, Mii) \
  101. SUB(Service, MM) \
  102. SUB(Service, NCM) \
  103. SUB(Service, NFC) \
  104. SUB(Service, NFP) \
  105. SUB(Service, NIFM) \
  106. SUB(Service, NIM) \
  107. SUB(Service, NPNS) \
  108. SUB(Service, NS) \
  109. SUB(Service, NVDRV) \
  110. SUB(Service, OLSC) \
  111. SUB(Service, PCIE) \
  112. SUB(Service, PCTL) \
  113. SUB(Service, PCV) \
  114. SUB(Service, PM) \
  115. SUB(Service, PREPO) \
  116. SUB(Service, PSC) \
  117. SUB(Service, PSM) \
  118. SUB(Service, SET) \
  119. SUB(Service, SM) \
  120. SUB(Service, SPL) \
  121. SUB(Service, SSL) \
  122. SUB(Service, TCAP) \
  123. SUB(Service, Time) \
  124. SUB(Service, USB) \
  125. SUB(Service, VI) \
  126. SUB(Service, WLAN) \
  127. CLS(HW) \
  128. SUB(HW, Memory) \
  129. SUB(HW, LCD) \
  130. SUB(HW, GPU) \
  131. SUB(HW, AES) \
  132. CLS(IPC) \
  133. CLS(Frontend) \
  134. CLS(Render) \
  135. SUB(Render, Software) \
  136. SUB(Render, OpenGL) \
  137. SUB(Render, Vulkan) \
  138. CLS(Shader) \
  139. SUB(Shader, SPIRV) \
  140. SUB(Shader, GLASM) \
  141. SUB(Shader, GLSL) \
  142. CLS(Audio) \
  143. SUB(Audio, DSP) \
  144. SUB(Audio, Sink) \
  145. CLS(Input) \
  146. CLS(Network) \
  147. CLS(Loader) \
  148. CLS(CheatEngine) \
  149. CLS(Crypto) \
  150. CLS(WebService)
  151. // GetClassName is a macro defined by Windows.h, grrr...
  152. const char* GetLogClassName(Class log_class) {
  153. switch (log_class) {
  154. #define CLS(x) \
  155. case Class::x: \
  156. return #x;
  157. #define SUB(x, y) \
  158. case Class::x##_##y: \
  159. return #x "." #y;
  160. ALL_LOG_CLASSES()
  161. #undef CLS
  162. #undef SUB
  163. case Class::Count:
  164. break;
  165. }
  166. return "Invalid";
  167. }
  168. const char* GetLevelName(Level log_level) {
  169. #define LVL(x) \
  170. case Level::x: \
  171. return #x
  172. switch (log_level) {
  173. LVL(Trace);
  174. LVL(Debug);
  175. LVL(Info);
  176. LVL(Warning);
  177. LVL(Error);
  178. LVL(Critical);
  179. case Level::Count:
  180. break;
  181. }
  182. #undef LVL
  183. return "Invalid";
  184. }
  185. Filter::Filter(Level default_level) {
  186. ResetAll(default_level);
  187. }
  188. void Filter::ResetAll(Level level) {
  189. class_levels.fill(level);
  190. }
  191. void Filter::SetClassLevel(Class log_class, Level level) {
  192. class_levels[static_cast<std::size_t>(log_class)] = level;
  193. }
  194. void Filter::ParseFilterString(std::string_view filter_view) {
  195. auto clause_begin = filter_view.cbegin();
  196. while (clause_begin != filter_view.cend()) {
  197. auto clause_end = std::find(clause_begin, filter_view.cend(), ' ');
  198. // If clause isn't empty
  199. if (clause_end != clause_begin) {
  200. ParseFilterRule(*this, clause_begin, clause_end);
  201. }
  202. if (clause_end != filter_view.cend()) {
  203. // Skip over the whitespace
  204. ++clause_end;
  205. }
  206. clause_begin = clause_end;
  207. }
  208. }
  209. bool Filter::CheckMessage(Class log_class, Level level) const {
  210. return static_cast<u8>(level) >=
  211. static_cast<u8>(class_levels[static_cast<std::size_t>(log_class)]);
  212. }
  213. bool Filter::IsDebug() const {
  214. return std::any_of(class_levels.begin(), class_levels.end(), [](const Level& l) {
  215. return static_cast<u8>(l) <= static_cast<u8>(Level::Debug);
  216. });
  217. }
  218. } // namespace Common::Log