00001
00002
00003
00004
00005
00006
00007
00008
00009
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
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
00046 _appLog.open((logPath + "/application.log").c_str(),std::ios::out);
00047 _kernelLog.open((logPath + "/kernel.log").c_str(), std::ios::out);
00048
00049
00050 _engineLog.open((logPath + "/engine.log").c_str(), std::ios::out);
00051 _pluginLog.open((logPath + "/plugin.log").c_str(), std::ios::out);
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
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
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
00080 logIt(target, LL_NORMAL, szBuf);
00081
00082
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
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
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
00112 logIt(target, level, szBuf);
00113
00114
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
00125 if (level > _logLevel) return;
00126
00127
00128 const char *szBuf = msg;
00129
00130
00131 time_t _time = time(NULL);
00132 tm* time = localtime(&_time);
00133
00134
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
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 };
00197