filter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, MNPP) \
  103. SUB(Service, NCM) \
  104. SUB(Service, NFC) \
  105. SUB(Service, NFP) \
  106. SUB(Service, NGCT) \
  107. SUB(Service, NIFM) \
  108. SUB(Service, NIM) \
  109. SUB(Service, NOTIF) \
  110. SUB(Service, NPNS) \
  111. SUB(Service, NS) \
  112. SUB(Service, NVDRV) \
  113. SUB(Service, NVFlinger) \
  114. SUB(Service, OLSC) \
  115. SUB(Service, PCIE) \
  116. SUB(Service, PCTL) \
  117. SUB(Service, PCV) \
  118. SUB(Service, PM) \
  119. SUB(Service, PREPO) \
  120. SUB(Service, PSC) \
  121. SUB(Service, PSM) \
  122. SUB(Service, SET) \
  123. SUB(Service, SM) \
  124. SUB(Service, SPL) \
  125. SUB(Service, SSL) \
  126. SUB(Service, TCAP) \
  127. SUB(Service, Time) \
  128. SUB(Service, USB) \
  129. SUB(Service, VI) \
  130. SUB(Service, WLAN) \
  131. CLS(HW) \
  132. SUB(HW, Memory) \
  133. SUB(HW, LCD) \
  134. SUB(HW, GPU) \
  135. SUB(HW, AES) \
  136. CLS(IPC) \
  137. CLS(Frontend) \
  138. CLS(Render) \
  139. SUB(Render, Software) \
  140. SUB(Render, OpenGL) \
  141. SUB(Render, Vulkan) \
  142. CLS(Shader) \
  143. SUB(Shader, SPIRV) \
  144. SUB(Shader, GLASM) \
  145. SUB(Shader, GLSL) \
  146. CLS(Audio) \
  147. SUB(Audio, DSP) \
  148. SUB(Audio, Sink) \
  149. CLS(Input) \
  150. CLS(Network) \
  151. CLS(Loader) \
  152. CLS(CheatEngine) \
  153. CLS(Crypto) \
  154. CLS(WebService)
  155. // GetClassName is a macro defined by Windows.h, grrr...
  156. const char* GetLogClassName(Class log_class) {
  157. switch (log_class) {
  158. #define CLS(x) \
  159. case Class::x: \
  160. return #x;
  161. #define SUB(x, y) \
  162. case Class::x##_##y: \
  163. return #x "." #y;
  164. ALL_LOG_CLASSES()
  165. #undef CLS
  166. #undef SUB
  167. case Class::Count:
  168. break;
  169. }
  170. return "Invalid";
  171. }
  172. const char* GetLevelName(Level log_level) {
  173. #define LVL(x) \
  174. case Level::x: \
  175. return #x
  176. switch (log_level) {
  177. LVL(Trace);
  178. LVL(Debug);
  179. LVL(Info);
  180. LVL(Warning);
  181. LVL(Error);
  182. LVL(Critical);
  183. case Level::Count:
  184. break;
  185. }
  186. #undef LVL
  187. return "Invalid";
  188. }
  189. Filter::Filter(Level default_level) {
  190. ResetAll(default_level);
  191. }
  192. void Filter::ResetAll(Level level) {
  193. class_levels.fill(level);
  194. }
  195. void Filter::SetClassLevel(Class log_class, Level level) {
  196. class_levels[static_cast<std::size_t>(log_class)] = level;
  197. }
  198. void Filter::ParseFilterString(std::string_view filter_view) {
  199. auto clause_begin = filter_view.cbegin();
  200. while (clause_begin != filter_view.cend()) {
  201. auto clause_end = std::find(clause_begin, filter_view.cend(), ' ');
  202. // If clause isn't empty
  203. if (clause_end != clause_begin) {
  204. ParseFilterRule(*this, clause_begin, clause_end);
  205. }
  206. if (clause_end != filter_view.cend()) {
  207. // Skip over the whitespace
  208. ++clause_end;
  209. }
  210. clause_begin = clause_end;
  211. }
  212. }
  213. bool Filter::CheckMessage(Class log_class, Level level) const {
  214. return static_cast<u8>(level) >=
  215. static_cast<u8>(class_levels[static_cast<std::size_t>(log_class)]);
  216. }
  217. bool Filter::IsDebug() const {
  218. return std::any_of(class_levels.begin(), class_levels.end(), [](const Level& l) {
  219. return static_cast<u8>(l) <= static_cast<u8>(Level::Debug);
  220. });
  221. }
  222. } // namespace Common::Log