You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.5 KiB

  1. #pragma once
  2. #include <set>
  3. #include <mutex>
  4. #include <cpputils/logging/matcher/matcher.h>
  5. #include <cpputils/logging/consumer/consumer.h>
  6. namespace utl {
  7. namespace logging {
  8. struct rule
  9. {
  10. private:
  11. mutable std::mutex _mutex;
  12. std::set<consumer*> _consumer;
  13. public:
  14. matcher_ptr_u logger_matcher;
  15. matcher_ptr_u consumer_matcher;
  16. log_level min_level;
  17. log_level max_level;
  18. inline bool is_enabled(log_level level) const
  19. {
  20. return min_level <= level
  21. && max_level >= level;
  22. }
  23. inline void register_consumer(consumer& consumer)
  24. {
  25. std::lock_guard<std::mutex> lk(_mutex);
  26. _consumer.insert(&consumer);
  27. }
  28. inline void unregister_consumer(consumer& consumer)
  29. {
  30. std::lock_guard<std::mutex> lk(_mutex);
  31. _consumer.erase(&consumer);
  32. }
  33. inline void log(data_ptr_s data)
  34. {
  35. std::lock_guard<std::mutex> lk(_mutex);
  36. if (is_enabled(data->level))
  37. {
  38. for (auto& c : _consumer)
  39. c->log(data);
  40. }
  41. }
  42. rule(matcher_ptr_u lm, matcher_ptr_u cm, log_level min, log_level max) :
  43. logger_matcher (std::move(lm)),
  44. consumer_matcher(std::move(cm)),
  45. min_level (min),
  46. max_level (max)
  47. { }
  48. };
  49. using rule_handle = void*;
  50. }
  51. }