2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "Policies/SingletonImp.h"
22 #include "Config/ConfigEnv.h"
27 INSTANTIATE_SINGLETON_1( Log
);
37 const int LogType_count
= int(LogError
) +1;
39 void Log::InitColors(std::string str
)
49 std::istringstream
ss(str
);
51 for(int i
= 0; i
< LogType_count
; ++i
)
58 if(color
[i
] < 0 || color
[i
] >= Color_count
)
62 for(int i
= 0; i
< LogType_count
; ++i
)
63 m_colors
[i
] = Color(color
[i
]);
68 void Log::SetColor(bool stdout_stream
, Color color
)
70 #if PLATFORM == PLATFORM_WINDOWS
72 static WORD WinColorFG
[Color_count
] =
75 FOREGROUND_RED
, // RED
76 FOREGROUND_GREEN
, // GREEN
77 FOREGROUND_RED
| FOREGROUND_GREEN
, // BROWN
78 FOREGROUND_BLUE
, // BLUE
79 FOREGROUND_RED
| FOREGROUND_BLUE
,// MAGENTA
80 FOREGROUND_GREEN
| FOREGROUND_BLUE
, // CYAN
81 FOREGROUND_RED
| FOREGROUND_GREEN
| FOREGROUND_BLUE
,// WHITE
83 FOREGROUND_RED
| FOREGROUND_GREEN
| FOREGROUND_INTENSITY
,
85 FOREGROUND_RED
| FOREGROUND_INTENSITY
,
87 FOREGROUND_GREEN
| FOREGROUND_INTENSITY
,
88 FOREGROUND_BLUE
| FOREGROUND_INTENSITY
, // BLUE_BOLD
90 FOREGROUND_RED
| FOREGROUND_BLUE
| FOREGROUND_INTENSITY
,
92 FOREGROUND_GREEN
| FOREGROUND_BLUE
| FOREGROUND_INTENSITY
,
94 FOREGROUND_RED
| FOREGROUND_GREEN
| FOREGROUND_BLUE
| FOREGROUND_INTENSITY
97 HANDLE hConsole
= GetStdHandle(stdout_stream
? STD_OUTPUT_HANDLE
: STD_ERROR_HANDLE
);
98 SetConsoleTextAttribute(hConsole
, WinColorFG
[color
]);
111 FG_BLACK
=30, FG_RED
, FG_GREEN
, FG_BROWN
, FG_BLUE
,
112 FG_MAGENTA
, FG_CYAN
, FG_WHITE
, FG_YELLOW
117 BG_BLACK
=40, BG_RED
, BG_GREEN
, BG_BROWN
, BG_BLUE
,
118 BG_MAGENTA
, BG_CYAN
, BG_WHITE
121 static uint8 UnixColorFG
[Color_count
] =
128 FG_MAGENTA
, // MAGENTA
135 FG_MAGENTA
, // LMAGENTA
140 fprintf((stdout_stream
? stdout
: stderr
), "\x1b[%d%sm",UnixColorFG
[color
],(color
>=YELLOW
&&color
<Color_count
?";1":""));
144 void Log::ResetColor(bool stdout_stream
)
146 #if PLATFORM == PLATFORM_WINDOWS
147 HANDLE hConsole
= GetStdHandle(stdout_stream
? STD_OUTPUT_HANDLE
: STD_ERROR_HANDLE
);
148 SetConsoleTextAttribute(hConsole
, FOREGROUND_BLUE
| FOREGROUND_GREEN
| FOREGROUND_RED
);
150 fprintf(( stdout_stream
? stdout
: stderr
), "\x1b[0m");
154 void Log::SetLogLevel(char *Level
)
156 int32 NewLevel
=atoi((char*)Level
);
159 m_logLevel
= NewLevel
;
161 printf( "LogLevel is %u\n",m_logLevel
);
164 void Log::SetLogFileLevel(char *Level
)
166 int32 NewLevel
=atoi((char*)Level
);
169 m_logFileLevel
= NewLevel
;
171 printf( "LogFileLevel is %u\n",m_logFileLevel
);
174 void Log::Initialize()
176 std::string logsDir
= sConfig
.GetStringDefault("LogsDir","");
180 if((logsDir
.at(logsDir
.length()-1)!='/') && (logsDir
.at(logsDir
.length()-1)!='\\'))
184 std::string logfn
=sConfig
.GetStringDefault("LogFile", "");
187 if(sConfig
.GetBoolDefault("LogTimestamp",false))
189 std::string logTimestamp
= GetTimestampStr();
190 logTimestamp
.insert(0,"_");
191 size_t dot_pos
= logfn
.find_last_of(".");
192 if(dot_pos
!=logfn
.npos
)
193 logfn
.insert(dot_pos
,logTimestamp
);
195 logfn
+= logTimestamp
;
198 logfile
= fopen((logsDir
+logfn
).c_str(), "w");
201 std::string gmlogname
= sConfig
.GetStringDefault("GMLogFile", "");
202 if(!gmlogname
.empty())
204 if(sConfig
.GetBoolDefault("GmLogTimestamp",false))
206 std::string gmLogTimestamp
= GetTimestampStr();
207 gmLogTimestamp
.insert(0,"_");
208 size_t dot_pos
= gmlogname
.find_last_of(".");
209 if(dot_pos
!=gmlogname
.npos
)
210 gmlogname
.insert(dot_pos
,gmLogTimestamp
);
212 gmlogname
+= gmLogTimestamp
;
214 gmLogfile
= fopen((logsDir
+gmlogname
).c_str(), "a");
217 std::string charlogname
= sConfig
.GetStringDefault("CharLogFile", "");
218 if(!charlogname
.empty())
220 if(sConfig
.GetBoolDefault("CharLogTimestamp",false))
222 std::string charLogTimestamp
= GetTimestampStr();
223 charLogTimestamp
.insert(0,"_");
224 size_t dot_pos
= charlogname
.find_last_of(".");
225 if(dot_pos
!=charlogname
.npos
)
226 charlogname
.insert(dot_pos
,charLogTimestamp
);
228 charlogname
+= charLogTimestamp
;
230 charLogfile
= fopen((logsDir
+charlogname
).c_str(), "a");
233 std::string dberlogname
= sConfig
.GetStringDefault("DBErrorLogFile", "");
234 if(!dberlogname
.empty())
236 dberLogfile
= fopen((logsDir
+dberlogname
).c_str(), "a");
238 std::string ralogname
= sConfig
.GetStringDefault("RaLogFile", "");
239 if(!ralogname
.empty())
241 raLogfile
= fopen((logsDir
+ralogname
).c_str(), "a");
243 m_includeTime
= sConfig
.GetBoolDefault("LogTime", false);
244 m_logLevel
= sConfig
.GetIntDefault("LogLevel", 0);
245 m_logFileLevel
= sConfig
.GetIntDefault("LogFileLevel", 0);
246 InitColors(sConfig
.GetStringDefault("LogColors", ""));
250 if(sConfig
.GetBoolDefault("LogFilter_TransportMoves", true))
251 m_logFilter
|= LOG_FILTER_TRANSPORT_MOVES
;
252 if(sConfig
.GetBoolDefault("LogFilter_CreatureMoves", true))
253 m_logFilter
|= LOG_FILTER_CREATURE_MOVES
;
254 if(sConfig
.GetBoolDefault("LogFilter_VisibilityChanges", true))
255 m_logFilter
|= LOG_FILTER_VISIBILITY_CHANGES
;
257 m_charLog_Dump
= sConfig
.GetBoolDefault("CharLogDump", false);
260 void Log::outTimestamp(FILE* file
)
262 time_t t
= time(NULL
);
263 tm
* aTm
= localtime(&t
);
265 // MM month (2 digits 01-12)
266 // DD day (2 digits 01-31)
267 // HH hour (2 digits 00-23)
268 // MM minutes (2 digits 00-59)
269 // SS seconds (2 digits 00-59)
270 fprintf(file
,"%-4d-%02d-%02d %02d:%02d:%02d ",aTm
->tm_year
+1900,aTm
->tm_mon
+1,aTm
->tm_mday
,aTm
->tm_hour
,aTm
->tm_min
,aTm
->tm_sec
);
275 time_t t
= time(NULL
);
276 tm
* aTm
= localtime(&t
);
278 // MM month (2 digits 01-12)
279 // DD day (2 digits 01-31)
280 // HH hour (2 digits 00-23)
281 // MM minutes (2 digits 00-59)
282 // SS seconds (2 digits 00-59)
283 printf("%02d:%02d:%02d ",aTm
->tm_hour
,aTm
->tm_min
,aTm
->tm_sec
);
286 std::string
Log::GetTimestampStr()
288 time_t t
= time(NULL
);
289 tm
* aTm
= localtime(&t
);
291 // MM month (2 digits 01-12)
292 // DD day (2 digits 01-31)
293 // HH hour (2 digits 00-23)
294 // MM minutes (2 digits 00-59)
295 // SS seconds (2 digits 00-59)
297 snprintf(buf
,20,"%04d-%02d-%02d_%02d-%02d-%02d",aTm
->tm_year
+1900,aTm
->tm_mon
+1,aTm
->tm_mday
,aTm
->tm_hour
,aTm
->tm_min
,aTm
->tm_sec
);
298 return std::string(buf
);
301 void Log::outTitle( const char * str
)
307 SetColor(true,WHITE
);
309 // not expected utf8 and then send as-is
318 fprintf(logfile
, str
);
319 fprintf(logfile
, "\n" );
326 void Log::outString()
333 outTimestamp(logfile
);
334 fprintf(logfile
, "\n" );
340 void Log::outString( const char * str
, ... )
346 SetColor(true,m_colors
[LogNormal
]);
351 UTF8PRINTF(stdout
,str
,);
359 outTimestamp(logfile
);
363 vfprintf(logfile
, str
, ap
);
364 fprintf(logfile
, "\n" );
372 void Log::outError( const char * err
, ... )
378 SetColor(false,m_colors
[LogError
]);
383 UTF8PRINTF(stderr
,err
,);
388 fprintf( stderr
, "\n" );
391 outTimestamp(logfile
);
392 fprintf(logfile
, "ERROR:" );
396 vfprintf(logfile
, err
, ap
);
399 fprintf(logfile
, "\n" );
405 void Log::outErrorDb( const char * err
, ... )
411 SetColor(false,m_colors
[LogError
]);
416 UTF8PRINTF(stderr
,err
,);
421 fprintf( stderr
, "\n" );
425 outTimestamp(logfile
);
426 fprintf(logfile
, "ERROR:" );
430 vfprintf(logfile
, err
, ap
);
433 fprintf(logfile
, "\n" );
439 outTimestamp(dberLogfile
);
443 vfprintf(dberLogfile
, err
, ap
);
446 fprintf(dberLogfile
, "\n" );
452 void Log::outBasic( const char * str
, ... )
460 SetColor(true,m_colors
[LogDetails
]);
465 UTF8PRINTF(stdout
,str
,);
473 if(logfile
&& m_logFileLevel
> 0)
476 outTimestamp(logfile
);
478 vfprintf(logfile
, str
, ap
);
479 fprintf(logfile
, "\n" );
486 void Log::outDetail( const char * str
, ... )
495 SetColor(true,m_colors
[LogDetails
]);
500 UTF8PRINTF(stdout
,str
,);
507 if(logfile
&& m_logFileLevel
> 1)
510 outTimestamp(logfile
);
512 vfprintf(logfile
, str
, ap
);
513 fprintf(logfile
, "\n" );
521 void Log::outDebugInLine( const char * str
, ... )
528 SetColor(true,m_colors
[LogDebug
]);
530 UTF8PRINTF(stdout
,str
,);
535 if(logfile
&& m_logFileLevel
> 2)
539 vfprintf(logfile
, str
, ap
);
544 void Log::outDebug( const char * str
, ... )
551 SetColor(true,m_colors
[LogDebug
]);
556 UTF8PRINTF(stdout
,str
,);
563 if(logfile
&& m_logFileLevel
> 2)
565 outTimestamp(logfile
);
569 vfprintf(logfile
, str
, ap
);
572 fprintf(logfile
, "\n" );
578 void Log::outCommand( const char * str
, ... )
586 SetColor(true,m_colors
[LogDetails
]);
591 UTF8PRINTF(stdout
,str
,);
598 if(logfile
&& m_logFileLevel
> 1)
601 outTimestamp(logfile
);
603 vfprintf(logfile
, str
, ap
);
604 fprintf(logfile
, "\n" );
611 outTimestamp(gmLogfile
);
613 vfprintf(gmLogfile
, str
, ap
);
614 fprintf(gmLogfile
, "\n" );
621 void Log::outChar(const char * str
, ... )
630 outTimestamp(charLogfile
);
632 vfprintf(charLogfile
, str
, ap
);
633 fprintf(charLogfile
, "\n" );
639 void Log::outCharDump( const char * str
, uint32 account_id
, uint32 guid
, const char * name
)
643 fprintf(charLogfile
, "== START DUMP == (account: %u guid: %u name: %s )\n%s\n== END DUMP ==\n",account_id
,guid
,name
,str
);
648 void Log::outMenu( const char * str
, ... )
653 SetColor(true,m_colors
[LogNormal
]);
658 UTF8PRINTF(stdout
,str
,);
664 outTimestamp(logfile
);
668 vfprintf(logfile
, str
, ap
);
671 fprintf(logfile
, "\n" );
677 void Log::outRALog( const char * str
, ... )
684 outTimestamp(raLogfile
);
686 vfprintf(raLogfile
, str
, ap
);
687 fprintf(raLogfile
, "\n" );
694 void outstring_log(const char * str
, ...)
702 vsnprintf(buf
,256, str
, ap
);
705 MaNGOS::Singleton
<Log
>::Instance().outString(buf
);
708 void detail_log(const char * str
, ...)
716 vsnprintf(buf
,256, str
, ap
);
719 MaNGOS::Singleton
<Log
>::Instance().outDetail(buf
);
722 void debug_log(const char * str
, ...)
730 vsnprintf(buf
,256, str
, ap
);
733 MaNGOS::Singleton
<Log
>::Instance().outDebug(buf
);
736 void error_log(const char * str
, ...)
744 vsnprintf(buf
,256, str
, ap
);
747 MaNGOS::Singleton
<Log
>::Instance().outError(buf
);
750 void error_db_log(const char * str
, ...)
758 vsnprintf(buf
,256, str
, ap
);
761 MaNGOS::Singleton
<Log
>::Instance().outErrorDb(buf
);