FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
logger.h
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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 "modules.h"
42 
43 #ifdef LOG_ENABLED
44 
47 #define FL_DBG(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_DEBUG, msg); } while(0)
48 
51 #define FL_LOG(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_LOG, msg); } while(0)
52 
55 #define FL_WARN(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_WARN, msg); } while(0)
56 
59 #define FL_ERR(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_ERROR, msg); } while(0)
60 
64 #define FL_PANIC(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_PANIC, msg); } while(0)
65 
66 #else
67 // empty definitions in case logs are turned off for speed
68 #define FL_DBG(logger, msg)
69 #define FL_LOG(logger, msg)
70 #define FL_WARN(logger, msg)
71 #define FL_ERR(logger, msg)
72 #define FL_PANIC(logger, msg)
73 #endif
74 
75 namespace FIFE {
76 
80  class LMsg {
81  public:
82  LMsg(const std::string& msg=""): str(msg) {}
83  ~LMsg() {}
84 
85  template <typename T> LMsg& operator<<(const T& t) {
86  std::ostringstream stream;
87  stream << t;
88  str += stream.str();
89  return *this;
90  }
91 
92  std::string str;
93  };
94 
97  class LogManager {
98  public:
103  enum LogLevel {
104  LEVEL_DEBUG = 0,
105  LEVEL_LOG = 1,
106  LEVEL_WARN = 2,
107  LEVEL_ERROR = 3,
108  LEVEL_PANIC = 4
109  };
110 
113  static LogManager* instance();
114 
117  ~LogManager();
118 
125  void log(LogLevel level, logmodule_t module, const std::string& msg);
126 
130  void setLevelFilter(LogLevel level);
131 
136 
145  void addVisibleModule(logmodule_t module);
146 
149  void removeVisibleModule(logmodule_t module);
150 
153  void clearVisibleModules();
154 
157  bool isVisible(logmodule_t module);
158 
161  void setLogToPrompt(bool log_to_promt);
162 
165  bool isLoggingToPrompt();
166 
169  void setLogToFile(bool logtofile);
170 
173  bool isLoggingToFile();
174 
178  std::string getModuleName(logmodule_t module);
179 
180  private:
181  void validateModule(logmodule_t m);
182 
183  // hidden constructor for singleton
184  LogManager();
185  // validates if definitions in module.h are valid
186  void validateModuleDescription(logmodule_t module);
187 
188  // singleton instance
189  static LogManager* m_instance;
190  // current filter level
191  LogLevel m_level;
192  // visibility array for modules
193  bool m_modules[LM_MODULE_MAX];
194  // used during module description validation to check cycles in hierarchy
195  std::vector<logmodule_t> module_check_stack;
196 
197  bool m_logtofile;
198  bool m_logtoprompt;
199 
200  std::ofstream* m_logfile;
201  };
202 
209  class Logger {
210  public:
213  Logger(logmodule_t module);
214 
217  ~Logger();
218 
221  void log(LogManager::LogLevel level, const std::string& msg);
222 
226  void log(LogManager::LogLevel level, const LMsg& msg);
227 
230  inline logmodule_t getModule() const { return m_module; }
231 
232  private:
233  logmodule_t m_module;
234  };
235 
245  struct pprint {
246  void* p;
247  pprint( void* _p ) : p(_p) {}
248  };
249 }
250 
251 namespace std {
261  template <class Ch, class Tr>
262  basic_ostream<Ch,Tr>& operator<<( basic_ostream<Ch,Tr>& s, const FIFE::pprint& p ) {
263  s << "0x"
264  << hex << setw( 2*sizeof(void*) ) << setfill('0')
265  << reinterpret_cast<unsigned long>( p.p );
266 
267  return s;
268  }
269 }
270 
271 
272 #endif