Skip to content

Commit 0216918

Browse files
author
Hugo Mercier
authored
Merge pull request #3869 from mhugo/cleanup_server
Some cleanups in server
2 parents 8e9d49a + 56036d2 commit 0216918

9 files changed

+161
-273
lines changed

src/core/qgsmessagelog.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ class CORE_EXPORT QgsMessageLog : public QObject
3737

3838
enum MessageLevel
3939
{
40+
ALL = 0,
4041
INFO = 0,
4142
WARNING = 1,
42-
CRITICAL = 2
43+
CRITICAL = 2,
44+
NONE = 3
4345
};
4446

4547
//! add a message to the instance (and create it if necessary)

src/server/qgsmapserviceexception.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
#include "qgsmapserviceexception.h"
1919

20-
QgsMapServiceException::QgsMapServiceException( const QString& code, const QString& message ): mCode( code ), mMessage( message )
20+
QgsMapServiceException::QgsMapServiceException( const QString& code, const QString& message ):
21+
QgsException( message ),
22+
mCode( code ), mMessage( message )
2123
{
2224

2325
}

src/server/qgsmapserviceexception.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <QString>
2222

23+
#include "qgsexception.h"
24+
2325
/** \ingroup server
2426
* \class QgsMapServiceException
2527
* \brief Exception class for WMS service exceptions.
@@ -31,7 +33,7 @@
3133
* * "OperationNotSupported"
3234
*/
3335

34-
class SERVER_EXPORT QgsMapServiceException
36+
class SERVER_EXPORT QgsMapServiceException : public QgsException
3537
{
3638
public:
3739
QgsMapServiceException( const QString& code, const QString& message );

src/server/qgsserver.cpp

+5-74
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ QgsServer::QgsServer( bool captureOutput )
8181
}
8282
sCaptureOutput = captureOutput;
8383
init();
84-
saveEnvVars();
8584
}
8685

8786

@@ -179,9 +178,9 @@ QFileInfo QgsServer::defaultProjectFile()
179178
* @param parameterMap
180179
* @param logLevel
181180
*/
182-
void QgsServer::printRequestParameters( const QMap< QString, QString>& parameterMap, int logLevel )
181+
void QgsServer::printRequestParameters( const QMap< QString, QString>& parameterMap, QgsMessageLog::MessageLevel logLevel )
183182
{
184-
if ( logLevel > 0 )
183+
if ( logLevel > QgsMessageLog::INFO )
185184
{
186185
return;
187186
}
@@ -241,38 +240,6 @@ void QgsServer::printRequestInfos()
241240
}
242241
}
243242

