Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Toggle main menu visibility
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
"
11
#include "
Dynamic/UnorderedMap.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
21
class
Log
22
{
23
friend
class
Skyfire::Singleton
<
Log
,
Skyfire
::Mutex>;
24
25
typedef
UNORDERED_MAP
<std::string, Logger>
LoggerMap
;
26
27
private
:
28
Log
();
29
~
Log
();
30
31
public
:
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
44
private
:
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);
51
uint8
NextAppenderId
();
52
void
CreateAppenderFromConfig
(std::string const& name);
53
void
CreateLoggerFromConfig
(std::string const& name);
54
void
ReadAppendersFromConfig
();
55
void
ReadLoggersFromConfig
();
56
57
AppenderMap
appenders
;
58
LoggerMap
loggers
;
59
uint8
AppenderId
;
60
61
std::string
m_logsDir
;
62
std::string
m_logsTimestamp
;
63
64
LogWorker
*
worker
;
65
};
66
67
inline
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
84
inline
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
98
inline
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
Appender.h
LogLevel
LogLevel
Definition
Appender.h:16
LogLevel::LOG_LEVEL_DISABLED
@ LOG_LEVEL_DISABLED
Definition
Appender.h:17
AppenderMap
UNORDERED_MAP< uint8, Appender * > AppenderMap
Definition
Appender.h:95
Define.h
uint8
std::uint8_t uint8
Definition
Define.h:79
ATTR_PRINTF
#define ATTR_PRINTF(F, V)
Definition
Define.h:55
LOGGER_ROOT
#define LOGGER_ROOT
Definition
Log.h:19
LogWorker.h
Logger.h
Singleton.h
UnorderedMap.h
UNORDERED_MAP
#define UNORDERED_MAP
Definition
UnorderedMap.h:58
Appender
Definition
Appender.h:70
Log::appenders
AppenderMap appenders
Definition
Log.h:57
Log::NextAppenderId
uint8 NextAppenderId()
Definition
Log.cpp:31
Log::ShouldLog
bool ShouldLog(std::string const &type, LogLevel level) const
Definition
Log.h:84
Log::CreateAppenderFromConfig
void CreateAppenderFromConfig(std::string const &name)
Definition
Log.cpp:57
Log::GetAppenderByName
Appender * GetAppenderByName(std::string const &name)
Definition
Log.cpp:48
Log::SetRealmId
void SetRealmId(uint32 id)
Definition
Log.cpp:349
Log::write
void write(LogMessage *msg) const
Definition
Log.cpp:258
Log::SetLogLevel
bool SetLogLevel(std::string const &name, char const *level, bool isLogger=true)
Definition
Log.cpp:282
Log::LoadFromConfig
void LoadFromConfig()
Definition
Log.cpp:369
Log::m_logsTimestamp
std::string m_logsTimestamp
Definition
Log.h:62
Log::ReadAppendersFromConfig
void ReadAppendersFromConfig()
Definition
Log.cpp:209
Log::AppenderId
uint8 AppenderId
Definition
Log.h:59
Log::GetTimestampStr
static std::string GetTimestampStr()
Definition
Log.cpp:272
Log::outMessage
void outMessage(std::string const &f, LogLevel level, char const *str,...) ATTR_PRINTF(4
Definition
Log.h:98
Log::outCommand
void void outCommand(uint32 account, const char *str,...) ATTR_PRINTF(3
Definition
Log.cpp:329
Log::worker
LogWorker * worker
Definition
Log.h:64
Log::loggers
LoggerMap loggers
Definition
Log.h:58
Log::m_logsDir
std::string m_logsDir
Definition
Log.h:61
Log::vlog
void vlog(std::string const &f, LogLevel level, char const *str, va_list argptr)
Definition
Log.cpp:251
Log::Close
void Close()
Definition
Log.cpp:356
Log::CreateLoggerFromConfig
void CreateLoggerFromConfig(std::string const &name)
Definition
Log.cpp:149
Log::LoggerMap
UNORDERED_MAP< std::string, Logger > LoggerMap
Definition
Log.h:25
Log::outCharDump
void void void outCharDump(char const *str, uint32 account_id, uint32 guid, char const *name)
Definition
Log.cpp:311
Log::Log
Log()
Definition
Log.cpp:20
Log::ReadLoggersFromConfig
void ReadLoggersFromConfig()
Definition
Log.cpp:220
Log::GetLoggerByType
Logger const * GetLoggerByType(std::string const &type) const
Definition
Log.h:67
LogWorker
Definition
LogWorker.h:14
Logger
Definition
Logger.h:12
Logger::getLogLevel
LogLevel getLogLevel() const
Definition
Logger.cpp:21
Skyfire::Singleton
Definition
Singleton.h:15
Skyfire
Definition
AuthPatchTransfer.h:12
src
server
shared
Logging
Log.h
Generated on
for Project SkyFire Core by
1.17.0