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_level.h>
11 #include <ifm3d/common/logging/log_writer.h>
12 #include <ifm3d/common/logging/log_writer_console.h>
13 #include <ifm3d/common/logging/log_writer_console_colored.h>
14 #include <ifm3d/common/logging/log_formatter_text.h>
15 #include <ifm3d/common/module_common.h>
16 
17 #include <vector>
18 #include <memory>
19 
20 namespace ifm3d
21 {
22  class IFM3D_EXPORT Logger
23  {
24  public:
25  Logger(LogLevel log_level = LogLevel::Warning)
26  : log_level_(log_level),
27  writer_(std::make_shared<LogWriterConsoleColored<LogFormatterText>>(
28  Output::StdErr))
29  {}
30 
31  Logger&
32  SetWriter(std::shared_ptr<LogWriter> writer)
33  {
34  this->writer_ = writer;
35  return *this;
36  }
37 
38  LogLevel
39  GetLogLevel() const
40  {
41  return this->log_level_;
42  }
43 
44  void
45  SetLogLevel(LogLevel log_level)
46  {
47  this->log_level_ = log_level;
48  }
49 
50  inline bool
51  ShouldLog(LogLevel log_level) const
52  {
53  return log_level <= this->log_level_;
54  }
55 
56  inline void
57  Write(const LogEntry& entry)
58  {
59  if (this->writer_)
60  {
61  this->writer_->Write(entry);
62  }
63  }
64 
65  static Logger& Get();
66 
67  private:
68  LogLevel log_level_;
69  std::shared_ptr<LogWriter> writer_;
70  };
71 
72 }
73 
74 #endif // IFM3D_COMMON_LOGGING_LOGGER_H
ifm3d::Logger
Definition: logger.h:22
ifm3d::LogEntry
Definition: log_entry.h:19
ifm3d::LogWriterConsoleColored
Definition: log_writer_console_colored.h:19