Skip to content
Permalink
Browse files

Merge pull request #3869 from mhugo/cleanup_server

Some cleanups in server
  • Loading branch information
Hugo Mercier
Hugo Mercier committed Dec 14, 2016
2 parents 8e9d49a + 56036d2 commit 02169186ec47b77297237f89837e08da6d839f36
@@ -37,9 +37,11 @@ class CORE_EXPORT QgsMessageLog : public QObject

enum MessageLevel
{
ALL = 0,
INFO = 0,
WARNING = 1,
CRITICAL = 2
CRITICAL = 2,
NONE = 3
};

//! add a message to the instance (and create it if necessary)
@@ -17,7 +17,9 @@

#include "qgsmapserviceexception.h"

QgsMapServiceException::QgsMapServiceException( const QString& code, const QString& message ): mCode( code ), mMessage( message )
QgsMapServiceException::QgsMapServiceException( const QString& code, const QString& message ):
QgsException( message ),
mCode( code ), mMessage( message )
{

}
@@ -20,6 +20,8 @@

#include <QString>

#include "qgsexception.h"

/** \ingroup server
* \class QgsMapServiceException
* \brief Exception class for WMS service exceptions.
@@ -31,7 +33,7 @@
* * "OperationNotSupported"
*/

class SERVER_EXPORT QgsMapServiceException
class SERVER_EXPORT QgsMapServiceException : public QgsException
{
public:
QgsMapServiceException( const QString& code, const QString& message );
@@ -81,7 +81,6 @@ QgsServer::QgsServer( bool captureOutput )
}
sCaptureOutput = captureOutput;
init();
saveEnvVars();
}


@@ -179,9 +178,9 @@ QFileInfo QgsServer::defaultProjectFile()
* @param parameterMap
* @param logLevel
*/
void QgsServer::printRequestParameters( const QMap< QString, QString>& parameterMap, int logLevel )
void QgsServer::printRequestParameters( const QMap< QString, QString>& parameterMap, QgsMessageLog::MessageLevel logLevel )
{
if ( logLevel > 0 )
if ( logLevel > QgsMessageLog::INFO )
{
return;
}
@@ -241,38 +240,6 @@ void QgsServer::printRequestInfos()
}
}

void QgsServer::dummyMessageHandler( QtMsgType type, const char *msg )
{
#if 0 //def QGSMSDEBUG
QString output;

switch ( type )
{
case QtDebugMsg:
output += "Debug: ";
break;
case QtCriticalMsg:
output += "Critical: ";
break;
case QtWarningMsg:
output += "Warning: ";
break;
case QtFatalMsg:
output += "Fatal: ";
}

output += msg;

QgsLogger::logMessageToFile( output );

if ( type == QtFatalMsg )
abort();
#else
Q_UNUSED( type );
Q_UNUSED( msg );
#endif
}

