Чистка кода

This commit is contained in:
Andrei Astafev 2019-10-04 15:33:49 +03:00
parent aaad6c2d60
commit ee11f4b875
11 changed files with 91 additions and 73 deletions

View File

@ -19,6 +19,9 @@ add_clazy_check(${current_target} ${current_target_sources})
add_uncrustify_format(${current_target} ${current_target_sources}) add_uncrustify_format(${current_target} ${current_target_sources})
add_pvs_check(${current_target}) add_pvs_check(${current_target})
add_dependencies(${current_target} base)
add_dependencies(${current_target} filesystem)
# Qt5 # Qt5
# qt_translation(TARGET ${current_target} TS_DIR ${CMAKE_SOURCE_DIR}/l10n LANGUAGES ru_RU) # qt_translation(TARGET ${current_target} TS_DIR ${CMAKE_SOURCE_DIR}/l10n LANGUAGES ru_RU)
target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src) target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src)
@ -27,7 +30,7 @@ target_include_directories(${current_target} SYSTEM PUBLIC ${SPDLOG_INCLUDE_DIRS
target_include_directories(${current_target} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS}) target_include_directories(${current_target} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_compile_options(${current_target} PUBLIC "${Qt5Core_EXECUTABLE_COMPILE_FLAGS}") target_compile_options(${current_target} PUBLIC "${Qt5Core_EXECUTABLE_COMPILE_FLAGS}")
target_link_libraries(${current_target} myx-filesystem_static) target_link_libraries(${current_target} myx-filesystem)
target_link_libraries(${current_target} Qt5::Core) target_link_libraries(${current_target} Qt5::Core)
# Имя выходного файла для цели # Имя выходного файла для цели

View File

@ -19,13 +19,16 @@ add_clazy_check(${current_target} ${current_target_sources})
add_uncrustify_format(${current_target} ${current_target_sources}) add_uncrustify_format(${current_target} ${current_target_sources})
add_pvs_check(${current_target}) add_pvs_check(${current_target})
add_dependencies(${current_target} base)
add_dependencies(${current_target} log)
# Qt5 # Qt5
# qt_translation(TARGET ${current_target} TS_DIR ${CMAKE_SOURCE_DIR}/l10n LANGUAGES ru_RU) # qt_translation(TARGET ${current_target} TS_DIR ${CMAKE_SOURCE_DIR}/l10n LANGUAGES ru_RU)
target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src) target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(${current_target} SYSTEM PUBLIC ${FMT_INCLUDE_DIRS}) target_include_directories(${current_target} SYSTEM PUBLIC ${FMT_INCLUDE_DIRS})
target_include_directories(${current_target} SYSTEM PUBLIC ${SPDLOG_INCLUDE_DIRS}) target_include_directories(${current_target} SYSTEM PUBLIC ${SPDLOG_INCLUDE_DIRS})
target_link_libraries(${current_target} myx-log_static) target_link_libraries(${current_target} myx-log)
# Имя выходного файла для цели # Имя выходного файла для цели
set_target_properties(${current_target} set_target_properties(${current_target}

View File

@ -5,17 +5,19 @@ namespace ML = myx::log;
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
ML::defaultLogger.enableStdoutSink(); (void)argc;
(void)argv;
ML::default_logger.enableStdoutSink();
spdlog::register_logger( ML::defaultLogger.logger() ); spdlog::register_logger( ML::default_logger.logger() );
spdlog::set_default_logger( ML::defaultLogger.logger() ); spdlog::set_default_logger( ML::default_logger.logger() );
ML::info( "aaaa" ); ML::info( "info" );
ML::trace( "bbbb" ); ML::trace( "trace" );
spdlog::set_pattern( "[%H:%M:%S %z] %v" ); spdlog::set_pattern( "[%H:%M:%S %z] %v" );
ML::info( "aaaa 111" ); ML::info( "new info" );
ML::trace( "bbbb 111" ); ML::trace( "new trace" );
return( 0 ); return( 0 );
} }

View File

@ -19,6 +19,9 @@ add_clazy_check(${current_target} ${current_target_sources})
add_uncrustify_format(${current_target} ${current_target_sources}) add_uncrustify_format(${current_target} ${current_target_sources})
add_pvs_check(${current_target}) add_pvs_check(${current_target})
add_dependencies(${current_target} base)
add_dependencies(${current_target} log)
# Qt5 # Qt5
# qt_translation(TARGET ${current_target} TS_DIR ${CMAKE_SOURCE_DIR}/l10n LANGUAGES ru_RU) # qt_translation(TARGET ${current_target} TS_DIR ${CMAKE_SOURCE_DIR}/l10n LANGUAGES ru_RU)
target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src) target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src)
@ -27,7 +30,7 @@ target_include_directories(${current_target} SYSTEM PUBLIC ${SPDLOG_INCLUDE_DIRS
target_include_directories(${current_target} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS}) target_include_directories(${current_target} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_compile_options(${current_target} PUBLIC "${Qt5Core_EXECUTABLE_COMPILE_FLAGS}") target_compile_options(${current_target} PUBLIC "${Qt5Core_EXECUTABLE_COMPILE_FLAGS}")
target_link_libraries(${current_target} myx-log_static) target_link_libraries(${current_target} myx-log)
target_link_libraries(${current_target} Qt5::Core) target_link_libraries(${current_target} Qt5::Core)
# Имя выходного файла для цели # Имя выходного файла для цели

View File

@ -6,19 +6,21 @@ namespace ML = myx::log;
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
ML::defaultLogger.enableStdoutSink(); (void)argc;
(void)argv;
ML::default_logger.enableStdoutSink();
spdlog::register_logger( ML::defaultLogger.logger() ); spdlog::register_logger( ML::default_logger.logger() );
spdlog::set_default_logger( ML::defaultLogger.logger() ); spdlog::set_default_logger( ML::default_logger.logger() );
qInstallMessageHandler( ML::spdlog_qt_message_handler ); qInstallMessageHandler( ML::spdlog_qt_message_handler );
qInfo() << "aaaa"; qInfo() << "info";
qDebug() << "bbbb"; qDebug() << "debug";
spdlog::set_pattern( "[%H:%M:%S %z] %v" ); spdlog::set_pattern( "[%H:%M:%S %z] %v" );
qInfo( "aaaa 111" ); qInfo( "new info" );
qDebug( "bbbb 111" ); qDebug( "new debug" );
return( 0 ); return( 0 );
} }

