carl  24.04
Computer ARithmetic Library
Filter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "LogLevel.h"
4 
5 #include <cassert>
6 #include <iostream>
7 #include <map>
8 #include <string>
9 
10 namespace carl::logging {
11 
12 /**
13  * This class checks if some log message shall be forwarded to some sink.
14  */
15 class Filter {
16  /// Mapping from channels to (minimal) log levels.
17  std::map<std::string, LogLevel> mData = {
18  std::make_pair(std::string(""), LogLevel::LVL_DEFAULT)
19  };
20 public:
21  /**
22  * Returns the internal filter data.
23  */
24  const auto& data() const {
25  return mData;
26  }
27  /**
28  * Set the minimum log level for some channel.
29  * Returns `*this`, hence calls to this method can be chained arbitrarily.
30  * @param channel Channel name.
31  * @param level LogLevel.
32  * @return This object.
33  */
34  Filter& operator()(const std::string& channel, LogLevel level) {
35  mData[channel] = level;
36  return *this;
37  }
38  /**
39  * Checks if the given log level is sufficient for the log message to be forwarded.
40  * @param channel Channel name.
41  * @param level LogLevel.
42  * @return If the message shall be forwarded.
43  */
44  bool check(const std::string& channel, LogLevel level) const noexcept {
45  auto tmp = channel;
46  auto it = mData.find(tmp);
47  while (!tmp.empty() && it == mData.end()) {
48  auto n = tmp.rfind('.');
49  tmp = (n == std::string::npos) ? "" : tmp.substr(0, n);
50  it = mData.find(tmp);
51  }
52  if (it == mData.end()) {
53  std::cout << "Did not find something for \"" << channel << "\"" << std::endl;
54  return true;
55  }
56  assert(it != mData.end());
57  return level >= it->second;
58  }
59  /**
60  * Streaming operator for a Filter.
61  * All the rules stored in the filter are printed in a human-readable fashion.
62  * @param os Output stream.
63  * @param f Filter.
64  * @return os.
65  */
66  friend std::ostream& operator<<(std::ostream& os, const Filter& f) {
67  os << "Filter:" << std::endl;
68  for (const auto& it: f.mData) {
69  os << "\t\"" << it.first << "\" -> " << it.second << std::endl;
70  }
71  return os;
72  }
73 };
74 
75 }
Contains a custom logging facility.
Definition: carl-logging.cpp:6
LogLevel
Indicated which log messages should be forwarded to some sink.
Definition: LogLevel.h:12
@ LVL_DEFAULT
Default log level.
This class checks if some log message shall be forwarded to some sink.
Definition: Filter.h:15
Filter & operator()(const std::string &channel, LogLevel level)
Set the minimum log level for some channel.
Definition: Filter.h:34
friend std::ostream & operator<<(std::ostream &os, const Filter &f)
Streaming operator for a Filter.
Definition: Filter.h:66
std::map< std::string, LogLevel > mData
Mapping from channels to (minimal) log levels.
Definition: Filter.h:17
bool check(const std::string &channel, LogLevel level) const noexcept
Checks if the given log level is sufficient for the log message to be forwarded.
Definition: Filter.h:44
const auto & data() const
Returns the internal filter data.
Definition: Filter.h:24