more control over webrequest timeout and logging
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 20 Dec 2008 13:40:32 +0000 (13:40 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 20 Dec 2008 13:40:32 +0000 (13:40 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@218 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/overview.cpp
src/overview.h
src/webrequest.cpp
src/webrequest.h

index 0d06a65..073b9c9 100644 (file)
@@ -101,6 +101,7 @@ MOverview::MOverview(MWebRequest*mw,QString pk)
        
        m=mb->addMenu(tr("C&onfigure"));
        m->addAction(tr("&Auto-Refresh settings..."),this,SLOT(setRefresh()));
+       m->addAction(tr("&Server Access settings..."),this,SLOT(webSettings()));
        
        m=mb->addMenu(tr("&Admin"));
        m->setEnabled(req->hasRole("_admin"));
@@ -304,6 +305,8 @@ MOverview::MOverview(MWebRequest*mw,QString pk)
        //status bar
        statusBar()->setSizeGripEnabled(true);
        
+       //make sure webrequest knows its settings
+       webSettings(false);
        //fill tables
        if(req->hasRole("geteventlist")){
                updateEvents();
@@ -1363,6 +1366,56 @@ void MOverview::setRefresh()
        rtimer.start();
 }
 
+void MOverview::webSettings(bool showdlg)
+{
+       QSettings set;
+       set.beginGroup("profiles/"+profilekey);
+       //get settings
+       MWebRequest::LogLevel lvl=MWebRequest::LogLevel(set.value("webloglevel", MWebRequest::LogOnError).toInt());
+       int timeout=set.value("webtimeout",30).toInt();
+       //dialog
+       if(showdlg){
+               QDialog d;
+               d.setWindowTitle(tr("Server Access Settings"));
+               QGridLayout*gl;
+               QHBoxLayout*hl;
+               d.setLayout(gl=new QGridLayout);
+               gl->addWidget(new QLabel(tr("Request Timeout (seconds):")),0,0);
+               QSpinBox*tmout;
+               gl->addWidget(tmout=new QSpinBox,0,1);
+               tmout->setRange(10,999);
+               tmout->setValue(timeout);
+               QComboBox *log;
+               gl->addWidget(new QLabel(tr("Log Level:")),1,0);
+               gl->addWidget(log=new QComboBox,1,1);
+               log->addItem(tr("Minimal Logging"),MWebRequest::LogMinimal);
+               log->addItem(tr("Log Details on Error"),MWebRequest::LogOnError);
+               log->addItem(tr("Always Log Details"),MWebRequest::LogDetailed);
+               switch(lvl){
+                       case MWebRequest::LogMinimal:log->setCurrentIndex(0);break;
+                       case MWebRequest::LogOnError:log->setCurrentIndex(1);break;
+                       case MWebRequest::LogDetailed:log->setCurrentIndex(2);break;
+               }
+               gl->setRowMinimumHeight(2,15);
+               gl->addLayout(hl=new QHBoxLayout,3,0,1,2);
+               hl->addStretch(10);
+               QPushButton*p;
+               hl->addWidget(p=new QPushButton(tr("&OK")));
+               connect(p,SIGNAL(clicked()),&d,SLOT(accept()));
+               hl->addWidget(p=new QPushButton(tr("&Cancel")));
+               connect(p,SIGNAL(clicked()),&d,SLOT(reject()));
+               if(d.exec()!=QDialog::Accepted)return;
+               //write settings
+               lvl=MWebRequest::LogLevel(log->itemData(log->currentIndex()).toInt());
+               timeout=tmout->value();
+               set.setValue("webloglevel",lvl);
+               set.setValue("webtimeout",timeout);
+       }
+       //reset webrequest
+       req->setLogLevel(lvl);
+       req->setTimeout(timeout*1000);
+}
+
 /**********************************************/
 
 MPasswordChange::MPasswordChange(QWidget*par,QString user)
index bc6927a..1a7bfb1 100644 (file)
@@ -143,6 +143,9 @@ class MOverview:public QMainWindow
                /**set refresh timeout*/
                void setRefresh();
                
+               /**web request settings dialog; shows the dialog per default, just copies settings from registry to webrequest object if false*/
+               void webSettings(bool dlg=true);
+               
        private:
                //my session object
                QPointer<MWebRequest>req;
index 5046f16..b9a072b 100644 (file)
 #include <QSettings>
 #include <QTimer>
 
+#include <QtDebug>
+
 MWebRequest::MWebRequest(QString pid)
        :temp(this,pid)
 {
        profileid=pid;
-       webtimeout=30000;
+       webtimeout=QSettings().value("profiles/"+pid+"/webtimeout",LogOnError).toInt()*1000;
+       loglvl=LogLevel(QSettings().value("profiles/"+pid+"/webloglevel",LogOnError).toInt());
        finid=0;finerr=fin=false;
        proxyport=-1;
        sessiontimeout=0;
@@ -56,11 +59,34 @@ QString MWebRequest::settingsGroup()const
        return "profsettings/"+profileid;
 }
 
+MWebRequest::LogLevel MWebRequest::logLevel()const
+{
+       return loglvl;
+}
+
+void MWebRequest::setLogLevel(LogLevel l)
+{
+       loglvl=l;
+}
+
+static inline QString esc(const QByteArray ar)
+{
+       QString r;
+       for(int i=0;i<ar.size();i++){
+               unsigned char a=ar[i];
+               if(a=='\\')r+="\\\\";else
+               if(a>=32 && a<=127)r+=(char)a;
+               else r+="\\x"+QByteArray((char*)&a,1).toHex();
+       }
+       return r;
+}
+
 bool MWebRequest::request(QString hreq,QByteArray data)
 {
        //show the user we are waiting
        WaitCursor wc;
        //set up request
+       QString log;
        QEventLoop loop(this);
        connect(this,SIGNAL(requestFinInt()),&loop,SLOT(quit()));
        int port=url.port();
@@ -68,7 +94,12 @@ bool MWebRequest::request(QString hreq,QByteArray data)
                if(url.scheme().toLower()=="http")port=80;
                else port=443;
        }
-       qDebug("connecting to host=%s port=%i scheme=%s request=%s",url.host().toAscii().data(),(int)port,url.scheme().toAscii().data(),hreq.toAscii().data());
+       qDebug()<<QString("New HTTP Request %1 URL %2\n\tto host=%3 port=%4 scheme=%5")
+               .arg(hreq)
+               .arg(url.toString())
+               .arg(url.host())
+               .arg(port)
+               .arg(url.scheme());
        QHttp::ConnectionMode conm;
        if(url.scheme().toLower()=="http")conm=QHttp::ConnectionModeHttp;
        else conm=QHttp::ConnectionModeHttps;
@@ -84,8 +115,9 @@ bool MWebRequest::request(QString hreq,QByteArray data)
        hrh.setValue("X-MagicSmoke-Session",sessionid);
        hrh.setContentLength(data.size());
        hrh.setContentType("application/x-magicsmoke; charset=UTF-8");
-       qDebug("----------------------->\nRequesting data with:\n%s\n\nRequest Body:\n%s\n<---->",hrh.toString().toAscii().data(),data.data());
        waitid=req.request(hrh,data);
+       if(loglvl>LogMinimal)
+               log=QString("Request %3 with header:\n%1\n\nRequest Body:\n%2\n<---->\n").arg(hrh.toString()).arg(esc(data)).arg(waitid);
        qDebug("started req %i",waitid);
        fin=false;errstr="";
        rspstatus="";rspdata.clear();
@@ -98,14 +130,18 @@ bool MWebRequest::request(QString hreq,QByteArray data)
                req.abort();
                errstr="Web Request timed out.";
                rspstatus="HTTP";
+               qDebug("Request %i timed out.",waitid);
+               if(loglvl&LogOnError)
+                       qDebug()<<log;
                return false;
        }
-       qDebug("Received Request finish for ID=%i Status=%s",finid,finerr?"Error":"Ok");
        //finished with low-level error?
        if(finerr){
                errstr="HTTP Error: "+req.errorString();
                rspstatus="HTTP";
-               qDebug("%s",errstr.toAscii().data());
+               log+=QString("Request %2 finished with HTTP level error: %1").arg(errstr).arg(waitid);
+               if(loglvl&LogOnError)
+                       qDebug()<<log;
                return false;
        }
        QHttpResponseHeader rsph=req.lastResponse();
@@ -113,14 +149,19 @@ bool MWebRequest::request(QString hreq,QByteArray data)
        if(rsph.statusCode()!=200){
                errstr="HTTP Error, return code "+QString::number(rsph.statusCode())+" "+rsph.reasonPhrase();
                rspstatus="HTTP";
+               log+=QString("Request %2 finished with HTTP level error: %1").arg(errstr).arg(waitid);
+               if(loglvl&LogOnError)
+                       qDebug()<<log;
                return false;
        }
        //get data
        rspdata=req.readAll();
        rspstatus=rsph.value("x-magicsmoke-status").remove('"').trimmed().toLower();
        //
-       qDebug("----->\nHTTP Response Headers:\n%s",rsph.toString().toAscii().data());
-       qDebug("HTTP Response Body:\n%s\n<------------------",rspdata.data());
+       if(loglvl==LogDetailed){
+               log+=QString ("----->\nHTTP Response %3 Headers:\n%1\nHTTP Response %3 Body:\n%2\n<------------------").arg(rsph.toString()).arg(esc(rspdata)).arg(waitid);
+               qDebug()<<log;
+       }
        return true;
 }
 
