ifm3d
logger.h
1 // -*- c++ -*-
2 /*
3  * Copyright 2023-present ifm electronic, gmbh
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef IFM3D_COMMON_LOGGING_LOGGER_H
8 #define IFM3D_COMMON_LOGGING_LOGGER_H
9 
10 #include <ifm3d/common/logging/log_formatter_text.h>
11 #include <ifm3d/common/logging/log_level.h>
12 #include <ifm3d/common/logging/log_writer.h>
13 #include <ifm3d/common/logging/log_writer_console.h>
14 #include <ifm3d/common/logging/log_writer_console_colored.h>
15 #include <ifm3d/common/module_common.h>
16 
17 #include <memory>
18 
19 namespace ifm3d
20 {
21  class IFM3D_EXPORT Logger
22  {
23  public:
24  Logger(LogLevel log_level = LogLevel::Warning)
25  : _log_level(log_level),
26  _writer(std::make_shared<LogWriterConsoleColored<LogFormatterText>>(
27  Output::StdErr))
28  {}
29 
30  Logger&
31  SetWriter(std::shared_ptr<LogWriter> writer)
32  {
33  this->_writer = writer;
34  return *this;
35  }
36 
37  [[nodiscard]] std::shared_ptr<LogWriter>
38  GetWriter() const
39  {
40  return this->_writer;
41  }
42 
43  [[nodiscard]] LogLevel
44  GetLogLevel() const
45  {
46  return this->_log_level;
47  }
48 
49  void
50  SetLogLevel(LogLevel log_level)
51  {
52  this->_log_level = log_level;
53  }
54 
55  [[nodiscard]] bool
56  ShouldLog(LogLevel log_level) const
57  {
58  return log_level <= this->_log_level;
59  }
60 
61  void
62  Write(const LogEntry& entry)
63  {
64  if (this->_writer)
65  {
66  this->_writer->Write(entry);
67  }
68  }
69 
70  static Logger& Get();
71 
72  private:
73  LogLevel _log_level;
74  std::shared_ptr<LogWriter> _writer;
75  };
76 
77 }
78 
79 #endif // IFM3D_COMMON_LOGGING_LOGGER_H
Definition: log_entry.h:20
Definition: log_writer_console_colored.h:29
Definition: logger.h:22