Updated Copyright year to 2013
[getmangos.git] / src / shared / Log.h
blobadc4b218e264171c9295924c4fc2d4caf6d6839a
1 /*
2 * Copyright (C) 2005-2013 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
19 #ifndef MANGOSSERVER_LOG_H
20 #define MANGOSSERVER_LOG_H
22 #include "Common.h"
23 #include "Policies/Singleton.h"
25 class Config;
26 class ByteBuffer;
28 enum LogLevel
30 LOG_LVL_MINIMAL = 0, // unconditional and errors
31 LOG_LVL_BASIC = 1,
32 LOG_LVL_DETAIL = 2,
33 LOG_LVL_DEBUG = 3
36 // bitmask (not forgot update logFilterData content)
37 enum LogFilters
39 LOG_FILTER_TRANSPORT_MOVES = 0x000001, // 0 any related to transport moves
40 LOG_FILTER_CREATURE_MOVES = 0x000002, // 1 creature move by cells
41 LOG_FILTER_VISIBILITY_CHANGES = 0x000004, // 2 update visibility for diff objects and players
42 LOG_FILTER_ACHIEVEMENT_UPDATES = 0x000008, // 3 achievement update broadcasts
43 LOG_FILTER_WEATHER = 0x000010, // 4 weather changes
44 LOG_FILTER_PLAYER_STATS = 0x000020, // 5 player save data
45 LOG_FILTER_SQL_TEXT = 0x000040, // 6 raw SQL text send to DB engine
46 LOG_FILTER_PLAYER_MOVES = 0x000080, // 7 player moves by grid/cell
47 LOG_FILTER_PERIODIC_AFFECTS = 0x000100, // 8 DoT/HoT apply trace
48 LOG_FILTER_AI_AND_MOVEGENSS = 0x000200, // 9 AI/movement generators debug output
49 LOG_FILTER_DAMAGE = 0x000400, // 10 Direct/Area damage trace
50 LOG_FILTER_COMBAT = 0x000800, // 11 attack states/roll attack results/etc
51 LOG_FILTER_SPELL_CAST = 0x001000, // 12 spell cast/aura apply/spell proc events
52 LOG_FILTER_DB_STRICTED_CHECK = 0x002000, // 13 stricted DB data checks output (with possible false reports) for DB devs
53 LOG_FILTER_AHBOT_SELLER = 0x004000, // 14 Auction House Bot seller part
54 LOG_FILTER_AHBOT_BUYER = 0x008000, // 15 Auction House Bot buyer part
55 LOG_FILTER_PATHFINDING = 0x010000, // 16 Pathfinding
58 #define LOG_FILTER_COUNT 17
60 struct LogFilterData
62 char const* name;
63 char const* configName;
64 bool defaultState;
67 extern LogFilterData logFilterData[LOG_FILTER_COUNT];
69 enum Color
71 BLACK,
72 RED,
73 GREEN,
74 BROWN,
75 BLUE,
76 MAGENTA,
77 CYAN,
78 GREY,
79 YELLOW,
80 LRED,
81 LGREEN,
82 LBLUE,
83 LMAGENTA,
84 LCYAN,
85 WHITE
88 const int Color_count = int(WHITE) + 1;
90 class Log : public MaNGOS::Singleton<Log, MaNGOS::ClassLevelLockable<Log, ACE_Thread_Mutex> >
92 friend class MaNGOS::OperatorNew<Log>;
93 Log();
95 ~Log()
97 if (logfile != NULL)
98 fclose(logfile);
99 logfile = NULL;
101 if (gmLogfile != NULL)
102 fclose(gmLogfile);
103 gmLogfile = NULL;
105 if (charLogfile != NULL)
106 fclose(charLogfile);
107 charLogfile = NULL;
109 if (dberLogfile != NULL)
110 fclose(dberLogfile);
111 dberLogfile = NULL;
113 if (raLogfile != NULL)
114 fclose(raLogfile);
115 raLogfile = NULL;
117 if (worldLogfile != NULL)
118 fclose(worldLogfile);
119 worldLogfile = NULL;
121 public:
122 void Initialize();
123 void InitColors(const std::string& init_str);
125 void outCommand(uint32 account, const char* str, ...) ATTR_PRINTF(3, 4);
126 void outString(); // any log level
127 // any log level
128 void outString(const char* str, ...) ATTR_PRINTF(2, 3);
129 // any log level
130 void outError(const char* err, ...) ATTR_PRINTF(2, 3);
131 // log level >= 1
132 void outBasic(const char* str, ...) ATTR_PRINTF(2, 3);
133 // log level >= 2
134 void outDetail(const char* str, ...) ATTR_PRINTF(2, 3);
135 // log level >= 3
136 void outDebug(const char* str, ...) ATTR_PRINTF(2, 3);
138 void outErrorDb(); // any log level
139 // any log level
140 void outErrorDb(const char* str, ...) ATTR_PRINTF(2, 3);
141 // any log level
142 void outChar(const char* str, ...) ATTR_PRINTF(2, 3);
143 // any log level
144 void outWorldPacketDump(uint32 socket, uint32 opcode, char const* opcodeName, ByteBuffer const* packet, bool incoming);
145 // any log level
146 void outCharDump(const char* str, uint32 account_id, uint32 guid, const char* name);
147 void outRALog(const char* str, ...) ATTR_PRINTF(2, 3);
148 uint32 GetLogLevel() const { return m_logLevel; }
149 void SetLogLevel(char* Level);
150 void SetLogFileLevel(char* Level);
151 void SetColor(bool stdout_stream, Color color);
152 void ResetColor(bool stdout_stream);
153 void outTime();
154 static void outTimestamp(FILE* file);
155 static std::string GetTimestampStr();
156 bool HasLogFilter(uint32 filter) const { return m_logFilter & filter; }
157 void SetLogFilter(LogFilters filter, bool on) { if (on) m_logFilter |= filter; else m_logFilter &= ~filter; }
158 bool HasLogLevelOrHigher(LogLevel loglvl) const { return m_logLevel >= loglvl || (m_logFileLevel >= loglvl && logfile); }
159 bool IsOutCharDump() const { return m_charLog_Dump; }
160 bool IsIncludeTime() const { return m_includeTime; }
162 static void WaitBeforeContinueIfNeed();
163 private:
164 FILE* openLogFile(char const* configFileName, char const* configTimeStampFlag, char const* mode);
165 FILE* openGmlogPerAccount(uint32 account);
167 FILE* raLogfile;
168 FILE* logfile;
169 FILE* gmLogfile;
170 FILE* charLogfile;
171 FILE* dberLogfile;
172 FILE* worldLogfile;
173 ACE_Thread_Mutex m_worldLogMtx;
175 // log/console control
176 LogLevel m_logLevel;
177 LogLevel m_logFileLevel;
178 bool m_colored;
179 bool m_includeTime;
180 Color m_colors[4];
181 uint32 m_logFilter;
183 // cache values for after initilization use (like gm log per account case)
184 std::string m_logsDir;
185 std::string m_logsTimestamp;
187 // char log control
188 bool m_charLog_Dump;
190 // gm log control
191 bool m_gmlog_per_account;
192 std::string m_gmlog_filename_format;
195 #define sLog MaNGOS::Singleton<Log>::Instance()
197 #define BASIC_LOG(...) \
198 do { \
199 if (sLog.HasLogLevelOrHigher(LOG_LVL_BASIC)) \
200 sLog.outBasic(__VA_ARGS__); \
201 } while(0)
203 #define BASIC_FILTER_LOG(F,...) \
204 do { \
205 if (sLog.HasLogLevelOrHigher(LOG_LVL_BASIC) && !sLog.HasLogFilter(F)) \
206 sLog.outBasic(__VA_ARGS__); \
207 } while(0)
209 #define DETAIL_LOG(...) \
210 do { \
211 if (sLog.HasLogLevelOrHigher(LOG_LVL_DETAIL)) \
212 sLog.outDetail(__VA_ARGS__); \
213 } while(0)
215 #define DETAIL_FILTER_LOG(F,...) \
216 do { \
217 if (sLog.HasLogLevelOrHigher(LOG_LVL_DETAIL) && !sLog.HasLogFilter(F)) \
218 sLog.outDetail(__VA_ARGS__); \
219 } while(0)
221 #define DEBUG_LOG(...) \
222 do { \
223 if (sLog.HasLogLevelOrHigher(LOG_LVL_DEBUG)) \
224 sLog.outDebug(__VA_ARGS__); \
225 } while(0)
227 #define DEBUG_FILTER_LOG(F,...) \
228 do { \
229 if (sLog.HasLogLevelOrHigher(LOG_LVL_DEBUG) && !sLog.HasLogFilter(F)) \
230 sLog.outDebug(__VA_ARGS__); \
231 } while(0)
233 #define ERROR_DB_FILTER_LOG(F,...) \
234 do { \
235 if (!sLog.HasLogFilter(F)) \
236 sLog.outErrorDb(__VA_ARGS__); \
237 } while(0)
239 #define ERROR_DB_STRICT_LOG(...) \
240 ERROR_DB_FILTER_LOG(LOG_FILTER_DB_STRICTED_CHECK, __VA_ARGS__)
242 // primary for script library
243 void MANGOS_DLL_SPEC outstring_log(const char* str, ...) ATTR_PRINTF(1, 2);
244 void MANGOS_DLL_SPEC detail_log(const char* str, ...) ATTR_PRINTF(1, 2);
245 void MANGOS_DLL_SPEC debug_log(const char* str, ...) ATTR_PRINTF(1, 2);
246 void MANGOS_DLL_SPEC error_log(const char* str, ...) ATTR_PRINTF(1, 2);
247 void MANGOS_DLL_SPEC error_db_log(const char* str, ...) ATTR_PRINTF(1, 2);
248 #endif