fix (core/logging/conf): Fix Logging for Modules (#11601)
* fix (core/logging/conf): Fix Logging for Modules Fix custom module logging and appender. Also included Logging Async Conf. * Update Log.cpp * Core/Logging: Force synchronous logging after io_service shutdown - fixes logging on worldserver shutdown https: //github.com/TrinityCore/TrinityCore/commit/c71987b1a1403fa20654cc24b37448ca807ff363 Co-Authored-By: Shauren <shauren.trinity@gmail.com> Co-authored-by: Shauren <shauren.trinity@gmail.com>
This commit is contained in:
@@ -194,6 +194,8 @@ void AppenderConsole::_write(LogMessage const* message)
|
|||||||
case LOG_LEVEL_FATAL:
|
case LOG_LEVEL_FATAL:
|
||||||
index = 0;
|
index = 0;
|
||||||
break;
|
break;
|
||||||
|
case LOG_LEVEL_ERROR:
|
||||||
|
[[fallthrough]];
|
||||||
default:
|
default:
|
||||||
index = 1;
|
index = 1;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -20,9 +20,11 @@
|
|||||||
#include "AppenderFile.h"
|
#include "AppenderFile.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Errors.h"
|
#include "Errors.h"
|
||||||
|
#include "IoContext.h"
|
||||||
#include "LogMessage.h"
|
#include "LogMessage.h"
|
||||||
#include "LogOperation.h"
|
#include "LogOperation.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
#include "Strand.h"
|
||||||
#include "StringConvert.h"
|
#include "StringConvert.h"
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
#include "Tokenize.h"
|
#include "Tokenize.h"
|
||||||
@@ -38,6 +40,7 @@ Log::Log() : AppenderId(0), highestLogLevel(LOG_LEVEL_FATAL)
|
|||||||
|
|
||||||
Log::~Log()
|
Log::~Log()
|
||||||
{
|
{
|
||||||
|
delete _strand;
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,7 +243,13 @@ void Log::write(std::unique_ptr<LogMessage>&& msg) const
|
|||||||
{
|
{
|
||||||
Logger const* logger = GetLoggerByType(msg->type);
|
Logger const* logger = GetLoggerByType(msg->type);
|
||||||
|
|
||||||
logger->write(msg.get());
|
if (_ioContext)
|
||||||
|
{
|
||||||
|
std::shared_ptr<LogOperation> logOperation = std::make_shared<LogOperation>(logger, std::move(msg));
|
||||||
|
Acore::Asio::post(*_ioContext, Acore::Asio::bind_executor(*_strand, [logOperation]() { logOperation->call(); }));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
logger->write(msg.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger const* Log::GetLoggerByType(std::string const& type) const
|
Logger const* Log::GetLoggerByType(std::string const& type) const
|
||||||
@@ -356,11 +365,24 @@ Log* Log::instance()
|
|||||||
return &instance;
|
return &instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::Initialize()
|
void Log::Initialize(Acore::Asio::IoContext* ioContext)
|
||||||
{
|
{
|
||||||
|
if (ioContext)
|
||||||
|
{
|
||||||
|
_ioContext = ioContext;
|
||||||
|
_strand = new Acore::Asio::Strand(*ioContext);
|
||||||
|
}
|
||||||
|
|
||||||
LoadFromConfig();
|
LoadFromConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Log::SetSynchronous()
|
||||||
|
{
|
||||||
|
delete _strand;
|
||||||
|
_strand = nullptr;
|
||||||
|
_ioContext = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
void Log::LoadFromConfig()
|
void Log::LoadFromConfig()
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
|
|||||||
@@ -29,6 +29,12 @@ class Appender;
|
|||||||
class Logger;
|
class Logger;
|
||||||
struct LogMessage;
|
struct LogMessage;
|
||||||
|
|
||||||
|
namespace Acore::Asio
|
||||||
|
{
|
||||||
|
class IoContext;
|
||||||
|
class Strand;
|
||||||
|
}
|
||||||
|
|
||||||
#define LOGGER_ROOT "root"
|
#define LOGGER_ROOT "root"
|
||||||
|
|
||||||
typedef Appender*(*AppenderCreatorFn)(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<std::string_view> const& extraArgs);
|
typedef Appender*(*AppenderCreatorFn)(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<std::string_view> const& extraArgs);
|
||||||
@@ -54,7 +60,8 @@ private:
|
|||||||
public:
|
public:
|
||||||
static Log* instance();
|
static Log* instance();
|
||||||
|
|
||||||
void Initialize();
|
void Initialize(Acore::Asio::IoContext* ioContext);
|
||||||
|
void SetSynchronous(); // Not threadsafe - should only be called from main() after all threads are joined
|
||||||
void LoadFromConfig();
|
void LoadFromConfig();
|
||||||
void Close();
|
void Close();
|
||||||
[[nodiscard]] bool ShouldLog(std::string const& type, LogLevel level) const;
|
[[nodiscard]] bool ShouldLog(std::string const& type, LogLevel level) const;
|
||||||
@@ -112,6 +119,8 @@ private:
|
|||||||
std::string m_logsDir;
|
std::string m_logsDir;
|
||||||
std::string m_logsTimestamp;
|
std::string m_logsTimestamp;
|
||||||
|
|
||||||
|
Acore::Asio::IoContext* _ioContext;
|
||||||
|
Acore::Asio::Strand* _strand;
|
||||||
// Deprecated debug filter logs
|
// Deprecated debug filter logs
|
||||||
DebugLogFilters _debugLogMask;
|
DebugLogFilters _debugLogMask;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,13 +23,13 @@
|
|||||||
// EnumUtils: DESCRIBE THIS
|
// EnumUtils: DESCRIBE THIS
|
||||||
enum LogLevel : uint8
|
enum LogLevel : uint8
|
||||||
{
|
{
|
||||||
LOG_LEVEL_DISABLED,
|
LOG_LEVEL_DISABLED = 0,
|
||||||
LOG_LEVEL_FATAL,
|
LOG_LEVEL_FATAL = 1,
|
||||||
LOG_LEVEL_ERROR,
|
LOG_LEVEL_ERROR = 2,
|
||||||
LOG_LEVEL_WARN,
|
LOG_LEVEL_WARN = 3,
|
||||||
LOG_LEVEL_INFO,
|
LOG_LEVEL_INFO = 4,
|
||||||
LOG_LEVEL_DEBUG,
|
LOG_LEVEL_DEBUG = 5,
|
||||||
LOG_LEVEL_TRACE,
|
LOG_LEVEL_TRACE = 6,
|
||||||
|
|
||||||
NUM_ENABLED_LOG_LEVELS = LOG_LEVEL_TRACE, // SKIP
|
NUM_ENABLED_LOG_LEVELS = LOG_LEVEL_TRACE, // SKIP
|
||||||
LOG_LEVEL_INVALID = 0xFF // SKIP
|
LOG_LEVEL_INVALID = 0xFF // SKIP
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
// Init logging
|
// Init logging
|
||||||
sLog->RegisterAppender<AppenderDB>();
|
sLog->RegisterAppender<AppenderDB>();
|
||||||
sLog->Initialize();
|
sLog->Initialize(nullptr);
|
||||||
|
|
||||||
Acore::Banner::Show("authserver",
|
Acore::Banner::Show("authserver",
|
||||||
[](std::string_view text)
|
[](std::string_view text)
|
||||||
|
|||||||
@@ -199,7 +199,8 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
// Init all logs
|
// Init all logs
|
||||||
sLog->RegisterAppender<AppenderDB>();
|
sLog->RegisterAppender<AppenderDB>();
|
||||||
sLog->Initialize();
|
// If logs are supposed to be handled async then we need to pass the IoContext into the Log singleton
|
||||||
|
sLog->Initialize(sConfigMgr->GetOption<bool>("Log.Async.Enable", false) ? ioContext.get() : nullptr);
|
||||||
|
|
||||||
Acore::Banner::Show("worldserver-daemon",
|
Acore::Banner::Show("worldserver-daemon",
|
||||||
[](std::string_view text)
|
[](std::string_view text)
|
||||||
@@ -421,6 +422,8 @@ int main(int argc, char** argv)
|
|||||||
// Shutdown starts here
|
// Shutdown starts here
|
||||||
threadPool.reset();
|
threadPool.reset();
|
||||||
|
|
||||||
|
sLog->SetSynchronous();
|
||||||
|
|
||||||
sScriptMgr->OnShutdown();
|
sScriptMgr->OnShutdown();
|
||||||
|
|
||||||
// set server offline
|
// set server offline
|
||||||
|
|||||||
@@ -3914,6 +3914,16 @@ Logger.module=4,Console Server
|
|||||||
#Logger.vehicles=4,Console Server
|
#Logger.vehicles=4,Console Server
|
||||||
#Logger.warden=4,Console Server
|
#Logger.warden=4,Console Server
|
||||||
#Logger.weather=4,Console Server
|
#Logger.weather=4,Console Server
|
||||||
|
|
||||||
|
#
|
||||||
|
# Log.Async.Enable
|
||||||
|
# Description: Enables asynchronous message logging.
|
||||||
|
# Default: 0 - (Disabled)
|
||||||
|
# 1 - (Enabled)
|
||||||
|
|
||||||
|
Log.Async.Enable = 0
|
||||||
|
|
||||||
|
#
|
||||||
###################################################################################################
|
###################################################################################################
|
||||||
|
|
||||||
###################################################################################################
|
###################################################################################################
|
||||||
|
|||||||
Reference in New Issue
Block a user