19 #ifndef TRINITYCORE_LOG_H
20 #define TRINITYCORE_LOG_H
27 #include <boost/asio/io_service.hpp>
28 #include <boost/asio/strand.hpp>
31 #include <unordered_map>
35 #define LOGGER_ROOT "root"
39 typedef std::unordered_map<std::string, Logger>
LoggerMap;
47 static Log* instance();
49 void Initialize(boost::asio::io_service* ioService);
50 void SetSynchronous();
51 void LoadFromConfig();
53 bool ShouldLog(std::string
const& type,
LogLevel level)
const;
54 bool SetLogLevel(std::string
const& name,
char const* level,
bool isLogger =
true);
56 template<
typename Format,
typename... Args>
59 write(Trinity::make_unique<LogMessage>(level, filter,
63 template<
typename Format,
typename... Args>
69 std::unique_ptr<LogMessage> msg =
73 msg->
param1 = std::to_string(account);
75 write(std::move(msg));
78 void outCharDump(
char const* str,
uint32 account_id,
uint64 guid,
char const* name);
80 void SetRealmId(
uint32 id);
82 template<
class AppenderImpl>
85 using Index =
typename AppenderImpl::TypeIndex;
87 ASSERT(itr == appenderFactory.end());
88 appenderFactory[
Index::value] = &CreateAppender<AppenderImpl>;
91 std::string
const&
GetLogsDir()
const {
return m_logsDir; }
95 static std::string GetTimestampStr();
96 void write(std::unique_ptr<LogMessage>&& msg)
const;
98 Logger const* GetLoggerByType(std::string
const& type)
const;
99 Appender* GetAppenderByName(std::string
const& name);
100 uint8 NextAppenderId();
101 void CreateAppenderFromConfig(std::string
const& name);
102 void CreateLoggerFromConfig(std::string
const& name);
103 void ReadAppendersFromConfig();
104 void ReadLoggersFromConfig();
121 LoggerMap::const_iterator it =
loggers.find(type);
123 return &(it->second);
129 size_t found = type.find_last_of(
".");
130 if (found != std::string::npos)
131 parentLogger = type.substr(0,found);
154 #define sLog Log::instance()
156 #define LOG_EXCEPTION_FREE(filterType__, level__, ...) \
160 sLog->outMessage(filterType__, level__, __VA_ARGS__); \
162 catch (std::exception& e) \
164 sLog->outMessage("server", LOG_LEVEL_ERROR, "Wrong format occurred (%s) at %s:%u.", \
165 e.what(), __FILE__, __LINE__); \
169 #if PLATFORM != PLATFORM_WINDOWS
170 void check_args(
const char*, ...)
ATTR_PRINTF(1, 2);
171 void check_args(
std::
string const&, ...);
174 #define TC_LOG_MESSAGE_BODY(filterType__, level__, ...) \
176 if (sLog->ShouldLog(filterType__, level__)) \
179 check_args(__VA_ARGS__); \
181 LOG_EXCEPTION_FREE(filterType__, level__, __VA_ARGS__); \
185 #define TC_LOG_MESSAGE_BODY(filterType__, level__, ...) \
186 __pragma(warning(push)) \
187 __pragma(warning(disable:4127)) \
189 if (sLog->ShouldLog(filterType__, level__)) \
190 LOG_EXCEPTION_FREE(filterType__, level__, __VA_ARGS__); \
192 __pragma(warning(pop))
195 #define TC_LOG_TRACE(filterType__, ...) \
196 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_TRACE, __VA_ARGS__)
198 #define TC_LOG_DEBUG(filterType__, ...) \
199 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_DEBUG, __VA_ARGS__)
201 #define TC_LOG_INFO(filterType__, ...) \
202 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_INFO, __VA_ARGS__)
204 #define TC_LOG_WARN(filterType__, ...) \
205 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_WARN, __VA_ARGS__)
207 #define TC_LOG_ERROR(filterType__, ...) \
208 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_ERROR, __VA_ARGS__)
210 #define TC_LOG_FATAL(filterType__, ...) \
211 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_FATAL, __VA_ARGS__)
std::unordered_map< uint8, AppenderCreatorFn > AppenderCreatorMap
Definition: Appender.h:118
LoggerMap loggers
Definition: Log.h:108
boost::asio::strand * _strand
Definition: Log.h:116
std::string m_logsDir
Definition: Log.h:112
Definition: Appender.h:36
std::unordered_map< uint8, Appender * > AppenderMap
Definition: Appender.h:114
AppenderCreatorMap appenderFactory
Definition: Log.h:106
LogLevel lowestLogLevel
Definition: Log.h:110
boost::asio::io_service * _ioService
Definition: Log.h:115
arena_t NULL
Definition: jemalloc_internal.h:624
void outMessage(std::string const &filter, LogLevel const level, Format &&fmt, Args &&...args)
Definition: Log.h:57
void outCommand(uint32 account, Format &&fmt, Args &&...args)
Definition: Log.h:64
std::string m_logsTimestamp
Definition: Log.h:113
#define LOGGER_ROOT
Definition: Log.h:35
#define ATTR_PRINTF(F, V)
Definition: Define.h:89
Definition: Appender.h:33
uint8 AppenderId
Definition: Log.h:109
uint32_t uint32
Definition: Define.h:150
static void Initialize(PCASC_SALSA20 pState, LPBYTE pbKey, DWORD cbKeyLength, LPBYTE pbVector)
Definition: CascDecrypt.cpp:81
LogLevel
Definition: Appender.h:31
uint64_t uint64
Definition: Define.h:149
LogLevel getLogLevel() const
Definition: Logger.cpp:33
#define TC_COMMON_API
Definition: Define.h:116
AppenderMap appenders
Definition: Log.h:107
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
std::string const & GetLogsDir() const
Definition: Log.h:91
Logger const * GetLoggerByType(std::string const &type) const
Definition: Log.h:119
uint8_t uint8
Definition: Define.h:152
#define ASSERT
Definition: Errors.h:55
const FieldDescriptor value
Definition: descriptor.h:1522
std::unordered_map< std::string, Logger > LoggerMap
Definition: Log.h:39
#define const
Definition: zconf.h:217
std::string param1
Definition: Appender.h:78
bool ShouldLog(std::string const &type, LogLevel level) const
Definition: Log.h:136
Definition: Appender.h:88
void RegisterAppender()
Definition: Log.h:83
std::string const & GetLogsTimestamp() const
Definition: Log.h:92