QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 }
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:105
Default implementation of message logging interface.
QString formatLogMessage(const QString &message, const QString &tag, Qgis::MessageLevel level=Qgis::MessageLevel::Info) const
Formats a log message.
virtual void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to stderr.
Writes message log into server logfile.
static QgsServerLogger * instance()
Gets the singleton instance.
void setLogLevel(Qgis::MessageLevel level)
Set the current log level.
void setLogFile(const QString &filename=QString())
Set the current log file.
void setLogStderr()
Activates logging to stderr.
void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level) override
Log a message from the server context.