View File

@ -125,18 +125,19 @@ void Paths::setConfigFile( const QString& configFile )
bool Paths::updatePaths() bool Paths::updatePaths()
{ {
char* path = nullptr; char* path = nullptr;
int length, dirname_length; int fullPathLength;
int dirNameLength;
length = wai_getExecutablePath( nullptr, 0, &dirname_length ); fullPathLength = wai_getExecutablePath( nullptr, 0, &dirNameLength );
if ( length > 0 ) if ( fullPathLength > 0 )
{ {
path = static_cast< char* >( malloc( static_cast< size_t >( length ) + 1 ) ); path = new( std::nothrow ) char[static_cast< size_t >( fullPathLength ) + 1 ];
if ( !path ) if ( path == nullptr )
{ {
return( false ); return( false );
} }
wai_getExecutablePath( path, length, &dirname_length ); wai_getExecutablePath( path, fullPathLength, &dirNameLength );
path[length] = '\0'; path[fullPathLength] = '\0';
} }
else else
{ {
@ -144,9 +145,9 @@ bool Paths::updatePaths()
} }
m_executableFile = QFile( path ); m_executableFile = QFile( path );
path[dirname_length] = '\0'; path[dirNameLength] = '\0';
m_binaryDirectory = path; m_binaryDirectory = path;
free( path ); delete [] path;
if ( m_binaryDirectory.absolutePath().endsWith( "/bin" ) ) if ( m_binaryDirectory.absolutePath().endsWith( "/bin" ) )
{ {

View File

@ -42,7 +42,7 @@ public:
bool updatePaths(); bool updatePaths();
bool makePaths(); bool makePaths();
QString findConfigFile( const QString& defaultConfigFile = "" ); QString findConfigFile( const QString& defaultConfigFile = QLatin1String("") );
}; // class Paths }; // class Paths
} // namespace filesystem } // namespace filesystem

View File

@ -1,11 +1,11 @@
#include "config.hpp"
#include "spdlog.hpp" #include "spdlog.hpp"
#include "config.hpp"
namespace myx { namespace myx {
namespace log { namespace log {
Logger defaultLogger( "default" ); Logger default_logger( "default" );
spdlog::level::level_enum Logger::outputLevel() const spdlog::level::level_enum Logger::outputLevel() const
@ -14,7 +14,7 @@ spdlog::level::level_enum Logger::outputLevel() const
} }
void Logger::setOutputLevel( const spdlog::level::level_enum& outputLevel ) void Logger::setOutputLevel( const spdlog::level::level_enum outputLevel )
{ {
m_outputLevel = outputLevel; m_outputLevel = outputLevel;
} }
@ -26,7 +26,7 @@ spdlog::level::level_enum Logger::flushLevel() const
} }
void Logger::setFlushLevel( const spdlog::level::level_enum& flushLevel ) void Logger::setFlushLevel( const spdlog::level::level_enum flushLevel )
{ {
m_flushLevel = flushLevel; m_flushLevel = flushLevel;
} }
@ -44,12 +44,12 @@ void Logger::setOutputPattern( const std::string& outputPattern )
} }
Logger::Logger( const std::string& name ) : Logger::Logger( std::string name ) :
m_logger( nullptr ), m_logger( nullptr ),
m_name( name ), m_name( std::move( name ) ),
m_outputPattern( "[%H:%M:%S %z] [%n] [%^---%L---%$] %v" ), 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( rotatingFileSize ),
m_maxRotatingFilesCount( 3 ), m_maxRotatingFilesCount( 3 ),
m_outputLevel( spdlog::level::trace ), m_outputLevel( spdlog::level::trace ),
m_flushLevel( spdlog::level::warn ), m_flushLevel( spdlog::level::warn ),
@ -113,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 ), auto sinks = m_logger->sinks();
m_logger->sinks().end() ); sinks.erase( std::remove( sinks.begin(), sinks.end(), m_nullSink ), sinks.end() );
m_nullSink.reset(); m_nullSink.reset();
m_nullSink = nullptr; m_nullSink = nullptr;
} }
@ -135,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 ), auto sinks = m_logger->sinks();
m_logger->sinks().end() ); sinks.erase( std::remove( sinks.begin(), sinks.end(), m_stdoutSink ), sinks.end() );
m_stdoutSink.reset(); m_stdoutSink.reset();
m_stdoutSink = nullptr; m_stdoutSink = nullptr;
} }
@ -157,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 ), auto sinks = m_logger->sinks();
m_logger->sinks().end() ); sinks.erase( std::remove( sinks.begin(), sinks.end(), m_stderrSink ), sinks.end() );
m_stderrSink.reset(); m_stderrSink.reset();
m_stderrSink = nullptr; m_stderrSink = nullptr;
} }
@ -179,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 ), auto sinks = m_logger->sinks();
m_logger->sinks().end() ); sinks.erase( std::remove( sinks.begin(), sinks.end(), m_syslogSink ), sinks.end() );
m_syslogSink.reset(); m_syslogSink.reset();
m_syslogSink = nullptr; m_syslogSink = nullptr;
} }
@ -201,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 ), auto sinks = m_logger->sinks();
m_logger->sinks().end() ); sinks.erase( std::remove( sinks.begin(), sinks.end(), m_basicFileSink ), sinks.end() );
m_basicFileSink.reset(); m_basicFileSink.reset();
m_basicFileSink = nullptr; m_basicFileSink = nullptr;
} }
@ -223,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 ), auto sinks = m_logger->sinks();
m_logger->sinks().end() ); sinks.erase( std::remove( sinks.begin(), sinks.end(), m_rotatingFileSink ), sinks.end() );
m_rotatingFileSink.reset(); m_rotatingFileSink.reset();
m_rotatingFileSink = nullptr; m_rotatingFileSink = nullptr;
} }
@ -247,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 ), auto sinks = m_logger->sinks();
m_logger->sinks().end() ); sinks.erase( std::remove( sinks.begin(), sinks.end(), m_dailyFileSink ), sinks.end() );
m_dailyFileSink.reset(); m_dailyFileSink.reset();
m_dailyFileSink = nullptr; m_dailyFileSink = nullptr;
} }