index 5e0e10f..d71643b 100644 (file)
@@ -111,6 +111,19 @@ class MWebRequest:public QObject
                /**returns the requested template as cached locally (see MTemplateStore for details)*/
                MTemplate getTemplate(QString);
                
+               /**log settings*/
+               enum LogLevel {
+                       /**minimal logging*/
+                       LogMinimal=0,
+                       /**log details on error*/
+                       LogOnError=1,
+                       /**always log details*/
+                       LogDetailed=0xf
+               };
+               
+               /**returns the current log level*/
+               LogLevel logLevel()const;
+               
                /**returns currently logged in user*/
                QString currentUser()const{return user;}
                
@@ -122,6 +135,9 @@ class MWebRequest:public QObject
                /**set proxy to be used*/
                void setProxy(QString,quint16,QString u=QString(),QString p=QString());
                
+               /**set log level*/
+               void setLogLevel(MWebRequest::LogLevel);
+               
                /**log in to web server; requires username/password and the local host name as arguments*/
                bool login(QString user,QString passwd,QString hostname);
                /**re-log in to web server with previously set values*/
@@ -153,6 +169,7 @@ class MWebRequest:public QObject
                QString proxyname,proxyuser,proxypass;
                QUrl url;
                int webtimeout,proxyport;
+               LogLevel loglvl;
                //login data
                QString user,passwd,sessionid,hostname;
                //accounting data