filter.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <chrono>
  6. #include <cstddef>
  7. #include "common/logging/log.h"
  8. namespace Common::Log {
  9. /**
  10. * Returns the name of the passed log class as a C-string. Subclasses are separated by periods
  11. * instead of underscores as in the enumeration.
  12. */
  13. const char* GetLogClassName(Class log_class);
  14. /**
  15. * Returns the name of the passed log level as a C-string.
  16. */
  17. const char* GetLevelName(Level log_level);
  18. /**
  19. * Implements a log message filter which allows different log classes to have different minimum
  20. * severity levels. The filter can be changed at runtime and can be parsed from a string to allow
  21. * editing via the interface or loading from a configuration file.
  22. */
  23. class Filter {
  24. public:
  25. /// Initializes the filter with all classes having `default_level` as the minimum level.
  26. explicit Filter(Level default_level = Level::Info);
  27. /// Resets the filter so that all classes have `level` as the minimum displayed level.
  28. void ResetAll(Level level);
  29. /// Sets the minimum level of `log_class` (and not of its subclasses) to `level`.
  30. void SetClassLevel(Class log_class, Level level);
  31. /**
  32. * Parses a filter string and applies it to this filter.
  33. *
  34. * A filter string consists of a space-separated list of filter rules, each of the format
  35. * `<class>:<level>`. `<class>` is a log class name, with subclasses separated using periods.
  36. * `*` is allowed as a class name and will reset all filters to the specified level. `<level>`
  37. * a severity level name which will be set as the minimum logging level of the matched classes.
  38. * Rules are applied left to right, with each rule overriding previous ones in the sequence.
  39. *
  40. * A few examples of filter rules:
  41. * - `*:Info` -- Resets the level of all classes to Info.
  42. * - `Service:Info` -- Sets the level of Service to Info.
  43. * - `Service.FS:Trace` -- Sets the level of the Service.FS class to Trace.
  44. */
  45. void ParseFilterString(std::string_view filter_view);
  46. /// Matches class/level combination against the filter, returning true if it passed.
  47. bool CheckMessage(Class log_class, Level level) const;
  48. /// Returns true if any logging classes are set to debug
  49. bool IsDebug() const;
  50. private:
  51. std::array<Level, static_cast<std::size_t>(Class::Count)> class_levels;
  52. };
  53. } // namespace Common::Log