View File

@ -1,6 +1,7 @@
#ifndef MYX_LOG_SPDLOG_HPP_ #ifndef MYX_LOG_SPDLOG_HPP_
#define MYX_LOG_SPDLOG_HPP_ #define MYX_LOG_SPDLOG_HPP_
#include <spdlog/sinks/sink.h>
#include <spdlog/sinks/null_sink.h> #include <spdlog/sinks/null_sink.h>
#include <spdlog/sinks/syslog_sink.h> #include <spdlog/sinks/syslog_sink.h>
#include <spdlog/sinks/basic_file_sink.h> #include <spdlog/sinks/basic_file_sink.h>
@ -11,6 +12,8 @@ namespace myx {
namespace log { namespace log {
const size_t rotatingFileSize = 256 * 1024;
class Logger class Logger
{ {
std::shared_ptr< spdlog::logger > m_logger; std::shared_ptr< spdlog::logger > m_logger;
@ -30,7 +33,7 @@ class Logger
std::shared_ptr< spdlog::sinks::daily_file_sink_st > m_dailyFileSink; std::shared_ptr< spdlog::sinks::daily_file_sink_st > m_dailyFileSink;
public: public:
Logger( const std::string& name ); Logger( std::string name );
void init(); void init();
std::shared_ptr< spdlog::logger > logger(); std::shared_ptr< spdlog::logger > logger();
@ -54,20 +57,20 @@ public:
void enableDailyFileSink(); void enableDailyFileSink();
void disableDailyFileSink(); void disableDailyFileSink();
spdlog::level::level_enum outputLevel() const; spdlog::level::level_enum outputLevel() const;
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; std::string outputPattern() const;
void setOutputPattern( const std::string& outputPattern ); void setOutputPattern( const std::string& outputPattern );
}; // class Logger }; // class Logger
extern Logger defaultLogger; extern 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
{ {
defaultLogger.logger()->trace( std::forward< F >( format ), default_logger.logger()->trace( std::forward< F >( format ),
std::forward< Args >( args )... ); std::forward< Args >( args )... );
} }
@ -75,7 +78,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
{ {
defaultLogger.logger()->debug( std::forward< F >( format ), default_logger.logger()->debug( std::forward< F >( format ),
std::forward< Args >( args )... ); std::forward< Args >( args )... );
} }
@ -83,7 +86,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
{ {
defaultLogger.logger()->info( std::forward< F >( format ), default_logger.logger()->info( std::forward< F >( format ),
std::forward< Args >( args )... ); std::forward< Args >( args )... );
} }
@ -91,7 +94,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
{ {
defaultLogger.logger()->warn( std::forward< F >( format ), default_logger.logger()->warn( std::forward< F >( format ),
std::forward< Args >( args )... ); std::forward< Args >( args )... );
} }
@ -99,7 +102,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
{ {
defaultLogger.logger()->error( std::forward< F >( format ), default_logger.logger()->error( std::forward< F >( format ),
std::forward< Args >( args )... ); std::forward< Args >( args )... );
} }
@ -107,7 +110,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
{ {
defaultLogger.logger()->log( lv, std::forward< F >( format ), default_logger.logger()->log( lv, std::forward< F >( format ),
std::forward< Args >( args )... ); std::forward< Args >( args )... );
} }

View File

@ -9,30 +9,31 @@ namespace log {
void spdlog_qt_message_handler( QtMsgType type, const QMessageLogContext& context, const QString& qMsg ) void spdlog_qt_message_handler( QtMsgType type, const QMessageLogContext& context, const QString& qMsg )
{ {
if ( !defaultLogger.logger() ) Q_UNUSED( context )
if ( !default_logger.logger() )
{ {
return; return;
} }
Q_UNUSED( context )
const char* msg = qMsg.toUtf8().constData(); auto message = qMsg.toUtf8();
switch ( type ) switch ( type )
{ {
case QtDebugMsg: case QtDebugMsg:
defaultLogger.logger()->debug( msg ); default_logger.logger()->debug( message.constData() );
break; break;
case QtInfoMsg: case QtInfoMsg:
defaultLogger.logger()->info( msg ); default_logger.logger()->info( message.constData() );
break; break;
case QtWarningMsg: case QtWarningMsg:
defaultLogger.logger()->warn( msg ); default_logger.logger()->warn( message.constData() );
break; break;
case QtCriticalMsg: case QtCriticalMsg:
defaultLogger.logger()->critical( msg ); default_logger.logger()->critical( message.constData() );
break; break;
case QtFatalMsg: case QtFatalMsg:
defaultLogger.logger()->critical( "FATAL: %s", msg ); default_logger.logger()->critical( "FATAL: %s", message.constData() );
QCoreApplication::exit( 1 ); QCoreApplication::exit( 1 );
break; break;
} }

View File

@ -263,7 +263,7 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
if (!fgets(buffer, sizeof(buffer), maps)) if (!fgets(buffer, sizeof(buffer), maps))
break; break;
if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8) if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %4s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8)
{ {
uint64_t addr = (uintptr_t)WAI_RETURN_ADDRESS(); uint64_t addr = (uintptr_t)WAI_RETURN_ADDRESS();
if (low <= addr && addr <= high) if (low <= addr && addr <= high)