WARPXM v1.10.0
Loading...
Searching...
No Matches
wxlogger.h
Go to the documentation of this file.
1#ifndef wxlogger_h
2#define wxlogger_h
3
4// WarpX includes
6#include "wxexcept.h"
7
8// std includes
9#include <iostream>
10#include <map>
11#include <stdexcept>
12#include <string>
13#include <vector>
14
19// forward declare WxLogStream class
20class WxLogStream;
21
25template<class T> class WxLoggerBase
26{
27public:
28 typedef std::map<std::string, T*, std::less<std::string>> LoggerMap_t;
29 typedef std::pair<std::string, T*> LoggerPair_t;
30
38 static T* get(const std::string& nm)
39 {
40
41 if (!_loggers)
42 _loggers = new LoggerMap_t();
43
44 // check if logger with given name is in map
45 typename LoggerMap_t::iterator i = _loggers->find(nm);
46 if (i != _loggers->end())
47 return i->second;
48
49 // it is not, so start creation process
50
51 // find location of '.'
52 int len = 0;
53 for (int i = nm.size(); i >= 0; --i, ++len)
54 if ((nm[i] == '.') || (i == 0))
55 {
56 // create logger with given name
57 T* l = new T(nm);
58 _loggers->insert(LoggerPair_t(nm, l));
59 // set its parent
60 if (i != 0)
61 l->_parent = WxLoggerBase<T>::get(nm.substr(0, nm.size() - len));
62 return l;
63 }
64 // this return statement is never executed
65 return 0;
66 }
67
72 static T* getSafe(const std::string& nm)
73 {
74
75 if (!_loggers)
76 _loggers = new LoggerMap_t();
77
78 typename LoggerMap_t::iterator i = _loggers->find(nm);
79 if (i != _loggers->end())
80 return i->second;
81 // thow exception
82 WxExcept wxe;
83 wxe << "Logger " << nm << " not found";
84 throw wxe;
85 }
86
90 static void cleanUp()
91 {
92 typename LoggerMap_t::iterator itr;
93 if (_loggers)
94 {
95 for (itr = _loggers->begin(); itr != _loggers->end(); ++itr)
96 delete itr->second;
97 _loggers->clear();
98 }
99 }
100
101private:
102 static LoggerMap_t* _loggers;
103};
104// initialize maps
105template<class T>
106std::map<std::string, T*, std::less<std::string>>* WxLoggerBase<T>::_loggers = NULL;
107
108class WxLogger : public WxLoggerBase<WxLogger>
109{
110public:
111 friend class WxLoggerBase<WxLogger>;
112
113 enum class eLevels
114 {
115 not_set = 0,
116 debug = 10,
117 info = 20,
118 warning = 30,
119 error = 40,
120 critical = 50,
121 disabled = 1000
122 };
123
124 // map type to map logger level to its string representation
125 typedef std::map<eLevels, std::string, std::less<eLevels>> LevelMap_t;
126
127 typedef std::map<std::string, eLevels, std::less<std::string>> StringMap_t;
128
134 WxLogger(const std::string& name);
135
139 virtual ~WxLogger();
140
144 void debug(const std::string& msg) const;
145
149 void info(const std::string& msg) const;
150
154 void warning(const std::string& msg) const;
155
159 void error(const std::string& msg) const;
160
164 void critical(const std::string& msg) const;
165
169 void log(const std::string& msg, eLevels withLevel) const;
170
174 void setLevel(enum eLevels level);
175
179 void setLevel(const std::string& level);
180
185
189 std::string getLevelStr();
190
198
203 void disable();
204
209 void enable();
210
215
220
225
230
235
236private:
237 WxLogger* _parent; // parent logger
238 std::string _name;
239 eLevels _level, _oldLevel;
240 LevelMap_t _levelMap;
241 StringMap_t _stringMap;
242 std::vector<WxLogRecordHandler*> _handlers;
243};
244
246#endif // wxlogger_h
Class to handle log messages generated by WarpX logging system.
Definition: wxlogrecordhandler.h:25
Provides interface to streaming iostreams to logs.
Definition: wxlogstream.h:19
Establishes basic interface of loggers.
Definition: wxlogger.h:26
static T * getSafe(const std::string &nm)
Returns a logger with a given name.
Definition: wxlogger.h:72
std::pair< std::string, T * > LoggerPair_t
Definition: wxlogger.h:29
std::map< std::string, T *, std::less< std::string > > LoggerMap_t
Definition: wxlogger.h:28
static void cleanUp()
Delete all loggers registered in the system.
Definition: wxlogger.h:90
static T * get(const std::string &nm)
Returns a logger with a given name.
Definition: wxlogger.h:38
Definition: wxlogger.h:109
std::map< std::string, eLevels, std::less< std::string > > StringMap_t
Definition: wxlogger.h:127
WxLogger(const std::string &name)
Creates a new logger with given name.
void setLevel(const std::string &level)
Set verbosity level passing a string.
void addHandler(WxLogRecordHandler *handler)
Add a new handler to the logger.
WxLogStream getWarningStream()
Return stream to log warning messages.
eLevels getLevel() const
Get verbosity level.
void debug(const std::string &msg) const
Log a debug message.
WxLogStream getInfoStream()
Return stream to log info messages.
void enable()
Re-enables logging to this logger.
WxLogStream getCriticalStream()
Return stream to log critical messages.
std::map< eLevels, std::string, std::less< eLevels > > LevelMap_t
Definition: wxlogger.h:125
void warning(const std::string &msg) const
Log a warning message.
void critical(const std::string &msg) const
Log a critical message.
void error(const std::string &msg) const
Log a error message.
void info(const std::string &msg) const
Log a info message.
void log(const std::string &msg, eLevels withLevel) const
Generic message logger.
WxLogStream getErrorStream()
Return stream to log error messages.
void disable()
Disables all logging to this logger.
virtual ~WxLogger()
Delete logger and all log record handlers.
WxLogStream getDebugStream()
Return stream to log debug messages.
std::string getLevelStr()
Get verbosity level as a string.
eLevels
Definition: wxlogger.h:114
void setLevel(enum eLevels level)
Set verbosity level.
wxm::lib::Except is the class to use for creating and throwing exceptions.
Definition: wxexcept.h:31