FIFE 2008.0
logger.h
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_LOGGER_H
00023 #define FIFE_LOGGER_H
00024 
00025 // Standard C++ library includes
00026 #include <iomanip>
00027 #include <iostream>
00028 #include <list>
00029 #include <sstream>
00030 #include <string>
00031 #include <vector>
00032 #include <map>
00033 #include <set>
00034 
00035 // 3rd party library includes
00036 
00037 // FIFE includes
00038 // These includes are split up in two parts, separated by one empty line
00039 // First block: files included from the FIFE root src directory
00040 // Second block: files included from the same folder
00041 #include "modules.h"
00042 
00043 #ifdef LOG_ENABLED
00044 
00047 #define FL_DBG(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_DEBUG, msg); } while(0)
00048 
00051 #define FL_LOG(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_LOG, msg); } while(0)
00052 
00055 #define FL_WARN(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_WARN, msg); } while(0)
00056 
00059 #define FL_ERR(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_ERROR, msg); } while(0)
00060 
00064 #define FL_PANIC(logger, msg) do { if (LogManager::instance()->isVisible(logger.getModule())) logger.log(LogManager::LEVEL_PANIC, msg); } while(0)
00065 
00066 #else
00067 // empty definitions in case logs are turned off for speed
00068 #define FL_DBG(logger, msg)
00069 #define FL_LOG(logger, msg)
00070 #define FL_WARN(logger, msg)
00071 #define FL_ERR(logger, msg)
00072 #define FL_PANIC(logger, msg)
00073 #endif
00074 
00075 namespace FIFE {
00076     
00080     class LMsg {
00081     public:
00082         LMsg(const std::string& msg=""): str(msg) {}
00083         ~LMsg() {}
00084         
00085         template <typename T> LMsg& operator<<(const T& t) {
00086             std::ostringstream stream;
00087             stream << t;
00088             str += stream.str();
00089             return *this;
00090         }
00091         
00092         std::string str;
00093     };
00094     
00097     class LogManager {
00098     public:
00103         enum LogLevel { 
00104             LEVEL_DEBUG = 0,
00105             LEVEL_LOG   = 1,
00106             LEVEL_WARN  = 2,
00107             LEVEL_ERROR = 3,
00108             LEVEL_PANIC = 4
00109         };
00110         
00113         static LogManager* instance();
00114         
00117         ~LogManager();
00118     
00125         void log(LogLevel level, logmodule_t module, const std::string& msg);
00126         
00130         void setLevelFilter(LogLevel level);
00131         
00135         LogLevel getLevelFilter();
00136         
00145         void addVisibleModule(logmodule_t module);
00146         
00149         void removeVisibleModule(logmodule_t module);
00150         
00153         void clearVisibleModules();
00154         
00157         bool isVisible(logmodule_t module);     
00158         
00161         void setLogToPrompt(bool log_to_promt);
00162         
00165         bool isLoggingToPrompt();
00166         
00169         void setLogToFile(bool logtofile);
00170         
00173         bool isLoggingToFile();
00174         
00178         std::string getModuleName(logmodule_t module);
00179         
00180     private:
00181         void validateModule(logmodule_t m);
00182         
00183         // hidden constructor for singleton
00184         LogManager();
00185         // validates if definitions in module.h are valid
00186         void validateModuleDescription(logmodule_t module);
00187         
00188         // singleton instance
00189         static LogManager* m_instance;
00190         // current filter level
00191         LogLevel m_level;
00192         // visibility array for modules
00193         bool m_modules[LM_MODULE_MAX];
00194         // used during module description validation to check cycles in hierarchy
00195         std::vector<logmodule_t> module_check_stack;
00196         
00197         bool m_logtofile;
00198         bool m_logtoprompt;
00199         
00200         std::ofstream* m_logfile;
00201     };
00202     
00209     class Logger {
00210     public:
00213         Logger(logmodule_t module);
00214         
00217         ~Logger();
00218         
00221         void log(LogManager::LogLevel level, const std::string& msg);
00222         
00226         void log(LogManager::LogLevel level, const LMsg& msg);
00227         
00230         inline logmodule_t getModule() const { return m_module; }
00231         
00232     private:
00233         logmodule_t m_module;
00234     };
00235     
00245     struct pprint {
00246         void* p;
00247         pprint( void* _p ) : p(_p) {}
00248     };
00249 }
00250 
00251 namespace std {
00261     template <class Ch, class Tr>
00262     basic_ostream<Ch,Tr>& operator<<( basic_ostream<Ch,Tr>& s, const FIFE::pprint& p ) {
00263         s << "0x"
00264             << hex << setw( 2*sizeof(void*) ) << setfill('0')
00265             << reinterpret_cast<unsigned long>( p.p );
00266 
00267         return s;
00268     }
00269 }
00270 
00271 
00272 #endif
 All Classes Namespaces Functions Variables Enumerations Enumerator