From c6717bbfee334ac1effa6b01cccbc6744e6fbf62 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Sun, 6 May 2007 12:27:41 +0000 Subject: [PATCH] added rudimental logging support --- src/global.h | 2 ++ src/main.cpp | 34 ++++++++++++++++++++++------------ src/server.cpp | 14 ++++++++++++-- src/server.h | 11 +++++++---- src/util.h | 30 ++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/global.h b/src/global.h index 77e03762..a9a75b93 100755 --- a/src/global.h +++ b/src/global.h @@ -45,6 +45,8 @@ #define VERSION "1.0cvs" #define APP_NAME "llcon" +// file name for logging file +#define LOG_FILE_NAME "llconsrvlog.txt" /* defined port number for client and server */ #define LLCON_PORT_NUMBER 22122 diff --git a/src/main.cpp b/src/main.cpp index b53496b7..48f72e3b 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,9 +37,10 @@ QApplication* pApp = NULL; int main ( int argc, char** argv ) { /* check if server or client application shall be started */ - bool bIsClient = true; - bool bUseGUI = true; - + bool bIsClient = true; + bool bUseGUI = true; + bool bUseServerLogging = false; + /* QT docu: argv()[0] is the program name, argv()[1] is the first argument and argv()[argc()-1] is the last argument */ if ( argc > 1 ) @@ -58,7 +59,16 @@ int main ( int argc, char** argv ) if ( !strShortOpt.compare ( argv[1] ) ) { bIsClient = false; - bUseGUI = false; + bUseGUI = false; + } + + /* "-sln": start server with GUI disabled and logging enabled */ + strShortOpt = "-sln"; + if ( !strShortOpt.compare ( argv[1] ) ) + { + bIsClient = false; + bUseGUI = false; + bUseServerLogging = true; } } @@ -83,17 +93,17 @@ int main ( int argc, char** argv ) pApp = &app; /* Needed for post-event routine */ /* Show dialog */ - ClientDlg.show (); - app.exec (); + ClientDlg.show(); + app.exec(); /* Save settings to init-file */ - Settings.Save (); + Settings.Save(); } else { /* server */ // actual server object - CServer Server; + CServer Server ( bUseServerLogging ); if ( bUseGUI ) { @@ -106,14 +116,14 @@ int main ( int argc, char** argv ) pApp = &app; /* Needed for post-event routine */ /* Show dialog */ - ServerDlg.show (); - app.exec (); + ServerDlg.show(); + app.exec(); } else { // only start application without using the GUI qDebug ( CAboutDlg::GetVersionAndNameStr ( false ) ); - app.exec (); + app.exec(); } } @@ -130,6 +140,6 @@ void PostWinMessage ( const _MESSAGE_IDENT MessID, const int iMessageParam, new CLlconEvent ( MessID, iMessageParam, iChanNum ); /* Qt will delete the event object when done */ - QThread::postEvent ( pApp->mainWidget (), LlconEv ); + QThread::postEvent ( pApp->mainWidget(), LlconEv ); } } diff --git a/src/server.cpp b/src/server.cpp index ec2510ea..7ed95f59 100755 --- a/src/server.cpp +++ b/src/server.cpp @@ -26,7 +26,7 @@ /* Implementation *************************************************************/ -CServer::CServer() : Socket ( &ChannelSet, this ) +CServer::CServer ( const bool bUseLogging ) : Socket ( &ChannelSet, this ) { vecsSendData.Init ( MIN_BLOCK_SIZE_SAMPLES ); @@ -47,6 +47,12 @@ CServer::CServer() : Socket ( &ChannelSet, this ) // class, do not use automatic start/stop of server in Windows version Start(); #endif + + // enable logging (if requested) + if ( bUseLogging ) + { + Logging.Start(); + } } void CServer::OnSendProtMessage ( int iChID, CVector vecMessage ) @@ -80,7 +86,11 @@ void CServer::Stop() // stop main timer Timer.stop(); - qDebug ( CLogTimeDate::toString() + "Server stopped" ); + // logging + QString strLogStr = CLogTimeDate::toString() + "Server stopped"; + + qDebug ( strLogStr ); // on console + Logging << strLogStr; // in log file } void CServer::OnTimer() diff --git a/src/server.h b/src/server.h index e0cd9ba3..fb909e2f 100755 --- a/src/server.h +++ b/src/server.h @@ -42,12 +42,12 @@ class CServer : public QObject Q_OBJECT public: - CServer (); - virtual ~CServer () {} + CServer ( const bool bUseLogging ); + virtual ~CServer() {} void Start (); void Stop (); - bool IsRunning() { return Timer.isActive (); } + bool IsRunning() { return Timer.isActive(); } void GetConCliParam ( CVector& vecHostAddresses, CVector& vecsName, @@ -60,7 +60,7 @@ public: bool GetTimingStdDev ( double& dCurTiStdDev ); - CChannelSet* GetChannelSet () { return &ChannelSet; } + CChannelSet* GetChannelSet() { return &ChannelSet; } protected: CVector ProcessData ( CVector >& vecvecdData, @@ -79,6 +79,9 @@ protected: CMovingAv RespTimeMoAvBuf; QTime TimeLastBlock; + // logging + CLogging Logging; + public slots: void OnTimer(); void OnSendProtMessage ( int iChID, CVector vecMessage ); diff --git a/src/util.h b/src/util.h index c1cf3e93..e430f4e1 100755 --- a/src/util.h +++ b/src/util.h @@ -467,5 +467,35 @@ public: }; +/* Time and Data to String conversion --------------------------------------- */ +class CLogging +{ +public: + CLogging() : bDoLogging ( false ), pFile ( NULL ) {} + virtual ~CLogging() + { + if ( pFile != NULL ) + { + fclose ( pFile ); + } + } + + void Start() + { + // open file + pFile = fopen ( LOG_FILE_NAME, "a" ); + } + + CLogging& operator<< ( const QString & sNewStr ) + { + fprintf ( pFile, "%s\n", sNewStr.latin1() ); + fflush ( pFile ); + return *this; + } + +protected: + bool bDoLogging; + FILE* pFile; +}; #endif /* !defined(UTIL_HOIH934256GEKJH98_3_43445KJIUHF1912__INCLUDED_) */