RADLib
RADical C++ application framework
RADLogging.h
Go to the documentation of this file.
1 #ifndef RADLOGGING_H
2 #define RADLOGGING_H
3 
9 #pragma once
10 
11 #include <RADCore/RADLicenseGate.h>
12 
13 #include <cstddef>
14 #include <memory>
15 #include <sstream>
16 #include <string>
17 
18 namespace RADLogging {
19 
21  enum class Level {
22  Trace = 0,
23  Debug,
24  Info,
25  Warning,
26  Error,
27  Critical,
28  Off
29  };
30 
32  class Sink {
33  public:
34  virtual ~Sink() = default;
36  virtual void write(Level level, const std::string& category, const std::string& line) = 0;
38  virtual void flush() {}
39  };
40 
42  class Logger {
43  public:
45  static Logger& instance();
47  void start();
49  void flush();
51  void shutdown();
55  Level level() const;
57  void setCategoryEnabled(const std::string& category, bool enabled);
59  bool shouldLog(Level level, const std::string& category) const;
61  void addSink(std::shared_ptr<Sink> sink);
63  void clearSinks();
65  void log(Level level, std::string category, std::string message);
66 
67  private:
68  Logger();
69  ~Logger();
70  struct Impl;
71  std::unique_ptr<Impl> pImpl_;
72  };
73 
75  class LogStream {
76  public:
78  LogStream(Level level, std::string category);
82  template<typename T>
83  LogStream& operator<<(const T& value) {
84  stream_ << value;
85  return *this;
86  }
87 
88  private:
89  Level level_;
90  std::string category_;
91  std::ostringstream stream_;
92  };
93 
95  std::shared_ptr<Sink> consoleSink(bool stderrForWarnings = true);
97  std::shared_ptr<Sink> fileSink(const std::string& path);
99  std::shared_ptr<Sink> rotatingFileSink(const std::string& path, size_t maxBytes, size_t maxFiles = 3);
101  std::shared_ptr<Sink> syslogSink(std::string ident = "RADLib");
103  std::string levelName(Level level);
104 
105 } // namespace RADLogging
106 
108 #define RADLOG_TRACE(category) RADLogging::LogStream(RADLogging::Level::Trace, category)
110 #define RADLOG_DEBUG(category) RADLogging::LogStream(RADLogging::Level::Debug, category)
112 #define RADLOG_INFO(category) RADLogging::LogStream(RADLogging::Level::Info, category)
114 #define RADLOG_WARNING(category) RADLogging::LogStream(RADLogging::Level::Warning, category)
116 #define RADLOG_ERROR(category) RADLogging::LogStream(RADLogging::Level::Error, category)
117 
118 #endif
Streaming log record; emits on destruction.
Definition: RADLogging.h:75
LogStream & operator<<(const T &value)
Appends a value.
Definition: RADLogging.h:83
LogStream(Level level, std::string category)
Creates a stream record.
~LogStream()
Emits the accumulated message.
Async process-wide logger.
Definition: RADLogging.h:42
static Logger & instance()
Returns singleton logger.
void setLevel(Level level)
Sets minimum level.
void start()
Starts worker thread.
void flush()
Flushes queued log messages.
void log(Level level, std::string category, std::string message)
Queues one log message.
Level level() const
Returns minimum level.
void shutdown()
Stops worker thread after draining queued messages.
void addSink(std::shared_ptr< Sink > sink)
Adds a sink. Existing sinks are preserved.
bool shouldLog(Level level, const std::string &category) const
Returns true when a message should be accepted.
void clearSinks()
Removes all sinks.
void setCategoryEnabled(const std::string &category, bool enabled)
Enables or disables a category.
Logger sink interface.
Definition: RADLogging.h:32
virtual void write(Level level, const std::string &category, const std::string &line)=0
Writes one already-formatted log line.
virtual void flush()
Flushes buffered output.
Definition: RADLogging.h:38
virtual ~Sink()=default
Definition: RADLogging.h:18
std::shared_ptr< Sink > consoleSink(bool stderrForWarnings=true)
Creates a console sink.
Level
Log severity.
Definition: RADLogging.h:21
std::string levelName(Level level)
Formats a level as text.
std::shared_ptr< Sink > syslogSink(std::string ident="RADLib")
Creates a syslog sink.
std::shared_ptr< Sink > rotatingFileSink(const std::string &path, size_t maxBytes, size_t maxFiles=3)
Creates a rotating file sink.
std::shared_ptr< Sink > fileSink(const std::string &path)
Creates a file sink.