18#ifndef TRINITYCORE_LOG_H
19#define TRINITYCORE_LOG_H
26#include <unordered_map>
41#define LOGGER_ROOT "root"
45template <
class AppenderImpl>
48 return new AppenderImpl(
id, std::move(name), level,
flags, extraArgs);
63 static Log* instance() noexcept;
65 void Initialize(
Trinity::Asio::IoContext* ioContext);
66 void SetAsynchronous(
Trinity::Asio::IoContext* ioContext);
67 void SetSynchronous();
68 void LoadFromConfig();
70 bool ShouldLog(
std::string_view type,
LogLevel level) const noexcept;
71 Logger const* GetEnabledLogger(
std::string_view type,
LogLevel level) const noexcept;
72 bool SetLogLevel(
std::
string const& name,
int32 level,
bool isLogger = true);
74 template<typename... Args>
75 void OutMessage(
std::string_view filter,
LogLevel level,
Trinity::FormatString<Args...>
fmt, Args&&... args) noexcept
80 template<
typename... Args>
86 template<
typename... Args>
95 void OutCharDump(std::string
const& str,
uint32 account_id,
uint64 guid, std::string
const& name)
const noexcept;
97 void SetRealmId(
uint32 id);
99 template<
class AppenderImpl>
102 this->RegisterAppender(AppenderImpl::type, &CreateAppender<AppenderImpl>);
108 void CreateAppenderFromConfigLine(std::string
const& name, std::string
const& options);
109 void CreateLoggerFromConfigLine(std::string
const& name, std::string
const& options);
111 template <
typename StringOrStringView>
112 static constexpr std::string_view
make_string_view(StringOrStringView
const& stringOrStringView)
114 return stringOrStringView;
117 template <
size_t CharArraySize>
120 return { std::begin(chars), (chars[CharArraySize - 1] ==
'\0' ? CharArraySize - 1 : CharArraySize) };
123 template <
size_t CharArraySize>
126 return { std::begin(chars), (chars[CharArraySize - 1] ==
'\0' ? CharArraySize - 1 : CharArraySize) };
130 static std::string GetTimestampStr();
132 Logger const* GetLoggerByType(std::string_view type)
const;
133 Appender* GetAppenderByName(std::string_view name);
134 uint8 NextAppenderId();
135 void CreateAppenderFromConfig(std::string
const& name);
136 void CreateLoggerFromConfig(std::string
const& name);
137 void ReadAppendersFromConfig();
138 void ReadLoggersFromConfig();
144 std::unordered_map<uint8, std::unique_ptr<Appender>>
appenders;
145 std::unordered_map<std::string_view, std::unique_ptr<Logger>>
loggers;
156#define sLog Log::instance()
158#define TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ...) \
160 Log* logInstance = sLog; \
161 if (Logger const* loggerInstance = logInstance->GetEnabledLogger(Log::make_string_view((filterType__)), (level__))) \
162 logInstance->OutMessageTo(loggerInstance, Log::make_string_view((filterType__)), (level__), \
163 Log::make_format_string_view((message__)), ## __VA_ARGS__); \
166#ifdef PERFORMANCE_PROFILING
167#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) ((void)0)
168#elif TRINITY_PLATFORM != TRINITY_PLATFORM_WINDOWS
169#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ## __VA_ARGS__)
171#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) \
172 __pragma(warning(push)) \
173 __pragma(warning(disable:4127)) \
174 TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ## __VA_ARGS__) \
175 __pragma(warning(pop))
178#define TC_LOG_TRACE(filterType__, message__, ...) \
179 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_TRACE, message__, ## __VA_ARGS__)
181#define TC_LOG_DEBUG(filterType__, message__, ...) \
182 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_DEBUG, message__, ## __VA_ARGS__)
184#define TC_LOG_INFO(filterType__, message__, ...) \
185 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_INFO, message__, ## __VA_ARGS__)
187#define TC_LOG_WARN(filterType__, message__, ...) \
188 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_WARN, message__, ## __VA_ARGS__)
190#define TC_LOG_ERROR(filterType__, message__, ...) \
191 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_ERROR, message__, ## __VA_ARGS__)
193#define TC_LOG_FATAL(filterType__, message__, ...) \
194 TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_FATAL, message__, ## __VA_ARGS__)
Appender * CreateAppender(uint8 id, std::string name, LogLevel level, AppenderFlags flags, std::vector< std::string_view > const &extraArgs)
Appender *(* AppenderCreatorFn)(uint8 id, std::string name, LogLevel level, AppenderFlags flags, std::vector< std::string_view > const &extraArgs)
std::unordered_map< std::string_view, std::unique_ptr< Logger > > loggers
Trinity::Asio::Strand * _strand
std::string const & GetLogsDir() const
void OutCommand(uint32 account, Trinity::FormatString< Args... > fmt, Args &&... args) noexcept
std::unordered_map< uint8, std::unique_ptr< Appender > > appenders
std::string m_logsTimestamp
Trinity::Asio::IoContext * _ioContext
static constexpr std::string_view make_string_view(StringOrStringView const &stringOrStringView)
Log & operator=(Log const &)=delete
Log & operator=(Log &&)=delete
static consteval std::string_view make_string_view(char const(&chars)[CharArraySize])
void OutMessageTo(Logger const *logger, std::string_view filter, LogLevel level, Trinity::FormatString< Args... > fmt, Args &&... args) noexcept
std::string const & GetLogsTimestamp() const
std::unordered_map< uint8, AppenderCreatorFn > appenderFactory
static consteval Trinity::FormatStringView make_format_string_view(char const(&chars)[CharArraySize])
fmt::format_args FormatArgs
fmt::format_string< Args... > FormatString
constexpr auto MakeFormatArgs(Args &&... args)
fmt::string_view FormatStringView