From bb9385efa1d895d586c5b44c66b93b3e2a4ffa1c Mon Sep 17 00:00:00 2001
From: Kjell Otto <otto.kjell@gmail.com>
Date: Mon, 11 Mar 2013 13:14:59 +0100
Subject: [PATCH] - Added the --debug parameter to x2goclient.

- Refactored the debugging of "x2goDebug" to contain __FILE__:__LINE__ in order to let users provide more useful informations.

- Added x2goInfof(NUM), x2goWarningf(NUM), x2goErrorf(NUM) to enable parsable outputs for the client.

- Added the show_printouts.sh file to be used for looking into sourcefiles info, warning and error messages. pure convenience here.
---
 .gitignore              |    7 +
 onmainwindow.cpp        |  969 +++++++++++++++++++++++++++++++----------------
 onmainwindow.h          |    3 +
 show_printouts.sh       |   30 ++
 sshmasterconnection.cpp |   88 ++++-
 x2goclientconfig.h      |    2 +-
 x2gologdebug.cpp        |    1 -
 x2gologdebug.h          |   33 +-
 8 files changed, 795 insertions(+), 338 deletions(-)
 create mode 100755 show_printouts.sh

diff --git a/.gitignore b/.gitignore
index 721e32c..bd81db2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,11 @@
 # Temporary object files/static libs.
 *.[oa]
 
+client_build/
+plugin_build/
+.build_man2html
+x2goclient
+
 # Generated by qmake.
 moc_*.cpp
 ui_*.h
@@ -14,4 +19,6 @@ x2goclient_*.qm
 
 # Generated by qmake when building on OS X.
 x2goclient.app
+x2goclient.dmg
 
+x2goclient.pro.user
diff --git a/onmainwindow.cpp b/onmainwindow.cpp
index c9741f8..f973014 100644
--- a/onmainwindow.cpp
+++ b/onmainwindow.cpp
@@ -36,18 +36,20 @@ bool	ONMainWindow::portable=false;
 QString ONMainWindow::homeDir;
 QString ONMainWindow::sessionCfg;
 
-
 #ifdef Q_OS_WIN
 QString ONMainWindow::u3Device;
 #endif
 
+bool ONMainWindow::debugging=false;
 
 ONMainWindow::ONMainWindow ( QWidget *parent ) :QMainWindow ( parent )
 {
 #ifdef Q_OS_LINUX
     image=shape=0;
 #endif
-    x2goDebug<<"ONMainWindow constructor"<<endl;
+    x2goInfof(1) << tr("Starting x2goclient...");
+    debugging = false;
+
     setFocusPolicy ( Qt::NoFocus );
     installTranslator();
     cleanAllFiles=false;
@@ -215,8 +217,8 @@ ONMainWindow::ONMainWindow ( QWidget *parent ) :QMainWindow ( parent )
             portableDataPath=QDir::currentPath();
 
         homeDir=portableDataPath;
-        x2goDebug<<"running in \"portable\" mode\n"<<
-                 "Data Dir is "<<portableDataPath;
+
+        x2goInfof(2)<<tr("Starting x2goclient in portable mode... data directory is: ")<<portableDataPath;
         QTimer *timer = new QTimer(this);
         connect(timer, SIGNAL(timeout()), this, SLOT(slotCheckPortableDir()));
         timer->start(1000);
@@ -233,7 +235,6 @@ ONMainWindow::ONMainWindow ( QWidget *parent ) :QMainWindow ( parent )
     trayIconInit();
 
 
-
     if ( embedMode )
     {
         miniMode=false;
@@ -262,13 +263,11 @@ ONMainWindow::ONMainWindow ( QWidget *parent ) :QMainWindow ( parent )
 
     QDesktopWidget wd;
 
-    //x2goDebug<<"primary screen geometry: "<<wd.screenGeometry(wd.screenNumber(this));
-
     if ( wd.screenGeometry(wd.screenNumber(this)).width() <1024 ||
             wd.screenGeometry(wd.screenNumber(this)).height() <768 )
     {
         miniMode=true;
-        x2goDebug<<"Switching to \"mini\" mode";
+        x2goDebug<<"Switching to \"mini\" mode...";
     }
 
 
@@ -383,7 +382,8 @@ ONMainWindow::ONMainWindow ( QWidget *parent ) :QMainWindow ( parent )
     initStatusDlg();
 
 #if defined(CFGPLUGIN) && defined(Q_OS_LINUX)
-    x2goDebug<<"create embedContainer"<<endl;
+
+    x2goDebug<<"Creating embedded container.";
     embedContainer=new QX11EmbedContainer ( fr );
 
 #endif
@@ -449,9 +449,11 @@ ONMainWindow::ONMainWindow ( QWidget *parent ) :QMainWindow ( parent )
 
     connect( xineramaTimer, SIGNAL(timeout()), this, SLOT(slotConfigXinerama()));
 
-    x2goDebug<<"ONMainWindows constructor finished, home Directory is:"<<homeDir<<endl;
+    x2goInfof(3)<<tr("Started  x2goclient.");
+    x2goDebug<<"$HOME=" + homeDir.toAscii();
     if (thinMode)
     {
+        x2goDebug<<"ThinMode is active.";
         showMaximized();
         activateWindow();
         raise();
@@ -461,10 +463,10 @@ ONMainWindow::ONMainWindow ( QWidget *parent ) :QMainWindow ( parent )
 
 ONMainWindow::~ONMainWindow()
 {
-    x2goDebug<<"ONMainWindow destructor";
+    x2goDebug<<"Destroying x2goclient...";
     if ( !closeEventSent )
         closeClient();
-    x2goDebug<<"end of ONMainWindow destructor";
+    x2goDebug<<"Destroyed  x2goclient.";
 }
 
 void ONMainWindow::slotShutdownThinClient()
@@ -509,13 +511,12 @@ void ONMainWindow::installTranslator()
     filename=filename.toLower();
     if ( !x2goclientTranslator->load ( filename ) )
     {
-        qDebug ( "Can't load translator (%s) !\n",
-                 filename.toLocal8Bit().data() );
+        x2goWarningf(1)<<tr("Can't load translator: ") + filename.toAscii();
     }
     else
     {
         QCoreApplication::installTranslator ( x2goclientTranslator );
-// 		x2goDebug<<"translator "<<filename<< " installed";
+        x2goInfof(4)<<tr("Translator: ") + filename.toAscii() + tr(" installed.");
     }
 
 
@@ -523,13 +524,12 @@ void ONMainWindow::installTranslator()
     filename=QString ( ":/qt_%1" ).arg ( QLocale::system().name() );
     if ( !qtTranslator->load ( filename ) )
     {
-        x2goDebug<< "Can't load translator "<<
-                 filename.toLocal8Bit().data() ;
+        x2goWarningf(2)<<tr("Can't load translator: ") + filename.toAscii();
     }
     else
     {
         QCoreApplication::installTranslator ( qtTranslator );
-// 		x2goDebug<<"translator "<<filename<< " installed";
+        x2goInfof(5)<<tr("Translator: ") + filename.toAscii() + tr(" installed.");
     }
 }
 
@@ -646,6 +646,7 @@ void ONMainWindow::initWidgetsEmbed()
     if ( config.confFS&& ( !config.useFs ) )
     {
         x2goDebug<<"hide share";
+
         act_shareFolder->setVisible ( false );
     }
 
@@ -907,7 +908,8 @@ void ONMainWindow::slotTestConnection()
 
 void ONMainWindow::slotChangeBrokerPass()
 {
-    x2goDebug<<"change broker pass";
+    x2goDebug<<"Changing broker password.";
+
     BrokerPassDlg passDlg;
     if (passDlg.exec()!=QDialog::Accepted)
         return;
@@ -930,7 +932,7 @@ void ONMainWindow::slotCheckPortableDir()
 {
     if (!QFile::exists(homeDir))
     {
-        x2goDebug<<"portable dir not exists, close";
+        x2goDebug<<"Portable directory does not exists, closing.";
         close();
     }
 }
@@ -1077,8 +1079,12 @@ void ONMainWindow::trayIconInit()
             trayIcon->setIcon(QIcon ( ":icons/128x128/x2go.png") );
             trayIcon->setToolTip(tr("Left mouse button to hide/restore - Right mouse button to display context menu"));
         }
-        trayIcon->show();
-        plugAppsInTray();
+
+        if (!startHidden)
+        {
+            trayIcon->show();
+            plugAppsInTray();
+        }
     }
 #endif
 }
@@ -1093,7 +1099,8 @@ QMenu* ONMainWindow::initTrayAppMenu(QString text, QPixmap icon)
 
 void ONMainWindow::slotAppMenuTriggered(QAction* action)
 {
-    x2goDebug<<"slotAppMenuTriggered :"<<action->data().toString()<<endl;
+    x2goDebug<<"slotAppMenuTriggered: "<<action->data().toString();
+
     if (action->data().toString() != "")
         runApplication(action->data().toString());
 }
@@ -1103,7 +1110,9 @@ void ONMainWindow::plugAppsInTray()
     if (!trayIcon)
         return;
     removeAppsFromTray();
-    x2goDebug<<"plugging apps\n";
+
+    x2goDebug<<"Plugging apps in tray.";
+
     bool empty=true;
     topActions.clear();
     foreach(Application app, applications)
