Ocular Engine
Logger.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_LOGGER__H__
19 #define __H__OCULAR_LOGGER__H__
20 
21 #include "ILoggerListener.hpp"
22 #include "Exceptions/Exception.hpp"
23 
24 #include <list>
25 #include <sstream>
26 #include <memory>
27 #include <mutex>
28 
29 //------------------------------------------------------------------------------------------
30 
35 namespace Ocular
36 {
41  namespace Core
42  {
47  class Logger
48  {
49  public:
50 
54  Logger();
55 
59  ~Logger();
60 
67  void registerListener(ILoggerListener* listener);
68 
72  template<typename T, typename... U>
73  void debug(T first, U... args)
74  {
75  m_Mutex.lock();
76 
77  m_CurrentMessage.channel = LoggerChannels::Debug;
78  m_IncompleteMessage.str(std::string());
79  m_IncompleteMessage << first;
80  log(args...);
81 
82  m_Mutex.unlock();
83  }
84 
92  template<typename T, typename... U>
93  void info(T first, U... args)
94  {
95  m_Mutex.lock();
96 
97  m_CurrentMessage.channel = LoggerChannels::Info;
98  m_IncompleteMessage.str(std::string());
99  m_IncompleteMessage << first;
100  log(args...);
101 
102  m_Mutex.unlock();
103  }
104 
113  template<typename T, typename... U>
114  void warning(T first, U... args)
115  {
116  m_Mutex.lock();
117 
118  m_CurrentMessage.channel = LoggerChannels::Warning;
119  m_IncompleteMessage.str(std::string());
120  m_IncompleteMessage << first;
121  log(args...);
122 
123  m_Mutex.unlock();
124  }
125 
133  template<typename T, typename... U>
134  void error(T first, U... args)
135  {
136  m_Mutex.lock();
137 
138  m_CurrentMessage.channel = LoggerChannels::Error;
139  m_IncompleteMessage.str(std::string());
140  m_IncompleteMessage << first;
141  log(args...);
142 
143  m_Mutex.unlock();
144  }
145 
149  void error(Exception& e);
150 
160  template<typename T, typename... U>
161  void fatal(T first, U... args)
162  {
163  m_Mutex.lock();
164 
165  m_CurrentMessage.channel = LoggerChannels::Fatal;
166  m_IncompleteMessage.str(std::string());
167  m_IncompleteMessage << first;
168  log(args...);
169 
170  m_Mutex.unlock();
171  }
172 
173  protected:
174 
175  private:
176 
180  template<typename T, typename... U>
181  void log(T first, U... last)
182  {
183  m_IncompleteMessage << first;
184  log(last...);
185  }
186 
190  void log();
191 
192  //--------------------------------------------
193 
194  std::stringstream m_IncompleteMessage;
195  LoggerMessage m_CurrentMessage;
196 
197  std::list<std::unique_ptr<ILoggerListener>> m_Listeners;
198 
199  std::mutex m_Mutex;
200 
201  };
202  }
206 }
211 //------------------------------------------------------------------------------------------
212 
213 #endif
void fatal(T first, U...args)
Definition: Logger.hpp:161
void info(T first, U...args)
Definition: Logger.hpp:93
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70
void registerListener(ILoggerListener *listener)
Definition: Logger.cpp:42
void debug(T first, U...args)
Definition: Logger.hpp:73
void error(T first, U...args)
Definition: Logger.hpp:134
Definition: ILoggerListener.hpp:42
void warning(T first, U...args)
Definition: Logger.hpp:114
Definition: Logger.hpp:47
Definition: Exception.hpp:42