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 2024-01-15 17:51:03 +01:00 committed by GitHub
parent caa42a89d3
commit 5c096748c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 313 additions and 27 deletions

3
.gitmodules vendored
View File

@ -25,3 +25,6 @@
[submodule "ThirdParty/qtadvanceddocking"] [submodule "ThirdParty/qtadvanceddocking"]
path = ThirdParty/qtadvanceddocking path = ThirdParty/qtadvanceddocking
url = https://github.com/CeetronSolutions/qtadvanceddocking.git url = https://github.com/CeetronSolutions/qtadvanceddocking.git
[submodule "ThirdParty/spdlog"]
path = ThirdParty/spdlog
url = https://github.com/gabime/spdlog.git

View File

@ -17,7 +17,6 @@
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
#include "RiaArgumentParser.h" #include "RiaArgumentParser.h"
#include "RiaLogging.h"
#include "RiaMainTools.h" #include "RiaMainTools.h"
#ifdef ENABLE_GRPC #ifdef ENABLE_GRPC
@ -72,8 +71,6 @@ int main( int argc, char* argv[] )
return 1; return 1;
} }
#endif #endif
// Global initialization
RiaLogging::loggerInstance()->setLevel( int( RILogLevel::RI_LL_DEBUG ) );
// Create feature manager before the application object is created // Create feature manager before the application object is created
RiaMainTools::initializeSingletons(); RiaMainTools::initializeSingletons();

View File

@ -103,8 +103,9 @@ void RiaConsoleApplication::initialize()
RiaApplication::initialize(); RiaApplication::initialize();
RiaLogging::setLoggerInstance( std::make_unique<RiaStdOutLogger>() ); auto logger = std::make_unique<RiaStdOutLogger>();
RiaLogging::loggerInstance()->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) ); logger->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) );
RiaLogging::appendLoggerInstance( std::move( logger ) );
m_socketServer = new RiaSocketServer( this ); m_socketServer = new RiaSocketServer( this );
} }

View File

@ -23,6 +23,7 @@
#include "RiaArgumentParser.h" #include "RiaArgumentParser.h"
#include "RiaBaseDefs.h" #include "RiaBaseDefs.h"
#include "RiaDefines.h" #include "RiaDefines.h"
#include "RiaFileLogger.h"
#include "RiaFilePathTools.h" #include "RiaFilePathTools.h"
#include "RiaFontCache.h" #include "RiaFontCache.h"
#include "RiaImportEclipseCaseTools.h" #include "RiaImportEclipseCaseTools.h"
@ -432,9 +433,18 @@ void RiaGuiApplication::initialize()
auto logger = std::make_unique<RiuMessagePanelLogger>(); auto logger = std::make_unique<RiuMessagePanelLogger>();
logger->addMessagePanel( m_mainWindow->messagePanel() ); logger->addMessagePanel( m_mainWindow->messagePanel() );
logger->addMessagePanel( m_mainPlotWindow->messagePanel() ); logger->addMessagePanel( m_mainPlotWindow->messagePanel() );
RiaLogging::setLoggerInstance( std::move( logger ) ); logger->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) );
RiaLogging::loggerInstance()->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) ); RiaLogging::appendLoggerInstance( std::move( logger ) );
auto filename = RiaPreferences::current()->loggerFilename();
if ( !filename.isEmpty() )
{
auto fileLogger = std::make_unique<RiaFileLogger>( filename.toStdString() );
fileLogger->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) );
RiaLogging::appendLoggerInstance( std::move( fileLogger ) );
}
} }
m_socketServer = new RiaSocketServer( this ); m_socketServer = new RiaSocketServer( this );
} }
@ -501,7 +511,7 @@ RiaApplication::ApplicationStatus RiaGuiApplication::handleArguments( gsl::not_n
auto stdLogger = std::make_unique<RiaStdOutLogger>(); auto stdLogger = std::make_unique<RiaStdOutLogger>();
stdLogger->setLevel( int( RILogLevel::RI_LL_DEBUG ) ); stdLogger->setLevel( int( RILogLevel::RI_LL_DEBUG ) );
RiaLogging::setLoggerInstance( std::move( stdLogger ) ); RiaLogging::appendLoggerInstance( std::move( stdLogger ) );
RiaRegressionTestRunner::instance()->executeRegressionTests( regressionTestPath, QStringList() ); RiaRegressionTestRunner::instance()->executeRegressionTests( regressionTestPath, QStringList() );
return ApplicationStatus::EXIT_COMPLETED; return ApplicationStatus::EXIT_COMPLETED;
@ -964,10 +974,14 @@ void RiaGuiApplication::createMainWindow()
m_mainWindow->showWindow(); m_mainWindow->showWindow();
// if there is an existing logger, reconnect to it // if there is an existing logger, reconnect to it
auto logger = dynamic_cast<RiuMessagePanelLogger*>( RiaLogging::loggerInstance() );
if ( logger ) for ( auto logger : RiaLogging::loggerInstances() )
{ {
logger->addMessagePanel( m_mainWindow->messagePanel() ); auto messagePanelLogger = dynamic_cast<RiuMessagePanelLogger*>( logger );
if ( messagePanelLogger )
{
messagePanelLogger->addMessagePanel( m_mainWindow->messagePanel() );
}
} }
} }

