Правка класса для журналирования
This commit is contained in:
parent
4a823ee85c
commit
746777a822
@ -5,13 +5,7 @@ namespace myx {
|
|||||||
|
|
||||||
namespace log {
|
namespace log {
|
||||||
|
|
||||||
static Logger defaultLogger( "default" );
|
Logger defaultLogger( "default" );
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr< spdlog::logger > default_logger()
|
|
||||||
{
|
|
||||||
return( static_cast< std::shared_ptr< spdlog::logger > >( defaultLogger.logger() ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
spdlog::level::level_enum Logger::outputLevel() const
|
spdlog::level::level_enum Logger::outputLevel() const
|
||||||
@ -38,9 +32,22 @@ void Logger::setFlushLevel( const spdlog::level::level_enum& flushLevel )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string Logger::outputPattern() const
|
||||||
|
{
|
||||||
|
return( m_outputPattern );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Logger::setOutputPattern( const std::string& outputPattern )
|
||||||
|
{
|
||||||
|
m_outputPattern = outputPattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Logger::Logger( const std::string& name ) :
|
Logger::Logger( const std::string& name ) :
|
||||||
|
m_logger( nullptr ),
|
||||||
m_name( name ),
|
m_name( name ),
|
||||||
m_logger( name, nullptr ),
|
m_outputPattern( "[%H:%M:%S %z] [%n] [%^---%L---%$] %v" ),
|
||||||
m_baseFileName( fmt::format( "{}_{}", PROJECT_NAME, "st" ) ),
|
m_baseFileName( fmt::format( "{}_{}", PROJECT_NAME, "st" ) ),
|
||||||
m_maxRotatingFileSize( 256 * 1024 ),
|
m_maxRotatingFileSize( 256 * 1024 ),
|
||||||
m_maxRotatingFilesCount( 3 ),
|
m_maxRotatingFilesCount( 3 ),
|
||||||
@ -54,16 +61,17 @@ Logger::Logger( const std::string& name ) :
|
|||||||
m_rotatingFileSink( nullptr ),
|
m_rotatingFileSink( nullptr ),
|
||||||
m_dailyFileSink( nullptr )
|
m_dailyFileSink( nullptr )
|
||||||
{
|
{
|
||||||
m_logger.flush_on( m_flushLevel );
|
m_logger = std::make_shared< spdlog::logger >( m_name, nullptr );
|
||||||
m_logger.set_level( m_outputLevel );
|
m_logger->sinks().clear();
|
||||||
spdlog::register_logger( logger() );
|
m_logger->flush_on( m_flushLevel );
|
||||||
spdlog::set_default_logger( logger() );
|
m_logger->set_level( m_outputLevel );
|
||||||
|
m_logger->set_pattern( m_outputPattern );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr< spdlog::logger > Logger::logger()
|
std::shared_ptr< spdlog::logger > Logger::logger()
|
||||||
{
|
{
|
||||||
return( static_cast< std::shared_ptr< spdlog::logger > >( &m_logger ) );
|
return( m_logger );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -96,7 +104,7 @@ void Logger::enableNullSink()
|
|||||||
if ( m_nullSink == nullptr )
|
if ( m_nullSink == nullptr )
|
||||||
{
|
{
|
||||||
m_nullSink = std::make_shared< spdlog::sinks::null_sink_st >();
|
m_nullSink = std::make_shared< spdlog::sinks::null_sink_st >();
|
||||||
m_logger.sinks().push_back( m_nullSink );
|
m_logger->sinks().push_back( m_nullSink );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,8 +113,8 @@ void Logger::disableNullSink()
|
|||||||
{
|
{
|
||||||
if ( m_nullSink != nullptr )
|
if ( m_nullSink != nullptr )
|
||||||
{
|
{
|
||||||
m_logger.sinks().erase( std::remove( m_logger.sinks().begin(), m_logger.sinks().end(), m_nullSink ),
|
m_logger->sinks().erase( std::remove( m_logger->sinks().begin(), m_logger->sinks().end(), m_nullSink ),
|
||||||
m_logger.sinks().end() );
|
m_logger->sinks().end() );
|
||||||
m_nullSink.reset();
|
m_nullSink.reset();
|
||||||
m_nullSink = nullptr;
|
m_nullSink = nullptr;
|
||||||
}
|
}
|
||||||
@ -118,7 +126,7 @@ void Logger::enableStdoutSink()
|
|||||||
if ( m_stdoutSink == nullptr )
|
if ( m_stdoutSink == nullptr )
|
||||||
{
|
{
|
||||||
m_stdoutSink = std::make_shared< spdlog::sinks::ansicolor_stdout_sink_st >();
|
m_stdoutSink = std::make_shared< spdlog::sinks::ansicolor_stdout_sink_st >();
|
||||||
m_logger.sinks().push_back( m_stdoutSink );
|
m_logger->sinks().push_back( m_stdoutSink );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,8 +135,8 @@ void Logger::disableStdoutSink()
|
|||||||
{
|
{
|
||||||
if ( m_stdoutSink != nullptr )
|
if ( m_stdoutSink != nullptr )
|
||||||
{
|
{
|
||||||
m_logger.sinks().erase( std::remove( m_logger.sinks().begin(), m_logger.sinks().end(), m_stdoutSink ),
|
m_logger->sinks().erase( std::remove( m_logger->sinks().begin(), m_logger->sinks().end(), m_stdoutSink ),
|
||||||
m_logger.sinks().end() );
|
m_logger->sinks().end() );
|
||||||
m_stdoutSink.reset();
|
m_stdoutSink.reset();
|
||||||
m_stdoutSink = nullptr;
|
m_stdoutSink = nullptr;
|
||||||
}
|
}
|
||||||
@ -140,7 +148,7 @@ void Logger::enableStderrSink()
|
|||||||
if ( m_stderrSink == nullptr )
|
if ( m_stderrSink == nullptr )
|
||||||
{
|
{
|
||||||
m_stderrSink = std::make_shared< spdlog::sinks::ansicolor_stderr_sink_st >();
|
m_stderrSink = std::make_shared< spdlog::sinks::ansicolor_stderr_sink_st >();
|
||||||
m_logger.sinks().push_back( m_stderrSink );
|
m_logger->sinks().push_back( m_stderrSink );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,8 +157,8 @@ void Logger::disableStderrSink()
|
|||||||
{
|
{
|
||||||
if ( m_stderrSink != nullptr )
|
if ( m_stderrSink != nullptr )
|
||||||
{
|
{
|
||||||
m_logger.sinks().erase( std::remove( m_logger.sinks().begin(), m_logger.sinks().end(), m_stderrSink ),
|
m_logger->sinks().erase( std::remove( m_logger->sinks().begin(), m_logger->sinks().end(), m_stderrSink ),
|
||||||
m_logger.sinks().end() );
|
m_logger->sinks().end() );
|
||||||
m_stderrSink.reset();
|
m_stderrSink.reset();
|
||||||
m_stderrSink = nullptr;
|
m_stderrSink = nullptr;
|
||||||
}
|
}
|
||||||
@ -162,7 +170,7 @@ void Logger::enableSyslogSink()
|
|||||||
if ( m_syslogSink == nullptr )
|
if ( m_syslogSink == nullptr )
|
||||||
{
|
{
|
||||||
m_syslogSink = std::make_shared< spdlog::sinks::syslog_sink_st >();
|
m_syslogSink = std::make_shared< spdlog::sinks::syslog_sink_st >();
|
||||||
m_logger.sinks().push_back( m_syslogSink );
|
m_logger->sinks().push_back( m_syslogSink );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,8 +179,8 @@ void Logger::disableSyslogSink()
|
|||||||
{
|
{
|
||||||
if ( m_syslogSink != nullptr )
|
if ( m_syslogSink != nullptr )
|
||||||
{
|
{
|
||||||
m_logger.sinks().erase( std::remove( m_logger.sinks().begin(), m_logger.sinks().end(), m_syslogSink ),
|
m_logger->sinks().erase( std::remove( m_logger->sinks().begin(), m_logger->sinks().end(), m_syslogSink ),
|
||||||
m_logger.sinks().end() );
|
m_logger->sinks().end() );
|
||||||
m_syslogSink.reset();
|
m_syslogSink.reset();
|
||||||
m_syslogSink = nullptr;
|
m_syslogSink = nullptr;
|
||||||
}
|
}
|
||||||
@ -184,7 +192,7 @@ void Logger::enableBasicFileSink()
|
|||||||
if ( m_basicFileSink == nullptr )
|
if ( m_basicFileSink == nullptr )
|
||||||
{
|
{
|
||||||
m_basicFileSink = std::make_shared< spdlog::sinks::basic_file_sink_st >( m_baseFileName );
|
m_basicFileSink = std::make_shared< spdlog::sinks::basic_file_sink_st >( m_baseFileName );
|
||||||
m_logger.sinks().push_back( m_basicFileSink );
|
m_logger->sinks().push_back( m_basicFileSink );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,8 +201,8 @@ void Logger::disableBasicFileSink()
|
|||||||
{
|
{
|
||||||
if ( m_basicFileSink != nullptr )
|
if ( m_basicFileSink != nullptr )
|
||||||
{
|
{
|
||||||
m_logger.sinks().erase( std::remove( m_logger.sinks().begin(), m_logger.sinks().end(), m_basicFileSink ),
|
m_logger->sinks().erase( std::remove( m_logger->sinks().begin(), m_logger->sinks().end(), m_basicFileSink ),
|
||||||
m_logger.sinks().end() );
|
m_logger->sinks().end() );
|
||||||
m_basicFileSink.reset();
|
m_basicFileSink.reset();
|
||||||
m_basicFileSink = nullptr;
|
m_basicFileSink = nullptr;
|
||||||
}
|
}
|
||||||
@ -206,7 +214,7 @@ void Logger::enableRotatingFileSink()
|
|||||||
if ( m_rotatingFileSink == nullptr )
|
if ( m_rotatingFileSink == nullptr )
|
||||||
{
|
{
|
||||||
m_rotatingFileSink = std::make_shared< spdlog::sinks::rotating_file_sink_st >( m_baseFileName, m_maxRotatingFileSize, m_maxRotatingFilesCount );
|
m_rotatingFileSink = std::make_shared< spdlog::sinks::rotating_file_sink_st >( m_baseFileName, m_maxRotatingFileSize, m_maxRotatingFilesCount );
|
||||||
m_logger.sinks().push_back( m_rotatingFileSink );
|
m_logger->sinks().push_back( m_rotatingFileSink );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,8 +223,8 @@ void Logger::disableRotatingFileSink()
|
|||||||
{
|
{
|
||||||
if ( m_rotatingFileSink != nullptr )
|
if ( m_rotatingFileSink != nullptr )
|
||||||
{
|
{
|
||||||
m_logger.sinks().erase( std::remove( m_logger.sinks().begin(), m_logger.sinks().end(), m_rotatingFileSink ),
|
m_logger->sinks().erase( std::remove( m_logger->sinks().begin(), m_logger->sinks().end(), m_rotatingFileSink ),
|
||||||
m_logger.sinks().end() );
|
m_logger->sinks().end() );
|
||||||
m_rotatingFileSink.reset();
|
m_rotatingFileSink.reset();
|
||||||
m_rotatingFileSink = nullptr;
|
m_rotatingFileSink = nullptr;
|
||||||
}
|
}
|
||||||
@ -230,7 +238,7 @@ void Logger::enableDailyFileSink()
|
|||||||
const int rotationHour = 23;
|
const int rotationHour = 23;
|
||||||
const int rotationMinute = 59;
|
const int rotationMinute = 59;
|
||||||
m_dailyFileSink = std::make_shared< spdlog::sinks::daily_file_sink_st >( m_baseFileName, rotationHour, rotationMinute );
|
m_dailyFileSink = std::make_shared< spdlog::sinks::daily_file_sink_st >( m_baseFileName, rotationHour, rotationMinute );
|
||||||
m_logger.sinks().push_back( m_dailyFileSink );
|
m_logger->sinks().push_back( m_dailyFileSink );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,8 +247,8 @@ void Logger::disableDailyFileSink()
|
|||||||
{
|
{
|
||||||
if ( m_dailyFileSink != nullptr )
|
if ( m_dailyFileSink != nullptr )
|
||||||
{
|
{
|
||||||
m_logger.sinks().erase( std::remove( m_logger.sinks().begin(), m_logger.sinks().end(), m_dailyFileSink ),
|
m_logger->sinks().erase( std::remove( m_logger->sinks().begin(), m_logger->sinks().end(), m_dailyFileSink ),
|
||||||
m_logger.sinks().end() );
|
m_logger->sinks().end() );
|
||||||
m_dailyFileSink.reset();
|
m_dailyFileSink.reset();
|
||||||
m_dailyFileSink = nullptr;
|
m_dailyFileSink = nullptr;
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,6 @@
|
|||||||
#include <spdlog/sinks/basic_file_sink.h>
|
#include <spdlog/sinks/basic_file_sink.h>
|
||||||
#include <spdlog/sinks/rotating_file_sink.h>
|
#include <spdlog/sinks/rotating_file_sink.h>
|
||||||
#include <spdlog/sinks/daily_file_sink.h>
|
#include <spdlog/sinks/daily_file_sink.h>
|
||||||
// #include <spdlog/async.h>
|
|
||||||
// #include <spdlog/async_logger.h>
|
|
||||||
|
|
||||||
namespace myx {
|
namespace myx {
|
||||||
|
|
||||||
@ -15,8 +13,9 @@ namespace log {
|
|||||||
|
|
||||||
class Logger
|
class Logger
|
||||||
{
|
{
|
||||||
|
std::shared_ptr< spdlog::logger > m_logger;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
spdlog::logger m_logger;
|
std::string m_outputPattern;
|
||||||
std::string m_baseFileName;
|
std::string m_baseFileName;
|
||||||
std::size_t m_maxRotatingFileSize;
|
std::size_t m_maxRotatingFileSize;
|
||||||
std::size_t m_maxRotatingFilesCount;
|
std::size_t m_maxRotatingFilesCount;
|
||||||
@ -33,6 +32,7 @@ class Logger
|
|||||||
public:
|
public:
|
||||||
Logger( const std::string& name );
|
Logger( const std::string& name );
|
||||||
|
|
||||||
|
void init();
|
||||||
std::shared_ptr< spdlog::logger > logger();
|
std::shared_ptr< spdlog::logger > logger();
|
||||||
std::string name() const;
|
std::string name() const;
|
||||||
void setName( const std::string& name );
|
void setName( const std::string& name );
|
||||||
@ -57,20 +57,17 @@ public:
|
|||||||
void setOutputLevel( const spdlog::level::level_enum& outputLevel );
|
void setOutputLevel( const spdlog::level::level_enum& outputLevel );
|
||||||
spdlog::level::level_enum flushLevel() const;
|
spdlog::level::level_enum flushLevel() const;
|
||||||
void setFlushLevel( const spdlog::level::level_enum& flushLevel );
|
void setFlushLevel( const spdlog::level::level_enum& flushLevel );
|
||||||
|
std::string outputPattern() const;
|
||||||
|
void setOutputPattern( const std::string& outputPattern );
|
||||||
}; // class Logger
|
}; // class Logger
|
||||||
|
|
||||||
|
extern Logger defaultLogger;
|
||||||
/**
|
|
||||||
* @brief default_logger
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
std::shared_ptr< spdlog::logger > default_logger();
|
|
||||||
|
|
||||||
|
|
||||||
template < class F, class ... Args >
|
template < class F, class ... Args >
|
||||||
void trace( F&& format, Args&& ... args ) noexcept
|
void trace( F&& format, Args&& ... args ) noexcept
|
||||||
{
|
{
|
||||||
default_logger()->trace( std::forward< F >( format ),
|
defaultLogger.logger()->trace( std::forward< F >( format ),
|
||||||
std::forward< Args >( args )... );
|
std::forward< Args >( args )... );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +75,7 @@ void trace( F&& format, Args&& ... args ) noexcept
|
|||||||
template < class F, class ... Args >
|
template < class F, class ... Args >
|
||||||
void debug( F&& format, Args&& ... args ) noexcept
|
void debug( F&& format, Args&& ... args ) noexcept
|
||||||
{
|
{
|
||||||
default_logger()->debug( std::forward< F >( format ),
|
defaultLogger.logger()->debug( std::forward< F >( format ),
|
||||||
std::forward< Args >( args )... );
|
std::forward< Args >( args )... );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +83,7 @@ void debug( F&& format, Args&& ... args ) noexcept
|
|||||||
template < class F, class ... Args >
|
template < class F, class ... Args >
|
||||||
void info( F&& format, Args&& ... args ) noexcept
|
void info( F&& format, Args&& ... args ) noexcept
|
||||||
{
|
{
|
||||||
default_logger()->info( std::forward< F >( format ),
|
defaultLogger.logger()->info( std::forward< F >( format ),
|
||||||
std::forward< Args >( args )... );
|
std::forward< Args >( args )... );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +91,7 @@ void info( F&& format, Args&& ... args ) noexcept
|
|||||||
template < class F, class ... Args >
|
template < class F, class ... Args >
|
||||||
void warn( F&& format, Args&& ... args ) noexcept
|
void warn( F&& format, Args&& ... args ) noexcept
|
||||||
{
|
{
|
||||||
default_logger()->warn( std::forward< F >( format ),
|
defaultLogger.logger()->warn( std::forward< F >( format ),
|
||||||
std::forward< Args >( args )... );
|
std::forward< Args >( args )... );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +99,7 @@ void warn( F&& format, Args&& ... args ) noexcept
|
|||||||
template < class F, class ... Args >
|
template < class F, class ... Args >
|
||||||
void error( F&& format, Args&& ... args ) noexcept
|
void error( F&& format, Args&& ... args ) noexcept
|
||||||
{
|
{
|
||||||
default_logger()->error( std::forward< F >( format ),
|
defaultLogger.logger()->error( std::forward< F >( format ),
|
||||||
std::forward< Args >( args )... );
|
std::forward< Args >( args )... );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +107,7 @@ void error( F&& format, Args&& ... args ) noexcept
|
|||||||
template < class F, class ... Args >
|
template < class F, class ... Args >
|
||||||
void log( spdlog::level::level_enum lv, F&& format, Args&& ... args ) noexcept
|
void log( spdlog::level::level_enum lv, F&& format, Args&& ... args ) noexcept
|
||||||
{
|
{
|
||||||
default_logger()->log( lv, std::forward< F >( format ),
|
defaultLogger.logger()->log( lv, std::forward< F >( format ),
|
||||||
std::forward< Args >( args )... );
|
std::forward< Args >( args )... );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
43
src/log/spdlog_qt.cpp
Normal file
43
src/log/spdlog_qt.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include "spdlog.hpp"
|
||||||
|
#include "spdlog_qt.hpp"
|
||||||
|
|
||||||
|
#include <QCoreApplication>
|
||||||
|
|
||||||
|
namespace myx {
|
||||||
|
|
||||||
|
namespace log {
|
||||||
|
|
||||||
|
void spdlog_qt_message_handler( QtMsgType type, const QMessageLogContext& context, const QString& qMsg )
|
||||||
|
{
|
||||||
|
if ( !defaultLogger.logger() )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Q_UNUSED( context )
|
||||||
|
|
||||||
|
const char* msg = qMsg.toUtf8().constData();
|
||||||
|
|
||||||
|
switch ( type )
|
||||||
|
{
|
||||||
|
case QtDebugMsg:
|
||||||
|
defaultLogger.logger()->debug( msg );
|
||||||
|
break;
|
||||||
|
case QtInfoMsg:
|
||||||
|
defaultLogger.logger()->info( msg );
|
||||||
|
break;
|
||||||
|
case QtWarningMsg:
|
||||||
|
defaultLogger.logger()->warn( msg );
|
||||||
|
break;
|
||||||
|
case QtCriticalMsg:
|
||||||
|
defaultLogger.logger()->critical( msg );
|
||||||
|
break;
|
||||||
|
case QtFatalMsg:
|
||||||
|
defaultLogger.logger()->critical( "FATAL: %s", msg );
|
||||||
|
QCoreApplication::exit( 1 );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} // messageHandler
|
||||||
|
|
||||||
|
} // namespace log
|
||||||
|
|
||||||
|
} // namespace myx
|
18
src/log/spdlog_qt.hpp
Normal file
18
src/log/spdlog_qt.hpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef MYX_LOG_SPDLOG_QT_HPP_
|
||||||
|
#define MYX_LOG_SPDLOG_QT_HPP_
|
||||||
|
|
||||||
|
#include <QMessageLogContext>
|
||||||
|
#include <QString>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
namespace myx {
|
||||||
|
|
||||||
|
namespace log {
|
||||||
|
|
||||||
|
void spdlog_qt_message_handler( QtMsgType type, const QMessageLogContext& context, const QString& qMsg );
|
||||||
|
|
||||||
|
} // namespace log
|
||||||
|
|
||||||
|
} // namespace myx
|
||||||
|
|
||||||
|
#endif // ifndef MYX_LOG_SPDLOG_QT_HPP_
|
Loading…
Reference in New Issue
Block a user