irccd  3.0.3
logger.hpp
1 /*
2  * logger.hpp -- irccd logging
3  *
4  * Copyright (c) 2013-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_DAEMON_LOGGER_HPP
20 #define IRCCD_DAEMON_LOGGER_HPP
21 
27 #include <irccd/sysconfig.hpp>
28 
29 #include <sstream>
30 #include <string>
31 #include <string_view>
32 #include <utility>
33 
34 namespace irccd {
35 
36 namespace daemon {
37 
41 namespace logger {
42 
43 class filter;
44 class sink;
45 
77 template <typename T>
78 struct type_traits;
79 
84 class logger : public std::ostream, public std::stringbuf {
85 private:
89  friend class sink;
90 
91  enum class level {
92  debug,
93  info,
94  warning
95  } level_;
96 
97  sink& parent_;
98 
99  std::string_view category_;
100  std::string_view component_;
101 
102  void debug(const std::string&);
103  void info(const std::string&);
104  void warning(const std::string&);
105  auto sync() -> int override;
106  logger(sink&, level, std::string_view, std::string_view) noexcept;
107 };
108 
121 class sink {
122 private:
126  friend class logger;
127 
128  // User options.
129  bool verbose_{false};
130  filter* filter_{nullptr};
131 
132 protected:
141  virtual void write_debug(const std::string& line) = 0;
142 
151  virtual void write_info(const std::string& line) = 0;
152 
161  virtual void write_warning(const std::string& line) = 0;
162 
163 public:
167  sink();
168 
172  virtual ~sink() = default;
173 
179  auto is_verbose() const noexcept -> bool;
180 
186  void set_verbose(bool mode) noexcept;
187 
193  void set_filter(filter& filter) noexcept;
194 
205  auto info(std::string_view category, std::string_view component) -> logger;
206 
214  template <typename Loggable>
215  auto info(const Loggable& loggable) -> logger
216  {
217  return info(
220  );
221  }
222 
232  auto warning(std::string_view category, std::string_view component) -> logger;
233 
241  template <typename Loggable>
242  auto warning(const Loggable& loggable) -> logger
243  {
244  return warning(
247  );
248  }
249 
260  auto debug(std::string_view category, std::string_view component) -> logger;
261 
269  template <typename Loggable>
270  auto debug(const Loggable& loggable) -> logger
271  {
272  return debug(
275  );
276  }
277 };
278 
283 class filter {
284 public:
288  virtual ~filter() = default;
289 
298  auto pre(std::string_view category,
299  std::string_view component,
300  std::string_view message) const -> std::string;
301 
302 
311  virtual auto pre_debug(std::string_view category,
312  std::string_view component,
313  std::string_view message) const -> std::string;
314 
323  virtual auto pre_info(std::string_view category,
324  std::string_view component,
325  std::string_view message) const -> std::string;
326 
335  virtual auto pre_warning(std::string_view category,
336  std::string_view component,
337  std::string_view message) const -> std::string;
338 };
339 
345 class console_sink : public sink {
346 protected:
350  void write_debug(const std::string& line) override;
351 
355  void write_info(const std::string& line) override;
356 
360  void write_warning(const std::string& line) override;
361 };
362 
367 class file_sink : public sink {
368 private:
369  std::string output_normal_;
370  std::string output_error_;
371 
372 protected:
376  void write_debug(const std::string& line) override;
377 
381  void write_info(const std::string& line) override;
382 
386  void write_warning(const std::string& line) override;
387 
388 public:
395  file_sink(std::string normal, std::string errors);
396 };
397 
404 class silent_sink : public sink {
405 protected:
409  void write_debug(const std::string& line) override;
410 
414  void write_info(const std::string& line) override;
415 
419  void write_warning(const std::string& line) override;
420 };
421 
422 #if defined(IRCCD_HAVE_SYSLOG)
423 
428 class syslog_sink : public sink {
429 protected:
433  void write_debug(const std::string& line) override;
434 
438  void write_info(const std::string& line) override;
439 
443  void write_warning(const std::string& line) override;
444 
445 public:
449  syslog_sink();
450 
454  ~syslog_sink();
455 };
456 
457 #endif // !IRCCD_HAVE_SYSLOG
458 
459 } // !logger
460 
461 } // !daemon
462 
463 } // !irccd
464 
465 #endif // !IRCCD_DAEMON_LOGGER_HPP
Logger implementation for console output using std::cout and std::cerr.
Definition: logger.hpp:345
void write_warning(const std::string &line) override
void write_debug(const std::string &line) override
void write_info(const std::string &line) override
Output to a files.
Definition: logger.hpp:367
void write_warning(const std::string &line) override
file_sink(std::string normal, std::string errors)
void write_debug(const std::string &line) override
void write_info(const std::string &line) override
Filter messages before printing them.
Definition: logger.hpp:283
virtual auto pre_info(std::string_view category, std::string_view component, std::string_view message) const -> std::string
virtual auto pre_warning(std::string_view category, std::string_view component, std::string_view message) const -> std::string
virtual auto pre_debug(std::string_view category, std::string_view component, std::string_view message) const -> std::string
auto pre(std::string_view category, std::string_view component, std::string_view message) const -> std::string
Logger object.
Definition: logger.hpp:84
Use to disable logs.
Definition: logger.hpp:404
void write_info(const std::string &line) override
void write_warning(const std::string &line) override
void write_debug(const std::string &line) override
Interface to implement new logger mechanisms.
Definition: logger.hpp:121
auto debug(std::string_view category, std::string_view component) -> logger
friend class logger
Make logger friend.
Definition: logger.hpp:126
virtual void write_debug(const std::string &line)=0
auto debug(const Loggable &loggable) -> logger
Definition: logger.hpp:270
auto warning(std::string_view category, std::string_view component) -> logger
auto info(std::string_view category, std::string_view component) -> logger
auto warning(const Loggable &loggable) -> logger
Definition: logger.hpp:242
virtual ~sink()=default
virtual void write_warning(const std::string &line)=0
auto is_verbose() const noexcept -> bool
virtual void write_info(const std::string &line)=0
void set_filter(filter &filter) noexcept
void set_verbose(bool mode) noexcept
Parent namespace.
Definition: acceptor.hpp:43
Definition: bot.hpp:253
Traits for loggable objects.
Definition: logger.hpp:78