Skip to content
Permalink
Browse files

[FEATURE][needs-doc] Introduce QGIS_SERVER_LOG_STDERR and deprecate f…

…ile logging
  • Loading branch information
Éric Lemoine
Éric Lemoine committed Sep 14, 2018
1 parent b16aa15 commit 5b808f6a22637ca052ca9d696d0a4a93c42dcde3
@@ -47,9 +47,16 @@ Set the current log level
.. versionadded:: 3.0
%End

void setLogFile( const QString &f );
void setLogFile( const QString &filename = QString() );
%Docstring
Set the current log file
%End

void setLogStderr();
%Docstring
Activates logging to stderr.

.. versionadded:: 3.4.
%End

public slots:
@@ -94,6 +94,15 @@ Returns the QGS project file to use.
Returns the log file.

:return: the path of the log file or an empty string if none is defined.
%End

bool logStderr() const;
%Docstring
Returns whether logging to stderr is activated.

:return: true if logging to stderr is activated, false otherwise.

.. versionadded:: 3.4
%End

qint64 cacheSize() const;
@@ -186,7 +186,14 @@ bool QgsServer::init()
// init and configure logger
QgsServerLogger::instance();
QgsServerLogger::instance()->setLogLevel( sSettings.logLevel() );
QgsServerLogger::instance()->setLogFile( sSettings.logFile() );
if ( ! sSettings.logFile().isEmpty() )
{
QgsServerLogger::instance()->setLogFile( sSettings.logFile() );
}
else if ( sSettings.logStderr() )
{
QgsServerLogger::instance()->setLogStderr();
}

// log settings currently used
sSettings.logSummary();
@@ -52,7 +52,7 @@ void QgsServerLogger::logMessage( const QString &message, const QString &tag, Qg
mTextStream << formattedMessage;
mTextStream.flush();
}
else if ( QString::compare( mLogFile.fileName(), QStringLiteral( "stderr" ), Qt::CaseInsensitive ) == 0 )
else if ( mLogStderr )
{
QgsMessageLogConsole::logMessage( message, tag, level );
}
@@ -63,17 +63,20 @@ void QgsServerLogger::setLogLevel( const Qgis::MessageLevel level )
mLogLevel = level;
}

void QgsServerLogger::setLogFile( const QString &f )
void QgsServerLogger::setLogFile( const QString &filename )
{
mTextStream.flush();
mLogFile.close();
mLogFile.setFileName( filename );

mLogFile.setFileName( f );

if ( ( ! f.isEmpty() ) &&
QString::compare( f, QStringLiteral( "stderr" ), Qt::CaseInsensitive ) != 0 &&
mLogFile.open( QIODevice::Append ) )
if ( ( ! filename.isEmpty() ) && mLogFile.open( QIODevice::Append ) )
{
mTextStream.setDevice( &mLogFile );
}
}

void QgsServerLogger::setLogStderr()
{
setLogFile();
mLogStderr = true;
}
@@ -59,7 +59,13 @@ class SERVER_EXPORT QgsServerLogger : public QgsMessageLogConsole
/**
* Set the current log file
*/
void setLogFile( const QString &f );
void setLogFile( const QString &filename = QString() );

/**
* Activates logging to stderr.
* \since QGIS 3.4.
*/
void setLogStderr();

public slots:

@@ -79,6 +85,7 @@ class SERVER_EXPORT QgsServerLogger : public QgsMessageLogConsole
static QgsServerLogger *sInstance;

QFile mLogFile;
bool mLogStderr = false;
QTextStream mTextStream;
Qgis::MessageLevel mLogLevel = Qgis::None;
};
@@ -87,6 +87,17 @@ void QgsServerSettings::initSettings()
};
mSettings[ sLogFile.envVar ] = sLogFile;

// log to stderr
const Setting sLogStderr = { QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR,
QgsServerSettingsEnv::DEFAULT_VALUE,
"Activate/Deactivate logging to stderr",
"",
QVariant::Bool,
QVariant( false ),
QVariant()
};
mSettings[ sLogStderr.envVar ] = sLogStderr;

// project file
const Setting sProject = { QgsServerSettingsEnv::QGIS_PROJECT_FILE,
QgsServerSettingsEnv::DEFAULT_VALUE,
@@ -281,6 +292,11 @@ QString QgsServerSettings::logFile() const
return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_FILE ).toString();
}

bool QgsServerSettings::logStderr() const
{
return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR ).toBool();
}

Qgis::MessageLevel QgsServerSettings::logLevel() const
{
return static_cast<Qgis::MessageLevel>( value( QgsServerSettingsEnv::QGIS_SERVER_LOG_LEVEL ).toInt() );
@@ -56,6 +56,7 @@ class SERVER_EXPORT QgsServerSettingsEnv : public QObject
QGIS_SERVER_MAX_THREADS,
QGIS_SERVER_LOG_LEVEL,
QGIS_SERVER_LOG_FILE,
QGIS_SERVER_LOG_STDERR,
QGIS_PROJECT_FILE,
MAX_CACHE_LAYERS,
QGIS_SERVER_CACHE_DIRECTORY,
@@ -148,6 +149,13 @@ class SERVER_EXPORT QgsServerSettings
*/
QString logFile() const;

/**
* Returns whether logging to stderr is activated.
* \returns true if logging to stderr is activated, false otherwise.
* \since QGIS 3.4
*/
bool logStderr() const;

/**
* Returns the cache size.
* \returns the cache size.
@@ -46,7 +46,12 @@ def test_logging_log_file(self):
exists = os.access(self.log_file, os.R_OK)
self.assertTrue(exists)

def test_logging_stderr(self):
def test_logging_log_file_stderr(self):
self.logger.setLogFile('stderr')
exists = os.access(self.log_file, os.R_OK)
self.assertFalse(exists)

def test_logging_stderr(self):
self.logger.setLogStderr()
exists = os.access(self.log_file, os.R_OK)
self.assertFalse(exists)

0 comments on commit 5b808f6

Please sign in to comment.
You can’t perform that action at this time.