Skip to content

Commit

Permalink
Add option to Preferences to flush file loggers when a signal is trig…
Browse files Browse the repository at this point in the history
…gered
  • Loading branch information
magnesj committed Feb 2, 2024
1 parent 710b91d commit 06766b3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ApplicationExeCode/RiaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "RiaArgumentParser.h"
#include "RiaMainTools.h"
#include "RiaPreferences.h"

#ifdef ENABLE_GRPC
#include "RiaGrpcConsoleApplication.h"
Expand All @@ -39,6 +40,10 @@
#include <unistd.h>
#endif

#include <signal.h>

void manageSegFailure( int signalCode );

RiaApplication* createApplication( int& argc, char* argv[] )
{
for ( int i = 1; i < argc; ++i )
Expand Down Expand Up @@ -109,6 +114,17 @@ int main( int argc, char* argv[] )
QLocale::setDefault( QLocale( QLocale::English, QLocale::UnitedStates ) );
setlocale( LC_NUMERIC, "C" );

// Set up signal handlers
if ( RiaPreferences::current()->loggerTrapSignalAndFlush() )
{
signal( SIGINT, manageSegFailure );
signal( SIGILL, manageSegFailure );
signal( SIGFPE, manageSegFailure );
signal( SIGSEGV, manageSegFailure );
signal( SIGTERM, manageSegFailure );
signal( SIGABRT, manageSegFailure );
}

// Handle the command line arguments.
// Todo: Move to a one-shot timer, delaying the execution until we are inside the event loop.
// The complete handling of the resulting ApplicationStatus must be moved along.
Expand Down
28 changes: 28 additions & 0 deletions ApplicationExeCode/RiaMainTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/////////////////////////////////////////////////////////////////////////////////

#include "RiaMainTools.h"
#include "RiaFileLogger.h"
#include "RiaLogging.h"
#include "RiaRegressionTestRunner.h"
#include "RiaSocketCommand.h"

Expand All @@ -25,6 +27,32 @@
#include "cafPdmDefaultObjectFactory.h"
#include "cafPdmUiFieldEditorHandle.h"

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void manageSegFailure( int signalCode )
{
// Executing function here is not safe, but works as expected on Windows. Behavior on Linux is undefined, but will
// work in some cases.
// https://github.com/gabime/spdlog/issues/1607

auto loggers = RiaLogging::loggerInstances();

QString str = QString( "Segmentation fault. Signal code: %1" ).arg( signalCode );

for ( auto logger : loggers )
{
if ( auto fileLogger = dynamic_cast<RiaFileLogger*>( logger ) )
{
fileLogger->error( str.toStdString().data() );

fileLogger->flush();
}
}

exit( 1 );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions ApplicationLibCode/Application/RiaPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ RiaPreferences::RiaPreferences()
m_loggerFilename.uiCapability()->setUiEditorTypeName( caf::PdmUiCheckBoxAndTextEditor::uiEditorTypeName() );

CAF_PDM_InitField( &m_loggerFlushInterval, "loggerFlushInterval", 500, "Logging Flush Interval [ms]" );
CAF_PDM_InitField( &m_loggerTrapSignalAndFlush, "loggerTrapSignalAndFlush", false, "Trap SIGNAL and Flush File Logs" );

CAF_PDM_InitField( &ssihubAddress, "ssihubAddress", QString( "http://" ), "SSIHUB Address" );
ssihubAddress.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::TOP );
Expand Down Expand Up @@ -478,6 +479,8 @@ void RiaPreferences::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering&
caf::PdmUiGroup* loggingGroup = uiOrdering.addNewGroup( "Logging" );
loggingGroup->add( &m_loggerFilename );
loggingGroup->add( &m_loggerFlushInterval );
loggingGroup->add( &m_loggerTrapSignalAndFlush );
m_loggerTrapSignalAndFlush.uiCapability()->setUiReadOnly( !m_loggerFilename().first );
m_loggerFlushInterval.uiCapability()->setUiReadOnly( !m_loggerFilename().first );
}
else if ( RiaApplication::enableDevelopmentFeatures() && uiConfigName == RiaPreferences::tabNameSystem() )
Expand Down Expand Up @@ -971,6 +974,14 @@ int RiaPreferences::loggerFlushInterval() const
return m_loggerFlushInterval();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaPreferences::loggerTrapSignalAndFlush() const
{
return m_loggerTrapSignalAndFlush();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions ApplicationLibCode/Application/RiaPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class RiaPreferences : public caf::PdmObject

QString loggerFilename() const;
int loggerFlushInterval() const;
bool loggerTrapSignalAndFlush() const;

RiaPreferencesGeoMech* geoMechPreferences() const;
RiaPreferencesSummary* summaryPreferences() const;
Expand Down Expand Up @@ -209,6 +210,7 @@ class RiaPreferences : public caf::PdmObject
// Logging
caf::PdmField<std::pair<bool, QString>> m_loggerFilename;
caf::PdmField<int> m_loggerFlushInterval;
caf::PdmField<bool> m_loggerTrapSignalAndFlush;

// Surface Import
caf::PdmField<double> m_surfaceImportResamplingDistance;
Expand Down
16 changes: 16 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaFileLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class RiaFileLogger::Impl
if ( m_spdlogger ) m_spdlogger->warn( message );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void flush()
{
if ( m_spdlogger ) m_spdlogger->flush();
}

private:
std::shared_ptr<spdlog::logger> m_spdlogger;
};
Expand Down Expand Up @@ -150,3 +158,11 @@ void RiaFileLogger::debug( const char* message )
{
m_impl->debug( message );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaFileLogger::flush()
{
if ( m_impl ) m_impl->flush();
}
2 changes: 2 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaFileLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class RiaFileLogger : public RiaLogger
void info( const char* message ) override;
void debug( const char* message ) override;

void flush();

private:
int m_logLevel;

Expand Down

0 comments on commit 06766b3

Please sign in to comment.