fifr::util
Logger.hxx
1 /*
2  * Copyright (c) 2017 Frank Fischer <frank-fischer@shadow-soft.de>
3  *
4  * This program is free software: you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>
16  */
17 
18 #include <ostream>
19 
20 namespace fifr
21 {
22 namespace util
23 {
25 class Logger
26 {
27 public:
28  class LogStream
29  {
30  public:
31  LogStream(const char* name, std::ostream* s) : stream_(s)
32  {
33  if (s) {
34  *s << name << " ";
35  }
36  }
37 
38  ~LogStream()
39  {
40  if (stream_) *stream_ << std::endl;
41  }
42 
43  std::ostream* stream() { return stream_; }
44 
45  private:
46  std::ostream* stream_;
47  };
48 
49 public:
50  enum class Level {
51  Debug,
52  Info,
53  Warn,
54  Error,
55  None,
56  };
57 
58 public:
59  Logger(std::ostream& out) : out_(out), level_(Level::Info) {}
60 
62  void set_level(Level level) { level_ = level; }
63 
65  Level level() const { return level_; }
66 
68  LogStream info() { return {"I ", level_ <= Level::Info ? &out_ : nullptr}; }
69 
71  LogStream warn() { return {"W ", level_ <= Level::Warn ? &out_ : nullptr}; }
72 
74  LogStream error() { return {"E ", level_ <= Level::Error ? &out_ : nullptr}; }
75 
77  LogStream debug() { return {"D ", level_ <= Level::Debug ? &out_ : nullptr}; }
78 
79 private:
80  std::ostream& out_;
81  Level level_;
82 
83 public:
86 };
87 
89 inline Logger::LogStream info()
90 {
92 }
93 
95 inline Logger::LogStream warn()
96 {
98 }
99 
101 inline Logger::LogStream error()
102 {
103  return Logger::default_logger.error();
104 }
105 
107 inline Logger::LogStream debug()
108 {
109  return Logger::default_logger.debug();
110 }
111 
113 template <class T>
114 Logger::LogStream& operator<<(Logger::LogStream& log, const T& x)
115 {
116  auto s = log.stream();
117  if (s) *s << x;
118  return log;
119 }
120 
122 template <class T>
123 Logger::LogStream& operator<<(Logger::LogStream&& log, const T& x)
124 {
125  auto s = log.stream();
126  if (s) *s << x;
127  return log;
128 }
129 } // namespace util
130 } // namespace fifr
A simple Logger.
Definition: Logger.hxx:25
Logger::LogStream info()
Return info stream for the default logger.
Definition: Logger.hxx:89
Logger::LogStream error()
Return error stream for the default logger.
Definition: Logger.hxx:101
Show info, warning and error messages.
LogStream debug()
Return debug log stream.
Definition: Logger.hxx:77
LogStream warn()
Return warning log stream.
Definition: Logger.hxx:71
Logger::LogStream warn()
Return warning stream for the default logger.
Definition: Logger.hxx:95
Level
Definition: Logger.hxx:50
Logger::LogStream debug()
Return debug stream for the default logger.
Definition: Logger.hxx:107
Level level() const
Return current log level.
Definition: Logger.hxx:65
LogStream error()
Return error log stream.
Definition: Logger.hxx:74
LogStream info()
Return info log stream.
Definition: Logger.hxx:68
Definition: AutoTuple.hxx:23
static Logger default_logger
The global default logger.
Definition: Logger.hxx:85
void set_level(Level level)
Set the log level.
Definition: Logger.hxx:62
Show debug messages and everything else.
Show warning and error messages.