/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
#ifndef TRINITYCORE_LOG_H
#define TRINITYCORE_LOG_H
#include "Define.h"
#include "AsioHacksFwd.h"
#include "LogCommon.h"
#include "StringFormat.h"
#include
#include
#include
class Appender;
class Logger;
struct LogMessage;
namespace Trinity
{
namespace Asio
{
class IoContext;
}
}
#define LOGGER_ROOT "root"
typedef Appender*(*AppenderCreatorFn)(uint8 id, std::string name, LogLevel level, AppenderFlags flags, std::vector const& extraArgs);
template
Appender* CreateAppender(uint8 id, std::string name, LogLevel level, AppenderFlags flags, std::vector const& extraArgs)
{
return new AppenderImpl(id, std::move(name), level, flags, extraArgs);
}
class TC_COMMON_API Log
{
private:
Log();
~Log();
Log(Log const&) = delete;
Log(Log&&) = delete;
Log& operator=(Log const&) = delete;
Log& operator=(Log&&) = delete;
public:
static Log* instance();
void Initialize(Trinity::Asio::IoContext* ioContext);
void SetSynchronous(); // Not threadsafe - should only be called from main() after all threads are joined
void LoadFromConfig();
void Close();
bool ShouldLog(std::string_view type, LogLevel level) const;
Logger const* GetEnabledLogger(std::string_view type, LogLevel level) const;
bool SetLogLevel(std::string const& name, int32 level, bool isLogger = true);
template
void OutMessage(std::string_view filter, LogLevel level, Trinity::FormatString fmt, Args&&... args)
{
this->OutMessageImpl(GetLoggerByType(filter), filter, level, fmt, Trinity::MakeFormatArgs(args...));
}
template
void OutMessageTo(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatString fmt, Args&&... args)
{
this->OutMessageImpl(logger, filter, level, fmt, Trinity::MakeFormatArgs(args...));
}
template
void OutCommand(uint32 account, Trinity::FormatString fmt, Args&&... args)
{
if (!ShouldLog("commands.gm", LOG_LEVEL_INFO))
return;
this->OutCommandImpl(account, fmt, Trinity::MakeFormatArgs(args...));
}
void OutCharDump(std::string const& str, uint32 account_id, uint64 guid, std::string const& name) const;
void SetRealmId(uint32 id);
template
void RegisterAppender()
{
this->RegisterAppender(AppenderImpl::type, &CreateAppender);
}
std::string const& GetLogsDir() const { return m_logsDir; }
std::string const& GetLogsTimestamp() const { return m_logsTimestamp; }
void CreateAppenderFromConfigLine(std::string const& name, std::string const& options);
void CreateLoggerFromConfigLine(std::string const& name, std::string const& options);
template
static constexpr std::string_view make_string_view(StringOrStringView const& stringOrStringView)
{
return stringOrStringView;
}
template
static consteval std::string_view make_string_view(char const(&chars)[CharArraySize])
{
return { std::begin(chars), (chars[CharArraySize - 1] == '\0' ? CharArraySize - 1 : CharArraySize) };
}
template
static consteval Trinity::FormatStringView make_format_string_view(char const(&chars)[CharArraySize])
{
return { std::begin(chars), (chars[CharArraySize - 1] == '\0' ? CharArraySize - 1 : CharArraySize) };
}
private:
static std::string GetTimestampStr();
Logger const* GetLoggerByType(std::string_view type) const;
Appender* GetAppenderByName(std::string_view name);
uint8 NextAppenderId();
void CreateAppenderFromConfig(std::string const& name);
void CreateLoggerFromConfig(std::string const& name);
void ReadAppendersFromConfig();
void ReadLoggersFromConfig();
void RegisterAppender(uint8 index, AppenderCreatorFn appenderCreateFn);
void OutMessageImpl(Logger const* logger, std::string_view filter, LogLevel level, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const;
void OutCommandImpl(uint32 account, Trinity::FormatStringView messageFormat, Trinity::FormatArgs messageFormatArgs) const;
std::unordered_map appenderFactory;
std::unordered_map> appenders;
std::unordered_map> loggers;
uint8 AppenderId;
LogLevel lowestLogLevel;
std::string m_logsDir;
std::string m_logsTimestamp;
Trinity::Asio::IoContext* _ioContext;
Trinity::Asio::Strand* _strand;
};
#define sLog Log::instance()
#define TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ...) \
do { \
Log* logInstance = sLog; \
if (Logger const* loggerInstance = logInstance->GetEnabledLogger(Log::make_string_view((filterType__)), (level__))) \
logInstance->OutMessageTo(loggerInstance, Log::make_string_view((filterType__)), (level__), \
Log::make_format_string_view((message__)), ## __VA_ARGS__); \
} while (0)
#ifdef PERFORMANCE_PROFILING
#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) ((void)0)
#elif TRINITY_PLATFORM != TRINITY_PLATFORM_WINDOWS
#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ## __VA_ARGS__)
#else
#define TC_LOG_MESSAGE_BODY(filterType__, level__, message__, ...) \
__pragma(warning(push)) \
__pragma(warning(disable:4127)) \
TC_LOG_MESSAGE_BODY_CORE(filterType__, level__, message__, ## __VA_ARGS__) \
__pragma(warning(pop))
#endif
#define TC_LOG_TRACE(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_TRACE, message__, ## __VA_ARGS__)
#define TC_LOG_DEBUG(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_DEBUG, message__, ## __VA_ARGS__)
#define TC_LOG_INFO(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_INFO, message__, ## __VA_ARGS__)
#define TC_LOG_WARN(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_WARN, message__, ## __VA_ARGS__)
#define TC_LOG_ERROR(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_ERROR, message__, ## __VA_ARGS__)
#define TC_LOG_FATAL(filterType__, message__, ...) \
TC_LOG_MESSAGE_BODY(filterType__, LOG_LEVEL_FATAL, message__, ## __VA_ARGS__)
#endif