244-
void QgsServer::dummyMessageHandler( QtMsgType type, const char *msg )
245-
{
246-
#if 0 //def QGSMSDEBUG
247-
QString output;
248-
249-
switch ( type )
250-
{
251-
case QtDebugMsg:
252-
output += "Debug: ";
253-
break;
254-
case QtCriticalMsg:
255-
output += "Critical: ";
256-
break;
257-
case QtWarningMsg:
258-
output += "Warning: ";
259-
break;
260-
case QtFatalMsg:
261-
output += "Fatal: ";
262-
}
263-
264-
output += msg;
265-
266-
QgsLogger::logMessageToFile( output );
267-
268-
if ( type == QtFatalMsg )
269-
abort();
270-
#else
271-
Q_UNUSED( type );
272-
Q_UNUSED( msg );
273-
#endif
274-
}
275-
276243
/**
277244
* @brief QgsServer::configPath
278245
* @param defaultConfigPath
@@ -317,10 +284,6 @@ bool QgsServer::init( )
317284

318285
QgsServerLogger::instance();
319286

320-
#ifndef _MSC_VER
321-
qInstallMsgHandler( dummyMessageHandler );
322-
#endif
323-
324287
QString optionsPath = getenv( "QGIS_OPTIONS_PATH" );
325288
if ( !optionsPath.isEmpty() )
326289
{
@@ -420,27 +383,20 @@ void QgsServer::putenv( const QString &var, const QString &val )
420383
*/
421384
QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryString )
422385
{
423-
//apply environment variables
424-
QHash< QString, QString >::const_iterator envIt = mEnvironmentVariables.constBegin();
425-
for ( ; envIt != mEnvironmentVariables.constEnd(); ++envIt )
426-
{
427-
putenv( envIt.key(), envIt.value() );
428-
}
429-
430386
/*
431387
* This is mainly for python bindings, passing QUERY_STRING
432388
* to handleRequest without using os.environment
433389
*/
434390
if ( ! queryString.isEmpty() )
435391
putenv( QStringLiteral( "QUERY_STRING" ), queryString );
436392

437-
int logLevel = QgsServerLogger::instance()->logLevel();
393+
QgsMessageLog::MessageLevel logLevel = QgsServerLogger::instance()->logLevel();
438394
QTime time; //used for measuring request time if loglevel < 1
439395
QgsProject::instance()->removeAllMapLayers();
440396

441397
qApp->processEvents();
442398

443-
if ( logLevel < 1 )
399+
if ( logLevel == QgsMessageLog::INFO )
444400
{
445401
time.start();
446402
printRequestInfos();
@@ -614,7 +570,7 @@ QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryStri
614570

615571
theRequestHandler->sendResponse();
616572

617-
if ( logLevel < 1 )
573+
if ( logLevel == QgsMessageLog::INFO )
618574
{
619575
QgsMessageLog::logMessage( "Request finished in " + QString::number( time.elapsed() ) + " ms", QStringLiteral( "Server" ), QgsMessageLog::INFO );
620576
}
@@ -637,28 +593,3 @@ void QgsServer::initPython()
637593
}
638594
#endif
639595

640-
#if 0
641-
// The following code was used to test type conversion in python bindings
642-
QPair<QByteArray, QByteArray> QgsServer::testQPair( QPair<QByteArray, QByteArray> pair )
643-
{
644-
return pair;
645-
}
646-
#endif
647-
648-
void QgsServer::saveEnvVars()
649-
{
650-
saveEnvVar( QStringLiteral( "MAX_CACHE_LAYERS" ) );
651-
saveEnvVar( QStringLiteral( "DEFAULT_DATUM_TRANSFORM" ) );
652-
}
653-
654-
void QgsServer::saveEnvVar( const QString& variableName )
655-
{
656-
const char* env = getenv( variableName.toLocal8Bit() );
657-
if ( !env )
658-
{
659-
return;
660-
}
661-
662-
mEnvironmentVariables.insert( variableName, QString::fromLocal8Bit( env ) );
663-
}
664-

src/server/qgsserver.h

+9-14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qgsconfigcache.h"
3434
#include "qgscapabilitiescache.h"
3535
#include "qgsmapsettings.h"
36+
#include "qgsmessagelog.h"
3637

3738
#ifdef HAVE_SERVER_PYTHON_PLUGINS
3839
#include "qgsserverplugins.h"
@@ -73,10 +74,6 @@ class SERVER_EXPORT QgsServer
7374
* @return the response headers and body QPair of QByteArray if called from python bindings, empty otherwise
7475
*/
7576
QPair<QByteArray, QByteArray> handleRequest( const QString& queryString = QString() );
76-
#if 0
77-
// The following code was used to test type conversion in python bindings
78-
QPair<QByteArray, QByteArray> testQPair( QPair<QByteArray, QByteArray> pair );
79-
#endif
8077

8178
#ifdef HAVE_SERVER_PYTHON_PLUGINS
8279
//! Returns a pointer to the server interface
@@ -92,11 +89,6 @@ class SERVER_EXPORT QgsServer
9289
//! Server initialization
9390
static bool init();
9491

95-
void saveEnvVars();
96-
97-
//! Saves environment variable into mEnvironmentVariables if defined
98-
void saveEnvVar( const QString& variableName );
99-
10092
// All functions that where previously in the main file are now
10193
// static methods of this class
10294
static QString configPath( const QString& defaultConfigPath,
@@ -105,10 +97,16 @@ class SERVER_EXPORT QgsServer
10597
static void dummyMessageHandler( QtMsgType type, const char *msg );
10698
// Mainly for debug
10799
static void printRequestInfos();
108-
// Mainly for debug
100+
101+
/**
102+
* @brief QgsServer::printRequestParameters prints the request parameters
103+
* @param parameterMap
104+
* @param logLevel
105+
*/
109106
static void printRequestParameters(
110107
const QMap< QString, QString>& parameterMap,
111-
int logLevel );
108+
QgsMessageLog::MessageLevel logLevel );
109+
112110
static QFileInfo defaultProjectFile();
113111
static QFileInfo defaultAdminSLD();
114112
static void setupNetworkAccessManager();
@@ -127,9 +125,6 @@ class SERVER_EXPORT QgsServer
127125
//! Initialization must run once for all servers
128126
static bool sInitialised;
129127
static bool sCaptureOutput;
130-
131-
//! Pass important environment variables to the fcgi processes
132-
QHash< QString, QString > mEnvironmentVariables;
133128
};
134129
#endif // QGSSERVER_H
135130

src/server/qgsserverlogger.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ QgsServerLogger* QgsServerLogger::instance()
3636

3737
QgsServerLogger::QgsServerLogger()
3838
: mLogFile( nullptr )
39-
, mLogLevel( 3 )
39+
, mLogLevel( QgsMessageLog::NONE )
4040
{
4141
//logfile
4242
QString filePath = getenv( "QGIS_SERVER_LOG_FILE" );
@@ -53,7 +53,7 @@ QgsServerLogger::QgsServerLogger()
5353
char* logLevelChar = getenv( "QGIS_SERVER_LOG_LEVEL" );
5454
if ( logLevelChar )
5555
{
56-
mLogLevel = atoi( logLevelChar );
56+
mLogLevel = static_cast<QgsMessageLog::MessageLevel>( atoi( logLevelChar ) );
5757
}
5858

5959
connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ), this,

src/server/qgsserverlogger.h

+17-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,26 @@ class QgsServerLogger: public QObject
3030
{
3131
Q_OBJECT
3232
public:
33+
34+
/**
35+
* Get the singleton instance
36+
*/
3337
static QgsServerLogger* instance();
3438

35-
int logLevel() const { return mLogLevel; }
36-
//QString logFile() const { return mLogFile; }
39+
/**
40+
* Get the current log level
41+
*/
42+
QgsMessageLog::MessageLevel logLevel() const { return mLogLevel; }
3743

3844
public slots:
45+
46+
/**
47+
* Log a message from the server context
48+
*
49+
* @param message the message
50+
* @param tag tag of the message
51+
* @param level log level of the message
52+
*/
3953
void logMessage( const QString& message, const QString& tag, QgsMessageLog::MessageLevel level );
4054

4155
protected:
@@ -46,7 +60,7 @@ class QgsServerLogger: public QObject
4660

4761
QFile mLogFile;
4862
QTextStream mTextStream;
49-
int mLogLevel;
63+
QgsMessageLog::MessageLevel mLogLevel;
5064
};
5165

5266
#endif // QGSSERVERLOGGER_H

0 commit comments

Comments
 (0)