carl  24.04
Computer ARithmetic Library
Formatter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Filter.h"
4 #include "LogLevel.h"
5 
6 #include <iomanip>
7 #include <iosfwd>
8 #ifdef THREAD_SAFE
9 #include <thread>
10 #endif
11 
12 namespace carl::logging {
13 
14 /**
15  * Formats a log messages.
16  */
17 class Formatter {
18  /// Width of the longest channel.
19  std::size_t channelwidth = 10;
20 public:
21  /// Print information like log level, file etc.
22  bool printInformation = true;
23 
24  virtual ~Formatter() noexcept = default;
25 
26  /**
27  * Extracts the maximum width of a channel to optimize the formatting.
28  * @param f Filter.
29  */
30  virtual void configure(const Filter& f) noexcept {
31  for (const auto& t: f.data()) {
32  if (t.first.size() > channelwidth) channelwidth = t.first.size();
33  }
34  }
35  /**
36  * Prints the prefix of a log message, i.e. everything that goes before the message given by the user, to the output stream.
37  * @param os Output stream.
38  * @param channel Channel name.
39  * @param level LogLevel.
40  * @param info Auxiliary information.
41  */
42  virtual void prefix(std::ostream& os, const std::string& channel, LogLevel level, const RecordInfo& info) {
43  if (!printInformation) return;
44  os.fill(' ');
45 #ifdef THREAD_SAFE
46  os << std::this_thread::get_id() << " ";
47 #endif
48  os << level << " ";
49 
50  std::string filename(carl::basename(info.filename));
51  std::size_t spacing = 1;
52  if (channelwidth + 15 > channel.size() + filename.size()) {
53  spacing = channelwidth + 15 - channel.size() - filename.size();
54  }
55  os << channel << std::string(spacing, ' ') << filename << ":" << std::left << std::setw(4) << info.line << " ";
56  os << std::resetiosflags(std::ios::adjustfield);
57  if (!info.func.empty()) {
58  os << info.func << "(): ";
59  }
60  }
61 
62  /**
63  * Prints the suffix of a log message, i.e. everything that goes after the message given by the user, to the output stream.
64  * Usually, this is only a newline.
65  * @param os Output stream.
66  */
67  virtual void suffix(std::ostream& os) {
68  os << std::endl;
69  }
70 };
71 
72 }
std::string basename(const std::string &filename)
Return the basename of a given filename.
Definition: logging_utils.h:33
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
This class checks if some log message shall be forwarded to some sink.
Definition: Filter.h:15
Formats a log messages.
Definition: Formatter.h:17
virtual void suffix(std::ostream &os)
Prints the suffix of a log message, i.e.
Definition: Formatter.h:67
bool printInformation
Print information like log level, file etc.
Definition: Formatter.h:22
virtual void configure(const Filter &f) noexcept
Extracts the maximum width of a channel to optimize the formatting.
Definition: Formatter.h:30
virtual void prefix(std::ostream &os, const std::string &channel, LogLevel level, const RecordInfo &info)
Prints the prefix of a log message, i.e.
Definition: Formatter.h:42
virtual ~Formatter() noexcept=default
std::size_t channelwidth
Width of the longest channel.
Definition: Formatter.h:19
Additional information about a log message.
Definition: logging.h:13
std::string filename
File name.
Definition: logging.h:15
std::string func
Function name.
Definition: logging.h:17
std::size_t line
Line number.
Definition: logging.h:19