QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsserverlogger.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsserverlogger.cpp
3  -------------------
4  begin : May 5, 2014
5  copyright : (C) 2014 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsserverlogger.h"
19 
20 QgsServerLogger *QgsServerLogger::sInstance = nullptr;
21 
23 {
24  if ( !sInstance )
25  {
26  sInstance = new QgsServerLogger();
27  }
28  return sInstance;
29 }
30 
33 {
34 }
35 
36 void QgsServerLogger::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
37 {
38  if ( mLogLevel > level )
39  {
40  return;
41  }
42  if ( mLogFile.isOpen() )
43  {
44  QString formattedMessage = formatLogMessage( message, tag, level );
45  mTextStream << formattedMessage;
46  mTextStream.flush();
47  }
48  else if ( mLogStderr )
49  {
50  QgsMessageLogConsole::logMessage( message, tag, level );
51  }
52 }
53 
55 {
56  mLogLevel = level;
57 }
58 
59 void QgsServerLogger::setLogFile( const QString &filename )
60 {
61  mTextStream.flush();
62  mLogFile.close();
63  mLogFile.setFileName( filename );
64 
65  if ( ( ! filename.isEmpty() ) && mLogFile.open( QIODevice::Append ) )
66  {
67  mTextStream.setDevice( &mLogFile );
68  }
69 }
70 
72 {
73  setLogFile();
74  mLogStderr = true;
75 }
QgsMessageLogConsole::formatLogMessage
QString formatLogMessage(const QString &message, const QString &tag, Qgis::MessageLevel level=Qgis::Info) const
Formats a log message.
Definition: qgsmessagelog.cpp:57
QgsServerLogger
Writes message log into server logfile.
Definition: qgsserverlogger.h:36
QgsServerLogger::setLogLevel
void setLogLevel(Qgis::MessageLevel level)
Set the current log level.
Definition: qgsserverlogger.cpp:54
QgsServerLogger::logMessage
void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level) override
Log a message from the server context.
Definition: qgsserverlogger.cpp:36
QgsServerLogger::instance
static QgsServerLogger * instance()
Gets the singleton instance.
Definition: qgsserverlogger.cpp:22
QgsMessageLogConsole::logMessage
virtual void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to stderr.
Definition: qgsmessagelog.cpp:50
QgsServerLogger::QgsServerLogger
QgsServerLogger()
Definition: qgsserverlogger.cpp:31
qgsserverlogger.h
QgsServerLogger::setLogFile
void setLogFile(const QString &filename=QString())
Set the current log file.
Definition: qgsserverlogger.cpp:59
QgsServerLogger::setLogStderr
void setLogStderr()
Activates logging to stderr.
Definition: qgsserverlogger.cpp:71
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:89
QgsMessageLogConsole
Default implementation of message logging interface.
Definition: qgsmessagelog.h:137