Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
Log.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef SKYFIRESERVER_LOG_H
7#define SKYFIRESERVER_LOG_H
8
9#include "Appender.h"
10#include "Define.h"
12#include "Logger.h"
13#include "LogWorker.h"
14#include "Platform/Singleton.h"
15
16#include <cstdarg>
17#include <string>
18
19#define LOGGER_ROOT "root"
20
21class Log
22{
23 friend class Skyfire::Singleton<Log, Skyfire::Mutex>;
24
25 typedef UNORDERED_MAP<std::string, Logger> LoggerMap;
26
27private:
28 Log();
29 ~Log();
30
31public:
32 void LoadFromConfig();
33 void Close();
34 bool ShouldLog(std::string const& type, LogLevel level) const;
35 bool SetLogLevel(std::string const& name, char const* level, bool isLogger = true);
36
37 void outMessage(std::string const& f, LogLevel level, char const* str, ...) ATTR_PRINTF(4, 5);
38
39 void outCommand(uint32 account, const char* str, ...) ATTR_PRINTF(3, 4);
40 void outCharDump(char const* str, uint32 account_id, uint32 guid, char const* name);
41
42 void SetRealmId(uint32 id);
43
44private:
45 static std::string GetTimestampStr();
46 void vlog(std::string const& f, LogLevel level, char const* str, va_list argptr);
47 void write(LogMessage* msg) const;
48
49 Logger const* GetLoggerByType(std::string const& type) const;
50 Appender* GetAppenderByName(std::string const& name);
52 void CreateAppenderFromConfig(std::string const& name);
53 void CreateLoggerFromConfig(std::string const& name);
56
60
61 std::string m_logsDir;
62 std::string m_logsTimestamp;
63
65};
66
67inline Logger const* Log::GetLoggerByType(std::string const& type) const
68{
69 LoggerMap::const_iterator it = loggers.find(type);
70 if (it != loggers.end())
71 return &(it->second);
72
73 if (type == LOGGER_ROOT)
74 return NULL;
75
76 std::string parentLogger = LOGGER_ROOT;
77 size_t found = type.find_last_of(".");
78 if (found != std::string::npos)
79 parentLogger = type.substr(0, found);
80
81 return GetLoggerByType(parentLogger);
82}
83
84inline bool Log::ShouldLog(std::string const& type, LogLevel level) const
85{
86 // TODO: Use cache to store "Type.sub1.sub2": "Type" equivalence, should
87 // Speed up in cases where requesting "Type.sub1.sub2" but only configured
88 // Logger "Type"
89
90 Logger const* logger = GetLoggerByType(type);
91 if (!logger)
92 return false;
93
94 LogLevel logLevel = logger->getLogLevel();
95 return logLevel != LogLevel::LOG_LEVEL_DISABLED && logLevel <= level;
96}
97
98inline void Log::outMessage(std::string const& filter, LogLevel level, const char* str, ...)
99{
100 va_list ap;
101 va_start(ap, str);
102 vlog(filter, level, str, ap);
103 va_end(ap);
104}
105
106#define sLog Skyfire::Singleton<Log, Skyfire::Mutex>::instance()
107
108#if COMPILER != COMPILER_MICROSOFT
109#define SF_LOG_MESSAGE_BODY(filterType__, level__, ...) \
110 do { \
111 if (sLog->ShouldLog(filterType__, level__)) \
112 sLog->outMessage(filterType__, level__, __VA_ARGS__); \
113 } while (0)
114#elif COMPILER != COMPILER_CLANG
115#define SF_LOG_MESSAGE_BODY(filterType__, level__, ...) \
116 do { \
117 if (sLog->ShouldLog(filterType__, level__)) \
118 sLog->outMessage(filterType__, level__, __VA_ARGS__); \
119 } while (0)
120#else
121#define SF_LOG_MESSAGE_BODY(filterType__, level__, ...) \
122 __pragma(warning(push)) \
123 __pragma(warning(disable:4127)) \
124 do { \
125 if (sLog->ShouldLog(filterType__, level__)) \
126 sLog->outMessage(filterType__, level__, __VA_ARGS__); \
127 } while (0) \
128 __pragma(warning(pop))
129#endif
130
131#define SF_LOG_TRACE(filterType__, ...) \
132 SF_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_TRACE, __VA_ARGS__)
133
134#define SF_LOG_DEBUG(filterType__, ...) \
135 SF_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_DEBUG, __VA_ARGS__)
136
137#define SF_LOG_INFO(filterType__, ...) \
138 SF_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_INFO, __VA_ARGS__)
139
140#define SF_LOG_WARN(filterType__, ...) \
141 SF_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_WARN, __VA_ARGS__)
142
143#define SF_LOG_ERROR(filterType__, ...) \
144 SF_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_ERROR, __VA_ARGS__)
145
146#define SF_LOG_FATAL(filterType__, ...) \
147 SF_LOG_MESSAGE_BODY(filterType__, LogLevel::LOG_LEVEL_FATAL, __VA_ARGS__)
148
149#endif
LogLevel
Definition Appender.h:16
@ LOG_LEVEL_DISABLED
Definition Appender.h:17
UNORDERED_MAP< uint8, Appender * > AppenderMap
Definition Appender.h:95
std::uint8_t uint8
Definition Define.h:79
#define ATTR_PRINTF(F, V)
Definition Define.h:55
#define LOGGER_ROOT
Definition Log.h:19
#define UNORDERED_MAP
AppenderMap appenders
Definition Log.h:57
uint8 NextAppenderId()
Definition Log.cpp:31
bool ShouldLog(std::string const &type, LogLevel level) const
Definition Log.h:84
void CreateAppenderFromConfig(std::string const &name)
Definition Log.cpp:57
Appender * GetAppenderByName(std::string const &name)
Definition Log.cpp:48
void SetRealmId(uint32 id)
Definition Log.cpp:349
void write(LogMessage *msg) const
Definition Log.cpp:258
bool SetLogLevel(std::string const &name, char const *level, bool isLogger=true)
Definition Log.cpp:282
void LoadFromConfig()
Definition Log.cpp:369
std::string m_logsTimestamp
Definition Log.h:62
void ReadAppendersFromConfig()
Definition Log.cpp:209
uint8 AppenderId
Definition Log.h:59
static std::string GetTimestampStr()
Definition Log.cpp:272
void outMessage(std::string const &f, LogLevel level, char const *str,...) ATTR_PRINTF(4
Definition Log.h:98
void void outCommand(uint32 account, const char *str,...) ATTR_PRINTF(3
Definition Log.cpp:329
LogWorker * worker
Definition Log.h:64
LoggerMap loggers
Definition Log.h:58
std::string m_logsDir
Definition Log.h:61
void vlog(std::string const &f, LogLevel level, char const *str, va_list argptr)
Definition Log.cpp:251
void Close()
Definition Log.cpp:356
void CreateLoggerFromConfig(std::string const &name)
Definition Log.cpp:149
UNORDERED_MAP< std::string, Logger > LoggerMap
Definition Log.h:25
void void void outCharDump(char const *str, uint32 account_id, uint32 guid, char const *name)
Definition Log.cpp:311
Log()
Definition Log.cpp:20
void ReadLoggersFromConfig()
Definition Log.cpp:220
Logger const * GetLoggerByType(std::string const &type) const
Definition Log.h:67
LogLevel getLogLevel() const
Definition Logger.cpp:21