libkipr  1.0.0
log.hpp
Go to the documentation of this file.
1 #ifndef _KIPR_LOG_LOG_HPP_
2 #define _KIPR_LOG_LOG_HPP_
3 
4 #include <sstream>
5 #include <memory>
6 
7 namespace kipr
8 {
9  namespace log
10  {
11  struct Flush {};
12 
13  extern const Flush flush;
14 
15  class Log;
16 
17  enum class Level : char
18  {
19  Fatal = '!',
20  Error = 'E',
21  Warning = 'W',
22  Info = 'I',
23  Debug = 'D',
24  Verbose = 'V'
25  };
26 
27  struct Location
28  {
29  std::string file;
30  unsigned line;
31  unsigned column;
32  };
33 
34  class LogStream
35  {
36  public:
38  LogStream(Log &log, const Level level);
40 
41  inline void operator <<(const Flush &_)
42  {
43  this->flush();
44  }
45 
46  template<typename T>
47  inline LogStream &operator <<(const T &t)
48  {
49  (*stream_) << t;
50  return *this;
51  }
52 
53  private:
54  void flush();
55 
56  Log &log_;
57  Level level_;
58  bool flushed_;
59 
60  std::unique_ptr<std::ostringstream> stream_;
61  };
62 
63  class Log
64  {
65  public:
66  typedef std::shared_ptr<Log> Ptr;
67  typedef std::shared_ptr<const Log> ConstPtr;
68 
69  Log(const std::string &module);
70  Log(Log &&rhs);
71 
72 
73  void log(const Level level, const std::string &message, const Location &location);
74  void log(const Level level, const std::string &message);
75  LogStream log(const Level level);
76 
77  inline void fatal(const std::string &message, const Location &location)
78  {
79  log(Level::Fatal, message, location);
80  }
81 
82  inline void fatal(const std::string &message)
83  {
84  log(Level::Fatal, message);
85  }
86 
87  inline LogStream fatal()
88  {
89  return log(Level::Fatal);
90  }
91 
92  inline void error(const std::string &message, const Location &location)
93  {
94  log(Level::Error, message, location);
95  }
96 
97  inline void error(const std::string &message)
98  {
99  log(Level::Error, message);
100  }
101 
102  inline LogStream error()
103  {
104  return log(Level::Error);
105  }
106 
107  inline void warning(const std::string &message, const Location &location)
108  {
109  log(Level::Warning, message, location);
110  }
111 
112  inline void warning(const std::string &message)
113  {
114  log(Level::Warning, message);
115  }
116 
118  {
119  return log(Level::Warning);
120  }
121 
122  inline void info(const std::string &message, const Location &location)
123  {
124  log(Level::Info, message, location);
125  }
126 
127  inline void info(const std::string &message)
128  {
129  log(Level::Info, message);
130  }
131 
132  inline LogStream info()
133  {
134  return log(Level::Info);
135  }
136 
137  inline void debug(const std::string &message, const Location &location)
138  {
139  log(Level::Debug, message, location);
140  }
141 
142  inline void debug(const std::string &message)
143  {
144  log(Level::Debug, message);
145  }
146 
147  inline LogStream debug()
148  {
149  return log(Level::Debug);
150  }
151 
152  inline void verbose(const std::string &message, const Location &location)
153  {
154  log(Level::Verbose, message, location);
155  }
156 
157  inline void verbose(const std::string &message)
158  {
159  log(Level::Verbose, message);
160  }
161 
163  {
164  return log(Level::Verbose);
165  }
166 
167  private:
168  Log(const Log &) = delete;
169 
170  std::string module_;
171  };
172  }
173 }
174 
175 namespace std
176 {
177  template<>
178  struct hash<kipr::log::Level>
179  {
180  size_t operator()(const kipr::log::Level &level) const
181  {
182  return std::hash<unsigned>()(static_cast<unsigned>(level));
183  }
184  };
185 }
186 
187 #endif
Definition: log.hpp:35
LogStream(LogStream &&rhs)
void operator<<(const Flush &_)
Definition: log.hpp:41
LogStream(Log &log, const Level level)
Definition: log.hpp:64
LogStream fatal()
Definition: log.hpp:87
void info(const std::string &message)
Definition: log.hpp:127
LogStream log(const Level level)
std::shared_ptr< Log > Ptr
Definition: log.hpp:66
LogStream error()
Definition: log.hpp:102
void fatal(const std::string &message)
Definition: log.hpp:82
void debug(const std::string &message)
Definition: log.hpp:142
void log(const Level level, const std::string &message, const Location &location)
void verbose(const std::string &message, const Location &location)
Definition: log.hpp:152
Log(const std::string &module)
void debug(const std::string &message, const Location &location)
Definition: log.hpp:137
void error(const std::string &message, const Location &location)
Definition: log.hpp:92
LogStream warning()
Definition: log.hpp:117
void error(const std::string &message)
Definition: log.hpp:97
LogStream verbose()
Definition: log.hpp:162
std::shared_ptr< const Log > ConstPtr
Definition: log.hpp:67
LogStream debug()
Definition: log.hpp:147
void warning(const std::string &message)
Definition: log.hpp:112
void fatal(const std::string &message, const Location &location)
Definition: log.hpp:77
void log(const Level level, const std::string &message)
void verbose(const std::string &message)
Definition: log.hpp:157
Log(Log &&rhs)
void info(const std::string &message, const Location &location)
Definition: log.hpp:122
LogStream info()
Definition: log.hpp:132
void warning(const std::string &message, const Location &location)
Definition: log.hpp:107
const Flush flush
Level
Definition: log.hpp:18
Definition: accel.hpp:7
Definition: log.hpp:176
Definition: log.hpp:11
Definition: log.hpp:28
unsigned column
Definition: log.hpp:31
unsigned line
Definition: log.hpp:30
std::string file
Definition: log.hpp:29
size_t operator()(const kipr::log::Level &level) const
Definition: log.hpp:180