From c00b27712be75fa59bb51c6adc716b9c2c60d320 Mon Sep 17 00:00:00 2001 From: Mordred Date: Fri, 2 Nov 2018 21:33:25 +0100 Subject: [PATCH] Missing log-folder will now be created atomatically --- src/common/Logging/Logger.cpp | 123 ++++++++++--------- src/servers/sapphire_zone/mainGameServer.cpp | 2 +- 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/common/Logging/Logger.cpp b/src/common/Logging/Logger.cpp index 96225ebd..22534330 100644 --- a/src/common/Logging/Logger.cpp +++ b/src/common/Logging/Logger.cpp @@ -6,67 +6,76 @@ #include // #include +#include // or #include + +namespace fs = std::experimental::filesystem; namespace Core { + Logger::Logger() + { -Logger::Logger() -{ - -} - -Logger::~Logger() -{ - -} - -void Logger::setLogPath( const std::string& logPath ) -{ - m_logFile = logPath; -} - -void Logger::init() -{ - spdlog::init_thread_pool( 8192, 1 ); - - auto stdout_sink = std::make_shared< spdlog::sinks::stdout_color_sink_mt >(); - auto daily_sink = std::make_shared< spdlog::sinks::daily_file_sink_mt >( m_logFile + ".log", 0, 0 ); - - std::vector sinks { stdout_sink, daily_sink }; - - auto logger = std::make_shared< spdlog::async_logger >( "logger", sinks.begin(), sinks.end(), - spdlog::thread_pool(), spdlog::async_overflow_policy::block ); - - - spdlog::register_logger( logger ); - spdlog::set_pattern( "[%H:%M:%S.%e] [%^%l%$] %v" ); - spdlog::set_level( spdlog::level::debug ); - // always flush the log on criticial messages, otherwise it's done by libc - // see: https://github.com/gabime/spdlog/wiki/7.-Flush-policy - // nb: if the server crashes, log data can be missing from the file unless something logs critical just before it does - spdlog::flush_on( spdlog::level::critical ); -} - -void Logger::error( const std::string& text ) -{ - spdlog::get( "logger" )->error( text ); -} - -void Logger::info( const std::string& text ) -{ - spdlog::get( "logger" )->info( text ); -} - -void Logger::debug( const std::string& text ) -{ - spdlog::get( "logger" )->debug( text ); -} - -void Logger::fatal( const std::string& text ) -{ - spdlog::get( "logger" )->critical( text ); -} - + } + + Logger::~Logger() + { + + } + + void Logger::setLogPath( const std::string& logPath ) + { + auto pos = logPath.find_last_of( '/' ); + + if( pos != std::string::npos ) + { + std::string realPath = logPath.substr( 0, pos ); + fs::create_directories( realPath ); + } + + m_logFile = logPath; + } + + void Logger::init() + { + spdlog::init_thread_pool( 8192, 1 ); + + auto stdout_sink = std::make_shared< spdlog::sinks::stdout_color_sink_mt >(); + auto daily_sink = std::make_shared< spdlog::sinks::daily_file_sink_mt >( m_logFile + ".log", 0, 0 ); + + std::vector< spdlog::sink_ptr > sinks { stdout_sink, daily_sink }; + + auto logger = std::make_shared< spdlog::async_logger >( "logger", sinks.begin(), sinks.end(), + spdlog::thread_pool(), spdlog::async_overflow_policy::block ); + + + spdlog::register_logger( logger ); + spdlog::set_pattern( "[%H:%M:%S.%e] [%^%l%$] %v" ); + spdlog::set_level( spdlog::level::debug ); + // always flush the log on criticial messages, otherwise it's done by libc + // see: https://github.com/gabime/spdlog/wiki/7.-Flush-policy + // nb: if the server crashes, log data can be missing from the file unless something logs critical just before it does + spdlog::flush_on( spdlog::level::critical ); + } + + void Logger::error( const std::string& text ) + { + spdlog::get( "logger" )->error( text ); + } + + void Logger::info( const std::string& text ) + { + spdlog::get( "logger" )->info( text ); + } + + void Logger::debug( const std::string& text ) + { + spdlog::get( "logger" )->debug( text ); + } + + void Logger::fatal( const std::string& text ) + { + spdlog::get( "logger" )->critical( text ); + } } diff --git a/src/servers/sapphire_zone/mainGameServer.cpp b/src/servers/sapphire_zone/mainGameServer.cpp index b45e71b0..f9a07c4a 100644 --- a/src/servers/sapphire_zone/mainGameServer.cpp +++ b/src/servers/sapphire_zone/mainGameServer.cpp @@ -29,7 +29,7 @@ bool setupFramework() auto pDebugCom = std::make_shared< DebugCommandHandler >(); auto pConfig = std::make_shared< ConfigMgr >(); - pLogger->setLogPath( "log/SapphireZone_" ); + pLogger->setLogPath( "log/SapphireZone" ); pLogger->init(); g_fw.set< ServerZone >( pServer );