filter.h 2.1 KB

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