SSP21-CPP
Logger.h
1 /*
2  * Copyright (c) 2019, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef LOG4CPP_LOGGER_H
26 #define LOG4CPP_LOGGER_H
27 
28 #include "log4cpp/ILogHandler.h"
29 #include "log4cpp/Strings.h"
30 
31 #include <memory>
32 #include <string>
33 
34 namespace log4cpp
35 {
36 
37 const uint32_t max_log_entry_size = 120;
38 
39 /**
40  * A copyable facade over a LogRoot class
41  */
42 class Logger
43 {
44 public:
45  struct Settings
46  {
47  Settings(ModuleId module, const std::string& id, LogLevels levels) : module(module), id(id), levels(levels)
48  {}
49 
50  ModuleId module;
51  std::string id;
52  LogLevels levels;
53  };
54 
55  Logger(const std::shared_ptr<ILogHandler>& backend, ModuleId moduleid, const std::string& id, LogLevels levels) :
56  backend(backend),
57  settings(std::make_shared<Settings>(moduleid, id, levels))
58  {}
59 
60  static Logger empty()
61  {
62  return Logger(nullptr, ModuleId(0), "", LogLevels(0));
63  }
64 
65  void log(const LogLevel& level, const char* location, const char* message)
66  {
67  if (backend)
68  {
69  backend->log(
70  this->settings->module,
71  this->settings->id.c_str(),
72  level,
73  location,
74  message
75  );
76  }
77  }
78 
79  Logger detach(const std::string& id) const
80  {
81  return Logger(this->backend, std::make_shared<Settings>(this->settings->module, id, this->settings->levels));
82  }
83 
84  template <typename... Args>
85  Logger detach_and_append(Args... args) const
86  {
87  return detach(Strings::concatenate(this->settings->id, args...));
88  }
89 
90  Logger detach(const std::string& id, LogLevels levels) const
91  {
92  return Logger(this->backend, std::make_shared<Settings>(this->settings->module, id, levels));
93  }
94 
95  Logger detach(LogLevels levels) const
96  {
97  return Logger(this->backend, std::make_shared<Settings>(this->settings->module, this->settings->id, levels));
98  }
99 
100  bool is_enabled(const LogLevel& level) const
101  {
102  return backend && settings->levels.is_set(level);
103  }
104 
105  LogLevels get_levels() const
106  {
107  return this->settings->levels;
108  }
109 
110  void set_levels(const LogLevels& filters)
111  {
112  this->settings->levels = filters;
113  }
114 
115  void rename(const std::string& id)
116  {
117  this->settings->id = id;
118  }
119 
120 private:
121  Logger(const std::shared_ptr<ILogHandler>& backend, const std::shared_ptr<Settings>& settings) :
122  backend(backend),
123  settings(settings)
124  {}
125 
126  Logger() = delete;
127  Logger& operator=(const Logger&) = delete;
128 
129  const std::shared_ptr<ILogHandler> backend;
130  const std::shared_ptr<Settings> settings;
131 };
132 
133 } //namespace log4cpp
134 
135 #endif //LOG4CPP_LOGGER_H
log4cpp header-only library namespace