/**
* @brief QgsServer::configPath
* @param defaultConfigPath
@@ -317,10 +284,6 @@ bool QgsServer::init( )

QgsServerLogger::instance();

#ifndef _MSC_VER
qInstallMsgHandler( dummyMessageHandler );
#endif

QString optionsPath = getenv( "QGIS_OPTIONS_PATH" );
if ( !optionsPath.isEmpty() )
{
@@ -420,27 +383,20 @@ void QgsServer::putenv( const QString &var, const QString &val )
*/
QPair<QByteArray, QByteArray> QgsServer::handleRequest( const QString& queryString )
{
//apply environment variables
QHash< QString, QString >::const_iterator envIt = mEnvironmentVariables.constBegin();
for ( ; envIt != mEnvironmentVariables.constEnd(); ++envIt )
{
putenv( envIt.key(), envIt.value() );
}

This comment has been minimized.

Copy link
@elpaso

elpaso Dec 15, 2016

Contributor

Removing this, scares me a bit, I can't remember why but I think this was necessary, a sort of way to pass env vars to the services.
BTW, there might be a better way, for example storing the env into mEnvironmentVariables and make it available in the QgsServerInterface so that plugins and services can use them.

This comment has been minimized.

Copy link
@mhugo

mhugo Dec 15, 2016

Probably, but this is undocumented, uncovered by tests and the possible use case is not obvious.
It looked to me it was some kind of way to recover env vars that would have been modified by a service ...
I will dig a little bit more ...

This comment has been minimized.

Copy link
@elpaso

elpaso Dec 15, 2016

Contributor

@mhugo if we remove all untested/undocumented code in the server I'm afraid it will reduce to no more than 100 lines of code 😄
But I totally agree that this env var passing/manipulation, if it proved to be required, should be made explicit and wrapped into an API.

This comment has been minimized.

Copy link
@mhugo

mhugo Dec 15, 2016

After more digging: it has been introduced in commit 170c955 to pass options to the maprenderer. And the maprenderer has been removed in ab4a83b. So, I think there is no interest in keeping that.
@mhugent ?

/*
* This is mainly for python bindings, passing QUERY_STRING
* to handleRequest without using os.environment
*/
if ( ! queryString.isEmpty() )
putenv( QStringLiteral( "QUERY_STRING" ), queryString );

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

qApp->processEvents();

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

theRequestHandler->sendResponse();

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

#if 0
// The following code was used to test type conversion in python bindings
QPair<QByteArray, QByteArray> QgsServer::testQPair( QPair<QByteArray, QByteArray> pair )
{
return pair;
}
#endif

void QgsServer::saveEnvVars()
{
saveEnvVar( QStringLiteral( "MAX_CACHE_LAYERS" ) );
saveEnvVar( QStringLiteral( "DEFAULT_DATUM_TRANSFORM" ) );
}

void QgsServer::saveEnvVar( const QString& variableName )
{
const char* env = getenv( variableName.toLocal8Bit() );
if ( !env )
{
return;
}

mEnvironmentVariables.insert( variableName, QString::fromLocal8Bit( env ) );
}

@@ -33,6 +33,7 @@
#include "qgsconfigcache.h"
#include "qgscapabilitiescache.h"
#include "qgsmapsettings.h"
#include "qgsmessagelog.h"

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

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

void saveEnvVars();

//! Saves environment variable into mEnvironmentVariables if defined
void saveEnvVar( const QString& variableName );

// All functions that where previously in the main file are now
// static methods of this class
static QString configPath( const QString& defaultConfigPath,
@@ -105,10 +97,16 @@ class SERVER_EXPORT QgsServer
static void dummyMessageHandler( QtMsgType type, const char *msg );
// Mainly for debug
static void printRequestInfos();
// Mainly for debug

/**
* @brief QgsServer::printRequestParameters prints the request parameters
* @param parameterMap
* @param logLevel
*/
static void printRequestParameters(
const QMap< QString, QString>& parameterMap,
int logLevel );
QgsMessageLog::MessageLevel logLevel );

static QFileInfo defaultProjectFile();
static QFileInfo defaultAdminSLD();
static void setupNetworkAccessManager();
@@ -127,9 +125,6 @@ class SERVER_EXPORT QgsServer
//! Initialization must run once for all servers
static bool sInitialised;
static bool sCaptureOutput;

//! Pass important environment variables to the fcgi processes
QHash< QString, QString > mEnvironmentVariables;
};
#endif // QGSSERVER_H

@@ -36,7 +36,7 @@ QgsServerLogger* QgsServerLogger::instance()

QgsServerLogger::QgsServerLogger()
: mLogFile( nullptr )
, mLogLevel( 3 )
, mLogLevel( QgsMessageLog::NONE )
{
//logfile
QString filePath = getenv( "QGIS_SERVER_LOG_FILE" );
@@ -53,7 +53,7 @@ QgsServerLogger::QgsServerLogger()
char* logLevelChar = getenv( "QGIS_SERVER_LOG_LEVEL" );
if ( logLevelChar )
{
mLogLevel = atoi( logLevelChar );
mLogLevel = static_cast<QgsMessageLog::MessageLevel>( atoi( logLevelChar ) );
}

connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ), this,
@@ -30,12 +30,26 @@ class QgsServerLogger: public QObject
{
Q_OBJECT
public:

/**
* Get the singleton instance
*/
static QgsServerLogger* instance();

int logLevel() const { return mLogLevel; }
//QString logFile() const { return mLogFile; }
/**
* Get the current log level
*/
QgsMessageLog::MessageLevel logLevel() const { return mLogLevel; }

public slots:

/**
* Log a message from the server context
*
* @param message the message
* @param tag tag of the message
* @param level log level of the message
*/
void logMessage( const QString& message, const QString& tag, QgsMessageLog::MessageLevel level );

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

QFile mLogFile;
QTextStream mTextStream;
int mLogLevel;
QgsMessageLog::MessageLevel mLogLevel;
};

#endif // QGSSERVERLOGGER_H

0 comments on commit 0216918

Please sign in to comment.
You can’t perform that action at this time.