Optionally log messages to file (#11059)

* Add spdlog as submodule
* Add RiaFileLogger
* Control use of file logger from preferences
This commit is contained in:
Magne Sjaastad authored and GitHub committed 2024-01-15 17:51:03 +01:00
1 parent caa42a89d3
commit 5c096748c8
13 files changed
+313 -27

No files matched your search

@@ -53,6 +53,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaOpenMPTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.h
)
set(SOURCE_GROUP_SOURCE_FILES
@@ -103,6 +104,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaOpenMPTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.cpp
)
list(APPEND CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES})
@@ -0,0 +1,152 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaFileLogger.h"
#include "RiaPreferences.h"
#include "spdlog/logger.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/spdlog.h"
class RiaFileLogger::Impl
{
public:
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Impl( const std::string& fileName )
{
try
{
m_spdlogger = spdlog::basic_logger_mt( "basic_logger", fileName );
auto flushInterval = RiaPreferences::current()->loggerFlushInterval();
spdlog::flush_every( std::chrono::milliseconds( flushInterval ) );
}
catch ( ... )
{
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void log( const std::string& message )
{
if ( m_spdlogger ) m_spdlogger->info( message );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void info( const std::string& message )
{
if ( m_spdlogger ) m_spdlogger->info( message );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void debug( const std::string& message )
{
if ( m_spdlogger ) m_spdlogger->debug( message );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void error( const std::string& message )
{
if ( m_spdlogger ) m_spdlogger->error( message );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void warning( const std::string& message )
{
if ( m_spdlogger ) m_spdlogger->warn( message );
}
private:
std::shared_ptr<spdlog::logger> m_spdlogger;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaFileLogger::RiaFileLogger( const std::string& filename )
: m_impl( new Impl( filename ) )
, m_logLevel( int( RILogLevel::RI_LL_DEBUG ) )
{
}
//--------------------------------------------------------------------------------------------------
/// The destructor must be located in the cpp file after the definition of RiaFileLogger::Impl to make sure the Impl class is defined when
/// the destructor of std::unique_ptr<Impl> is called
//--------------------------------------------------------------------------------------------------
RiaFileLogger::~RiaFileLogger() = default;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaFileLogger::level() const
{
return m_logLevel;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaFileLogger::setLevel( int logLevel )
{
m_logLevel = logLevel;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaFileLogger::error( const char* message )
{
m_impl->error( message );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaFileLogger::warning( const char* message )
{
m_impl->warning( message );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaFileLogger::info( const char* message )
{
m_impl->info( message );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaFileLogger::debug( const char* message )
{
m_impl->debug( message );
}
@@ -0,0 +1,45 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RiaLogging.h"
//==================================================================================================
//
//==================================================================================================
class RiaFileLogger : public RiaLogger
{
public:
explicit RiaFileLogger( const std::string& filename );
~RiaFileLogger() override;
int level() const override;
void setLevel( int logLevel ) override;
void error( const char* message ) override;
void warning( const char* message ) override;
void info( const char* message ) override;
void debug( const char* message ) override;
private:
int m_logLevel;
class Impl;
std::unique_ptr<Impl> m_impl;
};
@@ -169,22 +169,28 @@ void RiaDefaultConsoleLogger::writeToConsole( const std::string& str )
//
//==================================================================================================
std::unique_ptr<RiaLogger> RiaLogging::sm_logger = std::make_unique<RiaDefaultConsoleLogger>();
std::vector<std::unique_ptr<RiaLogger>> RiaLogging::sm_logger;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaLogger* RiaLogging::loggerInstance()
std::vector<RiaLogger*> RiaLogging::loggerInstances()
{
return sm_logger.get();
std::vector<RiaLogger*> loggerInstances;
for ( auto& logger : sm_logger )
{
loggerInstances.push_back( logger.get() );
}
return loggerInstances;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaLogging::setLoggerInstance( std::unique_ptr<RiaLogger> loggerInstance )
void RiaLogging::appendLoggerInstance( std::unique_ptr<RiaLogger> loggerInstance )
{
sm_logger = std::move( loggerInstance );
sm_logger.push_back( std::move( loggerInstance ) );
}
//--------------------------------------------------------------------------------------------------
@@ -202,10 +208,13 @@ RILogLevel RiaLogging::logLevelBasedOnPreferences()
//--------------------------------------------------------------------------------------------------
void RiaLogging::error( const QString& message )
{
if ( sm_logger && sm_logger->level() >= int( RILogLevel::RI_LL_ERROR ) )
for ( const auto& logger : sm_logger )
{
if ( logger && logger->level() >= int( RILogLevel::RI_LL_ERROR ) )
{
#pragma omp critical( critical_section_logging )
sm_logger->error( message.toLatin1().constData() );
logger->error( message.toLatin1().constData() );
}
}
}
@@ -214,10 +223,13 @@ void RiaLogging::error( const QString& message )
//--------------------------------------------------------------------------------------------------
void RiaLogging::warning( const QString& message )
{
if ( sm_logger && sm_logger->level() >= int( RILogLevel::RI_LL_WARNING ) )
for ( const auto& logger : sm_logger )
{
if ( logger && logger->level() >= int( RILogLevel::RI_LL_WARNING ) )
{
#pragma omp critical( critical_section_logging )
sm_logger->warning( message.toLatin1().constData() );
logger->warning( message.toLatin1().constData() );
}
}
}
@@ -226,10 +238,13 @@ void RiaLogging::warning( const QString& message )
//--------------------------------------------------------------------------------------------------
void RiaLogging::info( const QString& message )
{
if ( sm_logger && sm_logger->level() >= int( RILogLevel::RI_LL_INFO ) )
for ( const auto& logger : sm_logger )
{
if ( logger && logger->level() >= int( RILogLevel::RI_LL_INFO ) )
{
#pragma omp critical( critical_section_logging )
sm_logger->info( message.toLatin1().constData() );
logger->info( message.toLatin1().constData() );
}
}
}
@@ -238,10 +253,13 @@ void RiaLogging::info( const QString& message )
//--------------------------------------------------------------------------------------------------
void RiaLogging::debug( const QString& message )
{
if ( sm_logger && sm_logger->level() >= int( RILogLevel::RI_LL_DEBUG ) )
for ( const auto& logger : sm_logger )
{
if ( logger && logger->level() >= int( RILogLevel::RI_LL_DEBUG ) )
{
#pragma omp critical( critical_section_logging )
sm_logger->debug( message.toLatin1().constData() );
logger->debug( message.toLatin1().constData() );
}
}
}
@@ -60,8 +60,8 @@ public:
class RiaLogging
{
public:
static RiaLogger* loggerInstance();
static void setLoggerInstance( std::unique_ptr<RiaLogger> loggerInstance );
static std::vector<RiaLogger*> loggerInstances();
static void appendLoggerInstance( std::unique_ptr<RiaLogger> loggerInstance );
static RILogLevel logLevelBasedOnPreferences();
@@ -73,7 +73,7 @@ public:
static void errorInMessageBox( QWidget* parent, const QString& title, const QString& text );
private:
static std::unique_ptr<RiaLogger> sm_logger;
static std::vector<std::unique_ptr<RiaLogger>> sm_logger;
};
//==================================================================================================