QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #include "qgsapplication.h"
20 #include <QCoreApplication>
21 #include <QFile>
22 #include <QTextStream>
23 #include <QTime>
24 
25 #include <cstdlib>
26 
27 QgsServerLogger *QgsServerLogger::sInstance = nullptr;
28 
30 {
31  if ( !sInstance )
32  {
33  sInstance = new QgsServerLogger();
34  }
35  return sInstance;
36 }
37 
40 {
41 }
42 
43 void QgsServerLogger::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
44 {
45  if ( mLogLevel > level )
46  {
47  return;
48  }
49  if ( mLogFile.isOpen() )
50  {
51  QString formattedMessage = formatLogMessage( message, tag, level );
52  mTextStream << formattedMessage;
53  mTextStream.flush();
54  }
55  else if ( mLogStderr )
56  {
57  QgsMessageLogConsole::logMessage( message, tag, level );
58  }
59 }
60 
62 {
63  mLogLevel = level;
64 }
65 
66 void QgsServerLogger::setLogFile( const QString &filename )
67 {
68  mTextStream.flush();
69  mLogFile.close();
70  mLogFile.setFileName( filename );
71 
72  if ( ( ! filename.isEmpty() ) && mLogFile.open( QIODevice::Append ) )
73  {
74  mTextStream.setDevice( &mLogFile );
75  }
76 }
77 
79 {
80  setLogFile();
81  mLogStderr = true;
82 }
void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level) override
Log a message from the server context.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:79
void setLogLevel(Qgis::MessageLevel level)
Set the current log level.
virtual void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to stderr.
Writes message log into server logfile.
Default implementation of message logging interface.
void setLogStderr()
Activates logging to stderr.
QString formatLogMessage(const QString &message, const QString &tag, Qgis::MessageLevel level=Qgis::Info) const
Formats a log message.
static QgsServerLogger * instance()
Gets the singleton instance.
void setLogFile(const QString &filename=QString())
Set the current log file.