Add --loglevel argument for command line log level control

Introduce support for setting the application's log level via a new --loglevel command line argument.
Log level is parsed early in initialization and can be set to DISABLED, ERROR, WARNING, INFO, or DEBUG (case-insensitive).
The RILogLevel enum now includes a DISABLED value.
Logger initialization in both console and GUI applications now respects the command line log level if provided, otherwise defaults to preference-based settings.

Fixes by clang-format
This commit is contained in:
Magne Sjaastad committed 2026-01-23 13:31:58 +01:00
1 parent fde113259a
commit 30793beef2
7 files changed
+81 -7

No files matched your search

@@ -1539,6 +1539,29 @@ void RiaApplication::setCommandLineHelpText( const QString& commandLineHelpText
m_commandLineHelpText = commandLineHelpText;
}
//--------------------------------------------------------------------------------------------------
/// Parse and set the log level from Qt command line arguments
/// This method is called early during initialization, before the full argument parsing
/// Only sets m_logLevelFromCommandLine if a valid value is found, silently ignores errors
//--------------------------------------------------------------------------------------------------
void RiaApplication::parseLogLevelFromQtArguments()
{
QStringList args = QCoreApplication::arguments();
for ( int i = 1; i < args.size(); ++i )
{
if ( args[i] == "--loglevel" && i + 1 < args.size() )
{
QString logLevelString = args[i + 1];
std::optional<RILogLevel> logLevel = RiaLogging::parseLogLevelString( logLevelString );
if ( logLevel.has_value() )
{
m_logLevelFromCommandLine = int( logLevel.value() );
}
break;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -1645,6 +1668,9 @@ void RiaApplication::initialize()
RiaCafLoggingManager::initializeCafLogging();
initializeDataLoadController();
// Parse log level early so it's available before logger is created in subclass initialize()
parseLogLevelFromQtArguments();
}
//--------------------------------------------------------------------------------------------------
@@ -232,6 +232,7 @@ protected:
protected:
void initializeDataLoadController();
void parseLogLevelFromQtArguments();
cvf::ref<cvf::Font> m_defaultSceneFont;
cvf::ref<cvf::Font> m_defaultAnnotationFont;
@@ -264,6 +265,7 @@ protected:
bool m_runningWorkerProcess;
std::optional<int> m_threadCountFromCommandLine;
std::optional<int> m_logLevelFromCommandLine;
private:
static RiaApplication* s_riaApplication;
@@ -106,7 +106,17 @@ void RiaConsoleApplication::initialize()
RiaApplication::initialize();
auto logger = std::make_unique<RiaStdOutLogger>();
logger->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) );
// Use command line log level if provided, otherwise use preference-based level
if ( m_logLevelFromCommandLine.has_value() )
{
logger->setLevel( m_logLevelFromCommandLine.value() );
}
else
{
logger->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) );
}
RiaLogging::appendLoggerInstance( std::move( logger ) );
m_socketServer = new RiaSocketServer( this );
@@ -462,7 +462,16 @@ void RiaGuiApplication::initialize()
auto logger = std::make_unique<RiuMessagePanelLogger>();
logger->addMessagePanel( m_mainWindow->messagePanel() );
logger->addMessagePanel( m_mainPlotWindow->messagePanel() );
logger->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) );
// Use command line log level if provided, otherwise use preference-based level
if ( m_logLevelFromCommandLine.has_value() )
{
logger->setLevel( m_logLevelFromCommandLine.value() );
}
else
{
logger->setLevel( int( RiaLogging::logLevelBasedOnPreferences() ) );
}
RiaLogging::appendLoggerInstance( std::move( logger ) );
}
@@ -144,6 +144,11 @@ bool RiaArgumentParser::parseArguments( cvf::ProgramOptions* progOpt )
"Select the reader type used to access EGRID files, valid values are RESDATA or OPM_COMMON",
cvf::ProgramOptions::SINGLE_VALUE );
progOpt->registerOption( "loglevel",
"<level>",
"Set the log level. Valid values are DISABLED, ERROR, WARNING, INFO, DEBUG",
cvf::ProgramOptions::SINGLE_VALUE );
progOpt->setOptionPrefix( cvf::ProgramOptions::DOUBLE_DASH );
QStringList arguments = QCoreApplication::arguments();
@@ -242,6 +242,26 @@ RILogLevel RiaLogging::logLevelBasedOnPreferences()
return RILogLevel::RI_LL_INFO;
}
//--------------------------------------------------------------------------------------------------
/// Parse a log level string and return the corresponding enum value
/// @param logLevelString The string to parse (case-insensitive, whitespace is trimmed)
/// @return The corresponding RILogLevel enum value, or std::nullopt if the string is invalid
/// Valid values are: DISABLED, ERROR, WARNING, INFO, DEBUG
//--------------------------------------------------------------------------------------------------
std::optional<RILogLevel> RiaLogging::parseLogLevelString( const QString& logLevelString )
{
QString upperString = logLevelString.toUpper().trimmed();
if ( upperString == "DISABLED" ) return RILogLevel::RI_LL_DISABLED;
if ( upperString == "ERROR" ) return RILogLevel::RI_LL_ERROR;
if ( upperString == "WARNING" ) return RILogLevel::RI_LL_WARNING;
if ( upperString == "INFO" ) return RILogLevel::RI_LL_INFO;
if ( upperString == "DEBUG" ) return RILogLevel::RI_LL_DEBUG;
// Return nullopt for invalid input
return std::nullopt;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -29,10 +29,11 @@ class QWidget;
enum class RILogLevel
{
RI_LL_ERROR = 1,
RI_LL_WARNING = 2,
RI_LL_INFO = 3,
RI_LL_DEBUG = 4
RI_LL_DISABLED = 0,
RI_LL_ERROR = 1,
RI_LL_WARNING = 2,
RI_LL_INFO = 3,
RI_LL_DEBUG = 4
};
//==================================================================================================
@@ -65,7 +66,8 @@ public:
static std::vector<RiaLogger*> loggerInstances();
static void appendLoggerInstance( std::unique_ptr<RiaLogger> loggerInstance );
static RILogLevel logLevelBasedOnPreferences();
static RILogLevel logLevelBasedOnPreferences();
static std::optional<RILogLevel> parseLogLevelString( const QString& logLevelString );
static void error( const QString& message, const QString logKeyword = "" );
static void warning( const QString& message, const QString logKeyword = "" );