FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
logger.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_LOGGER_H
23 #define FIFE_LOGGER_H
24 
25 // Standard C++ library includes
26 #include <iomanip>
27 #include <iostream>
28 #include <list>
29 #include <sstream>
30 #include <string>
31 #include <vector>
32 #include <map>
33 #include <set>
34 
35 // 3rd party library includes
36 
37 // FIFE includes
38 // These includes are split up in two parts, separated by one empty line
39 // First block: files included from the FIFE root src directory
40 // Second block: files included from the same folder
41 #include "util/base/fife_stdint.h"
42 
43 #include "modules.h"
44 
45 #ifdef LOG_ENABLED
46 
49 #define FL_DBG(logger, msg) do { if (FIFE::LogManager::instance()->isVisible(logger.getModule())) logger.log(FIFE::LogManager::LEVEL_DEBUG, msg); } while(0)
50 
53 #define FL_LOG(logger, msg) do { if (FIFE::LogManager::instance()->isVisible(logger.getModule())) logger.log(FIFE::LogManager::LEVEL_LOG, msg); } while(0)
54 
57 #define FL_WARN(logger, msg) do { if (FIFE::LogManager::instance()->isVisible(logger.getModule())) logger.log(FIFE::LogManager::LEVEL_WARN, msg); } while(0)
58 
61 #define FL_ERR(logger, msg) do { if (FIFE::LogManager::instance()->isVisible(logger.getModule())) logger.log(FIFE::LogManager::LEVEL_ERROR, msg); } while(0)
62 
66 #define FL_PANIC(logger, msg) do { if (FIFE::LogManager::instance()->isVisible(logger.getModule())) logger.log(FIFE::LogManager::LEVEL_PANIC, msg); } while(0)
67 
68 #else
69 // empty definitions in case logs are turned off for speed
70 #define FL_DBG(logger, msg)
71 #define FL_LOG(logger, msg)
72 #define FL_WARN(logger, msg)
73 #define FL_ERR(logger, msg)
74 #define FL_PANIC(logger, msg)
75 #endif
76 
77 namespace FIFE {
78 
82  class LMsg {
83  public:
84  LMsg(const std::string& msg=""): str(msg) {}
85  ~LMsg() {}
86 
87  template <typename T> LMsg& operator<<(const T& t) {
88  std::ostringstream stream;
89  stream << t;
90  str += stream.str();
91  return *this;
92  }
93 
94  std::string str;
95  };
96 
99  class LogManager {
100  public:
105  enum LogLevel {
106  LEVEL_DEBUG = 0,
107  LEVEL_LOG = 1,
108  LEVEL_WARN = 2,
109  LEVEL_ERROR = 3,
110  LEVEL_PANIC = 4
111  };
112 
115  static LogManager* instance();
116 
119  ~LogManager();
120 
127  void log(LogLevel level, logmodule_t module, const std::string& msg);
128 
132  void setLevelFilter(LogLevel level);
133 
137  LogLevel getLevelFilter();
138 
147  void addVisibleModule(logmodule_t module);
148 
151  void removeVisibleModule(logmodule_t module);
152 
155  void clearVisibleModules();
156 
159  bool isVisible(logmodule_t module);
160 
163  void setLogToPrompt(bool logtoprompt);
164 
167  bool isLogToPrompt();
168 
171  void setLogToFile(bool logtofile);
172 
175  bool isLogToFile();
176 
180  std::string getModuleName(logmodule_t module);
181 
182  private:
183  void validateModule(logmodule_t m);
184 
185  // hidden constructor for singleton
186  LogManager();
187  // validates if definitions in module.h are valid
188  void validateModuleDescription(logmodule_t module);
189 
190  // singleton instance
192  // current filter level
194  // visibility array for modules
195  bool m_modules[LM_MODULE_MAX];
196  // used during module description validation to check cycles in hierarchy
197  std::vector<logmodule_t> module_check_stack;
198 
201 
202  std::ofstream* m_logfile;
203  };
204 
211  class Logger {
212  public:
215  Logger(logmodule_t module);
216 
219  ~Logger();
220 
223  void log(LogManager::LogLevel level, const std::string& msg);
224 
228  void log(LogManager::LogLevel level, const LMsg& msg);
229 
232  inline logmodule_t getModule() const { return m_module; }
233 
234  private:
236  };
237 
247  struct pprint {
248  void* p;
249  pprint( void* _p ) : p(_p) {}
250  };
251 }
252 
253 namespace std {
263  template <class Ch, class Tr>
264  basic_ostream<Ch,Tr>& operator<<( basic_ostream<Ch,Tr>& s, const FIFE::pprint& p ) {
265  s << "0x"
266  << hex << setw( 2*sizeof(void*) ) << setfill('0')
267  << reinterpret_cast<uint64_t>( p.p );
268 
269  return s;
270  }
271 }
272 
273 
274 #endif
LMsg(const std::string &msg="")
Definition: logger.h:84
void * p
Definition: logger.h:248
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
~LMsg()
Definition: logger.h:85
Definition: logger.h:253
bool m_logtoprompt
Definition: logger.h:200
logmodule_t getModule() const
gets module where this logger is associated to
Definition: logger.h:232
std::vector< logmodule_t > module_check_stack
Definition: logger.h:197
pprint(void *_p)
Definition: logger.h:249
std::string str
Definition: logger.h:94
std::ofstream * m_logfile
Definition: logger.h:202
logmodule_t
Modules available for logging.
Definition: modules.h:36
LogLevel m_level
Definition: logger.h:193
Helper for printing a pointer.
Definition: logger.h:247
logmodule_t m_module
Definition: logger.h:235
Create a Logger instance to communicate with LogManager Logger stores information about the current m...
Definition: logger.h:211
Logmanager takes care of log filtering and output direction.
Definition: logger.h:99
LMsg & operator<<(const T &t)
Definition: logger.h:87
static LogManager * m_instance
Definition: logger.h:191
LogLevel
Loglevel is used to set a treshold for output messages + related filter E.g.
Definition: logger.h:105
bool m_logtofile
Definition: logger.h:199