View File

@ -34,6 +34,7 @@
#include "cafPdmFieldCvfColor.h" #include "cafPdmFieldCvfColor.h"
#include "cafPdmSettings.h" #include "cafPdmSettings.h"
#include "cafPdmUiCheckBoxAndTextEditor.h"
#include "cafPdmUiCheckBoxEditor.h" #include "cafPdmUiCheckBoxEditor.h"
#include "cafPdmUiComboBoxEditor.h" #include "cafPdmUiComboBoxEditor.h"
#include "cafPdmUiFieldHandle.h" #include "cafPdmUiFieldHandle.h"
@ -125,6 +126,18 @@ RiaPreferences::RiaPreferences()
m_pythonExecutable.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::TOP ); m_pythonExecutable.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::TOP );
CAF_PDM_InitField( &showPythonDebugInfo, "pythonDebugInfo", false, "Show Python Debug Info" ); CAF_PDM_InitField( &showPythonDebugInfo, "pythonDebugInfo", false, "Show Python Debug Info" );
auto defaultFilename = QStandardPaths::writableLocation( QStandardPaths::DocumentsLocation );
if ( defaultFilename.isEmpty() )
{
defaultFilename = QStandardPaths::writableLocation( QStandardPaths::HomeLocation );
}
defaultFilename += "/ResInsight.log";
CAF_PDM_InitField( &m_loggerFilename, "loggerFilename", std::make_pair( false, defaultFilename ), "Logging To File" );
m_loggerFilename.uiCapability()->setUiEditorTypeName( caf::PdmUiCheckBoxAndTextEditor::uiEditorTypeName() );
CAF_PDM_InitField( &m_loggerFlushInterval, "loggerFlushInterval", 500, "Logging Flush Interval [ms]" );
CAF_PDM_InitField( &ssihubAddress, "ssihubAddress", QString( "http://" ), "SSIHUB Address" ); CAF_PDM_InitField( &ssihubAddress, "ssihubAddress", QString( "http://" ), "SSIHUB Address" );
ssihubAddress.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::TOP ); ssihubAddress.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::TOP );
@ -461,6 +474,11 @@ void RiaPreferences::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering&
caf::PdmUiGroup* otherGroup = uiOrdering.addNewGroup( "Other" ); caf::PdmUiGroup* otherGroup = uiOrdering.addNewGroup( "Other" );
otherGroup->add( &m_gridCalculationExpressionFolder ); otherGroup->add( &m_gridCalculationExpressionFolder );
otherGroup->add( &m_summaryCalculationExpressionFolder ); otherGroup->add( &m_summaryCalculationExpressionFolder );
caf::PdmUiGroup* loggingGroup = uiOrdering.addNewGroup( "Logging" );
loggingGroup->add( &m_loggerFilename );
loggingGroup->add( &m_loggerFlushInterval );
m_loggerFlushInterval.uiCapability()->setUiReadOnly( !m_loggerFilename().first );
} }
else if ( RiaApplication::enableDevelopmentFeatures() && uiConfigName == RiaPreferences::tabNameSystem() ) else if ( RiaApplication::enableDevelopmentFeatures() && uiConfigName == RiaPreferences::tabNameSystem() )
{ {
@ -932,6 +950,27 @@ QString RiaPreferences::octaveExecutable() const
return m_octaveExecutable().trimmed(); return m_octaveExecutable().trimmed();
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaPreferences::loggerFilename() const
{
if ( m_loggerFilename().first )
{
return m_loggerFilename().second;
}
return {};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaPreferences::loggerFlushInterval() const
{
return m_loggerFlushInterval();
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -117,6 +117,9 @@ public:
QString pythonExecutable() const; QString pythonExecutable() const;
QString octaveExecutable() const; QString octaveExecutable() const;
QString loggerFilename() const;
int loggerFlushInterval() const;
RiaPreferencesGeoMech* geoMechPreferences() const; RiaPreferencesGeoMech* geoMechPreferences() const;
RiaPreferencesSummary* summaryPreferences() const; RiaPreferencesSummary* summaryPreferences() const;
RiaPreferencesSystem* systemPreferences() const; RiaPreferencesSystem* systemPreferences() const;
@ -203,6 +206,10 @@ private:
caf::PdmField<QString> m_octaveExecutable; caf::PdmField<QString> m_octaveExecutable;
caf::PdmField<QString> m_pythonExecutable; caf::PdmField<QString> m_pythonExecutable;
// Logging
caf::PdmField<std::pair<bool, QString>> m_loggerFilename;
caf::PdmField<int> m_loggerFlushInterval;
// Surface Import // Surface Import
caf::PdmField<double> m_surfaceImportResamplingDistance; caf::PdmField<double> m_surfaceImportResamplingDistance;

View File

@ -53,6 +53,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaOpenMPTools.h ${CMAKE_CURRENT_LIST_DIR}/RiaOpenMPTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.h ${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.h ${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.h
) )
set(SOURCE_GROUP_SOURCE_FILES set(SOURCE_GROUP_SOURCE_FILES
@ -103,6 +104,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaOpenMPTools.cpp ${CMAKE_CURRENT_LIST_DIR}/RiaOpenMPTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.cpp ${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.cpp ${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.cpp
) )
list(APPEND CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES}) list(APPEND CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES})

View File

@ -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 );
}

View File

@ -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;
};

View File

@ -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 ) 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 ) #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 ) 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 ) #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 ) 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 ) #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 ) 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 ) #pragma omp critical( critical_section_logging )
sm_logger->debug( message.toLatin1().constData() ); logger->debug( message.toLatin1().constData() );
}
} }
} }

