1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-25 22:17:45 +00:00

Missing log-folder will now be created atomatically

This commit is contained in:
Mordred 2018-11-02 21:33:25 +01:00
parent 2a6246f490
commit c00b27712b
2 changed files with 67 additions and 58 deletions

View file

@ -6,67 +6,76 @@
#include <spdlog/sinks/daily_file_sink.h> #include <spdlog/sinks/daily_file_sink.h>
// #include <iostream> // #include <iostream>
#include <experimental/filesystem> // or #include <filesystem>
namespace fs = std::experimental::filesystem;
namespace Core namespace Core
{ {
Logger::Logger()
{
Logger::Logger() }
{
Logger::~Logger()
} {
Logger::~Logger() }
{
void Logger::setLogPath( const std::string& logPath )
} {
auto pos = logPath.find_last_of( '/' );
void Logger::setLogPath( const std::string& logPath )
{ if( pos != std::string::npos )
m_logFile = logPath; {
} std::string realPath = logPath.substr( 0, pos );
fs::create_directories( realPath );
void Logger::init() }
{
spdlog::init_thread_pool( 8192, 1 ); m_logFile = logPath;
}
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 ); void Logger::init()
{
std::vector<spdlog::sink_ptr> sinks { stdout_sink, daily_sink }; spdlog::init_thread_pool( 8192, 1 );
auto logger = std::make_shared< spdlog::async_logger >( "logger", sinks.begin(), sinks.end(), auto stdout_sink = std::make_shared< spdlog::sinks::stdout_color_sink_mt >();
spdlog::thread_pool(), spdlog::async_overflow_policy::block ); 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 };
spdlog::register_logger( logger );
spdlog::set_pattern( "[%H:%M:%S.%e] [%^%l%$] %v" ); auto logger = std::make_shared< spdlog::async_logger >( "logger", sinks.begin(), sinks.end(),
spdlog::set_level( spdlog::level::debug ); spdlog::thread_pool(), spdlog::async_overflow_policy::block );
// 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::register_logger( logger );
spdlog::flush_on( spdlog::level::critical ); 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
void Logger::error( const std::string& text ) // 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::get( "logger" )->error( text ); spdlog::flush_on( spdlog::level::critical );
} }
void Logger::info( const std::string& text ) void Logger::error( const std::string& text )
{ {
spdlog::get( "logger" )->info( text ); spdlog::get( "logger" )->error( text );
} }
void Logger::debug( const std::string& text ) void Logger::info( const std::string& text )
{ {
spdlog::get( "logger" )->debug( text ); spdlog::get( "logger" )->info( text );
} }
void Logger::fatal( const std::string& text ) void Logger::debug( const std::string& text )
{ {
spdlog::get( "logger" )->critical( text ); spdlog::get( "logger" )->debug( text );
} }
void Logger::fatal( const std::string& text )
{
spdlog::get( "logger" )->critical( text );
}
} }

View file

@ -29,7 +29,7 @@ bool setupFramework()
auto pDebugCom = std::make_shared< DebugCommandHandler >(); auto pDebugCom = std::make_shared< DebugCommandHandler >();
auto pConfig = std::make_shared< ConfigMgr >(); auto pConfig = std::make_shared< ConfigMgr >();
pLogger->setLogPath( "log/SapphireZone_" ); pLogger->setLogPath( "log/SapphireZone" );
pLogger->init(); pLogger->init();
g_fw.set< ServerZone >( pServer ); g_fw.set< ServerZone >( pServer );