From 24765e68914329b6082d83b39d3e6b1f654a02ba Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Sat, 5 Jun 2010 14:20:19 +0000 Subject: [PATCH] Fixed issue 506 - plus some quite major refactoring --- lib/arch/CArchAppUtilWindows.cpp | 34 ++++++++++++++++++-------------- lib/arch/CArchAppUtilWindows.h | 2 ++ lib/base/LogOutputters.cpp | 15 +++++--------- lib/base/LogOutputters.h | 2 +- lib/synergy/CApp.cpp | 22 ++++++++++++--------- lib/synergy/CApp.h | 6 ++++++ lib/synergy/CClientApp.cpp | 15 ++------------ lib/synergy/CServerApp.cpp | 14 ++----------- 8 files changed, 50 insertions(+), 60 deletions(-) diff --git a/lib/arch/CArchAppUtilWindows.cpp b/lib/arch/CArchAppUtilWindows.cpp index a8268176..e04315a6 100644 --- a/lib/arch/CArchAppUtilWindows.cpp +++ b/lib/arch/CArchAppUtilWindows.cpp @@ -200,21 +200,8 @@ mainLoopStatic() int CArchAppUtilWindows::daemonNTMainLoop(int argc, const char** argv) { - app().parseArgs(argc, argv); - - if (app().argsBase().m_debugServiceWait) - { - while(true) - { - // this code is only executed when the process is launched via the - // windows service controller (and --debug-service-wait arg is - // used). to debug, set a breakpoint on this line so that - // execution is delayed until the debugger is attached. - ARCH->sleep(1); - LOG((CLOG_INFO "waiting for debugger to attach")); - } - } - + app().initialize(argc, argv); + debugServiceWait(); app().argsBase().m_backend = false; app().loadConfig(); return CArchMiscWindows::runDaemon(mainLoopStatic); @@ -301,3 +288,20 @@ CArchAppUtilWindows::instance() { return (CArchAppUtilWindows&)CArchAppUtil::instance(); } + +void +CArchAppUtilWindows::debugServiceWait() +{ + if (app().argsBase().m_debugServiceWait) + { + while(true) + { + // this code is only executed when the process is launched via the + // windows service controller (and --debug-service-wait arg is + // used). to debug, set a breakpoint on this line so that + // execution is delayed until the debugger is attached. + ARCH->sleep(1); + LOG((CLOG_INFO "waiting for debugger to attach")); + } + } +} diff --git a/lib/arch/CArchAppUtilWindows.h b/lib/arch/CArchAppUtilWindows.h index d6618ab8..0f1c2a24 100644 --- a/lib/arch/CArchAppUtilWindows.h +++ b/lib/arch/CArchAppUtilWindows.h @@ -56,6 +56,8 @@ public: int daemonNTMainLoop(int argc, const char** argv); + void debugServiceWait(); + int run(int argc, char** argv, CreateTaskBarReceiverFunc createTaskBarReceiver); void exitApp(int code); diff --git a/lib/base/LogOutputters.cpp b/lib/base/LogOutputters.cpp index 9aaaae84..cde5802a 100644 --- a/lib/base/LogOutputters.cpp +++ b/lib/base/LogOutputters.cpp @@ -252,30 +252,25 @@ CBufferedLogOutputter::write(ELevel, const char* message) // CFileLogOutputter // -CFileLogOutputter::CFileLogOutputter(const char * logFile) +CFileLogOutputter::CFileLogOutputter(const char* logFile) { assert(logFile != NULL); - - m_handle.open(logFile, std::fstream::app); - // open file handle + m_fileName = logFile; } CFileLogOutputter::~CFileLogOutputter() { - // close file handle - if (m_handle.is_open()) - m_handle.close(); } bool CFileLogOutputter::write(ILogOutputter::ELevel level, const char *message) { + std::ofstream m_handle; + m_handle.open(m_fileName.c_str(), std::fstream::app); if (m_handle.is_open() && m_handle.fail() != true) { m_handle << message << std::endl; - - // write buffer to file - m_handle.flush(); } + m_handle.close(); return true; } diff --git a/lib/base/LogOutputters.h b/lib/base/LogOutputters.h index f08b5108..01bae609 100644 --- a/lib/base/LogOutputters.h +++ b/lib/base/LogOutputters.h @@ -77,7 +77,7 @@ public: virtual void show(bool showIfEmpty); virtual bool write(ELevel level, const char* message); private: - std::ofstream m_handle; + std::string m_fileName; }; //! Write log to system log diff --git a/lib/synergy/CApp.cpp b/lib/synergy/CApp.cpp index 75f7af68..bc0e3982 100644 --- a/lib/synergy/CApp.cpp +++ b/lib/synergy/CApp.cpp @@ -43,9 +43,6 @@ s_suspended(false) CApp::~CApp() { delete m_args; - - CLOG->remove(m_fileLog); - delete m_fileLog; } CApp::CArgsBase::CArgsBase() : @@ -214,12 +211,6 @@ CApp::parseArgs(int argc, const char* const* argv, int& i) break; } } - - // increase default filter level for daemon. the user must - // explicitly request another level for a daemon. - if (argsBase().m_daemon && argsBase().m_logFilter == NULL) { - argsBase().m_logFilter = "NOTE"; - } } void @@ -318,3 +309,16 @@ CApp::loggingFilterWarning() } } } + +void +CApp::initialize(int argc, const char** argv) +{ + // parse command line + parseArgs(argc, argv); + + // setup file logging after parsing args + setupFileLogging(); + + // load configuration + loadConfig(); +} diff --git a/lib/synergy/CApp.h b/lib/synergy/CApp.h index 91188c65..44fe6648 100644 --- a/lib/synergy/CApp.h +++ b/lib/synergy/CApp.h @@ -97,6 +97,12 @@ public: // If messages will be hidden (to improve performance), warn user. void loggingFilterWarning(); + // Parses args, sets up file logging, and loads the config. + void initialize(int argc, const char** argv); + + // HACK: accept non-const, but make it const anyway + void initialize(int argc, char** argv) { initialize(argc, (const char**)argv); } + protected: virtual void parseArgs(int argc, const char* const* argv, int &i); virtual bool parseArg(const int& argc, const char* const* argv, int& i); diff --git a/lib/synergy/CClientApp.cpp b/lib/synergy/CClientApp.cpp index da98b82e..f468ffa7 100644 --- a/lib/synergy/CClientApp.cpp +++ b/lib/synergy/CClientApp.cpp @@ -412,8 +412,7 @@ CClientApp::closeClient(CClient* client) int CClientApp::foregroundStartup(int argc, char** argv) { - // parse command line - parseArgs(argc, argv); + initialize(argc, argv); // never daemonize return mainLoop(); @@ -477,11 +476,6 @@ CClientApp::stopClient() int CClientApp::mainLoop() { - // logging to files - CFileLogOutputter* fileLog = NULL; - - setupFileLogging(); - // create socket multiplexer. this must happen after daemonization // on unix because threads evaporate across a fork(). CSocketMultiplexer multiplexer; @@ -528,12 +522,7 @@ daemonMainLoopStatic(int argc, const char** argv) int CClientApp::standardStartup(int argc, char** argv) { - if (!args().m_daemon) { - ARCH->showConsole(false); - } - - // parse command line - parseArgs(argc, argv); + initialize(argc, argv); // daemonize if requested if (args().m_daemon) { diff --git a/lib/synergy/CServerApp.cpp b/lib/synergy/CServerApp.cpp index a9c7a3dc..f39e10ab 100644 --- a/lib/synergy/CServerApp.cpp +++ b/lib/synergy/CServerApp.cpp @@ -717,8 +717,6 @@ int CServerApp::mainLoop() // create the event queue CEventQueue eventQueue; - setupFileLogging(); - // if configuration has no screens then add this system // as the default if (args().m_config->begin() == args().m_config->end()) { @@ -841,11 +839,7 @@ int daemonMainLoopStatic(int argc, const char** argv) { int CServerApp::standardStartup(int argc, char** argv) { - // parse command line - parseArgs(argc, argv); - - // load configuration - loadConfig(); + initialize(argc, argv); // daemonize if requested if (args().m_daemon) { @@ -859,11 +853,7 @@ CServerApp::standardStartup(int argc, char** argv) int CServerApp::foregroundStartup(int argc, char** argv) { - // parse command line - parseArgs(argc, argv); - - // load configuration - loadConfig(); + initialize(argc, argv); // never daemonize return mainLoop();