@@ -1133,7 +1142,9 @@ void ONMainWindow::removeAppsFromTray()
 {
     if (!trayIcon)
         return;
-    x2goDebug<<"remove apps\n";
+
+    x2goDebug<<"Removing apps from tray";
+
     for (int i=0; i<=Application::OTHER; ++i)
     {
         appMenu[i]->clear();
@@ -1237,13 +1248,13 @@ void ONMainWindow::slotResize ( const QSize sz )
 
 void ONMainWindow::closeClient()
 {
+    x2goInfof(6)<<tr("Closing x2goclient...");
     if(trayIcon)
         trayIcon->hide();
     closeEventSent=true;
     if ( !startMaximized && !startHidden && !embedMode )
     {
-        x2goDebug<<"saving settings";
-
+        x2goDebug<<"Saving settings...";
         X2goSettings st ( "sizes" );
 
         st.setting()->setValue ( "mainwindow/size",
@@ -1252,7 +1263,7 @@ void ONMainWindow::closeClient()
         st.setting()->setValue ( "mainwindow/maximized",
                                  QVariant ( isMaximized() ) );
         st.setting()->sync();
-        x2goDebug<<"done";
+        x2goDebug<<"Saved  settings.";
 #ifdef Q_OS_LINUX
         if (image)
             XFreePixmap(QX11Info::display(),image);
@@ -1264,20 +1275,19 @@ void ONMainWindow::closeClient()
     {
         if ( nxproxy->state() ==QProcess::Running )
         {
-            x2goDebug<<"terminate proxy";
+            x2goDebug<<"Terminating proxy...";
             nxproxy->terminate();
-            x2goDebug<<"done";
+            x2goDebug<<"Terminated  proxy.";
         }
-        x2goDebug<<"delete proxy process";
+        x2goDebug<<"Deleting proxy...";
         delete nxproxy;
-        x2goDebug<<"done";
-
+        x2goDebug<<"Deleted  proxy.";
     }
     if ( sshConnection && !useLdap)
     {
-        x2goDebug<<"waiting sshConnection to finish\n";
+        x2goDebug<<"Waiting for the SSH connection to finish...";
         delete sshConnection;
-        x2goDebug<<"sshConnection is closed\n";
+        x2goDebug<<"Waited  for the SSH connection to finish.";
         sshConnection=0;
     }
     if (useLdap)
@@ -1286,51 +1296,29 @@ void ONMainWindow::closeClient()
         {
             if (serverSshConnections[i])
             {
-                x2goDebug<<"waiting sshConnection to finish\n";
+                x2goDebug<<"Waiting for the SSH connection to finish...";
                 delete serverSshConnections[i];
-                x2goDebug<<"sshConnection is closed\n";
+                x2goDebug<<"Waited  for the SSH connection to finish.";
             }
         }
     }
 
-    /*	if ( tunnel!=0l )
-    	{
-    		x2goDebug<<"delete tunnel";
-    		delete tunnel;
-    		x2goDebug<<"done";
-    	}
-    	if ( sndTunnel!=0l )
-    	{
-    		x2goDebug<<"delete snd tunnel";
-    		delete sndTunnel;
-    		x2goDebug<<"done";
-    	}
-    	if ( fsTunnel!=0l )
-    	{
-    		x2goDebug<<"delete fs tunnel";
-    		delete fsTunnel;
-    		x2goDebug<<"done";
-    	}
-    	*/
     if ( soundServer )
     {
-        x2goDebug<<"delete snd server";
+        x2goDebug<<"Deleting the sound server...";
         delete soundServer;
-        x2goDebug<<"done";
+        x2goDebug<<"Deleted  the sound server.";
     }
     if ( gpgAgent!=0l )
     {
-        x2goDebug<<"terminate gpg-agent";
         if ( gpgAgent->state() ==QProcess::Running )
+        {
+            x2goDebug<<"Terminating gpg-agent...";
             gpgAgent->terminate();
-        x2goDebug<<"done";
-    }
-    /*	if ( useSshAgent )
-    	{
-    		x2goDebug<<"terminate ssh-agent";
-    		finishSshAgent();
-    		x2goDebug<<"done";
-    	}*/
+            x2goDebug<<"Terminated  gpg-agent.";
+        }
+    }
+
 #ifndef Q_OS_WIN
     if ( agentPid.length() >0 )
     {
@@ -1345,13 +1333,15 @@ void ONMainWindow::closeClient()
 #ifdef Q_OS_WIN
     if ( pulseServer )
     {
+        x2goDebug<<"Deleting the pulse timer...";
         delete pulseTimer;
-
-        x2goDebug<<"terminate pulse";
+        x2goDebug<<"Deleted  the pulse timer.";
+        x2goDebug<<"Killing the pulse sound server...";
         pulseServer->kill();
-        x2goDebug<<"done\ndelete pulse process";
+        x2goDebug<<"Killed  the pulse sound server.";
+        x2goDebug<<"Deleting the pulse process...";
         delete pulseServer;
-        x2goDebug<<"done";
+        x2goDebug<<"Deleted  the pulse process.";
 
         QDir dr ( homeDir );
         dr.remove ( pulseDir+"/config.pa" );
@@ -1361,11 +1351,12 @@ void ONMainWindow::closeClient()
     }
     if ( xorg )
     {
-        x2goDebug<<"terminate xorg";
+        x2goDebug<<"Terminating xorg...";
         xorg->terminate();
-        x2goDebug<<"done\ndelete xorg process";
+        x2goDebug<<"Terminated  xorg.";
+        x2goDebug<<"Deleting xorg...";
         delete xorg;
-        x2goDebug<<"done";
+        x2goDebug<<"Deleted xorg.";
     }
 
     if ( winSshdStarted )
@@ -1377,8 +1368,9 @@ void ONMainWindow::closeClient()
 #else
     if ( userSshd && sshd )
     {
+        x2goDebug<<"Terminating sshd...";
         sshd->terminate();
-        x2goDebug<<"terminating userspace sshd";
+        x2goDebug<<"Terminated  sshd.";
         delete sshd;
     }
 
@@ -1407,11 +1399,14 @@ void ONMainWindow::closeClient()
         cleanPortable();
     }
     SshMasterConnection::finalizeLibSsh();
+
+    x2goInfof(7)<<tr("Closed  x2goclient.");
 }
 
 void ONMainWindow::closeEvent ( QCloseEvent* event )
 {
-    x2goDebug<<"close event";
+    x2goDebug<<"Close event received.";
+
     if (trayNoclose && !brokerMode)
     {
         hide();
@@ -1574,7 +1569,9 @@ void ONMainWindow::displayUsers()
     if ( usePGPCard  && !PGPInited)
     {
         PGPInited=true;
-        x2goDebug<<"Users loaded, starting smart card daemon\n";
+
+        x2goDebug<<"Users loaded, starting smart card daemon.";
+
         QTimer::singleShot ( 10, this, SLOT ( slotStartPGPAuth() ) );
     }
 }
@@ -1734,8 +1731,8 @@ void ONMainWindow::slotPassEnter()
         {
             sPort=sL.front().c_str();
         }
-        x2goDebug<<server.name<<": ssh port is "<<sPort;
 
+        x2goDebug<<"SSH-Server("<<server.name<<") port: "<<sPort;
 
         QString sFactor="1";
         list<string> serialNumber=LDAPSession::getStringAttrValues (
@@ -1744,7 +1741,9 @@ void ONMainWindow::slotPassEnter()
         {
             sFactor=serialNumber.front().c_str();
         }
-        x2goDebug<<server.name<<": factor is "<<sFactor;
+
+        x2goDebug<<"SSH-Server("<<server.name<<") factor: "<<sFactor;
+
         server.factor=sFactor.toFloat();
         server.sess=0;
         server.sshPort=sPort;
@@ -2140,10 +2139,16 @@ void ONMainWindow::slotReadSessions()
         st= new X2goSettings( "sessions" );
 
     QStringList slst=st->setting()->childGroups();
-    x2goDebug<<"read "<<slst.size()<<" sessions from config file";
+
+    x2goDebug<<"Reading "<<slst.size()<<" sessions from config file.";
+
     if (brokerMode && (slst.size()==0))
     {
-        QMessageBox::critical(this,tr("Error"),tr("X2Go sessions not found"));
+        QString error = tr("No X2Go sessions found, closing.");
+        if(!startHidden)
+            QMessageBox::critical(this,tr("Error"),error);
+        else
+            x2goErrorf(1)<<error; // tr("No X2Go sessions not found, closing.");
         close();
         return;
     }
@@ -2329,7 +2334,9 @@ void ONMainWindow::displayToolBar ( bool show )
 bool ONMainWindow::initLdapSession ( bool showError )
 {
 #ifdef USELDAP
-    x2goDebug<<"initing LDAP Session"<<endl;
+
+    x2goDebug<<"Initializing LDAP sessions...";
+
     try
     {
         ld=new LDAPSession ( ldapServer.toStdString(),
@@ -2340,7 +2347,9 @@ bool ONMainWindow::initLdapSession ( bool showError )
         QString message="Exeption0 in: ";
         message=message+e.err_type.c_str();
         message=message+" : "+e.err_str.c_str();
-        x2goDebug <<message<<endl;
+
+        x2goDebug<<message;
+
         if ( ldapServer1.length() )
         {
             try
@@ -2354,7 +2363,9 @@ bool ONMainWindow::initLdapSession ( bool showError )
                 QString message="Exeption1 in: ";
                 message=message+e.err_type.c_str();
                 message=message+" : "+e.err_str.c_str();
-                x2goDebug <<message<<endl;
+
+                x2goDebug<<message;
+
                 if ( ldapServer2.length() )
                 {
                     try
@@ -2372,8 +2383,8 @@ bool ONMainWindow::initLdapSession ( bool showError )
                                 e.err_type.c_str();
                         message=message+" : "+
                                 e.err_str.c_str();
-                        x2goDebug <<message<<endl;
-                        x2goDebug<<"return false"<<endl;
+
+                        x2goDebug<<message;
                         if ( showError )
                             QMessageBox::critical (
                                 0l,tr ( "Error" ),
@@ -2382,12 +2393,14 @@ bool ONMainWindow::initLdapSession ( bool showError )
                                 QMessageBox::
                                 NoButton );
 
+                        x2goDebug<<"Returning false, 3rd try failed.";
                         return false;
                     }
                 }
                 else
                 {
-                    x2goDebug<<"return false"<<endl;
+                    x2goDebug<<"Returning false.";
+
                     if ( showError )
                         QMessageBox::critical (
                             0l,tr ( "Error" ),
@@ -2401,7 +2414,8 @@ bool ONMainWindow::initLdapSession ( bool showError )
         }
         else
         {
-            x2goDebug<<"return false"<<endl;
+            x2goDebug<<"Returning false.";
+
             if ( showError )
                 QMessageBox::critical ( 0l,tr ( "Error" ),
                                         message,QMessageBox::Ok,
@@ -2531,6 +2545,7 @@ bool ONMainWindow::initLdapSession ( bool showError )
         }
     }
 #endif
+    x2goDebug<<"Initialized  LDAP sessions.";
     return true;
 
 }
@@ -2723,7 +2738,7 @@ void ONMainWindow::slotSelectedFromList ( SessionButton* session )
     }
     if ( currentKey != QString::null && currentKey != "" && nopass )
     {
-        x2goDebug<<"Have key, starting session"<<endl;
+        x2goDebug<<"Starting session with key.";
         slotSessEnter();
     }
     if ( cardReady || autologin || krblogin  )
@@ -2758,17 +2773,20 @@ SshMasterConnection* ONMainWindow::startSshConnection ( QString host, QString po
         QString proxylogin, QString proxypassword, QString proxyKey,
         bool proxyAutologin)
 {
-
+    x2goInfof(8)<<tr("Starting connection to server: ") + host + ":" + port;
     SshMasterConnection* con;
-    x2goDebug<<"start new ssh connection to server:"<<host<<":"<<port<<"krb: "<<krbLogin<<endl;
+
+    x2goDebug<<"Start new ssh connection to server:"<<host<<":"<<port<<" krbLogin: "<<krbLogin;
+
     for ( int i=0; i<sshEnv.size(); ++i )
     {
 #ifndef Q_OS_WIN
         QStringList args=sshEnv[i].split ( "=" );
-        x2goDebug<<"set Env "<<args[0]<<" to "<<args[1]<<endl;
+        x2goDebug<<"Setting ENV " + args[0] + tr(" to ") + args[1];
         setenv ( args[0].toAscii(),args[1].toAscii(),1 );
 #else
-        x2goDebug<<"set Env "<<sshEnv[i]<<endl;
+        x2goDebug<<"Set ENV: "<<sshEnv[i];
+
         _putenv ( sshEnv[i].toAscii() );
 #endif
     }
@@ -2809,6 +2827,7 @@ SshMasterConnection* ONMainWindow::startSshConnection ( QString host, QString po
 
 void ONMainWindow::slotSshConnectionError ( QString message, QString lastSessionError )
 {
+    x2goErrorf(2)<< tr("Connection Error(") + message + "): " + lastSessionError;
     if ( sshConnection )
     {
         sshConnection->wait();
@@ -2816,26 +2835,35 @@ void ONMainWindow::slotSshConnectionError ( QString message, QString lastSession
         sshConnection=0l;
     }
 
-    QMessageBox::critical ( 0l,message,lastSessionError,
-                            QMessageBox::Ok,
-                            QMessageBox::NoButton );
-    setEnabled ( true );
-    passForm->setEnabled ( true );
-    slotShowPassForm();
-    pass->setFocus();
-    pass->selectAll();
+    if (!startHidden)
+    {
+        QMessageBox::critical ( 0l,message,lastSessionError,
+                                QMessageBox::Ok,
+                                QMessageBox::NoButton );
+        setEnabled ( true );
+        passForm->setEnabled ( true );
+        slotShowPassForm();
+        pass->setFocus();
+        pass->selectAll();
 
 
-    passForm->setEnabled ( true );
-    if ( startHidden )
+        passForm->setEnabled ( true );
+    }
+    else
     {
-        startHidden=false;
-        slotResize();
-        show();
-        activateWindow();
-        raise();
+        // In order to get this interaction free, we need to free this from windows and stuff
+        //    if ( startHidden )
+        //    {
+        //        startHidden=false;
+        //        slotResize();
+        //        show();
+        //        activateWindow();
+        //        raise();
+        //    }
+
+        // completely quit the application
+        trayQuit();
     }
-
 }
 
 
@@ -2843,7 +2871,7 @@ void ONMainWindow::slotSshConnectionError ( QString message, QString lastSession
 
 void ONMainWindow::slotSshConnectionOk()
 {
-    x2goDebug<<"ssh connection ok"<<endl;
+    x2goDebug<<"SSH connection established.";
     passForm->setEnabled ( true );
     if ( useLdap )
     {
@@ -2856,14 +2884,19 @@ void ONMainWindow::slotSshConnectionOk()
 
 SshMasterConnection*  ONMainWindow::findServerSshConnection(QString host)
 {
+    x2goDebug<<"Searching for SSH connections...";
     for (int i=0; i<serverSshConnections.count(); ++i)
     {
         if (serverSshConnections[i])
         {
             if (serverSshConnections[i]->getHost()==host)
+            {
+                x2goDebug<<"Found SSH connection.";
                 return serverSshConnections[i];
+            }
         }
     }
+    x2goWarningf(3)<< tr("Couldn't find a SSH connection.");
     return 0l;
 }
 
@@ -2872,7 +2905,7 @@ void ONMainWindow::slotServSshConnectionOk(QString server)
     SshMasterConnection* con=findServerSshConnection(server);
     if (!con)
         return;
-    x2goDebug<<"getting sessions on "<<server<<endl;
+    x2goDebug<<"Getting sessions on Host: " + server;
     con->executeCommand( "export HOSTNAME && x2golistsessions", this, SLOT (slotListAllSessions ( bool,QString,int ) ));
 }
 
@@ -2989,6 +3022,21 @@ void ONMainWindow::slotSshUserAuthError ( QString error )
         sshConnection=0l;
     }
 
+    //    if ( startHidden )
+    //    {
+    //        startHidden=false;
+    //        slotResize();
+    //        show();
+    //        activateWindow();
+    //        raise();
+    //    }
+    // hidden means hidden, we'll close the client afterwards.
+    if ( startHidden )
+    {
+        x2goErrorf(3)<< tr("Authentication failed: ") + error;
+        trayQuit();
+    }
+
     QMessageBox::critical ( 0l,tr ( "Authentication failed" ),error,
                             QMessageBox::Ok,
                             QMessageBox::NoButton );
@@ -3001,14 +3049,6 @@ void ONMainWindow::slotSshUserAuthError ( QString error )
 
 
     passForm->setEnabled ( true );
-    if ( startHidden )
-    {
-        startHidden=false;
-        slotResize();
-        show();
-        activateWindow();
-        raise();
-    }
 }
 
 void ONMainWindow::slotSessEnter()
@@ -3023,7 +3063,8 @@ void ONMainWindow::slotSessEnter()
     {
         if (!config.brokerAuthenticated)
         {
-            x2goDebug<<"starting broker request";
+
+            x2goDebug<<"Starting broker request.";
             slotStartBroker();
             return;
         }
@@ -3056,7 +3097,7 @@ void ONMainWindow::slotSessEnter()
 
 void ONMainWindow::continueNormalSession()
 {
-    x2goDebug<<"continue normal x2go session"<<endl;
+    x2goDebug<<"Continue normal x2go session";
     if (brokerMode)
     {
         slotListSessions(true,QString::null,0);
@@ -3248,7 +3289,8 @@ bool ONMainWindow::startSession ( const QString& sid )
     if(brokerMode)
     {
         sshPort=config.sshport;
-        x2goDebug<<"server: "<<host;
+
+        x2goDebug<<"Server: "<<host;
     }
 
     QString cmd=st->setting()->value ( sid+"/command",
@@ -3399,9 +3441,24 @@ void ONMainWindow::slotListSessions ( bool result,QString output,
                     message;
         }
 
-        QMessageBox::critical ( 0l,tr ( "Error" ),message,
-                                QMessageBox::Ok,
-                                QMessageBox::NoButton );
+        if ( !startHidden )
+        {
+            QMessageBox::critical ( 0l,tr ( "Error" ),message,
+                                    QMessageBox::Ok,
+                                    QMessageBox::NoButton );
+        }
+        else
+        {
+            QString printout = tr( "Connection failed: ")  + output.toAscii();
+
+            if ( output.indexOf ( "publickey,password" ) !=-1 )
+                x2goErrorf(4)<< tr( "Connection failed: ")  + output + tr(" - Wrong password.");
+            else
+                x2goErrorf(5)<< tr( "Connection failed: ")  + output;
+            trayQuit();
+        }
+
+
 // 		currentKey=QString::null;
         setEnabled ( true );
         passForm->setEnabled ( true );
@@ -3420,17 +3477,16 @@ void ONMainWindow::slotListSessions ( bool result,QString output,
     }
     if ( managedMode || brokerMode )
     {
-        x2goDebug<<"sess data:"<<config.sessiondata;
+        x2goDebug<<"Session data: " + config.sessiondata;
         if ( config.sessiondata.indexOf ( "|S|" ) ==-1 )
         {
-            x2goDebug<<"start new managed session";
+            x2goDebug<<"Starting new managed session.";
             startNewSession();
         }
         else
         {
-            x2goSession s=getSessionFromString (
-                              config.sessiondata );
-            x2goDebug<<"resuming managed session:"<<s.sessionId;
+            x2goSession s=getSessionFromString (config.sessiondata);
+            x2goDebug<<"Resuming managed session with Id: " + s.sessionId;
             resumeSession ( s );
         }
         return;
@@ -3718,7 +3774,9 @@ void ONMainWindow::startNewSession()
 
     QString geometry;
 #ifdef Q_OS_WIN
-    x2goDebug<<"fullscreen: "<<fullscreen;
+
+    x2goDebug<<"Fullscreen: "<<fullscreen;
+
     maximizeProxyWin=false;
     proxyWinWidth=width;
     proxyWinHeight=height;
@@ -3740,7 +3798,9 @@ void ONMainWindow::startNewSession()
 #ifdef Q_OS_WIN
 //        fullscreen=false;
         maximizeProxyWin=true;
-        x2goDebug<<"maximizeProxyWin: "<<maximizeProxyWin;
+
+        x2goDebug<<"Maximize proxy win: "<<maximizeProxyWin;
+
 #endif
     }
     if ( ! fullscreen )
@@ -3843,7 +3903,9 @@ void ONMainWindow::startNewSession()
              shadowUser+"XSHAD"+shadowDisplay;
     }
     resumingSession.fullscreen=fullscreen;
-    x2goDebug<<cmd<<endl;
+
+    x2goDebug<<"Executing remote command: "<<cmd;
+
     if ( managedMode )
     {
         slotRetResumeSess ( true,config.sessiondata,0 );
@@ -4007,7 +4069,8 @@ void ONMainWindow::resumeSession ( const x2goSession& s )
 #ifdef Q_OS_WIN
 //        fullscreen=false;
         maximizeProxyWin=true;
-        x2goDebug<<"maximizeProxyWin: "<<maximizeProxyWin;
+
+        x2goDebug<<"Maximize proxy win: "<<maximizeProxyWin;
 
 #endif
     }
@@ -4098,10 +4161,10 @@ void ONMainWindow::selectSession ( QStringList& sessions )
     setEnabled ( true );
     sessionStatusDlg->hide();
     passForm->hide();
-// 	x2goDebug<<"check type";
+
     if ( !shadowSession )
     {
-// 		x2goDebug<<"! shadow";
+        x2goDebug<<"No shadow session.";
         if ( !miniMode )
             selectSesDlgLayout->setContentsMargins ( 25,25,10,10 );
 
@@ -4110,13 +4173,13 @@ void ONMainWindow::selectSession ( QStringList& sessions )
         bTerm->show();
         sOk->show();
         sCancel->show();
-// 		x2goDebug<<"standart buttons ok";
+
         desktopFilter->hide();
         desktopFilterCb->hide();
         bShadow->hide();
         bShadowView->hide();
         bCancel->hide();
-// 		x2goDebug<<"shadow buttons ok";
+
 // 		model->clear();
         model->removeRows ( 0,model->rowCount() );
         selectSessionLabel->setText ( tr ( "Select session:" ) );
@@ -4124,10 +4187,9 @@ void ONMainWindow::selectSession ( QStringList& sessions )
         QFontMetrics fm ( sessTv->font() );
         for ( int row = 0; row < sessions.size(); ++row )
         {
-
-// 			x2goDebug<<"decoding sessionstr:"<<sessions[row];
+            x2goDebug<<"Decoding Sessionstring:" + sessions[row];
             x2goSession s=getSessionFromString ( sessions[row] );
-// 			x2goDebug<<"listing id:"<<s.sessionId;
+
             selectedSessions.append ( s );
             QStandardItem *item;
 
@@ -4412,13 +4474,18 @@ void ONMainWindow::slotTermSessFromSt()
 #ifdef Q_OS_LINUX
     if (directRDP)
     {
-        x2goDebug<<"terminating direct RDP session";
+
+        x2goDebug<<"Terminating direct RDP session.";
+
         nxproxy->terminate();
         proxyRunning=false;
         return;
     }
 #endif
-    /*	x2goDebug <<"disconnect export"<<endl;
+
+    x2goDebug<<"Disconnect export.";
+
+    /*
     	disconnect ( sbExp,SIGNAL ( clicked() ),this,
     	             SLOT ( slot_exportDirectory() ) );*/
     sbExp->setEnabled ( false );
@@ -4560,7 +4627,9 @@ void ONMainWindow::slotRetResumeSess ( bool result,
                                        QString output,
                                        int )
 {
-    x2goDebug<<"Agent output:"<<output;
+
+    x2goDebug<<"Agent output: "<<output;
+
     if ( result==false )
     {
         QString message=tr ( "<b>Connection failed</b>\n" ) +output;
@@ -4716,17 +4785,23 @@ void ONMainWindow::slotRetResumeSess ( bool result,
             if (line.indexOf("gr_port=")!=-1)
             {
                 resumingSession.grPort=line.replace("gr_port=","");
-                x2goDebug<<"new gr_port: "<<resumingSession.grPort<<endl;
+
+                x2goDebug<<"New gr_port: "<<resumingSession.grPort;
+
             }
             if (line.indexOf("sound_port=")!=-1)
             {
                 resumingSession.sndPort=line.replace("sound_port=","");
-                x2goDebug<<"new sound_port: "<<resumingSession.sndPort<<endl;
+
+                x2goDebug<<"New sound_port: "<<resumingSession.sndPort;
+
             }
             if (line.indexOf("fs_port=")!=-1)
             {
                 resumingSession.fsPort=line.replace("fs_port=","");
-                x2goDebug<<"new fs_port: "<<resumingSession.fsPort<<endl;
+
+                x2goDebug<<"New fs_port: "<<resumingSession.fsPort;
+
             }
         }
         if (resumingSession.published)
@@ -4827,13 +4902,13 @@ void ONMainWindow::slotRetResumeSess ( bool result,
             }
 #endif
             if ( sysPulse )
-
+            {
                 sshConnection->copyFile(
                     "/var/run/pulse/.pulse-cookie",
                     "~/.x2go/C-"+
                     resumingSession.sessionId+
                     "/.pulse-cookie", this, SLOT ( slotPCookieReady ( bool, QString,int )));
-
+            }
             else
             {
 #ifndef Q_OS_WIN
@@ -4938,7 +5013,9 @@ void ONMainWindow::slotTunnelOk(int)
     //waiting for X
     if ( !winServersReady )
     {
-        x2goDebug<<"waiting for win-servers";
+
+        x2goDebug<<"Waiting for win-servers.";
+
         QTimer::singleShot ( 100, this, SLOT ( slotTunnelOk() ) );
         return;
     }
@@ -4951,9 +5028,13 @@ void ONMainWindow::slotTunnelOk(int)
     if ( !d.exists() )
         if ( !d.mkpath ( dirpath ) )
         {
-            QString message=tr ( "Unable to create folder:" ) +
-                            dirpath;
-            QMessageBox::critical ( 0l,tr ( "Error" ),message,
+            if (startHidden)
+            {
+                x2goErrorf(6)<< tr( "Unable to create folder: ")  + dirpath;
+                trayQuit();
+            }
+
+            QMessageBox::critical ( 0l,tr ( "Error" ), tr ( "Unable to create folder: " ) + dirpath,
                                     QMessageBox::Ok,
                                     QMessageBox::NoButton );
             slotShowPassForm();
@@ -4977,9 +5058,13 @@ void ONMainWindow::slotTunnelOk(int)
     QFile file ( dirpath+"/options" );
     if ( !file.open ( QIODevice::WriteOnly | QIODevice::Text ) )
     {
-        QString message=tr ( "Unable to write file:" ) +
-                        dirpath+"/options";
-        QMessageBox::critical ( 0l,tr ( "Error" ),message,
+        if (startHidden)
+        {
+            x2goErrorf(7)<< tr( "Unable to write file: " ) + dirpath + "/options";
+            trayQuit();
+        }
+
+        QMessageBox::critical ( 0l,tr ( "Error" ), tr( "Unable to write file: " ) + dirpath + "/options",
                                 QMessageBox::Ok,
                                 QMessageBox::NoButton );
         slotShowPassForm();
@@ -5010,6 +5095,7 @@ void ONMainWindow::slotTunnelOk(int)
     for ( int l=0; l<env.size(); ++l )
     {
 // 		x2goDebug<<env[l]<<endl;
+
         if ( env[l].indexOf ( "X2GO_LIB" ) ==0 )
         {
             x2golibpath=env[l].split ( "=" ) [1];
@@ -5039,13 +5125,17 @@ void ONMainWindow::slotTunnelOk(int)
     if ( dispInd==-1 )
     {
         env <<"DISPLAY=localhost:"+disp;
+
         /*		x2goDebug<<"new env DISPLAY"<<"DISPLAY=localhost:"+disp<<endl;*/
+
     }
     else
     {
         env[dispInd]="DISPLAY=localhost:"+disp;
+
         /*		x2goDebug<<"existing env DISPLAY("<<dispInd<<
         		") DISPLAY=localhost:"+disp<<endl;*/
+
     }
 #endif // Q_OS_WIN
 #if defined ( Q_OS_DARWIN )
@@ -5062,17 +5152,14 @@ void ONMainWindow::slotTunnelOk(int)
     }
     if (dispInd == -1)
     {
-        x2goDebug << "No DISPLAY variable found in global environment, using autodetected setting.";
+
+        x2goDebug<< "No DISPLAY variable found in global environment, using autodetected setting.";
+
         env.append ("DISPLAY=" + disp);
     }
 #endif
     nxproxy->setEnvironment ( env );
-    /*	x2goDebug<<"new env:"<<endl;
-    	for ( int l=0;l<env.size();++l )
-    	{
-    		x2goDebug<<env[l]<<endl;
-    	}
-    	x2goDebug<<"##########################"<<endl;*/
+
     connect ( nxproxy,SIGNAL ( error ( QProcess::ProcessError ) ),this,
               SLOT ( slotProxyError ( QProcess::ProcessError ) ) );
     connect ( nxproxy,SIGNAL ( finished ( int,QProcess::ExitStatus ) ),this,
@@ -5091,7 +5178,9 @@ void ONMainWindow::slotTunnelOk(int)
     dir.cd ( "exe" );
     proxyCmd="\""+dir.absolutePath() +"/\""+proxyCmd;
 #endif //Q_OS_DARWIN
-    x2goDebug<<"starting nxproxy with: "<<proxyCmd<<endl;
+
+    x2goDebug<<"Starting NX proxy, command: " + proxyCmd;
+
     nxproxy->start ( proxyCmd );
     proxyRunning=true;
 //always search for proxyWin
@@ -5212,7 +5301,8 @@ void ONMainWindow::slotProxyFinished ( int,QProcess::ExitStatus )
     if ( spoolTimer )
         delete spoolTimer;
 
-    x2goDebug<<"deleting proxy"<<endl;
+    x2goDebug<<"Deleting Proxy." ;
+
     disconnect ( nxproxy,SIGNAL ( error ( QProcess::ProcessError ) ),this,
                  SLOT ( slotProxyError ( QProcess::ProcessError ) ) );
     disconnect ( nxproxy,SIGNAL ( finished ( int,QProcess::ExitStatus ) ),this,
@@ -5237,7 +5327,7 @@ void ONMainWindow::slotProxyFinished ( int,QProcess::ExitStatus )
     if(proxyErrString.indexOf("No data received from remote proxy")!=-1)
     {
         emergencyExit=true;
-        x2goDebug<<"Emergency exit"<<endl;
+        x2goWarningf(4)<< tr( "Emergency exit." );
     }
 
 #if ! (defined (CFGPLUGIN))
@@ -5246,21 +5336,22 @@ void ONMainWindow::slotProxyFinished ( int,QProcess::ExitStatus )
         if ( nxproxy->state() ==QProcess::Running )
         {
             emergencyExit=true;
-            x2goDebug<<"waiting for proxy to exit"<<endl;
+            x2goWarningf(5)<< tr( "Waiting for proxy to exit." );
             if ( !nxproxy->waitForFinished ( 3000 ) )
             {
-                x2goDebug<<"Failed, try to kill"<<endl;
+                x2goWarningf(6)<< tr( "Failed, killing the proxy." );
                 nxproxy->kill();
             }
         }
-        x2goDebug<<"nxproxy not running"<<endl;
+
 #ifdef Q_OS_LINUX
         if (directRDP)
             nxproxy=0;
 #endif
     }
 #endif
-    x2goDebug<<"proxy deleted"<<endl;
+    x2goDebug<<"Waiting for proxy to exit.";
+
     spoolTimer=0l;
     tunnel=sndTunnel=fsTunnel=0l;
     soundServer=0l;
@@ -5279,14 +5370,14 @@ void ONMainWindow::slotProxyFinished ( int,QProcess::ExitStatus )
     if ( !emergencyExit && !shadowSession && !usePGPCard && ! ( embedMode &&
             ( config.checkexitstatus==false ) ) )
     {
-        x2goDebug<<"checking exit status";
+        x2goDebug<<"Checking exit status.";
         check_cmd_status();
     }
     else
     {
-        x2goDebug<<"deleting sshConnection instance: "<<endl;
+        x2goDebug<<"Deleting SSH connection instance.";
         delete sshConnection;
-        x2goDebug<<"done"<<endl;
+        x2goDebug<<"Deleted  SSH connection instance." ;
         sshConnection=0;
     }
     if ( startHidden )
@@ -5324,6 +5415,7 @@ void ONMainWindow::slotProxyFinished ( int,QProcess::ExitStatus )
         sessionStatusDlg->hide();
         resumeSession ( resumingSession );
     }
+    x2goDebug<<"Finished Proxy.";
     setStatStatus ( tr ( "Finished" ) );
 }
 
@@ -5334,7 +5426,9 @@ void ONMainWindow::slotProxyStderr()
     if ( nxproxy )
         reserr= nxproxy->readAllStandardError();
     proxyErrString+=reserr;
-    x2goDebug<<reserr<<endl;
+
+    x2goDebug<<"Proxy wrote on stderr: "<<reserr;
+
     stInfo->insertPlainText ( reserr );
     stInfo->ensureCursorVisible();
     if ( stInfo->toPlainText().indexOf (
@@ -5421,7 +5515,9 @@ void ONMainWindow::slotProxyStderr()
 #if defined( Q_OS_WIN ) && defined (CFGPLUGIN)
     if ( reserr.indexOf ( "Session terminated at" ) !=-1 )
     {
-        x2goDebug<<"proxy finished"<<endl;
+
+        x2goDebug<<"Proxy finished.";
+
         slotProxyFinished ( 0, QProcess::NormalExit );
     }
 #endif
@@ -5433,8 +5529,8 @@ void ONMainWindow::slotProxyStderr()
 void ONMainWindow::slotProxyStdout()
 {
     QString resout ( nxproxy->readAllStandardOutput() );
-    x2goDebug<<resout<<endl;
 
+    x2goDebug<<"Proxy wrote on stdout: "<<resout;
 }
 
 
@@ -5588,7 +5684,7 @@ bool ONMainWindow::termSession ( QString sessId, bool warn )
         nxproxy->terminate();
         return true;
     }
-
+    x2goDebug<<"Terminating session.";
     sshConnection->executeCommand ( "x2goterminate-session "+sessId, this, SLOT ( slotRetTermSess ( bool,
                                     QString,int) )  );
     proxyRunning=false;
@@ -5970,13 +6066,15 @@ void ONMainWindow::slotReadApplications(bool result, QString output,
             if (line.indexOf("Name["+localshort+"]=")!=-1  || line.indexOf("Name["+locallong+"]=")!=-1)
             {
                 app.name=QString::fromUtf8(line.split("=")[1].toAscii());
-//                 x2goDebug<<"local name: "<<app.name<<endl;
+
+                //                 x2goDebug<<"local name: "<<app.name<<endl;
+
                 localname=true;
             }
             if (line.indexOf("Comment["+localshort+"]=")!=-1 || line.indexOf("Comment["+locallong+"]=")!=-1)
             {
                 app.comment=QString::fromUtf8(line.split("=")[1].toAscii());
-//                 x2goDebug<<"local comment: "<<app.comment<<endl;
+                //                 x2goDebug<<"local comment: "<<app.comment<<endl;
                 localcomment=true;
             }
             if (line.indexOf("Name=")!=-1 && !localname)
@@ -5984,12 +6082,12 @@ void ONMainWindow::slotReadApplications(bool result, QString output,
                 app.name=line.split("=")[1];
                 if (app.name==autostartApp)
                     startAppFound=true;
-//                 x2goDebug<<"name: "<<app.name<<endl;
+                //                 x2goDebug<<"name: "<<app.name<<endl;
             }
             if (line.indexOf("Comment=")!=-1 && !localcomment)
             {
                 app.comment=line.split("=")[1];
-//                 x2goDebug<<"comment: "<<app.comment<<endl;
+                //                 x2goDebug<<"comment: "<<app.comment<<endl;
             }
             if (line.indexOf("Exec=")!=-1)
             {
@@ -6000,7 +6098,7 @@ void ONMainWindow::slotReadApplications(bool result, QString output,
                 app.exec.replace("%u","",Qt::CaseInsensitive);
                 if (app.exec==autostartApp)
                     startAppFound=true;
-//                 x2goDebug<<"exec: "<<app.exec<<endl;
+                //                 x2goDebug<<"exec: "<<app.exec<<endl;
             }
             if (line.indexOf("Categories=")!=-1)
             {
@@ -6067,10 +6165,13 @@ void ONMainWindow::slotReadApplications(bool result, QString output,
     plugAppsInTray();
     if (runStartApp && autostartApp.length()>1)
     {
-        if (!startAppFound)
-            x2goDebug<<"Autostart application "<<autostartApp<< " not found in Desktop files"<<endl;
+        if (!startAppFound) {
+            x2goDebug<<"Autostart application "<<autostartApp<< " not found in Desktop files.";
+        }
         else
+        {
             runApplication(autostartApp);
+        }
     }
 }
 
@@ -6089,6 +6190,12 @@ bool ONMainWindow::parseParameter ( QString param )
         return false;
     }
 
+    if (param == "--debug")
+    {
+        ONMainWindow::debugging = true;
+        return true;
+    }
+
     if ( param == "--portable" )
     {
         ONMainWindow::portable=true;
@@ -6403,10 +6510,11 @@ bool ONMainWindow::parseParameter ( QString param )
         if (find!=-1 && lind !=-1)
         {
             QString label=value.mid(find+1,lind-find-1);
-            x2goDebug<< "searching for drive with label: "<<label;
+            x2goDebug<<"Searching for drive with label: "<<label;
+
             QString drive=wapiGetDriveByLabel(label);
             value.replace("("+label+")",drive);
-            x2goDebug<<"new path: "<<value;
+            x2goDebug<<"New path: "<<value;
         }
 #endif
         if (! dr.exists(value))
@@ -6623,11 +6731,15 @@ bool ONMainWindow::packParameter ( QString val )
 
 void ONMainWindow::printError ( QString param )
 {
-    qCritical ( "%s", ( tr ( "wrong parameter: " ) +param ).
-                toLocal8Bit().data() );
-#ifdef Q_OS_WIN
-    x2goDebug<<tr ( "wrong parameter: " ) +param <<endl;
-#endif
+    if( !startHidden )
+    {
+        qCritical ( "%s", ( tr ( "Wrong parameter: " ) +param ).
+                    toLocal8Bit().data() );
+    }
+    else
+    {
+        x2goErrorf(8)<<tr("Wrong parameter: ")<<param;
+    }
 }
 
 void ONMainWindow::showHelp()
@@ -6637,6 +6749,7 @@ void ONMainWindow::showHelp()
         "Options:\n"
         "--help\t\t\t\t show this message\n"
         "--help-pack\t\t\t show available pack methods\n"
+        "--debug\t\t\t\t enables extensive output for console output.\n"
         "--no-menu\t\t\t hide menu bar\n"
         "--no-session-edit\t\t not allow user to edit preconfigured\n"
         "\t\t\t\t sessions\n"
@@ -6661,7 +6774,7 @@ void ONMainWindow::showHelp()
         "--command=<cmd>\t\t\t Set default command, default value 'KDE'\n"
         "--session=<session>\t\t Start session 'session'\n"
         "--user=<username>\t\t select user 'username'\n"
-        "--geomerty=<W>x<H>|fullscreen\t set default geometry, default "
+        "--geometry=<W>x<H>|fullscreen\t set default geometry, default "
         "value '800x600'\n"
         "--dpi=<dpi>\t\t\t set dpi of x2goagent to dpi, default not set\n"
         "--link=<modem|isdn|adsl|wan|lan> set default link type, "
@@ -6677,7 +6790,10 @@ void ONMainWindow::showHelp()
         "applications\" mode\n"
         "--session-conf=<file>\t\t path to alternative session config\n";
     qCritical ( "%s",helpMsg.toLocal8Bit().data() );
-    QMessageBox::information ( this,tr ( "Options" ),helpMsg );
+    if (!startHidden)
+    {
+        QMessageBox::information ( this,tr ( "Options" ),helpMsg );
+    }
 }
 
 void ONMainWindow::showHelpPack()
@@ -6749,10 +6865,12 @@ void ONMainWindow::slotGetServers ( bool result, QString output,
                     x2goServers[j].sess=
                         lst[1].toInt() *
                         x2goServers[j].factor;
+
                     x2goDebug<<x2goServers[j].name<<
                              ": sessions "<<
                              lst[1].toInt() <<
                              ", multiplied "<<x2goServers[j].sess;
+
                     break;
                 }
         }
@@ -6826,22 +6944,26 @@ void ONMainWindow::slotListAllSessions ( bool result,QString output,
                 ( listedSessions.size() ==1 &&
                   listedSessions[0].length() <5 ) )
         {
-// 	x2goDebug<<"start New Session";
+            x2goDebug<<"Start new session.";
             startNewSession();
         }
         else if ( listedSessions.size() ==1 )
         {
-// 	x2goDebug<<"have one session";
+            x2goDebug<<"Already having a session.";
             x2goSession s=getSessionFromString (
                               listedSessions[0] );
-// 		x2goDebug<<"will procceed one session";
+
+            x2goDebug<<"Will procceed with session.";
+
             QDesktopWidget wd;
             if ( s.status=="S" && isColorDepthOk (
                         wd.depth(),s.colorDepth ) )
+            {
                 resumeSession ( s );
+            }
             else
             {
-// 				x2goDebug<<"select one Session";
+                x2goDebug<<"Please select one session...";
                 selectSession ( listedSessions );
             }
         }
@@ -6926,7 +7048,8 @@ void ONMainWindow::exportDirs ( QString exports,bool removable )
     dr.dirList=exports;
     dr.key=createRSAKey();
     QString passwd;
-    x2goDebug<<"key created on: "<<dr.key;
+
+    x2goDebug<<"Key created on: "<<dr.key;
 
     passwd=getCurrentPass();
 
@@ -7093,15 +7216,17 @@ QString ONMainWindow::createRSAKey()
 
     QStringList args;
 
-    args<<"-t"<<"rsa"<<"-b"<<"1024"<<"-N"<<""<<"-f"<<keyName;
-// 	x2goDebug <<keyName<<endl;
+    args<<"-t"<<"rsa"<<"-b"<<"1024"<<"-N"<<""<<"-f"<<keyName<<"-q";
+
+    x2goDebug<<"ssh-keygen " + args.join(" ");
 
     if ( QProcess::execute ( "ssh-keygen",args ) !=0 )
     {
-        x2goDebug <<"ssh-keygen failed" <<endl;
+        x2goDebug<<"ssh-keygen failed." <<endl;
         return QString::null;
     }
-// 	x2goDebug <<"ssh-keygen ok" <<endl;
+    x2goDebug<<"ssh-keygen succeeded.";
+
 
     QFile rsa ( "/etc/ssh/ssh_host_rsa_key.pub" );
 #ifdef Q_OS_WIN
@@ -7137,7 +7262,7 @@ QString ONMainWindow::createRSAKey()
         rsa_pub = rsa.readLine();
     else
     {
-        x2goDebug<<"error: rsa file empty";
+        x2goErrorf(9)<<tr("RSA file empty.");
         return QString::null;
     }
 
@@ -7146,7 +7271,7 @@ QString ONMainWindow::createRSAKey()
                 QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append )
        )
     {
-        x2goDebug<<"error openning key:"<<keyName;
+        x2goErrorf(10)<<tr("Can not open key: ")<<keyName;
         return keyName;
     }
     QTextStream out ( &file );
@@ -7158,9 +7283,12 @@ QString ONMainWindow::createRSAKey()
 void ONMainWindow::slotCopyKey ( bool result, QString output, int pid)
 {
     fsExportKey=sshConnection->getSourceFile(pid);
-    x2goDebug<<"exported key "<<fsExportKey;
+
+    x2goDebug<<"Exported key: "<<fsExportKey;
+
     QFile::remove ( fsExportKey );
-    x2goDebug<<"key removed";
+    x2goDebug<<"Key removed.";
+
     if ( result==false )
     {
         QString message=tr ( "<b>Connection failed</b>\n" ) +output;
@@ -7170,9 +7298,23 @@ void ONMainWindow::slotCopyKey ( bool result, QString output, int pid)
                     message;
         }
 
-        QMessageBox::critical ( 0l,tr ( "Error" ),message,
-                                QMessageBox::Ok,
-                                QMessageBox::NoButton );
+        if (!startHidden)
+        {
+            QMessageBox::critical ( 0l,tr ( "Error" ),message,
+                                    QMessageBox::Ok,
+                                    QMessageBox::NoButton );
+        }
+        else
+        {
+            QString printout = tr( "Connection failed: ")  + output.toAscii();
+
+            if ( output.indexOf ( "publickey,password" ) !=-1 )
+                x2goErrorf(11)<< tr( "Connection failed: ")  + output + tr(" - Wrong password.");
+            else
+                x2goErrorf(12)<< tr( "Connection failed: ")  + output;
+            trayQuit();
+        }
+
         QFile::remove ( fsExportKey+".pub" );
         return;
     }
@@ -7240,7 +7382,9 @@ void ONMainWindow::slotRetExportDir ( bool result,QString output,
 #ifdef Q_OS_WIN
     QDir dir;
     dir.mkpath ( authofname+"\\.x2go\\.ssh" );
-    x2goDebug<<"Creating "<<authofname+"\\.x2go\\.ssh"<<endl;
+
+    x2goDebug<<"Creating "<<authofname+"\\.x2go\\.ssh";
+
     authofname=wapiShortFileName ( authofname ) +"/.x2go";
 #endif
     authofname+="/.ssh/authorized_keys" ;
@@ -7296,13 +7440,15 @@ void ONMainWindow::slotExtTimer()
             ( QFile::ReadUser|QFile::WriteUser|QFile::ExeUser|
               QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner ) )
     {
-        x2goDebug <<"Wrong permissions on "<<readLoginsFrom <<":"<<endl;
-        x2goDebug << ( int ) ( QFile::permissions (
+
+        x2goDebug<<"Wrong permissions on "<<readLoginsFrom <<":";
+        x2goDebug<< ( int ) ( QFile::permissions (
                                    readLoginsFrom+"/." ) )
                   <<"must be"<< ( int ) ( QFile::ReadUser|QFile::WriteUser
                                           |QFile::ExeUser|QFile::ReadOwner|
                                           QFile::WriteOwner|
                                           QFile::ExeOwner ) <<endl;
+
         if ( extLogin )
             extTimer->stop();
         return;
@@ -7332,9 +7478,9 @@ void ONMainWindow::slotExtTimer()
                 }
                 if ( args[0]=="logout" )
                 {
-                    x2goDebug <<
-                              " I HAVE external logout"<<
-                              endl;
+
+                    x2goDebug<<"External logout.";
+
                     args[1].replace ( "\n","" );
                     if ( args[1].size() )
                         logoutDir=args[1];
@@ -7348,7 +7494,7 @@ void ONMainWindow::slotExtTimer()
     {
         if ( logoutDir != QString::null )
         {
-            x2goDebug <<"external logout"<<endl;
+            x2goDebug<<"External logout received";
             externalLogout ( logoutDir );
         }
     }
@@ -7356,7 +7502,7 @@ void ONMainWindow::slotExtTimer()
     {
         if ( loginDir != QString::null )
         {
-            x2goDebug <<"external login"<<endl;
+            x2goDebug<<"External login.";
             externalLogin ( loginDir );
         }
     }
@@ -7371,9 +7517,10 @@ void ONMainWindow::slotExportTimer()
             QFile::ExeUser|
             QFile::ReadOwner|QFile::WriteOwner|QFile::ExeOwner ) )
     {
-        x2goDebug <<"Wrong permissions on "<<
+
+        x2goDebug<<"Wrong permissions on "<<
                   readExportsFrom <<":"<<endl;
-        x2goDebug << ( int ) ( QFile::permissions (
+        x2goDebug<< ( int ) ( QFile::permissions (
                                    readExportsFrom+"/." ) )
                   <<"must be"<< ( int ) ( QFile::ReadUser|QFile::WriteUser
                                           |QFile::ExeUser|QFile::ReadOwner|
@@ -7707,7 +7854,9 @@ void ONMainWindow::slotScDaemonError()
 {
     QString stdOut ( scDaemon->readAllStandardError() );
     stdOut=stdOut.simplified();
-    x2goDebug<<"SCDAEMON err:"<<stdOut<<endl;
+
+    x2goDebug<<"SCDAEMON error: "<<stdOut;
+
     if ( stdOut.indexOf ( "updating slot" ) !=-1 ||
             stdOut.indexOf ( "updating status of slot" ) !=-1 )
     {
@@ -7725,7 +7874,8 @@ void ONMainWindow::slotScDaemonOut()
 {
     QString stdOut ( scDaemon->readAllStandardOutput() );
     stdOut=stdOut.simplified();
-    x2goDebug<<"SCDAEMON out:"<<stdOut<<endl;
+
+    x2goDebug<<"SCDAEMON out: "<<stdOut;
 }
 
 void ONMainWindow::slotScDaemonFinished ( int , QProcess::ExitStatus )
@@ -7733,7 +7883,9 @@ void ONMainWindow::slotScDaemonFinished ( int , QProcess::ExitStatus )
     scDaemon=0l;
     if ( isScDaemonOk )
     {
-        x2goDebug<<"scDaemon finished"<<endl;
+
+        x2goDebug<<"SCDAEMON finished."<<endl;
+
         gpg=new QProcess ( this );
         QStringList arguments;
         arguments<<"--card-status";
@@ -7755,7 +7907,9 @@ void ONMainWindow::slotGpgError()
 {
     QString stdOut ( gpg->readAllStandardError() );
     stdOut=stdOut.simplified();
-    x2goDebug<<"GPG err:"<<stdOut<<endl;
+
+    x2goDebug<<"GPG error: "<<stdOut;
+
     if ( stdOut.indexOf ( "failed" ) !=-1 )
     {
         QMessageBox::critical ( 0l,tr ( "Error" ),
@@ -7770,13 +7924,17 @@ void ONMainWindow::slotGpgError()
 void ONMainWindow::slotGpgFinished ( int exitCode,
                                      QProcess::ExitStatus exitStatus )
 {
-    x2goDebug<<"gpg finished, exit code:"<<exitCode<<" exit status:"<<
-             exitStatus<<endl;
+
+    x2goDebug<<"GPG finished, exit code: "<<exitCode;
+    x2goDebug<<"GPG finished, exit status:"<<exitStatus;
+
     if ( exitStatus==0 )
     {
         QString stdOut ( gpg->readAllStandardOutput() );
         stdOut.chop ( 1 );
-        x2goDebug<<"GPG out:"<<stdOut<<endl;
+
+        x2goDebug<<"GPG out: "<<stdOut;
+
         QStringList lines=stdOut.split ( "\n" );
         QString login;
         QString appId;
@@ -7801,10 +7959,14 @@ void ONMainWindow::slotGpgFinished ( int exitCode,
         appId=appId.simplified();
         login=login.simplified();
         authKey=authKey.simplified();
-        x2goDebug<<"card data: "<<appId<<login<<authKey<<endl;
+
+        x2goDebug<<"Card data: "<<appId<<login<<authKey;
+
         if ( login=="[not set]" || authKey == "[none]" )
         {
-            x2goDebug<<"Card not configured\n";
+
+            x2goErrorf(13)<<tr("Card not configured.");
+
             QMessageBox::critical (
                 0l,tr ( "Error" ),
                 tr (
@@ -7880,15 +8042,16 @@ void ONMainWindow::slotGpgAgentFinished ( int , QProcess::ExitStatus )
     QString gpg_agent_info=envLst[0].split ( "=" ) [1];
     QString ssh_auth_sock=envLst[2].split ( "=" ) [1];
     agentPid=envLst[4].split ( "=" ) [1];
-    x2goDebug<<gpg_agent_info<<ssh_auth_sock<<agentPid<<endl;
-    x2goDebug<<"GPGAGENT out:"<<envLst[0]<<envLst[2]<<envLst[4]<<endl;
+
+    x2goDebug<<"GPG-agent info: "<<gpg_agent_info<<ssh_auth_sock<<agentPid;
+    x2goDebug<<"GPG-agent pid: "<<agentPid;
+    x2goDebug<<"GPG-agent out:"<<envLst[0]<<envLst[2]<<envLst[4];
 
     agentCheckTimer->start ( 1000 );
     cardReady=true;
 
     sshEnv.clear();
     sshEnv<<envLst[0]<<envLst[2]<<envLst[4];
-// 	x2goDebug<<"sshenv:"<<sshEnv<<endl;
 
     if ( !useLdap )
     {
@@ -7912,7 +8075,9 @@ void ONMainWindow::slotGpgAgentFinished ( int , QProcess::ExitStatus )
         sshadd.waitForFinished ( -1 );
         QString sshout ( sshadd.readAllStandardOutput() );
         sshout=sshout.simplified();
-        x2goDebug<<"SSH-ADD out:"<<sshout<<endl;
+
+        x2goDebug<<"SSH-ADD out: "<<sshout;
+
         if(brokerMode)
         {
             broker->getUserSessions();
@@ -7931,7 +8096,9 @@ void ONMainWindow::slotGpgAgentFinished ( int , QProcess::ExitStatus )
             sshadd.waitForFinished ( -1 );
             QString sshout ( sshadd.readAllStandardOutput() );
             sshout=sshout.simplified();
-            x2goDebug<<"SSH-ADD out:"<<sshout<<endl;
+
+            x2goDebug<<"SSH-ADD out: "<<sshout;
+
             return;
         }
 
@@ -7956,13 +8123,14 @@ void ONMainWindow::slotCheckAgentProcess()
         if ( nxproxy )
             if ( nxproxy->state() ==QProcess::Running )
             {
-                x2goDebug<<"Suspending session\n";
+                x2goDebug<<"Suspending session...";
                 slotSuspendSessFromSt();
+                x2goDebug<<"Suspended  session.";
 //                 nxproxy->terminate();
             }
     }
 
-    x2goDebug<<"gpg-agent finished\n";
+    x2goDebug<<"GPG-Agent finished.";
     slotStartPGPAuth();
 }
 
@@ -7992,7 +8160,9 @@ QString ONMainWindow::getXDisplay()
         // Mac OS X 10.4 compatibility mode.
         // There, it is possible no $DISPLAY variable is set.
         // Start X11 manually. First, find a free display number.
-        x2goDebug << "entering 10.4 compat mode, checking for free X11 display";
+
+        x2goDebug<< "entering 10.4 compat mode, checking for free X11 display";
+
 
         int xFreeDisp = 0;
         QDir xtmpdir ("/tmp/.X11-unix");
@@ -8022,7 +8192,8 @@ QString ONMainWindow::getXDisplay()
         if (xFreeDisp != -1)
         {
             xsocket = "/tmp/.X11-unix/X" + QString::number (xFreeDisp);
-            x2goDebug << "Successfully detected free socket " << xsocket << ".";
+
+            x2goDebug<< "Successfully detected free socket " << xsocket << ".";
         }
 
         if (!(xsocket.isEmpty ()))
@@ -8032,18 +8203,21 @@ QString ONMainWindow::getXDisplay()
             QProcessEnvironment env = QProcessEnvironment::systemEnvironment ();
             QProcess* startx = new QProcess (this);
 
-            x2goDebug << "Starting the X server on free display port.";
+            x2goDebug<< "Starting the X server on free display port.";
+
             env.insert (0, "PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11R6/bin");
 
             startx->setProcessEnvironment (env);
             startx->start (xname + QString (" ") + xopt, QIODevice::NotOpen);
             if (startx->waitForStarted (3000))
             {
-                x2goDebug << "sleeping for three seconds";
+
+                x2goDebug<< "Sleeping for three seconds";
                 int sleeptime = 3;
                 while ((sleeptime = sleep (sleeptime))) {};
 
-                x2goDebug << "Leaving OS X 10.4 compat mode.";
+                x2goDebug<< "Leaving OS X 10.4 compat mode.";
+
             }
         }
     }
@@ -8071,7 +8245,9 @@ QString ONMainWindow::getXDisplay()
             }
             // Prepend the well-known socket path.
             xsocket.prepend ("/tmp/.X11-unix/X");
-            x2goDebug << "xsocket in compat mode: " << xsocket;
+
+            x2goDebug<< "xsocket in compat mode: " << xsocket;
+
         }
 
         unixSocket.connectToServer (xsocket);
@@ -8096,7 +8272,7 @@ QString ONMainWindow::getXDisplay()
 
                 if (ok)
                 {
-                    x2goDebug << "Returning" << QString (":") + xsocket;
+                    x2goDebug<<"Returning " << QString (":") + xsocket;
                     return (QString (":") + xsocket);
                 }
             }
@@ -8222,7 +8398,9 @@ void ONMainWindow::slotExecXmodmap()
 //  	    "echo add mod5 = ISO_Level3_Shift"
         ")| DISPLAY=:"
         +resumingSession.display+" xmodmap - ";
-    x2goDebug<<"cmd:"<<cmd;
+
+    x2goDebug<<"Executing xmodmap with cmd: "<<cmd;
+
     SshProcess* xmodProc;
     try
     {
@@ -8248,7 +8426,11 @@ void ONMainWindow::slotExecXmodmap()
 
 void ONMainWindow::check_cmd_status()
 {
-    x2goDebug<<"check command message"<<endl;
+    QString passwd;
+    QString user=getCurrentUname();
+    QString host=resumingSession.server;
+    passwd=getCurrentPass();
+
     sshConnection->executeCommand ( "x2gocmdexitmessage "+
                                     resumingSession.sessionId , this, SLOT(slotCmdMessage(bool, QString, int)));
 }
@@ -8256,6 +8438,7 @@ void ONMainWindow::check_cmd_status()
 void ONMainWindow::slotCmdMessage ( bool result,QString output,
                                     int)
 {
+    x2goDebug<<"Command Message: " + output;
     if ( result==false )
     {
         cardReady=false;
@@ -8282,10 +8465,18 @@ void ONMainWindow::slotCmdMessage ( bool result,QString output,
     {
         QString cmd=output;
         cmd.replace ( "X2GORUNCOMMAND ERR NOEXEC:","" );
-        QMessageBox::critical ( 0l,tr ( "Error" ),
-                                tr ( "Unable to execute: " ) +
-                                cmd,QMessageBox::Ok,
-                                QMessageBox::NoButton );
+
+        if(startHidden)
+        {
+            x2goErrorf(14)<< tr( "Unable to execute: ") + cmd;
+        }
+        else
+        {
+            QMessageBox::critical ( 0l,tr ( "Error" ),
+                                    tr ( "Unable to execute: " ) +
+                                    cmd,QMessageBox::Ok,
+                                    QMessageBox::NoButton );
+        }
     }
     delete sshConnection;
     sshConnection=0;
@@ -8295,8 +8486,10 @@ void ONMainWindow::slotCmdMessage ( bool result,QString output,
 int ONMainWindow::startSshFsTunnel()
 {
     fsTunReady=false;
-    x2goDebug<<"starting fs tunnel for:"<<resumingSession.sessionId<<
-             "\nfs port: "<<resumingSession.fsPort;
+
+    x2goDebug<<"Starting fs tunnel for: "<<resumingSession.sessionId;
+    x2goDebug<<"fs port: "<<resumingSession.fsPort;
+
 
     if ( resumingSession.fsPort.length() <=0 )
     {
@@ -8368,7 +8561,9 @@ void ONMainWindow::startX2goMount()
 #ifdef Q_OS_WIN
     QDir tdir;
     tdir.mkpath ( authofname+"\\.x2go\\.ssh" );
-    x2goDebug<<"Creating "<<authofname+"\\.x2go\\.ssh"<<endl;
+
+    x2goDebug<<"Creating "<<authofname+"\\.x2go\\.ssh";
+
     authofname=wapiShortFileName ( authofname ) +"/.x2go";
 #endif
     authofname+= "/.ssh/authorized_keys" ;
@@ -8488,7 +8683,6 @@ void ONMainWindow::startX2goMount()
 
     dir->pid=sshConnection->executeCommand(cmd,this,SLOT ( slotRetExportDir ( bool,
                                            QString,int) ));
-
 }
 
 void ONMainWindow::slotCheckPrintSpool()
@@ -8574,14 +8768,18 @@ bool ONMainWindow::isServerRunning ( int port )
     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
     if (iResult != 0)
     {
-        x2goDebug<<"WARNING: WSAStartup failed: "<< iResult<<endl;
+
+        x2goDebug<<"WARNING: WSAStartup failed: "<< iResult;
+
         return false;
     }
 
     addr.s_addr = inet_addr("127.0.0.1");
     if (addr.s_addr == INADDR_NONE)
     {
+
         x2goDebug<< "WARNING:  The IPv4 address entered must be a legal address\n";
+
         return false;
     }
 
@@ -8589,10 +8787,13 @@ bool ONMainWindow::isServerRunning ( int port )
     localHost = gethostbyaddr((char*)&addr,4, AF_INET);
     if (!localHost)
     {
-        x2goDebug<<"WARNING: gethostbyaddr failed: "<<WSAGetLastError()<<endl;
+
+        x2goDebug<<"WARNING: gethostbyaddr failed: "<<WSAGetLastError();
+
         return false;
     }
-    x2goDebug<<"got localhost"<<endl;
+    x2goDebug<<"got localhost";
+
 
     localIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);
 
@@ -8603,7 +8804,8 @@ bool ONMainWindow::isServerRunning ( int port )
     ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     if (ConnectSocket == INVALID_SOCKET)
     {
-        x2goDebug<<"WARNING: socket failed with error: "<< WSAGetLastError()<<endl;
+        x2goDebug<<"WARNING: socket failed with error: "<< WSAGetLastError();
+
         return false;
     }
 
@@ -8611,11 +8813,15 @@ bool ONMainWindow::isServerRunning ( int port )
     if (iResult == SOCKET_ERROR)
     {
         closesocket(ConnectSocket);
-        x2goDebug<<"Port is free: "<<port<<endl;
+
+        x2goDebug<<"Port is free: "<<port;
+
         return false;
     }
     closesocket(ConnectSocket);
-    x2goDebug<<"Port already used: "<<port<<endl;
+
+    x2goDebug<<"Port already used: "<<port;
+
     return true;
 #endif
     QTcpSocket tcpSocket ( 0 );
@@ -8673,7 +8879,9 @@ void ONMainWindow::startXOrg ()
     if (internalX==VCXSRV)
         exec=appDir+"\\vcxsrv\\vcxsrv.exe";
     winServersReady=false;
+
     x2goDebug<<"using internal X: "<<useInternalX;
+
 //#ifdef CFGCLIENT
     if (!useInternalX || internalX!=XMING)
     {
@@ -8694,10 +8902,13 @@ void ONMainWindow::startXOrg ()
                 break;
             case WIN:
                 cmdLine=xorgWinOptions;
-                x2goDebug<<"WxH:"<<xorgWidth<<"x"<<xorgHeight<<endl;
+
+                x2goDebug<<"WxH:"<<xorgWidth<<"x"<<xorgHeight;
+
                 cmdLine.replace("%w",xorgWidth);
                 cmdLine.replace("%h",xorgHeight);
-                x2goDebug<<cmdLine<<endl;
+
+                x2goDebug<<cmdLine;
                 break;
             }
         }
@@ -8733,7 +8944,9 @@ void ONMainWindow::startXOrg ()
         xorg-> setWorkingDirectory ( workingDir);
     }
 
-    x2goDebug<<"running"<<exec<<" "<<args.join(" ");
+
+    x2goDebug<<"Running "<<exec<<" "<<args.join(" ");
+
     xorg->start ( exec, args );
 
 
@@ -8760,7 +8973,9 @@ void ONMainWindow::slotCheckXOrgConnection()
     ++waitingForX;
     if (isServerRunning(6000+xDisplay))
     {
-        x2goDebug<<"X is started";
+
+        x2goDebug<<"X is started.";
+
         slotSetWinServersReady();
     }
     else
@@ -8775,7 +8990,9 @@ void ONMainWindow::slotCheckXOrgConnection()
         }
         else
         {
-            x2goDebug<<"waiting for X";
+
+            x2goDebug<<"...waiting for X.";
+
             QTimer::singleShot(1000, this, SLOT(slotCheckXOrgConnection()));
         }
     }
@@ -8805,7 +9022,8 @@ void WinServerStarter::run()
 
 void ONMainWindow::startWinServers()
 {
-    x2goDebug<<"Starting win servers"<<endl;
+
+    x2goDebug<<"Starting win servers...";
 
     QString etcDir=homeDir+"/.x2go/etc";
     QDir dr ( homeDir );
@@ -8833,7 +9051,9 @@ void ONMainWindow::startWinServers()
         startPulsed();
     }
 // #ifdef CFGCLIENT
-//     x2goDebug<<"xorg settings: "<<startXorgOnStart <<" "<< useXming<<endl;
+
+    x2goDebug<<"Xorg settings: "<< startXorgOnStart <<" useXming: "<< useXming;
+
     if ( useInternalX && (internalX== XMING))
     {
 // #endif
@@ -8880,23 +9100,29 @@ void ONMainWindow::saveCygnusSettings()
     {
         if ( haveCySolEntry() )
         {
-            x2goDebug<<"Cygnus Solutions entry exist";
+
+            x2goDebug<<"Cygnus Solutions entry exists.";
+
             cySolEntry=true;
         }
         else
         {
             cySolEntry=false;
-            x2goDebug<<"Cygnus Solutions entry not exist";
+
+            x2goDebug<<"Cygnus Solutions entry not exist.";
+
         }
 
         if ( haveCygwinEntry() )
         {
-            x2goDebug<<"Cygwin entry exist";
+
+            x2goDebug<<"Cygwin entry exist.";
+
             cyEntry=true;
         }
         else
         {
-            x2goDebug<<"Cygwin entry not exist";
+            x2goDebug<<"Cygwin entry not exist.";
             cyEntry=false;
         }
     }
@@ -8906,7 +9132,9 @@ void ONMainWindow::saveCygnusSettings()
                       QSettings::NativeFormat );
     oldEtcDir=QString::null;
     oldEtcDir=etcst.value ( "native",oldEtcDir ).toString();
-    x2goDebug<<"old etc:"<<oldEtcDir<<endl;
+
+    x2goDebug<<"Old etc: "<<oldEtcDir;
+
     QString newEtc=homeDir+"/.x2go/etc";
     QDir d ( newEtc );
     if ( !d.exists() )
@@ -8915,31 +9143,40 @@ void ONMainWindow::saveCygnusSettings()
 
     etcst.setValue ( "native",wapiShortFileName ( newEtc ) );
     etcst.sync();
-    x2goDebug<<"new etc:"<<wapiShortFileName ( newEtc ) <<endl;
+
+    x2goDebug<<"New etc: "<<wapiShortFileName ( newEtc );
+
 
     QSettings binst ( "HKEY_CURRENT_USER\\Software"
                       "\\Cygnus Solutions\\Cygwin\\mounts v2\\/bin",
                       QSettings::NativeFormat );
     oldBinDir=QString::null;
     oldBinDir=binst.value ( "native",oldBinDir ).toString();
-    x2goDebug<<"old bin:"<<oldBinDir<<endl;
+
+    x2goDebug<<"Old bin: "<<oldBinDir;
+
     QString newBin=appDir;
     newBin.replace ( "/","\\" );
     binst.setValue ( "native",wapiShortFileName ( newBin ) );
     binst.sync();
-    x2goDebug<<"new bin:"<<newBin<<endl;
+
+    x2goDebug<<"New bin: "<<newBin;
 
     QSettings tmpst ( "HKEY_CURRENT_USER\\Software"
                       "\\Cygnus Solutions\\Cygwin\\mounts v2\\/tmp",
                       QSettings::NativeFormat );
     oldTmpDir=QString::null;
     oldTmpDir=tmpst.value ( "native",oldTmpDir ).toString();
-    x2goDebug<<"old tmp:"<<oldTmpDir<<endl;
+
+    x2goDebug<<"Old tmp: "<<oldTmpDir;
+
     QString newTmp=QDir::tempPath();
     newTmp.replace ( "/","\\" );
     tmpst.setValue ( "native",wapiShortFileName ( newTmp ) );
     tmpst.sync();
-    x2goDebug<<"new tmp:"<<newTmp<<endl;
+
+    x2goDebug<<"New tmp: "<<newTmp;
+
 }
 
 void ONMainWindow::restoreCygnusSettings()
@@ -8950,7 +9187,9 @@ void ONMainWindow::restoreCygnusSettings()
         QSettings st ( "HKEY_CURRENT_USER\\Software"
                        "\\Cygnus Solutions\\Cygwin\\mounts v2\\/etc",
                        QSettings::NativeFormat );
-        x2goDebug<<"Removing /etc from cygwin mounts\n";
+
+        x2goDebug<<"Removing /etc from cygwin mounts.";
+
         st.remove ( "" );
         st.sync();
     }
@@ -8961,14 +9200,18 @@ void ONMainWindow::restoreCygnusSettings()
                        QSettings::NativeFormat );
         st.setValue ( "native",oldEtcDir );
         st.sync();
-        x2goDebug<<"Restoring /etc in cygwin mounts\n";
+
+        x2goDebug<<"Restoring /etc in cygwin mounts.";
+
     }
     if ( oldBinDir==QString::null )
     {
         QSettings st ( "HKEY_CURRENT_USER\\Software"
                        "\\Cygnus Solutions\\Cygwin\\mounts v2\\/bin",
                        QSettings::NativeFormat );
-        x2goDebug<<"Removing /bin from cygwin mounts\n";
+
+        x2goDebug<<"Removing /bin from cygwin mounts.";
+
         st.remove ( "" );
         st.sync();
     }
@@ -8979,14 +9222,18 @@ void ONMainWindow::restoreCygnusSettings()
                        QSettings::NativeFormat );
         st.setValue ( "native",oldBinDir );
         st.sync();
-        x2goDebug<<"Restoring /bin in cygwin mounts\n";
+
+        x2goDebug<<"Restoring /bin in cygwin mounts.";
+
     }
     if ( oldTmpDir==QString::null )
     {
         QSettings st ( "HKEY_CURRENT_USER\\Software"
                        "\\Cygnus Solutions\\Cygwin\\mounts v2\\/tmp",
                        QSettings::NativeFormat );
-        x2goDebug<<"Removing /tmp from cygwin mounts\n";
+
+        x2goDebug<<"Removing /tmp from cygwin mounts.";
+
         st.remove ( "" );
         st.sync();
     }
@@ -8997,7 +9244,9 @@ void ONMainWindow::restoreCygnusSettings()
                        QSettings::NativeFormat );
         st.setValue ( "native",oldTmpDir );
         st.sync();
-        x2goDebug<<"Restoring /tmp in cygwin mounts\n";
+
+        x2goDebug<<"Restoring /tmp in cygwin mounts.";
+
     }
     if ( ONMainWindow::portable )
     {
@@ -9017,7 +9266,8 @@ void ONMainWindow::removeCygwinEntry()
     QSettings st ( "HKEY_CURRENT_USER\\Software"
                    "\\Cygnus Solutions\\Cygwin",
                    QSettings::NativeFormat );
-    x2goDebug<<"Removing cygwin\n";
+    x2goDebug<<"Removing cygwin.";
+
     st.remove ( "" );
     st.sync();
 
@@ -9028,7 +9278,9 @@ void ONMainWindow::removeCySolEntry()
     QSettings st ( "HKEY_CURRENT_USER\\Software"
                    "\\Cygnus Solutions",
                    QSettings::NativeFormat );
-    x2goDebug<<"Removing cygnus solutions\n";
+
+    x2goDebug<<"Removing cygnus solutions.";
+
     st.remove ( "" );
     st.sync();
 }
@@ -9045,7 +9297,9 @@ void ONMainWindow::startPulsed()
     QDir dr ( homeDir );
     dr.mkpath ( pulseDir );
     pulseDir=wapiShortFileName ( pulseDir );
-    x2goDebug<<"template: "<<pulseDir+"/tmp"<<endl;
+
+    x2goDebug<<"pulse template: "<<pulseDir+"/tmp";
+
     QTemporaryFile* fl=new QTemporaryFile ( pulseDir+"/tmp" );
     fl->open();
     pulseDir=fl->fileName();
@@ -9053,7 +9307,9 @@ void ONMainWindow::startPulsed()
     delete fl;
     QFile::remove ( pulseDir );
     dr.mkpath ( pulseDir );
-    x2goDebug<<"pulse tmp file: "<<pulseDir<<endl;
+
+    x2goDebug<<"pulse tmp file: "<<pulseDir;
+
     QStringList pEnv=QProcess::systemEnvironment();
     for ( int i=0; i<pEnv.size(); ++i )
     {
@@ -9093,12 +9349,15 @@ void ONMainWindow::startPulsed()
     pulseServer->setWorkingDirectory ( wapiShortFileName (
                                            appDir+"\\pulse" ) );
     pulseServer->start ( "pulse\\pulseaudio.exe",pulseArgs );
-    x2goDebug<<"starting pulse\\pulseaudio.exe "<<pulseArgs.join ( " " ) <<
+
+    x2goDebug<<"Starting pulse\\pulseaudio.exe "<<pulseArgs.join ( " " ) <<
              " working dir: "<<
-             wapiShortFileName ( appDir+"\\pulse" ) <<endl;
+             wapiShortFileName ( appDir+"\\pulse" );
+
     pulseTimer=new QTimer(this);
-    x2goDebug<<"connect timer";
+
     connect (pulseTimer, SIGNAL(timeout()), this, SLOT(slotCheckPulse()));
+    x2goDebug<<"Connected timer.";
     pulseTimer->start(2000);
 }
 
@@ -9108,7 +9367,9 @@ void ONMainWindow::slotCheckPulse()
     if(pulseServer->state()!=QProcess::Running)
     {
         pulseServer->start ( "pulse\\pulseaudio.exe",pulseArgs );
-        x2goDebug<<"restarting pulse\\pulseaudio.exe "<<pulseArgs.join ( " " );
+
+        x2goDebug<<"Restarting pulse\\pulseaudio.exe "<<pulseArgs.join ( " " );
+
     }
 }
 
@@ -9116,7 +9377,9 @@ void ONMainWindow::slotCheckPulse()
 // #ifdef CFGCLIENT
 void ONMainWindow::xorgSettings()
 {
-    x2goDebug<<"getting xorg settings"<<endl;
+
+    x2goDebug<<"Getting Xorg settings.";
+
 
     X2goSettings st ( "settings" );
 
@@ -9151,7 +9414,9 @@ void ONMainWindow::xorgSettings()
 
 void ONMainWindow::slotSetWinServersReady()
 {
-    x2goDebug<<"all winservers are started\n";
+
+    x2goDebug<<"All winservers are started.";
+
     winServersReady=true;
     restoreCygnusSettings();
 }
@@ -9171,10 +9436,12 @@ void ONMainWindow::generateEtcFiles()
         QString sid, sys, user, grsid, grname;
         if ( !wapiAccountInfo ( &sid,&user,&grsid, &grname, &sys ) )
         {
-// 			x2goDebug<<"Get account info failed\n";
+
+            x2goDebug<<"Get account info failed.";
             close();
         }
 
+
 // 		x2goDebug<<"sid: "<<sid <<" system:"<<
 // 		sys<< " user: "<<user<<" group sid:"<<grsid<<
 // 		"group name: "<<grname<<endl;
@@ -9209,7 +9476,9 @@ void ONMainWindow::generateEtcFiles()
            <<appDir<<"/sftp-server\n";
 #endif
         file.close();
-        x2goDebug<<etcDir +"/sshd_config created";
+
+        x2goDebug<<etcDir +"/sshd_config created.";
+
 #ifdef Q_OS_WIN
     }
 #endif
@@ -9223,7 +9492,9 @@ void ONMainWindow::generateHostDsaKey()
     if ( !QFile::exists ( etcDir+"/ssh_host_dsa_key" ) ||
             !QFile::exists ( etcDir+"/ssh_host_dsa_key.pub" ) )
     {
-        /*		x2goDebug<<"Generating host DSA key\n";*/
+
+        x2goDebug<<"Generating host DSA key.";
+
 #ifdef Q_OS_WIN
         QString fname=cygwinPath ( wapiShortFileName ( etcDir ) ) +
                       "/ssh_host_dsa_key";
@@ -9262,7 +9533,9 @@ void ONMainWindow::startSshd()
     desktopName+=getenv ( "USERNAME" );
     char* desktop=new char[desktopName.size() +1];
     strcpy ( desktop,desktopName.c_str() );
-    x2goDebug<<"Creating desktop: "<<desktop<<endl;
+
+    x2goDebug<<"Creating desktop: "<<desktop;
+
     if ( !CreateDesktopA (
                 desktop,
                 0,
@@ -9273,7 +9546,9 @@ void ONMainWindow::startSshd()
             ) )
     {
         strcpy ( desktop,"" );
-        x2goDebug<<"Desktop creation failed, using default\n";
+
+        x2goDebug<<"Desktop creation failed, using default.";
+
     }
     ZeroMemory ( &si, sizeof ( si ) );
     ZeroMemory ( &sshd, sizeof ( sshd ) );
@@ -9299,7 +9574,8 @@ void ONMainWindow::startSshd()
     arguments<<"-f"<<etcDir +"/sshd_config"<< "-h" <<
              etcDir+"/ssh_host_dsa_key"<<"-D"<<"-p"<<clientSshPort;
     sshd->start ( appDir+"/sshd",arguments );
-    x2goDebug<<"Usermode sshd started";
+    x2goDebug<<"Usermode sshd started.";
+
 #endif
 }
 
@@ -9401,7 +9677,14 @@ void ONMainWindow::slotSetProxyWinFullscreen()
 void ONMainWindow::resizeProxyWinOnDisplay(int disp)
 {
     QRect geom=QApplication::desktop()->screenGeometry(disp-1);
-    //x2goDebug<<"resizing proxy win to fit display "<<disp<<"("<<geom<<")"<<endl;
+
+    QString geoStr =
+            "(x: " + QString("%1").arg(geom.x()) +
+            ", y: "+ QString("%1").arg(geom.y()) +
+            ", w: "+ QString("%1").arg(geom.width()) +
+            ", h: "+ QString("%1").arg(geom.height());
+    x2goDebug<<"Resizing proxy window to fit Display: " + QString("%1").arg(disp) + " " + geoStr;
+
 #ifdef Q_OS_LINUX
     XSync(QX11Info::display(),false);
     XMoveWindow(QX11Info::display(), proxyWinId,geom.x(),geom.y());
@@ -9444,14 +9727,21 @@ void ONMainWindow::slotConfigXinerama()
     QRect newGeometry=proxyWinGeometry();
     if (newGeometry.isNull())
     {
-//     x2goDebug<<"error getting window geometry (window closed?)\n";
+        x2goWarningf(7)<< tr("Error getting window geometry (window closed)?");
         xineramaTimer->stop();
         return;
     }
     if (newGeometry==lastDisplayGeometry)
         return;
     lastDisplayGeometry=newGeometry;
-//    x2goDebug<<"New proxy geometry: "<<lastDisplayGeometry<<endl;
+
+    QString geoStr =
+            "(x: " + QString("%1").arg(lastDisplayGeometry.x()) +
+            ", y: "+ QString("%1").arg(lastDisplayGeometry.y()) +
+            ", w: "+ QString("%1").arg(lastDisplayGeometry.width()) +
+            ", h: "+ QString("%1").arg(lastDisplayGeometry.height());
+    x2goDebug<<"New proxy geometry: " + geoStr;
+
     QDesktopWidget* root=QApplication::desktop();
     QList<QRect> newXineramaScreens;
     for (int i=0; i< root->numScreens(); ++i)
@@ -9463,17 +9753,19 @@ void ONMainWindow::slotConfigXinerama()
             intersection=root->screenGeometry(i).intersected(lastDisplayGeometry);
         if (!intersection.isNull())
         {
-//            x2goDebug<<"intersected with "<<i<<": "<<intersection<<endl;
+            //            x2goDebug<<"intersected with "<<i<<": "<<intersection<<endl;
             intersection.moveLeft(intersection.x()-lastDisplayGeometry.x());
             intersection.moveTop(intersection.y()-lastDisplayGeometry.y());
-//            x2goDebug<<"xinerama screen: "<<intersection<<endl;
+
+            //            x2goDebug<<"xinerama screen: "<<intersection<<endl;
             newXineramaScreens<<intersection;
         }
     }
     if (xineramaScreens != newXineramaScreens)
     {
         xineramaScreens=newXineramaScreens;
-//        x2goDebug<<"xinerama screen changed, new screens: "<<xineramaScreens<<endl;
+        //        x2goDebug<<"xinerama screen changed, new screens: "<<xineramaScreens<<endl;
+
         xineramaTimer->stop();
         QStringList screens;
         foreach (QRect disp, xineramaScreens)
@@ -9482,7 +9774,6 @@ void ONMainWindow::slotConfigXinerama()
         QString cmd="export DISPLAY=:"+resumingSession.display+";printf '"+screens.join("\\\\n")+"' >  ~/.x2go/C-"+
                     resumingSession.sessionId+"/xinerama.conf";
 
-        x2goDebug<<cmd<<endl;
         sshConnection->executeCommand(cmd, this, SLOT(slotXineramaConfigured()));
     }
 }
@@ -9514,12 +9805,14 @@ void ONMainWindow::slotXineramaConfigured()
 void ONMainWindow::slotFindProxyWin()
 {
 #ifndef Q_OS_DARWIN
-//     x2goDebug<<"search proxy win: "<<"X2GO-"+resumingSession.sessionId;
+    x2goDebug<<"Searching proxy win: X2GO-" + resumingSession.sessionId;
+
     proxyWinId=findWindow ( "X2GO-"+resumingSession.sessionId );
     bool xinerama=defaultXinerama;
     if ( proxyWinId )
     {
-        x2goDebug<<"proxy win found:"<<proxyWinId;
+        x2goDebug<<"Proxy win found: " + QString("%1").arg(proxyWinId);
+
         setProxyWinTitle();
         proxyWinTimer->stop();
         if (!embedMode)
@@ -9555,7 +9848,8 @@ void ONMainWindow::slotFindProxyWin()
             }
             if (xinerama)
             {
-                x2goDebug<<"Starting xinerama timer\n";
+                x2goDebug<<"Starting Xinerama Timer.";
+
                 lastDisplayGeometry=QRect();
                 xineramaScreens.clear();
                 xineramaTimer->start(500);
@@ -9564,23 +9858,26 @@ void ONMainWindow::slotFindProxyWin()
 
         if ( embedMode )
         {
-            x2goDebug<<"checking rootless config";
+            x2goDebug<<"Checking rootless config.";
+
             if ( config.rootless )
             {
-                x2goDebug<<"win is rootless";
+                x2goDebug<<"Window is rootless.";
                 act_embedContol->setEnabled ( false );
             }
             else
                 slotAttachProxyWindow();
         }
 #ifdef Q_OS_WIN
-        x2goDebug<<"maximizeProxyWin: "<<maximizeProxyWin;
+        x2goDebug<<"Maximize proxy win: "<<maximizeProxyWin;
+
         if ( !startEmbedded )
         {
             if ( maximizeProxyWin )
             {
                 QDesktopWidget dw;
-                x2goDebug<<"making proxy win full screen";
+
+                x2goDebug<<"Making proxy win full screen.";
                 wapiSetFSWindow ( ( HWND ) proxyWinId,
                                   dw.screenGeometry ( this ) );
                 /*wapiShowWindow ( ( HWND ) proxyWinId,
@@ -9621,9 +9918,8 @@ void ONMainWindow::slotDetachProxyWindow()
     //and reconnect
     if ( !embedControlChanged )
     {
-        x2goDebug<<"\n";
         slotFindProxyWin();
-        x2goDebug<<"proxy win detached, proxywin is:"<<proxyWinId<<endl;
+        x2goDebug<<"Proxy win detached, proxywin is: "<<proxyWinId;
     }
 #endif
     embedControlChanged=false;
@@ -9632,7 +9928,8 @@ void ONMainWindow::slotDetachProxyWindow()
 
 void ONMainWindow::slotAttachProxyWindow()
 {
-    x2goDebug<<"slotAttachProxy";
+    x2goDebug<<"slotAttachProxyWindow.";
+
     if ( startEmbedded )
     {
         embedControlChanged=false;
@@ -9646,7 +9943,9 @@ void ONMainWindow::slotAttachProxyWindow()
     }
     else
     {
-        x2goDebug<<"start embedded was false";
+
+        x2goDebug<<"Start embedded was false.";
+
         startEmbedded=true;
     }
 
@@ -9954,7 +10253,9 @@ void ONMainWindow::slotChangeKbdLayout(const QString& layout)
 #ifdef Q_OS_LINUX
     QStringList args;
     args<<"-layout"<<layout;
-    x2goDebug<<"running setxkbmap with params: "<<args.join(" ");
+
+    x2goDebug<<"Running setxkbmap with params: "<<args.join(" ");
+
     QProcess::startDetached("setxkbmap",args);
 #endif
 }
@@ -10727,8 +11028,10 @@ void ONMainWindow::slotActivateWindow()
         QApplication::setActiveWindow ( this ) ;
         activateWindow();
 
+
         /*		x2goDebug<<"focus:"<<pass->hasFocus();
-        		x2goDebug<<"activ:"<<pass->isActiveWindow();*/
+                x2goDebug<<"activ:"<<pass->isActiveWindow();*/
+
         QTimer::singleShot ( 50, this,
                              SLOT ( slotEmbedToolBarToolTip() ) );
     }
@@ -10876,7 +11179,6 @@ QString ONMainWindow::u3DataPath()
     QString dpath;
     for ( int i=0; i<env.size(); ++i )
     {
-// 		x2goDebug << env[i];
         //check if we have U3 System
         if ( env[i].indexOf ( "U3_APP_DATA_PATH=" ) ==0 )
         {
@@ -10912,14 +11214,18 @@ void ONMainWindow::cleanPortable()
 
 void ONMainWindow::removeDir ( QString path )
 {
-    x2goDebug<<"entering " <<path;
+
+    x2goDebug<<"removeDir, entering: " <<path;
+
     QDir dr ( path );
     QStringList files=dr.entryList ( QDir::Files );
     for ( int i=0; i<files.size(); ++i )
     {
         if ( files[i]!="known_hosts" || cleanAllFiles)
         {
-            x2goDebug<<"cleaning file:"<<path+"/"+files[i];
+
+            x2goDebug<<"Cleaning file: "<<path+"/"+files[i];
+
             dr.remove ( path+"/"+files[i] );
         }
     }
@@ -10971,6 +11277,7 @@ long ONMainWindow::X11FindWindow ( QString text, long rootWin )
 
 long ONMainWindow::findWindow ( QString text )
 {
+    x2goDebug<<"Searching window with title: " + text;
 #ifdef Q_OS_LINUX
     return X11FindWindow ( text );
 #endif
@@ -10985,7 +11292,9 @@ long ONMainWindow::findWindow ( QString text )
 void ONMainWindow::setX2goconfig ( const QString& text )
 {
     m_x2goconfig=text;
-    x2goDebug<<"have session config";
+
+    x2goDebug<<"Haveing a session config.";
+
     initWidgetsEmbed();
 }
 
@@ -10995,12 +11304,16 @@ void ONMainWindow::doPluginInit()
     Dl_info info;
     dladdr ( ( void* ) & ( ONMainWindow::getPortable ),&info );
     QString fname=info.dli_fname;
+
     x2goDebug<<"Application name:" <<fname;
+
     QString clientDir;
     QString pluginDir;
     int pos=fname.lastIndexOf ( "/" );
     pluginDir=fname.left ( pos );
-    x2goDebug<<"Plugin Dir:" <<pluginDir;
+
+    x2goDebug<<"Plugin directory: " <<pluginDir;
+
     QDir dr ( pluginDir );
     if ( dr.exists ( "x2goclient/x2goclient" ) )
     {
@@ -11026,7 +11339,9 @@ void ONMainWindow::doPluginInit()
             clientDir=pluginDir;
         }
     }
-    x2goDebug<<"Client Dir:"<<clientDir;
+
+    x2goDebug<<"Client directory: "<<clientDir;
+
     QString path=getenv ( "PATH" );
     path=clientDir+":"+pluginDir+":"+path;
     setenv ( "PATH",path.toAscii (),1 );
@@ -11102,7 +11417,9 @@ void ONMainWindow::slotUpdateEmbedWindow()
         oldContainerSize=embedContainer->size();
         oldChildPos= mapToGlobal (
                          QPoint ( 0,0 ) );
-        x2goDebug<<"updating embedded window"<<endl;
+
+        x2goDebug<<"Updating embedded window.";
+
     }
 }
 
@@ -11115,7 +11432,9 @@ void ONMainWindow::embedWindow ( long wndId )
     childId=wndId;
     embedContainer->show();
 #ifdef Q_OS_LINUX
-    x2goDebug<<"embedding "<<wndId<<" in container"<<endl;
+
+    x2goDebug<<"Embedding window with id "<<wndId<<" in container.";
+
     embedContainer->embedClient ( wndId );
 #endif
 #ifdef Q_OS_WIN
diff --git a/onmainwindow.h b/onmainwindow.h
index 5a48cba..41eeca7 100644
--- a/onmainwindow.h
+++ b/onmainwindow.h
@@ -324,6 +324,9 @@ public:
         ARTS,
         ESD
     };
+
+    static bool debugging;
+
     static bool portable;
     ONMainWindow ( QWidget *parent = 0 );
     ~ONMainWindow();
diff --git a/show_printouts.sh b/show_printouts.sh
new file mode 100755
index 0000000..3f16c24
--- /dev/null
+++ b/show_printouts.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+die () {
+    echo >&2 "$@"
+    exit 1
+}
+
+[ "$#" -eq 1 ] || die "You have to provide the filename to be parsed for printouts, exiting."
+
+echo "-----------------------------------------------------------------------------------------------------"
+echo "DEBUGS"
+echo "-----------------------------------------------------------------------------------------------------"
+cat -n $1 | grep 'x2goDebug'     | sed -e 's/\(^\s*[0-9]*\)[ \t\/\*]*x2goDebug\s*<<\s*\(.*\)$/\1:\t \2/'   | sed -e 's/,//g' | sort -k1n
+echo "$DEBUGS"
+
+echo "-----------------------------------------------------------------------------------------------------"
+echo "INFOS"
+echo "-----------------------------------------------------------------------------------------------------"
+cat -n $1 | grep 'x2goInfof('    | sed -e 's/\(^\s*[0-9]*\)[ \t\/\*]*x2goInfof(\([0-9]*\))\s*<<\s*\(.*\)$/\1:\t\2\t\3/'    | sed -e 's/,//g' | sort -k2n
+
+echo "-----------------------------------------------------------------------------------------------------"
+echo "WARNINGS"
+echo "-----------------------------------------------------------------------------------------------------"
+cat -n $1 | grep 'x2goWarningf(' | sed -e 's/\(^\s*[0-9]*\)[ \t\/\*]*x2goWarningf(\([0-9]*\))\s*<<\s*\(.*\)$/\1:\t\2\t\3/' | sed -e 's/,//g' | sort -k2n
+
+echo "-----------------------------------------------------------------------------------------------------"
+echo "ERRORS"
+echo "-----------------------------------------------------------------------------------------------------"
+cat -n $1 | grep 'x2goErrorf('   | sed -e 's/\(^\s*[0-9]*\)[ \t\/\*]*x2goErrorf(\([0-9]*\))\s*<<\s*\(.*\)$/\1:\t\2\t\3/'   | sed -e 's/,//g' | sort -k2n
+
diff --git a/sshmasterconnection.cpp b/sshmasterconnection.cpp
index 0db4077..0b6beb9 100644
--- a/sshmasterconnection.cpp
+++ b/sshmasterconnection.cpp
@@ -319,7 +319,9 @@ void SshMasterConnection::run()
         if ( ssh_init() !=0 )
         {
             QString err=tr ( "Can not initialize libssh" );
+#ifdef DEBUG
             x2goDebug<<err<<endl;
+#endif
             emit connectionError ( err,"" );
             quit();
             return;
@@ -345,7 +347,9 @@ void SshMasterConnection::run()
     if ( my_ssh_session == NULL )
     {
         QString err=tr ( "Can not create ssh session" );
+#ifdef DEBUG
         x2goDebug<<err<<endl;
+#endif
         emit connectionError ( err,"" );
         if ( reverseTunnel )
             emit ioErr ( reverseTunnelCreator,err,"" );
@@ -373,7 +377,9 @@ void SshMasterConnection::run()
         if (!tcpProxySocket->waitForConnected(30000))
         {
             QString message=tr ( "Can not connect to proxy server" );
+#ifdef DEBUG
             x2goDebug<<message<<endl;
+#endif
             emit connectionError ( "Proxy", message );
             ssh_free ( my_ssh_session );
             quit();
@@ -394,12 +400,16 @@ void SshMasterConnection::run()
     {
         if(disconnectSessionFlag)
         {
+#ifdef DEBUG
             x2goDebug<<"session already disconnected, exiting"<<endl;
+#endif
             return;
         }
         QString err=ssh_get_error ( my_ssh_session );
         QString message=tr ( "Can not connect to " ) +host+":"+QString::number ( port );
+#ifdef DEBUG
         x2goDebug<<message<<" - "<<err;
+#endif
         emit connectionError ( message, err );
         if ( reverseTunnel )
             emit ioErr ( reverseTunnelCreator,message,err );
@@ -409,7 +419,9 @@ void SshMasterConnection::run()
     }
     if(disconnectSessionFlag)
     {
+#ifdef DEBUG
         x2goDebug<<"session already disconnected, exiting"<<endl;
+#endif
         return;
     }
     QString errMsg;
@@ -418,7 +430,9 @@ void SshMasterConnection::run()
     {
         if(disconnectSessionFlag)
         {
+#ifdef DEBUG
             x2goDebug<<"session already disconnected, exiting"<<endl;
+#endif
             return;
         }
         writeHostKey=writeHostKeyReady=false;
@@ -443,7 +457,9 @@ void SshMasterConnection::run()
 
     if(disconnectSessionFlag)
     {
+#ifdef DEBUG
         x2goDebug<<"session already disconnected, exiting"<<endl;
+#endif
         return;
     }
     ssh_options_set ( my_ssh_session, SSH_OPTIONS_USER, user.toAscii() );
@@ -459,7 +475,9 @@ void SshMasterConnection::run()
     {
         if(disconnectSessionFlag)
         {
+#ifdef DEBUG
             x2goDebug<<"session already disconnected, exiting"<<endl;
+#endif
             return;
         }
 #ifdef DEBUG
@@ -471,14 +489,18 @@ void SshMasterConnection::run()
     {
         if(disconnectSessionFlag)
         {
+#ifdef DEBUG
             x2goDebug<<"session already disconnected, exiting"<<endl;
+#endif
             return;
         }
         QString err;
         if (!kerberos)
             err=ssh_get_error ( my_ssh_session );
         QString message=tr ( "Authentication failed" );
+#ifdef DEBUG
         x2goDebug<<message<<" - "<<err;
+#endif
         emit userAuthError ( authErrors.join ( "\n" ) );
         if ( reverseTunnel )
             emit ioErr ( reverseTunnelCreator,message,err );
@@ -504,12 +526,16 @@ void SshMasterConnection::run()
         {
             if(disconnectSessionFlag)
             {
+#ifdef DEBUG
                 x2goDebug<<"session already disconnected, exiting"<<endl;
+#endif
                 return;
             }
             QString err=ssh_get_error ( my_ssh_session );
             QString message=tr ( "channel_forward_listen failed" );
+#ifdef DEBUG
             x2goDebug<<message<<" - "<<err;
+#endif
             emit ioErr ( reverseTunnelCreator, message, err );
             ssh_disconnect ( my_ssh_session );
             ssh_free ( my_ssh_session );
@@ -923,13 +949,17 @@ void SshMasterConnection::copy()
         ssh_scp scp=ssh_scp_new ( my_ssh_session, SSH_SCP_WRITE|SSH_SCP_RECURSIVE, dstPath.toAscii() );
         if ( scp == NULL )
         {
+#ifdef DEBUG
             x2goDebug<<"Error allocating scp session: "<< ssh_get_error ( my_ssh_session ) <<endl;
+#endif
             return;
         }
         int rc = ssh_scp_init ( scp );
         if ( rc != SSH_OK )
         {
+#ifdef DEBUG
             x2goDebug<<"Error initializing scp session: "<< ssh_get_error ( my_ssh_session ) <<endl;
+#endif
             ssh_scp_free ( scp );
             return;
         }
@@ -950,7 +980,9 @@ void SshMasterConnection::copy()
         {
             QString errMsg=tr ( "Can not create remote file " ) +copyRequests[i].dst;
             QString serr=ssh_get_error ( my_ssh_session );
+#ifdef DEBUG
             x2goDebug<<errMsg<<" - "<<serr<<endl;
+#endif
             emit copyErr ( copyRequests[i].creator, errMsg, serr );
             copyRequests.removeAt ( i );
             ssh_scp_close ( scp );
@@ -962,7 +994,9 @@ void SshMasterConnection::copy()
         {
             QString serr=ssh_get_error ( my_ssh_session );
             QString errMsg=tr ( "Can not write to remote file " ) +copyRequests[i].dst;
+#ifdef DEBUG
             x2goDebug<<errMsg<<" - "<<serr<<endl;
+#endif
             emit copyErr ( copyRequests[i].creator, errMsg, serr );
             copyRequests.removeAt ( i );
             ssh_scp_close ( scp );
@@ -1075,7 +1109,9 @@ void SshMasterConnection::channelLoop()
                 {
                     QString errMsg=tr ( "can not connect to " ) +
                                    reverseTunnelLocalHost+":"+QString::number ( reverseTunnelLocalPort );
+#ifdef DEBUG
                     x2goDebug<<errMsg<<endl;
+#endif
                     emit ioErr ( reverseTunnelCreator, errMsg, "" );
                     continue;
                 }
@@ -1108,7 +1144,9 @@ void SshMasterConnection::channelLoop()
         channelConnectionsMutex.lock();
         if ( channelConnections.size() <=0 )
         {
-//             x2goDebug<<"no channel connections, waiting\n";
+#ifdef DEBUG
+            //             x2goDebug<<"no channel connections, waiting\n";
+#endif
             channelConnectionsMutex.unlock();
             usleep ( 500 );
             continue;
@@ -1148,7 +1186,9 @@ void SshMasterConnection::channelLoop()
                         QString err=ssh_get_error ( my_ssh_session );
                         QString errorMsg=tr ( "channel_open_forward failed" );
                         emit ioErr ( channelConnections[i].creator, errorMsg, err );
+#ifdef DEBUG
                         x2goDebug<<errorMsg<<": "<<err<<endl;
+#endif
                     }
 #ifdef DEBUG
                     else
@@ -1167,14 +1207,18 @@ void SshMasterConnection::channelLoop()
                         QString err=ssh_get_error ( my_ssh_session );
                         QString errorMsg=tr ( "channel_open_session failed" );
                         emit ioErr ( channelConnections[i].creator, errorMsg, err );
+#ifdef DEBUG
                         x2goDebug<<errorMsg<<": "<<err<<endl;
+#endif
                     }
                     else if ( channel_request_exec ( channel, channelConnections[i].command.toAscii() ) != SSH_OK )
                     {
                         QString err=ssh_get_error ( my_ssh_session );
                         QString errorMsg=tr ( "channel_request_exec failed" );
                         emit ioErr ( channelConnections[i].creator, errorMsg, err );
+#ifdef DEBUG
                         x2goDebug<<errorMsg<<": "<<err<<endl;
+#endif
                     }
 #ifdef DEBUG
                     else
@@ -1196,11 +1240,15 @@ void SshMasterConnection::channelLoop()
 
         if ( retval == -1 )
         {
+#ifdef DEBUG
             x2goDebug<<"select error\n";
+#endif
             continue;
         }
 
-//         x2goDebug<<"select exited"<<endl;
+#ifdef DEBUG
+        //         x2goDebug<<"select exited"<<endl;
+#endif
 
         channelConnectionsMutex.lock();
         for ( int i=channelConnections.size()-1; i>=0; --i )
@@ -1211,10 +1259,14 @@ void SshMasterConnection::channelLoop()
                 continue;
             if ( channel_poll ( channel,1 ) >0 )
             {
-//              x2goDebug<<"read err data from channel\n";
+#ifdef DEBUG
+                //              x2goDebug<<"read err data from channel\n";
+#endif
                 nbytes = channel_read ( channel, buffer, sizeof ( buffer )-1, 1 );
                 emit stdErr ( channelConnections[i].creator, QByteArray ( buffer,nbytes ) );
-//              x2goDebug<<nbytes<<" err from channel"<<endl;
+#ifdef DEBUG
+                //              x2goDebug<<nbytes<<" err from channel"<<endl;
+#endif
             }
             int rez=channel_poll ( channel,0 );
             if ( rez==SSH_EOF )
@@ -1228,9 +1280,13 @@ void SshMasterConnection::channelLoop()
             }
             if ( rez>0 )
             {
-//                  x2goDebug<<"read data from channel "<<channel<<endl;
+#ifdef DEBUG
+                //                  x2goDebug<<"read data from channel "<<channel<<endl;
+#endif
                 nbytes = channel_read ( channel, buffer, sizeof ( buffer )-1, 0 );
-//                  x2goDebug<<nbytes<<" from channel "<<channel<<endl;
+#ifdef DEBUG
+                //                  x2goDebug<<nbytes<<" from channel "<<channel<<endl;
+#endif
                 if ( nbytes > 0 )
                 {
                     if ( tcpSocket>0 )
@@ -1238,12 +1294,16 @@ void SshMasterConnection::channelLoop()
                         if ( send ( tcpSocket,buffer, nbytes,0 ) != nbytes )
                         {
                             QString errMsg=tr ( "error writing to socket" );
+#ifdef DEBUG
                             x2goDebug<<"error writing "<<nbytes<<" to tcp socket"<<tcpSocket<<endl;
+#endif
                             emit ioErr ( channelConnections[i].creator,errMsg,"" );
                             finalize ( i );
                             continue;
                         }
-//                      x2goDebug<<"wrote "<<nbytes<<" to tcp socket "<<tcpSocket<<endl;
+#ifdef DEBUG
+                        //                      x2goDebug<<"wrote "<<nbytes<<" to tcp socket "<<tcpSocket<<endl;
+#endif
                     }
                     else
                     {
@@ -1257,7 +1317,9 @@ void SshMasterConnection::channelLoop()
                     QString err=ssh_get_error ( my_ssh_session );
                     QString errorMsg=tr ( "error reading channel" );
                     emit ioErr ( channelConnections[i].creator, errorMsg, err );
+#ifdef DEBUG
                     x2goDebug<<errorMsg<<" - "<<err<<endl;
+#endif
                     finalize ( i );
                     continue;
                 }
@@ -1279,7 +1341,9 @@ void SshMasterConnection::channelLoop()
             if ( FD_ISSET ( tcpSocket,&rfds ) )
             {
                 nbytes = recv ( tcpSocket, buffer, sizeof ( buffer )-1,0 );
-//                  x2goDebug<<nbytes<<" bytes from tcp socket "<<tcpSocket<<endl;
+#ifdef DEBUG
+                //                  x2goDebug<<nbytes<<" bytes from tcp socket "<<tcpSocket<<endl;
+#endif
                 if ( nbytes > 0 )
                 {
                     if ( channel_write ( channel, buffer, nbytes ) !=nbytes )
@@ -1287,11 +1351,15 @@ void SshMasterConnection::channelLoop()
                         QString err=ssh_get_error ( my_ssh_session );
                         QString errorMsg=tr ( "channel_write failed" );
                         emit ioErr ( channelConnections[i].creator, errorMsg, err );
+#ifdef DEBUG
                         x2goDebug<<errorMsg<<" - "<<err<<endl;
+#endif
                         finalize ( i );
                         continue;
                     }
-//                      x2goDebug<<nbytes<<" bytes wrote to channel"<<channel<<endl;
+#ifdef DEBUG
+                    //                      x2goDebug<<nbytes<<" bytes wrote to channel"<<channel<<endl;
+#endif
                 }
                 if ( nbytes < 0 )
                 {
@@ -1299,7 +1367,9 @@ void SshMasterConnection::channelLoop()
                     QString err="";
                     QString errorMsg=tr ( "error reading tcp socket" );
                     emit ioErr ( channelConnections[i].creator, errorMsg, err );
+#ifdef DEBUG
                     x2goDebug<<errorMsg<<" - "<<err<<endl;
+#endif
                     finalize ( i );
                     continue;
                 }
diff --git a/x2goclientconfig.h b/x2goclientconfig.h
index 341c1f7..e857e53 100644
--- a/x2goclientconfig.h
+++ b/x2goclientconfig.h
@@ -23,7 +23,7 @@
 #include <qglobal.h>
 
 
-// #define LOGFILE QDir::homePath()+"/x2goclient.log"
+//#define LOGFILE QDir::homePath()+"/x2goclient.log"
 
 #if !defined Q_OS_WIN
 #define USELDAP
diff --git a/x2gologdebug.cpp b/x2gologdebug.cpp
index c21c1c3..0fd8ada 100644
--- a/x2gologdebug.cpp
+++ b/x2gologdebug.cpp
@@ -28,7 +28,6 @@ X2goLogDebug::X2goLogDebug():QTextStream()
     }
 }
 
-
 X2goLogDebug::~X2goLogDebug()
 {
  	logFile.close();
diff --git a/x2gologdebug.h b/x2gologdebug.h
index b35f783..ded6d8d 100644
--- a/x2gologdebug.h
+++ b/x2gologdebug.h
@@ -20,21 +20,50 @@
 #include <QTextStream>
 #include <QFile>
 #include "x2goclientconfig.h"
+#include "onmainwindow.h"
 /**
 	@author Oleksandr Shneyder <oleksandr.shneyder@obviously-nice.de>
 */
+
+#define __x2goPrefix      "x2go-"
+#define __x2goDebugPrefix "DEBUG-"
+#define __x2goInfoPrefix  "INFO-"
+#define __x2goWarningPrefix  "WARNING-"
+#define __x2goErrorPrefix "ERROR-"
+#define __x2goPostfix     "> "
+
 #ifdef LOGFILE
 class X2goLogDebug: public QTextStream
 {
 public:
     X2goLogDebug();
     ~X2goLogDebug();
+
  private:
      QFile logFile;
 };
-#define x2goDebug X2goLogDebug()
+
+#define __x2goDebug   X2goLogDebug()<<"\n"
+#define __x2goInfo    X2goLogDebug()<<"\n"
+#define __x2goWarning X2goLogDebug()<<"\n"
+#define __x2goError   X2goLogDebug()<<"\n"
+
 #else
+
 #include <QDebug>
-#define x2goDebug qDebug()
+
+#define __x2goDebug   qDebug().nospace()
+#define __x2goInfo    qDebug().nospace()
+#define __x2goWarning qWarning().nospace()
+#define __x2goError   qCritical().nospace()
+
 #endif //LOGFILE
+
+#define x2goDebugf        __x2goDebug  <<__x2goPrefix<<__x2goDebugPrefix  <<__FILE__<<":"<<__LINE__<<__x2goPostfix
+#define x2goInfof(NUM)    __x2goInfo   <<__x2goPrefix<<__x2goInfoPrefix   <<NUM                    <<__x2goPostfix
+#define x2goWarningf(NUM) __x2goWarning<<__x2goPrefix<<__x2goWarningPrefix<<NUM                    <<__x2goPostfix
+#define x2goErrorf(NUM)   __x2goError  <<__x2goPrefix<<__x2goErrorPrefix  <<NUM                    <<__x2goPostfix
+
+#define x2goDebug      if (ONMainWindow::debugging) x2goDebugf
+
 #endif //X2GOLOGDEBUG_H
-- 
1.7.9.5

