filter.h 2.5 KB

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