1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 22:37:45 +00:00
sapphire/src/common/Logging/Logger.cpp

87 lines
2.2 KiB
C++
Raw Normal View History

2017-08-08 13:53:47 +02:00
#include "Logger.h"
2018-10-26 20:06:12 +11:00
#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/daily_file_sink.h>
// #include <iostream>
#include <experimental/filesystem> // or #include <filesystem>
namespace fs = std::experimental::filesystem;
2017-08-08 13:53:47 +02:00
namespace Sapphire
2018-10-27 22:51:16 +02:00
{
2017-08-08 13:53:47 +02:00
Logger::Logger()
{
2017-08-08 13:53:47 +02:00
}
Logger::~Logger()
{
}
2018-12-23 03:53:08 +01:00
void Logger::init( const std::string& logPath )
{
auto pos = logPath.find_last_of( fs::path::preferred_separator );
if( pos != std::string::npos )
{
std::string realPath = logPath.substr( 0, pos );
fs::create_directories( realPath );
}
spdlog::init_thread_pool( 8192, 1 );
auto stdout_sink = std::make_shared< spdlog::sinks::stdout_color_sink_mt >();
2018-12-23 03:53:08 +01:00
auto daily_sink = std::make_shared< spdlog::sinks::daily_file_sink_mt >( logPath + ".log", 0, 0 );
2018-10-27 00:11:29 +02:00
std::vector< spdlog::sink_ptr > sinks { stdout_sink, daily_sink };
2018-10-27 00:11:29 +02:00
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::warn( const std::string& text )
{
spdlog::get( "logger" )->warn( 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 );
}
2017-08-08 13:53:47 +02:00
void Logger::fatal( const std::string& text )
{
spdlog::get( "logger" )->critical( text );
}
2017-08-08 13:53:47 +02:00
void Logger::trace( const std::string& text )
{
spdlog::get( "logger" )->trace( text );
}
2018-10-25 14:47:06 +02:00
}