Skip to content

Commit 29c39b9

Browse files
committedMay 13, 2014
[FEATURE]: Server logging also in release mode
2 parents 436973d + 8707cf2 commit 29c39b9

File tree

5 files changed

+181
-35
lines changed

5 files changed

+181
-35
lines changed
 

‎src/mapserver/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ SET ( qgis_mapserv_SRCS
3939
qgshostedrdsbuilder.cpp
4040
qgsremotedatasourcebuilder.cpp
4141
qgssentdatasourcebuilder.cpp
42+
qgsserverlogger.cpp
4243
qgsmsutils.cpp
4344
qgswcsprojectparser.cpp
4445
qgswfsprojectparser.cpp
@@ -58,6 +59,7 @@ SET (qgis_mapserv_MOC_HDRS
5859
qgscapabilitiescache.h
5960
qgsconfigcache.h
6061
qgsmslayercache.h
62+
qgsserverlogger.h
6163
)
6264

6365
SET (qgis_mapserv_RCCS

‎src/mapserver/qgis_map_serv.cpp

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qgspallabeling.h"
3434
#include "qgsnetworkaccessmanager.h"
3535
#include "qgsmaplayerregistry.h"
36+
#include "qgsserverlogger.h"
3637

3738
#include <QDomDocument>
3839
#include <QNetworkDiskCache>
@@ -49,7 +50,7 @@
4950

5051
void dummyMessageHandler( QtMsgType type, const char *msg )
5152
{
52-
#ifdef QGSMSDEBUG
53+
#if 0 //def QGSMSDEBUG
5354
QString output;
5455

5556
switch ( type )
@@ -81,53 +82,61 @@ void dummyMessageHandler( QtMsgType type, const char *msg )
8182

8283
void printRequestInfos()
8384
{
84-
#ifdef QGSMSDEBUG
85-
//print out some infos about the request
86-
QgsDebugMsg( "************************new request**********************" );
87-
QgsDebugMsg( QDateTime::currentDateTime().toString( "yyyy-MM-dd hh:mm:ss" ) );
88-
85+
QgsMessageLog::logMessage( "********************new request***************", "Server", QgsMessageLog::INFO );
8986
if ( getenv( "REMOTE_ADDR" ) != NULL )
9087
{
91-
QgsDebugMsg( "remote ip: " + QString( getenv( "REMOTE_ADDR" ) ) );
88+
QgsMessageLog::logMessage( "remote ip: " + QString( getenv( "REMOTE_ADDR" ) ), "Server", QgsMessageLog::INFO );
9289
}
9390
if ( getenv( "REMOTE_HOST" ) != NULL )
9491
{
95-
QgsDebugMsg( "remote host: " + QString( getenv( "REMOTE_HOST" ) ) );
92+
QgsMessageLog::logMessage( "remote ip: " + QString( getenv( "REMOTE_ADDR" ) ), "Server", QgsMessageLog::INFO );
9693
}
9794
if ( getenv( "REMOTE_USER" ) != NULL )
9895
{
99-
QgsDebugMsg( "remote user: " + QString( getenv( "REMOTE_USER" ) ) );
96+
QgsMessageLog::logMessage( "remote user: " + QString( getenv( "REMOTE_USER" ) ), "Server", QgsMessageLog::INFO );
10097
}
10198
if ( getenv( "REMOTE_IDENT" ) != NULL )
10299
{
103-
QgsDebugMsg( "REMOTE_IDENT: " + QString( getenv( "REMOTE_IDENT" ) ) );
100+
QgsMessageLog::logMessage( "REMOTE_IDENT: " + QString( getenv( "REMOTE_IDENT" ) ), "Server", QgsMessageLog::INFO );
104101
}
105102
if ( getenv( "CONTENT_TYPE" ) != NULL )
106103
{
107-
QgsDebugMsg( "CONTENT_TYPE: " + QString( getenv( "CONTENT_TYPE" ) ) );
104+
QgsMessageLog::logMessage( "CONTENT_TYPE: " + QString( getenv( "CONTENT_TYPE" ) ), "Server", QgsMessageLog::INFO );
108105
}
109106
if ( getenv( "AUTH_TYPE" ) != NULL )
110107
{
111-
QgsDebugMsg( "AUTH_TYPE: " + QString( getenv( "AUTH_TYPE" ) ) );
108+
QgsMessageLog::logMessage( "AUTH_TYPE: " + QString( getenv( "AUTH_TYPE" ) ), "Server", QgsMessageLog::INFO );
112109
}
113110
if ( getenv( "HTTP_USER_AGENT" ) != NULL )
114111
{
115-
QgsDebugMsg( "HTTP_USER_AGENT: " + QString( getenv( "HTTP_USER_AGENT" ) ) );
112+
QgsMessageLog::logMessage( "HTTP_USER_AGENT: " + QString( getenv( "HTTP_USER_AGENT" ) ), "Server", QgsMessageLog::INFO );
116113
}
117114
if ( getenv( "HTTP_PROXY" ) != NULL )
118115
{
119-
QgsDebugMsg( "HTTP_PROXY: " + QString( getenv( "HTTP_PROXY" ) ) );
116+
QgsMessageLog::logMessage( "HTTP_PROXY: " + QString( getenv( "HTTP_PROXY" ) ), "Server", QgsMessageLog::INFO );
120117
}
121118
if ( getenv( "HTTPS_PROXY" ) != NULL )
122119
{
123-
QgsDebugMsg( "HTTPS_PROXY: " + QString( getenv( "HTTPS_PROXY" ) ) );
120+
QgsMessageLog::logMessage( "HTTPS_PROXY: " + QString( getenv( "HTTPS_PROXY" ) ), "Server", QgsMessageLog::INFO );
124121
}
125122
if ( getenv( "NO_PROXY" ) != NULL )
126123
{
127-
QgsDebugMsg( "NO_PROXY: " + QString( getenv( "NO_PROXY" ) ) );
124+
QgsMessageLog::logMessage( "NO_PROXY: " + QString( getenv( "NO_PROXY" ) ), "Server", QgsMessageLog::INFO );
128125
}
126+
}
129127

130-
#endif //QGSMSDEBUG
128+
void printRequestParameters( const QMap< QString, QString>& parameterMap, int logLevel )
129+
{
130+
if ( logLevel > 0 )
131+
{
132+
return;
133+
}
134+
135+
QMap< QString, QString>::const_iterator pIt = parameterMap.constBegin();
136+
for ( ; pIt != parameterMap.constEnd(); ++pIt )
137+
{
138+
QgsMessageLog::logMessage( pIt.key() + ":" + pIt.value(), "Server", QgsMessageLog::INFO );
139+
}
131140
}
132141

133142
QFileInfo defaultProjectFile()
@@ -289,25 +298,20 @@ int main( int argc, char * argv[] )
289298
QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" );
290299
#endif
291300

292-
QString logFile = QgsLogger::logFile();
301+
int logLevel = QgsServerLogger::instance()->logLevel();
302+
QTime time; //used for measuring request time if loglevel < 1
293303

294304
while ( fcgi_accept() >= 0 )
295305
{
296306
QgsMapLayerRegistry::instance()->removeAllMapLayers();
297307
qgsapp.processEvents();
298308

299-
300-
if ( !logFile.isEmpty() )
309+
if ( logLevel < 1 )
301310
{
302-
#ifdef Q_WS_WIN
303-
putenv( QString( "QGIS_LOG_FILE=%1" ).arg( logFile ).toLocal8Bit().constData() );
304-
#else
305-
setenv( "QGIS_LOG_FILE", logFile.toLocal8Bit().constData(), 1 );
306-
#endif
311+
time.start();
312+
printRequestInfos();
307313
}
308314

309-
printRequestInfos(); //print request infos if in debug mode
310-
311315
//Request handler
312316
QScopedPointer<QgsRequestHandler> theRequestHandler( createRequestHandler() );
313317
QMap<QString, QString> parameterMap;
@@ -317,11 +321,12 @@ int main( int argc, char * argv[] )
317321
}
318322
catch ( QgsMapServiceException& e )
319323
{
320-
QgsDebugMsg( "An exception was thrown during input parsing" );
324+
QgsMessageLog::logMessage( "Parse input exception: " + e.message(), "Server", QgsMessageLog::CRITICAL );
321325
theRequestHandler->sendServiceException( e );
322326
continue;
323327
}
324328

329+
printRequestParameters( parameterMap, logLevel );
325330
QMap<QString, QString>::const_iterator paramIt;
326331

327332
//Config file path
@@ -332,6 +337,7 @@ int main( int argc, char * argv[] )
332337
paramIt = parameterMap.find( "SERVICE" );
333338
if ( paramIt == parameterMap.constEnd() )
334339
{
340+
QgsMessageLog::logMessage( "Exception: SERVICE parameter is missing", "Server", QgsMessageLog::CRITICAL );
335341
theRequestHandler->sendServiceException( QgsMapServiceException( "ServiceNotSpecified", "Service not specified. The SERVICE parameter is mandatory" ) );
336342
continue;
337343
}
@@ -345,7 +351,8 @@ int main( int argc, char * argv[] )
345351
QgsWCSProjectParser* p = QgsConfigCache::instance()->wcsConfiguration( configFilePath );
346352
if ( !p )
347353
{
348-
//error handling
354+
theRequestHandler->sendServiceException( QgsMapServiceException( "Project file error", "Error reading the project file" ) );
355+
continue;
349356
}
350357
QgsWCSServer wcsServer( configFilePath, parameterMap, p, theRequestHandler.take() );
351358
wcsServer.executeRequest();
@@ -355,7 +362,8 @@ int main( int argc, char * argv[] )
355362
QgsWFSProjectParser* p = QgsConfigCache::instance()->wfsConfiguration( configFilePath );
356363
if ( !p )
357364
{
358-
//error handling
365+
theRequestHandler->sendServiceException( QgsMapServiceException( "Project file error", "Error reading the project file" ) );
366+
continue;
359367
}
360368
QgsWFSServer wfsServer( configFilePath, parameterMap, p, theRequestHandler.take() );
361369
wfsServer.executeRequest();
@@ -365,12 +373,18 @@ int main( int argc, char * argv[] )
365373
QgsWMSConfigParser* p = QgsConfigCache::instance()->wmsConfiguration( configFilePath, parameterMap );
366374
if ( !p )
367375
{
368-
//error handling
376+
theRequestHandler->sendServiceException( QgsMapServiceException( "WMS configuration error", "There was an error reading gthe project file or the SLD configuration" ) );
377+
continue;
369378
}
370379
//adminConfigParser->loadLabelSettings( theMapRenderer->labelingEngine() );
371380
QgsWMSServer wmsServer( configFilePath, parameterMap, p, theRequestHandler.take(), theMapRenderer.data(), &capabilitiesCache );
372381
wmsServer.executeRequest();
373382
}
383+
384+
if ( logLevel < 1 )
385+
{
386+
QgsMessageLog::logMessage( "Request finished in " + QString::number( time.elapsed() ) + " ms", "Server", QgsMessageLog::INFO );
387+
}
374388
}
375389

376390
return 0;

‎src/mapserver/qgsconfigcache.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgsconfigcache.h"
19+
#include "qgsmessagelog.h"
1920
#include "qgswcsprojectparser.h"
2021
#include "qgswfsprojectparser.h"
2122
#include "qgswmsprojectparser.h"
@@ -118,9 +119,15 @@ QDomDocument* QgsConfigCache::xmlDocument( const QString& filePath )
118119
{
119120
//first open file
120121
QFile configFile( filePath );
121-
if ( !configFile.exists() || !configFile.open( QIODevice::ReadOnly ) )
122+
if ( !configFile.exists() )
122123
{
123-
QgsDebugMsg( "File unreadable: " + filePath );
124+
QgsMessageLog::logMessage( "Error, configuration file '" + filePath + "' does not exist", "Server", QgsMessageLog::CRITICAL );
125+
return 0;
126+
}
127+
128+
if ( !configFile.open( QIODevice::ReadOnly ) )
129+
{
130+
QgsMessageLog::logMessage( "Error, cannot open configuration file '" + filePath + "'", "Server", QgsMessageLog::CRITICAL );
124131
return 0;
125132
}
126133

@@ -130,8 +137,8 @@ QDomDocument* QgsConfigCache::xmlDocument( const QString& filePath )
130137
int line, column;
131138
if ( !xmlDoc->setContent( &configFile, true, &errorMsg, &line, &column ) )
132139
{
133-
QgsDebugMsg( QString( "Parse error %1 at row %2, column %3 in %4 " )
134-
.arg( errorMsg ).arg( line ).arg( column ).arg( filePath ) );
140+
QgsMessageLog::logMessage( "Error parsing file '" + filePath +
141+
QString( "': parse error %1 at row %2, column %3" ).arg( errorMsg ).arg( line ).arg( column ), "Server", QgsMessageLog::CRITICAL );
135142
delete xmlDoc;
136143
return 0;
137144
}

‎src/mapserver/qgsserverlogger.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 <QCoreApplication>
20+
#include <QFile>
21+
#include <QTextStream>
22+
#include <QTime>
23+
24+
QgsServerLogger* QgsServerLogger::mInstance = 0;
25+
26+
QgsServerLogger* QgsServerLogger::instance()
27+
{
28+
if ( mInstance == 0 )
29+
{
30+
mInstance = new QgsServerLogger();
31+
}
32+
return mInstance;
33+
}
34+
35+
QgsServerLogger::QgsServerLogger(): mLogFile( 0 )
36+
{
37+
//logfile
38+
QString filePath = getenv( "QGIS_LOG_FILE" );
39+
mLogFile.setFileName( filePath );
40+
if ( mLogFile.open( QIODevice::Append ) )
41+
{
42+
mTextStream.setDevice( &mLogFile );
43+
}
44+
45+
//log level
46+
char* logLevelChar = getenv( "QGIS_LOG_LEVEL" );
Code has comments. Press enter to view.
47+
if ( logLevelChar )
48+
{
49+
mLogLevel = atoi( logLevelChar );
50+
}
51+
else
52+
{
53+
mLogLevel = 3;
54+
}
55+
56+
connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ), this,
57+
SLOT( logMessage( QString, QString, QgsMessageLog::MessageLevel ) ) );
58+
}
59+
60+
void QgsServerLogger::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
61+
{
62+
Q_UNUSED( tag );
63+
if ( !mLogFile.isOpen() || mLogLevel > level )
64+
{
65+
return;
66+
}
67+
68+
mTextStream << ( "[" + QString::number( qlonglong( QCoreApplication::applicationPid() ) ) + "]["
69+
+ QTime::currentTime().toString() + "] " + message + "\n" );
70+
mTextStream.flush();
71+
}

‎src/mapserver/qgsserverlogger.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/***************************************************************************
2+
qgsserverlogger.h
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+
#ifndef QGSSERVERLOGGER_H
19+
#define QGSSERVERLOGGER_H
20+
21+
#include "qgsmessagelog.h"
22+
23+
#include <QFile>
24+
#include <QObject>
25+
#include <QString>
26+
#include <QTextStream>
27+
28+
/**Writes message log into server logfile*/
29+
class QgsServerLogger: public QObject
30+
{
31+
Q_OBJECT
32+
public:
33+
static QgsServerLogger* instance();
34+
35+
int logLevel() const { return mLogLevel; }
36+
//QString logFile() const { return mLogFile; }
37+
38+
public slots:
39+
void logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level );
40+
41+
protected:
42+
QgsServerLogger();
43+
44+
private:
45+
static QgsServerLogger* mInstance;
46+
47+
QFile mLogFile;
48+
QTextStream mTextStream;
49+
int mLogLevel;
50+
};
51+
52+
#endif // QGSSERVERLOGGER_H

0 commit comments

Comments
 (0)
Please sign in to comment.