Log.h

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 #ifndef __NR_LOG_H_
00014 #define __NR_LOG_H_
00015 
00016 
00017 //----------------------------------------------------------------------------------
00018 // Includes
00019 //----------------------------------------------------------------------------------
00020 #include "Prerequisities.h"
00021 #include "Engine.h"
00022 
00023 //----------------------------------------------------------------------------------
00024 // Some usefull macros which makes our life easear
00025 //----------------------------------------------------------------------------------
00026 #define NR_Log nrEngine::Engine::sLog()->log
00027 
00028 
00029 namespace nrEngine{
00030 
00031         //! Simple class giving you logging functionality for your application.
00032         /**
00033         * Log Class is using to generate and print logging information on console,
00034         * MessageBox(WIN32) or in a log file on the disk. You can generate log messages
00035         * either by giving the message directly or by using message ID-Number of needed.
00036         * This guarantee to be log messages not hardcoded and they can also be localized (language). All used
00037         * log messages should be stored in an extra file to allow logging by Msg-Id-Number.
00038         *
00039         * \ingroup engine
00040         **/
00041         class _NRExport Log{
00042                 public:
00043                 
00044                         //! Log target is an unsigned integer number
00045                         typedef uint32 LogTarget;
00046                         
00047                         //! Enumeration containing all available log targets
00048                         enum {
00049                                 
00050                                 //! Log the messages to the client log file. Here you should log all messages coming from client.
00051                                 LOG_CLIENT = 1 << 0,
00052                                 
00053                                 //! Log here messages coming from server if you using networking
00054                                 LOG_SERVER = 1 << 1,
00055                         
00056                                 //! Use this target to log all application logs coming from your game
00057                                 LOG_APP = 1 << 2,
00058                         
00059                                 //! This target will be used by kernel to log it's own messages
00060                                 LOG_KERNEL = 1 << 3,
00061                         
00062                                 //! The engine will use this target to log it own messages. Note: Kernel has got it's own target
00063                                 LOG_ENGINE = 1 << 4,
00064                         
00065                                 //! This will log to standard console output
00066                                 LOG_CONSOLE = 1 << 5,
00067                         
00068                                 //! Messages coming from plugins should be logged here
00069                                 LOG_PLUGIN = 1 << 6,
00070 
00071                                 //! Log to all targets above
00072                                 LOG_ANYTHING = 0xFFFFFFFF
00073                         };
00074 
00075                         /**
00076                          * Each log message has a certain log level.
00077                          * Each log message that should be logged has such kind of information
00078                          * about the log level.<br>
00079                          * You can specify which kind of level information should be logged.
00080                          * All messsages with the type above the specified type will be logged.
00081                          * i.e. setting upo the log engine to log messages with LL_WARNING
00082                          * will log fatal errors, errors and warnings.
00083                          **/
00084                         typedef enum _LogLevel{
00085                         
00086                                 //! Messages of this level are produced by fatal errors
00087                                 LL_FATAL_ERROR = 0,
00088 
00089                                 //! This are normal error messages
00090                                 LL_ERROR,
00091 
00092                                 //! Only warning, without an error
00093                                 LL_WARNING,
00094 
00095                                 //! Normal logging, some useful information
00096                                 LL_NORMAL,
00097 
00098                                 //! Just a little bit more information than normal
00099                                 LL_DEBUG,
00100 
00101                                 //! Very verbose message types, say anything anywhere
00102                                 LL_CHATTY
00103 
00104                         } LogLevel;
00105 
00106                         
00107                         /**
00108                         * Initialize logging subsystem
00109                         * @param logPath - Path to directory where log files will be created
00110                         * @return either OK or:
00111                         *                       - LOG_ERROR if there was an error by creating the log files
00112                         **/
00113                         Result initialize(const std::string& logPath);
00114                         
00115                         /**
00116                         * Log any message to the given log target.
00117                         * You can bitwise combine log targets (i.e. LOG_CONSOLE | LOG_APP)
00118                         * to log to one or more log targets. Each message logged
00119                         * through this method will get a normal log level.
00120                         * \param target Target where the log message should be written
00121                         * \param msg Formatted message to log
00122                         **/
00123                         void log(LogTarget target, const char* msg, ...);
00124 
00125                         /**
00126                         * Same as the normal log function, but here you can specifiy also
00127                         * the level of logged messages.
00128                         **/
00129                         void log(LogTarget target, LogLevel level, const char* msg, ...);
00130                                                 
00131                         /**
00132                         * This function will setup logging echo. So you can for example specify
00133                         * that all messages given to the engine can be echoing to the console.
00134                         * @param from Echo messages from this target
00135                         * @param to All messages will be echoed here
00136                         **/
00137                         void setEcho(LogTarget from, LogTarget to){
00138                                 _echoMap[(int32)from] = (int32)to;      
00139                         }
00140 
00141                         /**
00142                         * Setup the default log level. All messages with the level lesser or
00143                         * equal to the specified will be logged.
00144                         * \param logLevel Level treshold of logged messages
00145                         **/
00146                         void setLevel(LogLevel logLevel){
00147                                 _logLevel = logLevel;
00148                         }
00149 
00150                                                 
00151                 private:
00152 
00153                         //! Only engine's core class is allowed to create the instance
00154                         friend class Engine;
00155 
00156                         /**
00157                         * Create an instance of the log subsystem
00158                         **/
00159                         Log();
00160 
00161                         /**
00162                         * Remove the log subsystem from the memory
00163                         **/
00164                         ~Log();
00165 
00166                         // This are our logging targets
00167                         std::ofstream _appLog;
00168                         std::ofstream _engineLog;
00169                         std::ofstream _clientLog;
00170                         std::ofstream _serverLog;
00171                         std::ofstream _kernelLog;
00172                         std::ofstream _pluginLog;
00173 
00174                         // Log path containing log files
00175                         std::string logPath;
00176                 
00177                         // map which stores the logging echo targets
00178                         std::map <int32, int32> _echoMap;
00179 
00180                         // Log level information
00181                         LogLevel        _logLevel;
00182                         
00183                         // logging function
00184                         void logIt(int32 target, LogLevel level, const char *msg);
00185 
00186                         // get log level string
00187                         std::string getLevelStr(LogLevel level);
00188                         
00189         };
00190         
00191 }; // end namespace
00192 
00193 
00194 #endif

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1