View File

@ -60,8 +60,8 @@ public:
class RiaLogging class RiaLogging
{ {
public: public:
static RiaLogger* loggerInstance(); static std::vector<RiaLogger*> loggerInstances();
static void setLoggerInstance( std::unique_ptr<RiaLogger> loggerInstance ); static void appendLoggerInstance( std::unique_ptr<RiaLogger> loggerInstance );
static RILogLevel logLevelBasedOnPreferences(); static RILogLevel logLevelBasedOnPreferences();
@ -73,7 +73,7 @@ public:
static void errorInMessageBox( QWidget* parent, const QString& title, const QString& text ); static void errorInMessageBox( QWidget* parent, const QString& title, const QString& text );
private: private:
static std::unique_ptr<RiaLogger> sm_logger; static std::vector<std::unique_ptr<RiaLogger>> sm_logger;
}; };
//================================================================================================== //==================================================================================================

View File

@ -623,6 +623,13 @@ set_property(
add_subdirectory(ThirdParty/tomlplusplus) add_subdirectory(ThirdParty/tomlplusplus)
# ##############################################################################
# spdlog
# ##############################################################################
add_subdirectory(ThirdParty/spdlog)
list(APPEND THIRD_PARTY_LIBRARIES spdlog)
# ############################################################################## # ##############################################################################
# Thirdparty libraries are put in ThirdParty solution folder # Thirdparty libraries are put in ThirdParty solution folder
# ############################################################################## # ##############################################################################

1
ThirdParty/spdlog vendored Submodule

@ -0,0 +1 @@
Subproject commit 8979f7fb2a119754e5fe7fe6d8155f67d934316a