Fixed issue 506 - plus some quite major refactoring

This commit is contained in:
Nick Bolton 2010-06-05 14:20:19 +00:00
parent a19e800b99
commit 24765e6891
8 changed files with 50 additions and 60 deletions

View File

@ -200,21 +200,8 @@ mainLoopStatic()
int int
CArchAppUtilWindows::daemonNTMainLoop(int argc, const char** argv) CArchAppUtilWindows::daemonNTMainLoop(int argc, const char** argv)
{ {
app().parseArgs(argc, argv); app().initialize(argc, argv);
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"));
}
}
app().argsBase().m_backend = false; app().argsBase().m_backend = false;
app().loadConfig(); app().loadConfig();
return CArchMiscWindows::runDaemon(mainLoopStatic); return CArchMiscWindows::runDaemon(mainLoopStatic);
@ -301,3 +288,20 @@ CArchAppUtilWindows::instance()
{ {
return (CArchAppUtilWindows&)CArchAppUtil::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"));
}
}
}

View File

@ -56,6 +56,8 @@ public:
int daemonNTMainLoop(int argc, const char** argv); int daemonNTMainLoop(int argc, const char** argv);
void debugServiceWait();
int run(int argc, char** argv, CreateTaskBarReceiverFunc createTaskBarReceiver); int run(int argc, char** argv, CreateTaskBarReceiverFunc createTaskBarReceiver);
void exitApp(int code); void exitApp(int code);

View File

@ -255,27 +255,22 @@ CBufferedLogOutputter::write(ELevel, const char* message)
CFileLogOutputter::CFileLogOutputter(const char* logFile) CFileLogOutputter::CFileLogOutputter(const char* logFile)
{ {
assert(logFile != NULL); assert(logFile != NULL);
m_fileName = logFile;
m_handle.open(logFile, std::fstream::app);
// open file handle
} }
CFileLogOutputter::~CFileLogOutputter() CFileLogOutputter::~CFileLogOutputter()
{ {
// close file handle
if (m_handle.is_open())
m_handle.close();
} }
bool bool
CFileLogOutputter::write(ILogOutputter::ELevel level, const char *message) 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) { if (m_handle.is_open() && m_handle.fail() != true) {
m_handle << message << std::endl; m_handle << message << std::endl;
// write buffer to file
m_handle.flush();
} }
m_handle.close();
return true; return true;
} }

View File

@ -77,7 +77,7 @@ public:
virtual void show(bool showIfEmpty); virtual void show(bool showIfEmpty);
virtual bool write(ELevel level, const char* message); virtual bool write(ELevel level, const char* message);
private: private:
std::ofstream m_handle; std::string m_fileName;
}; };
//! Write log to system log //! Write log to system log

View File

@ -43,9 +43,6 @@ s_suspended(false)
CApp::~CApp() CApp::~CApp()
{ {
delete m_args; delete m_args;
CLOG->remove(m_fileLog);
delete m_fileLog;
} }
CApp::CArgsBase::CArgsBase() : CApp::CArgsBase::CArgsBase() :
@ -214,12 +211,6 @@ CApp::parseArgs(int argc, const char* const* argv, int& i)
break; 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 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();
}

View File

@ -97,6 +97,12 @@ public:
// If messages will be hidden (to improve performance), warn user. // If messages will be hidden (to improve performance), warn user.
void loggingFilterWarning(); 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: protected:
virtual void parseArgs(int argc, const char* const* argv, int &i); virtual void parseArgs(int argc, const char* const* argv, int &i);
virtual bool parseArg(const int& argc, const char* const* argv, int& i); virtual bool parseArg(const int& argc, const char* const* argv, int& i);

View File

@ -412,8 +412,7 @@ CClientApp::closeClient(CClient* client)
int int
CClientApp::foregroundStartup(int argc, char** argv) CClientApp::foregroundStartup(int argc, char** argv)
{ {
// parse command line initialize(argc, argv);
parseArgs(argc, argv);
// never daemonize // never daemonize
return mainLoop(); return mainLoop();
@ -477,11 +476,6 @@ CClientApp::stopClient()
int int
CClientApp::mainLoop() CClientApp::mainLoop()
{ {
// logging to files
CFileLogOutputter* fileLog = NULL;
setupFileLogging();
// create socket multiplexer. this must happen after daemonization // create socket multiplexer. this must happen after daemonization
// on unix because threads evaporate across a fork(). // on unix because threads evaporate across a fork().
CSocketMultiplexer multiplexer; CSocketMultiplexer multiplexer;
@ -528,12 +522,7 @@ daemonMainLoopStatic(int argc, const char** argv)
int int
CClientApp::standardStartup(int argc, char** argv) CClientApp::standardStartup(int argc, char** argv)
{ {
if (!args().m_daemon) { initialize(argc, argv);
ARCH->showConsole(false);
}
// parse command line
parseArgs(argc, argv);
// daemonize if requested // daemonize if requested
if (args().m_daemon) { if (args().m_daemon) {

View File

@ -717,8 +717,6 @@ int CServerApp::mainLoop()
// create the event queue // create the event queue
CEventQueue eventQueue; CEventQueue eventQueue;
setupFileLogging();
// if configuration has no screens then add this system // if configuration has no screens then add this system
// as the default // as the default
if (args().m_config->begin() == args().m_config->end()) { if (args().m_config->begin() == args().m_config->end()) {
@ -841,11 +839,7 @@ int daemonMainLoopStatic(int argc, const char** argv) {
int int
CServerApp::standardStartup(int argc, char** argv) CServerApp::standardStartup(int argc, char** argv)
{ {
// parse command line initialize(argc, argv);
parseArgs(argc, argv);
// load configuration
loadConfig();
// daemonize if requested // daemonize if requested
if (args().m_daemon) { if (args().m_daemon) {
@ -859,11 +853,7 @@ CServerApp::standardStartup(int argc, char** argv)
int int
CServerApp::foregroundStartup(int argc, char** argv) CServerApp::foregroundStartup(int argc, char** argv)
{ {
// parse command line initialize(argc, argv);
parseArgs(argc, argv);
// load configuration
loadConfig();
// never daemonize // never daemonize
return mainLoop(); return mainLoop();