Log.cpp

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 
00014 #include "Log.h"
00015 #include <iostream>
00016 #include <time.h>
00017 
00018 namespace nrEngine{
00019 
00020         //-------------------------------------------------------------------------
00021         Log::Log()
00022         {
00023                 _logLevel = LL_NORMAL;
00024         }
00025         
00026         //-------------------------------------------------------------------------
00027         Log::~Log()
00028         {       
00029                 log (LOG_ANYTHING, LL_DEBUG, "Logging stopped");
00030                 
00031                 // close streams
00032                 _kernelLog.close();
00033                 _engineLog.close();
00034                 _serverLog.close();
00035                 _clientLog.close();
00036                 _pluginLog.close();
00037                 _appLog.close();
00038         }
00039         
00040         //-------------------------------------------------------------------------
00041         Result Log::initialize(const std::string& logPath){
00042                 
00043                 this->logPath = logPath;
00044                 
00045                 // open log files
00046                 _appLog.open((logPath + "/application.log").c_str(),std::ios::out);// | ios::app);
00047                 _kernelLog.open((logPath + "/kernel.log").c_str(), std::ios::out);// | ios::app);
00048                 //_clientLog.open((logPath + "/client.log").c_str(), std::ios::out);// | ios::app);
00049                 //_serverLog.open((logPath + "/server.log").c_str(), std::ios::out);// | ios::app);
00050                 _engineLog.open((logPath + "/engine.log").c_str(), std::ios::out);// | ios::app);
00051                 _pluginLog.open((logPath + "/plugin.log").c_str(), std::ios::out);// | ios::app);
00052                         
00053                 log (LOG_ANYTHING, LL_DEBUG, "Logging activated");
00054                                 
00055                 return OK;
00056         }
00057         
00058         //-------------------------------------------------------------------------
00059         void Log::log(LogTarget target, const char* msg, ...)
00060         {
00061                 // check whenver we have already initialised the log engine
00062                 /*if (logPath.length() == 0){
00063                         static bool warned = false;
00064                         if (warned == false){
00065                                 logIt(LOG_CONSOLE, LL_WARNING, "You can not log messages before you initialize the log engine");
00066                                 warned = true;
00067                         }
00068                         return;
00069                 }*/
00070                         
00071                 // get messages 
00072                 va_list args; 
00073                 va_start(args,msg);
00074                 char szBuf[2056];
00075                 memset(szBuf, 0, sizeof(char)*2056);
00076                 vsprintf(szBuf,msg,args);
00077                 va_end(args);
00078                 
00079                 // log the message
00080                 logIt(target, LL_NORMAL, szBuf);
00081                 
00082                 // echo logging
00083                 if (_echoMap[target]){
00084                         logIt(_echoMap[target], LL_NORMAL, szBuf);
00085                 }
00086 
00087         }
00088         
00089         //-------------------------------------------------------------------------
00090         void Log::log(LogTarget target, LogLevel level, const char* msg, ...)
00091         {
00092         
00093                 // check whenver we have already initialised the log engine
00094                 /*if (logPath.length() == 0){
00095                         static bool warned = false;
00096                         if (warned == false){
00097                                 logIt(LOG_CONSOLE, LL_WARNING, "You can not log messages before you initialize the log engine");
00098                                 warned = true;
00099                         }
00100                         return;
00101                 }*/
00102                         
00103                 // get messages 
00104                 va_list args; 
00105                 va_start(args,msg);
00106                 char szBuf[2056];
00107                 memset(szBuf, 0, sizeof(char)*2056);
00108                 vsprintf(szBuf,msg,args);
00109                 va_end(args);
00110                 
00111                 // log the message
00112                 logIt(target, level, szBuf);
00113                 
00114                 // echo logging
00115                 if (_echoMap[target]){
00116                         logIt(_echoMap[target], level, szBuf);
00117                 }
00118                 
00119         }
00120 
00121         //-------------------------------------------------------------------------
00122         void Log::logIt(int32 target, LogLevel level, const char *msg){
00123 
00124                 // check whenever the coming message is allowed to be logged
00125                 if (level > _logLevel) return;
00126                 
00127                 // get messages 
00128                 const char *szBuf = msg;
00129                 
00130                 // Get time & date string
00131                 time_t _time = time(NULL);
00132                 tm* time = localtime(&_time);
00133                 
00134                 // create the string before the message         
00135                 char timeDate[255];
00136                 std::string sLevel = getLevelStr(level);
00137                 if (time != NULL)
00138                         sprintf(timeDate, "%02d:%02d:%02d - %s", time->tm_hour, time->tm_min, time->tm_sec, sLevel.c_str());
00139                 else
00140                         sprintf(timeDate, "%s", sLevel.c_str());
00141 
00142                                                 
00143                 // Check whereever to log
00144                 if((target & LOG_APP) && _appLog.is_open()){
00145                         _appLog << timeDate << ": " << szBuf << "\n";
00146                         _appLog.flush();
00147                 }
00148                 
00149                 if((target & LOG_CLIENT) && _clientLog.is_open()){
00150                         _clientLog << timeDate << ": " << szBuf << "\n";
00151                         _clientLog.flush();
00152                 }
00153                 
00154                 if((target & LOG_SERVER) && _serverLog.is_open()){
00155                         _serverLog << timeDate << ": " << szBuf << "\n";
00156                         _serverLog.flush();
00157                 }
00158                 
00159                 if((target & LOG_KERNEL) && _kernelLog.is_open()){
00160                         _kernelLog << timeDate << ": " << szBuf << "\n";
00161                         _kernelLog.flush();
00162                 }
00163                 
00164                 if((target & LOG_ENGINE) && _engineLog.is_open()){
00165                         _engineLog << timeDate << ": " << szBuf << "\n";
00166                         _engineLog.flush();
00167                 }
00168                 
00169                 if((target & LOG_PLUGIN) && _pluginLog.is_open()){
00170                         _pluginLog << timeDate << ": " << szBuf << "\n";
00171                         _pluginLog.flush();
00172                 }
00173         
00174                 if (target & LOG_CONSOLE){
00175                         std::cout << timeDate << ": " << szBuf << "\n";
00176                         std::cout.flush();
00177                 }
00178                         
00179         }
00180 
00181         //-------------------------------------------------------------------------
00182         std::string Log::getLevelStr(LogLevel level)
00183         {
00184                 switch(level){
00185                         case LL_FATAL_ERROR: return "FatalError";
00186                         case LL_ERROR: return "Error";
00187                         case LL_WARNING: return "Warning";
00188                         case LL_NORMAL: return "Log";
00189                         case LL_DEBUG: return "Debug";
00190                         case LL_CHATTY: return "Info";
00191                         default: return "";
00192                 }
00193                 return "";
00194         }
00195         
00196 };// end namespace
00197 

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