Made daemon take full responsibility for logging #4128

This commit is contained in:
Xinyu Hou 2014-10-27 14:11:43 +00:00
parent 39b534f3b8
commit 6756296d85
13 changed files with 118 additions and 26 deletions

View File

@ -50,6 +50,18 @@ public:
*/ */
virtual std::string getSystemDirectory() = 0; virtual std::string getSystemDirectory() = 0;
//! Get installed directory
/*!
Returns the directory in which Synergy is installed.
*/
virtual std::string getInstalledDirectory() = 0;
//! Get log directory
/*!
Returns the log file directory.
*/
virtual std::string getLogDirectory() = 0;
//! Concatenate path components //! Concatenate path components
/*! /*!
Concatenate pathname components with a directory separator Concatenate pathname components with a directory separator

View File

@ -88,6 +88,22 @@ CArchFileUnix::getSystemDirectory()
return "/etc"; return "/etc";
} }
std::string
CArchFileUnix::getInstalledDirectory()
{
#if WINAPI_XWINDOWS
return "/bin";
#else
return "";
#endif
}
std::string
CArchFileUnix::getLogDirectory()
{
return "/var/log";
}
std::string std::string
CArchFileUnix::concatPath(const std::string& prefix, CArchFileUnix::concatPath(const std::string& prefix,
const std::string& suffix) const std::string& suffix)

View File

@ -32,6 +32,8 @@ public:
virtual const char* getBasename(const char* pathname); virtual const char* getBasename(const char* pathname);
virtual std::string getUserDirectory(); virtual std::string getUserDirectory();
virtual std::string getSystemDirectory(); virtual std::string getSystemDirectory();
virtual std::string getInstalledDirectory();
virtual std::string getLogDirectory();
virtual std::string concatPath(const std::string& prefix, virtual std::string concatPath(const std::string& prefix,
const std::string& suffix); const std::string& suffix);
}; };

View File

@ -121,6 +121,24 @@ CArchFileWindows::getSystemDirectory()
} }
} }
std::string
CArchFileWindows::getInstalledDirectory()
{
char fileNameBuffer[MAX_PATH];
GetModuleFileName(NULL, fileNameBuffer, MAX_PATH);
std::string fileName(fileNameBuffer);
size_t lastSlash = fileName.find_last_of("\\");
fileName = fileName.substr(0, lastSlash);
return fileName;
}
std::string
CArchFileWindows::getLogDirectory()
{
return getInstalledDirectory();
}
std::string std::string
CArchFileWindows::concatPath(const std::string& prefix, CArchFileWindows::concatPath(const std::string& prefix,
const std::string& suffix) const std::string& suffix)

View File

@ -32,6 +32,8 @@ public:
virtual const char* getBasename(const char* pathname); virtual const char* getBasename(const char* pathname);
virtual std::string getUserDirectory(); virtual std::string getUserDirectory();
virtual std::string getSystemDirectory(); virtual std::string getSystemDirectory();
virtual std::string getInstalledDirectory();
virtual std::string getLogDirectory();
virtual std::string concatPath(const std::string& prefix, virtual std::string concatPath(const std::string& prefix,
const std::string& suffix); const std::string& suffix);
}; };

View File

@ -235,14 +235,20 @@ CBufferedLogOutputter::write(ELevel, const char* message)
CFileLogOutputter::CFileLogOutputter(const char* logFile) CFileLogOutputter::CFileLogOutputter(const char* logFile)
{ {
assert(logFile != NULL); setLogFilename(logFile);
m_fileName = logFile;
} }
CFileLogOutputter::~CFileLogOutputter() CFileLogOutputter::~CFileLogOutputter()
{ {
} }
void
CFileLogOutputter::setLogFilename(const char* logFile)
{
assert(logFile != NULL);
m_fileName = logFile;
}
bool bool
CFileLogOutputter::write(ELevel level, const char *message) CFileLogOutputter::write(ELevel level, const char *message)
{ {

View File

@ -79,6 +79,9 @@ public:
virtual void close(); virtual void close();
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);
void setLogFilename(const char* title);
private: private:
std::string m_fileName; std::string m_fileName;
}; };

View File

@ -141,7 +141,7 @@ CIpcClientProxy::send(const CIpcMessage& message)
// also, don't allow the dtor to destroy the stream while we're using it. // also, don't allow the dtor to destroy the stream while we're using it.
CArchMutexLock lock(m_writeMutex); CArchMutexLock lock(m_writeMutex);
LOG((CLOG_DEBUG "ipc write: %d", message.type())); LOG((CLOG_DEBUG4 "ipc write: %d", message.type()));
switch (message.type()) { switch (message.type()) {
case kIpcLogLine: { case kIpcLogLine: {

View File

@ -83,7 +83,7 @@ CIpcServerProxy::handleData(const CEvent&, void*)
void void
CIpcServerProxy::send(const CIpcMessage& message) CIpcServerProxy::send(const CIpcMessage& message)
{ {
LOG((CLOG_DEBUG "ipc write: %d", message.type())); LOG((CLOG_DEBUG4 "ipc write: %d", message.type()));
switch (message.type()) { switch (message.type()) {
case kIpcHello: { case kIpcHello: {

View File

@ -28,6 +28,7 @@
#include "arch/win32/ArchDaemonWindows.h" #include "arch/win32/ArchDaemonWindows.h"
#include "arch/win32/XArchWindows.h" #include "arch/win32/XArchWindows.h"
#include "arch/Arch.h" #include "arch/Arch.h"
#include "base/log_outputters.h"
#include "base/TMethodJob.h" #include "base/TMethodJob.h"
#include "base/Log.h" #include "base/Log.h"
#include "common/Version.h" #include "common/Version.h"
@ -56,7 +57,8 @@ CMSWindowsWatchdog::CMSWindowsWatchdog(
m_ipcLogOutputter(ipcLogOutputter), m_ipcLogOutputter(ipcLogOutputter),
m_elevateProcess(false), m_elevateProcess(false),
m_processFailures(0), m_processFailures(0),
m_processRunning(false) m_processRunning(false),
m_fileLogOutputter(NULL)
{ {
} }
@ -245,6 +247,12 @@ CMSWindowsWatchdog::isProcessActive()
return exitCode == STILL_ACTIVE; return exitCode == STILL_ACTIVE;
} }
void
CMSWindowsWatchdog::setFileLogOutputter(CFileLogOutputter* outputter)
{
m_fileLogOutputter = outputter;
}
void void
CMSWindowsWatchdog::startProcess() CMSWindowsWatchdog::startProcess()
{ {
@ -383,8 +391,11 @@ CMSWindowsWatchdog::outputLoop(void*)
// send process output over IPC to GUI, and force it to be sent // send process output over IPC to GUI, and force it to be sent
// which bypasses the ipc logging anti-recursion mechanism. // which bypasses the ipc logging anti-recursion mechanism.
m_ipcLogOutputter.write(kINFO, buffer, true); m_ipcLogOutputter.write(kINFO, buffer, true);
}
if (m_fileLogOutputter != NULL) {
m_fileLogOutputter->write(kINFO, buffer);
}
}
} }
} }

View File

@ -29,6 +29,7 @@
class CThread; class CThread;
class CIpcLogOutputter; class CIpcLogOutputter;
class CIpcServer; class CIpcServer;
class CFileLogOutputter;
class CMSWindowsWatchdog { class CMSWindowsWatchdog {
public: public:
@ -43,6 +44,7 @@ public:
void setCommand(const std::string& command, bool elevate); void setCommand(const std::string& command, bool elevate);
void stop(); void stop();
bool isProcessActive(); bool isProcessActive();
void setFileLogOutputter(CFileLogOutputter* outputter);
private: private:
void mainLoop(void*); void mainLoop(void*);
@ -70,6 +72,7 @@ private:
PROCESS_INFORMATION m_processInfo; PROCESS_INFORMATION m_processInfo;
int m_processFailures; int m_processFailures;
bool m_processRunning; bool m_processRunning;
CFileLogOutputter* m_fileLogOutputter;
}; };
//! Relauncher error //! Relauncher error

View File

@ -87,7 +87,8 @@ CDaemonApp::CDaemonApp() :
#if SYSAPI_WIN32 #if SYSAPI_WIN32
m_watchdog(nullptr), m_watchdog(nullptr),
#endif #endif
m_events(nullptr) m_events(nullptr),
m_fileLogOutputter(nullptr)
{ {
s_instance = this; s_instance = this;
} }
@ -197,8 +198,10 @@ CDaemonApp::mainLoop(bool logToFile)
{ {
DAEMON_RUNNING(true); DAEMON_RUNNING(true);
if (logToFile) if (logToFile) {
CLOG->insert(new CFileLogOutputter(logPath().c_str())); m_fileLogOutputter = new CFileLogOutputter(logFilename().c_str());
CLOG->insert(m_fileLogOutputter);
}
// 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().
@ -213,6 +216,7 @@ CDaemonApp::mainLoop(bool logToFile)
#if SYSAPI_WIN32 #if SYSAPI_WIN32
m_watchdog = new CMSWindowsWatchdog(false, *m_ipcServer, *m_ipcLogOutputter); m_watchdog = new CMSWindowsWatchdog(false, *m_ipcServer, *m_ipcLogOutputter);
m_watchdog->setFileLogOutputter(m_fileLogOutputter);
#endif #endif
m_events->adoptHandler( m_events->adoptHandler(
@ -270,21 +274,17 @@ CDaemonApp::foregroundError(const char* message)
} }
std::string std::string
CDaemonApp::logPath() CDaemonApp::logFilename()
{ {
#ifdef SYSAPI_WIN32 string logFilename;
// TODO: move to CArchMiscWindows logFilename = ARCH->setting("LogFilename");
// on windows, log to the same dir as the binary. if (logFilename.empty()) {
char fileNameBuffer[MAX_PATH]; logFilename = ARCH->getLogDirectory();
GetModuleFileName(NULL, fileNameBuffer, MAX_PATH); logFilename.append("/");
string fileName(fileNameBuffer); logFilename.append(LOG_FILENAME);
size_t lastSlash = fileName.find_last_of("\\"); }
string path(fileName.substr(0, lastSlash));
path.append("\\").append(LOG_FILENAME); return logFilename;
return path;
#elif SYSAPI_UNIX
return "/var/log/" LOG_FILENAME;
#endif
} }
void void
@ -337,6 +337,23 @@ CDaemonApp::handleIpcMessage(const CEvent& e, void*)
LOG((CLOG_ERR "failed to save LogLevel setting, %s", e.what())); LOG((CLOG_ERR "failed to save LogLevel setting, %s", e.what()));
} }
} }
#if SYSAPI_WIN32
CString logFilename;
if (argBase->m_logFile != NULL) {
logFilename = CString(argBase->m_logFile);
ARCH->setting("LogFilename", logFilename);
m_watchdog->setFileLogOutputter(m_fileLogOutputter);
command = CArgParser::assembleCommand(argsArray, "--log", 1);
LOG((CLOG_DEBUG "removed log file argument and filename %s from command ", logFilename.c_str()));
LOG((CLOG_DEBUG "new command, elevate=%d command=%s", cm->elevate(), command.c_str()));
}
else {
m_watchdog->setFileLogOutputter(NULL);
}
m_fileLogOutputter->setLogFilename(logFilename.c_str());
#endif
} }
else { else {
LOG((CLOG_DEBUG "empty command, elevate=%d", cm->elevate())); LOG((CLOG_DEBUG "empty command, elevate=%d", cm->elevate()));

View File

@ -25,6 +25,7 @@
class CEvent; class CEvent;
class CIpcLogOutputter; class CIpcLogOutputter;
class CFileLogOutputter;
#if SYSAPI_WIN32 #if SYSAPI_WIN32
class CMSWindowsWatchdog; class CMSWindowsWatchdog;
@ -41,7 +42,7 @@ public:
private: private:
void daemonize(); void daemonize();
void foregroundError(const char* message); void foregroundError(const char* message);
std::string logPath(); std::string logFilename();
void handleIpcMessage(const CEvent&, void*); void handleIpcMessage(const CEvent&, void*);
public: public:
@ -55,6 +56,7 @@ private:
CIpcServer* m_ipcServer; CIpcServer* m_ipcServer;
CIpcLogOutputter* m_ipcLogOutputter; CIpcLogOutputter* m_ipcLogOutputter;
IEventQueue* m_events; IEventQueue* m_events;
CFileLogOutputter* m_fileLogOutputter;
}; };
#define LOG_FILENAME "synergyd.log" #define LOG_FILENAME "synergyd.log"