logger.h
Go to the documentation of this file.
1 // Logs all output to a log file for the run
2 // Author: Max Schwarz <max.schwarz@uni-bonn.de>
3 
4 #ifndef LOGGER_H
5 #define LOGGER_H
6 
7 #include <memory>
8 #include <string>
9 
10 #include "log_event.h"
11 
12 namespace rosmon
13 {
14 
15 class Logger
16 {
17 public:
18  virtual ~Logger() = default;
19  virtual void log(const LogEvent& event) = 0;
20 };
21 
25 class FileLogger : public Logger
26 {
27 public:
33  explicit FileLogger(const std::string& path, bool flush = false);
34  ~FileLogger() override;
35 
37  void log(const LogEvent& event) override;
38 private:
39  FILE* m_file = nullptr;
40  bool m_flush = false;
41 };
42 
46 class SyslogLogger : public Logger
47 {
48 public:
49  explicit SyslogLogger(const std::string& launchFileName);
50 
51  void log(const LogEvent& event) override;
52 
53 private:
54  std::string m_tag;
55 };
56 
60 class SystemdLogger : public Logger
61 {
62 public:
63  class NotAvailable : public std::runtime_error
64  {
65  public:
66  NotAvailable(const std::string& msg)
67  : std::runtime_error{msg}
68  {}
69  };
70 
71  explicit SystemdLogger(const std::string& launchFileName);
72  ~SystemdLogger() override;
73 
74  void log(const LogEvent& event) override;
75 
76 private:
77  std::string m_launchFileName;
78  int m_fd = -1;
79 };
80 
81 }
82 
83 #endif
NotAvailable(const std::string &msg)
Definition: logger.h:66
Write log messages into a log file.
Definition: logger.h:25
std::string m_launchFileName
Definition: logger.h:77
virtual void log(const LogEvent &event)=0
std::string m_tag
Definition: logger.h:54
Write log messages to systemd journal.
Definition: logger.h:60
Write log messages to syslog.
Definition: logger.h:46
virtual ~Logger()=default


rosmon_core
Author(s): Max Schwarz
autogenerated on Fri Jun 16 2023 02:15:06