diff options
Diffstat (limited to 'src/dbus/server')
-rw-r--r-- | src/dbus/server/main.cpp | 9 | ||||
-rw-r--r-- | src/dbus/server/server.cpp | 116 | ||||
-rw-r--r-- | src/dbus/server/server.h | 34 | ||||
-rw-r--r-- | src/dbus/server/session-helper.cpp | 122 | ||||
-rw-r--r-- | src/dbus/server/session-helper.h | 12 | ||||
-rw-r--r-- | src/dbus/server/session.cpp | 80 | ||||
-rw-r--r-- | src/dbus/server/session.h | 10 | ||||
-rw-r--r-- | src/dbus/server/sync-helper.cpp | 11 |
8 files changed, 247 insertions, 147 deletions
diff --git a/src/dbus/server/main.cpp b/src/dbus/server/main.cpp index ed424efd..a8d8c6d9 100644 --- a/src/dbus/server/main.cpp +++ b/src/dbus/server/main.cpp @@ -152,9 +152,9 @@ int main(int argc, char **argv, char **envp) setvbuf(stdout, NULL, _IONBF, 0); // Redirect output and optionally log to syslog. - LogRedirect redirect(true); - redirect.setLevel(stdoutEnabled ? level : Logger::NONE); - std::auto_ptr<Logger> syslogger; + PushLogger<LogRedirect> redirect(new LogRedirect(LogRedirect::STDERR_AND_STDOUT)); + redirect->setLevel(stdoutEnabled ? level : Logger::NONE); + PushLogger<LoggerSyslog> syslogger; if (syslogEnabled && level > Logger::NONE) { syslogger.reset(new LoggerSyslog(execName)); syslogger->setLevel(level); @@ -206,6 +206,9 @@ int main(int argc, char **argv, char **envp) SE_LOG_DEBUG(NULL, "flushing D-Bus connection"); conn.flush(); conn.reset(); + SE_LOG_INFO(NULL, "terminating, closing logging"); + syslogger.reset(); + redirect.reset(); SE_LOG_INFO(NULL, "terminating"); return 0; } catch ( const std::exception &ex ) { diff --git a/src/dbus/server/server.cpp b/src/dbus/server/server.cpp index 4f885d6e..da96c860 100644 --- a/src/dbus/server/server.cpp +++ b/src/dbus/server/server.cpp @@ -48,6 +48,87 @@ static void logIdle(bool idle) SE_LOG_DEBUG(NULL, "server is %s", idle ? "idle" : "not idle"); } +class ServerLogger : public Logger +{ + Logger::Handle m_parentLogger; + // Currently a strong reference. Would be a weak reference + // if we had proper reference counting for Server. + boost::shared_ptr<Server> m_server; + +public: + ServerLogger(const boost::shared_ptr<Server> &server) : + m_parentLogger(Logger::instance()), + m_server(server) + { + } + + virtual void remove() throw () + { + // Hold the Logger mutex while cutting our connection to the + // server. The code using m_server below does the same and + // holds the mutex while logging. That way we prevent threads + // from holding onto the server while it tries to shut down. + // + // This is important because the server's live time is not + // really controlled via the boost::shared_ptr, it may + // destruct while there are still references. See + // Server::m_logger instantiation below. + RecMutex::Guard guard = lock(); + m_server.reset(); + } + + virtual void messagev(const MessageOptions &options, + const char *format, + va_list args) + { + // Ensure that remove() cannot proceed while we have the + // server in use. + RecMutex::Guard guard = lock(); + Server *server = m_server.get(); + message2DBus(server, + options, + format, + args, + server ? server->getPath() : "", + getProcessName()); + } + + /** + * @param server may be NULL, in which case logging only goes to parent + */ + void message2DBus(Server *server, + const MessageOptions &options, + const char *format, + va_list args, + const std::string &dbusPath, + const std::string &procname) + { + // Keeps logging consistent: otherwise thread A might log to + // parent, thread B to parent and D-Bus, then thread A + // finishes its logging via D-Bus. The order of log messages + // would then not be the same in the parent and D-Bus. + RecMutex::Guard guard = lock(); + + // iterating over args in messagev() is destructive, must make a copy first + va_list argsCopy; + va_copy(argsCopy, args); + m_parentLogger.messagev(options, format, args); + + if (server) { + try { + if (options.m_level <= server->getDBusLogLevel()) { + string log = StringPrintfV(format, argsCopy); + server->logOutput(dbusPath, options.m_level, log, procname); + } + } catch (...) { + remove(); + // Give up on server logging silently. + } + } + va_end(argsCopy); + } +}; + void Server::clientGone(Client *c) { for (Clients_t::iterator it = m_clients.begin(); @@ -282,7 +363,11 @@ Server::Server(GMainLoop *loop, m_logOutputSignal(*this, "LogOutput"), m_autoTerm(m_loop, m_shutdownRequested, duration), m_dbusLogLevel(Logger::INFO), - m_parentLogger(Logger::instance()) + // TODO (?): turn Server into a proper reference counted instance. + // This would help with dangling references to it when other threads + // use it for logging, see ServerLogger. However, with mutex locking + // in ServerLogger that shouldn't be a problem. + m_logger(new ServerLogger(boost::shared_ptr<Server>(this, NopDestructor()))) { struct timeval tv; gettimeofday(&tv, NULL); @@ -327,8 +412,9 @@ void Server::activate() // out object isn't visible to it yet. GDBusCXX::DBusObjectHelper::activate(); - Logger::pushLogger(this); - setLevel(Logger::DEBUG); + // Push ourselves as logger for the time being. + m_logger->setLevel(Logger::DEBUG); + m_pushLogger.reset(m_logger); m_presence.reset(new PresenceStatus(*this)); @@ -363,7 +449,9 @@ Server::~Server() m_connman.reset(); m_networkManager.reset(); m_presence.reset(); - Logger::popLogger(); + + m_pushLogger.reset(); + m_logger.reset(); } bool Server::shutdown() @@ -909,24 +997,16 @@ void Server::updateDevice(const string &deviceId, } } -void Server::messagev(const MessageOptions &options, - const char *format, - va_list args, - const std::string &dbusPath, - const std::string &procname) +void Server::message2DBus(const Logger::MessageOptions &options, + const char *format, + va_list args, + const std::string &dbusPath, + const std::string &procname) { - // iterating over args in messagev() is destructive, must make a copy first - va_list argsCopy; - va_copy(argsCopy, args); - m_parentLogger.messagev(options, format, args); // prefix is used to set session path // for general server output, the object path field is dbus server // the object path can't be empty for object paths prevent using empty string. - if (options.m_level <= m_dbusLogLevel) { - string log = StringPrintfV(format, argsCopy); - logOutput(dbusPath, options.m_level, log, procname); - } - va_end(argsCopy); + m_logger->message2DBus(this, options, format, args, dbusPath, procname); } void Server::logOutput(const GDBusCXX::DBusObject_t &path, diff --git a/src/dbus/server/server.h b/src/dbus/server/server.h index 95163331..673958b4 100644 --- a/src/dbus/server/server.h +++ b/src/dbus/server/server.h @@ -53,14 +53,15 @@ class NetworkManagerClient; // TODO: avoid polluting namespace using namespace std; +class ServerLogger; + /** * Implements the main org.syncevolution.Server interface. * * The Server class is responsible for listening to clients and * spinning of sync sessions as requested by clients. */ -class Server : public GDBusCXX::DBusObjectHelper, - public Logger +class Server : public GDBusCXX::DBusObjectHelper { GMainLoop *m_loop; bool &m_shutdownRequested; @@ -391,9 +392,10 @@ class Server : public GDBusCXX::DBusObjectHelper, // The level of detail for D-Bus logging signals. Logger::Level m_dbusLogLevel; - //records the parent logger, dbus server acts as logger to - //send signals to clients and put logs in the parent logger. - Logger &m_parentLogger; + // Created in constructor and captures parent logger there, + // then pushed as default logger in activate(). + boost::shared_ptr<ServerLogger> m_logger; + PushLogger<ServerLogger> m_pushLogger; /** * All active timeouts created by addTimeout(). @@ -653,23 +655,11 @@ public: */ bool notificationsEnabled(); - /** - * implement virtual method from LogStdout. - * Not only print the message in the console - * but also send them as signals to clients - */ - virtual void messagev(const MessageOptions &options, - const char *format, - va_list args) { - messagev(options, format, args, - getPath(), - getProcessName()); - } - void messagev(const MessageOptions &options, - const char *format, - va_list args, - const std::string &dbusPath, - const std::string &procname); + void message2DBus(const Logger::MessageOptions &options, + const char *format, + va_list args, + const std::string &dbusPath, + const std::string &procname); }; // extensions to the D-Bus server, created dynamically by main() diff --git a/src/dbus/server/session-helper.cpp b/src/dbus/server/session-helper.cpp index cffd8e52..0df0004b 100644 --- a/src/dbus/server/session-helper.cpp +++ b/src/dbus/server/session-helper.cpp @@ -30,10 +30,82 @@ SE_BEGIN_CXX +static void dumpString(const std::string &output) +{ + fputs(output.c_str(), stdout); +} + +/** + * Same logging approach as in Server class: pretend that we + * have reference counting for the SessionHelper class and + * use mutex locking to prevent dangling pointers. + */ +class SessionHelperLogger : public Logger +{ + boost::shared_ptr<LogRedirect> m_parentLogger; + boost::shared_ptr<SessionHelper> m_helper; + +public: + SessionHelperLogger(const boost::shared_ptr<LogRedirect> &parentLogger, + const boost::shared_ptr<SessionHelper> &helper): + m_parentLogger(parentLogger), + m_helper(helper) + { + } + + virtual void remove() throw () + { + RecMutex::Guard guard = lock(); + m_helper.reset(); + } + + virtual void messagev(const MessageOptions &options, + const char *format, + va_list args) + { + RecMutex::Guard guard = lock(); + static bool dbg = getenv("SYNCEVOLUTION_DEBUG"); + + if (dbg) { + // let parent LogRedirect or utility function handle the output *in addition* to + // logging via D-Bus + va_list argsCopy; + va_copy(argsCopy, args); + if (m_parentLogger) { + m_parentLogger->messagev(options, format, argsCopy); + } else { + formatLines(options.m_level, DEBUG, + options.m_processName, + options.m_prefix, + format, argsCopy, + boost::bind(dumpString, _1)); + } + va_end(argsCopy); + } else if (m_parentLogger) { + // Only flush parent logger, to capture output sent to + // stdout/stderr by some library and send it via D-Bus + // (recursively!) before printing out own, new output. + m_parentLogger->flush(); + } + + if (m_helper) { + // send to parent + string log = StringPrintfV(format, args); + string strLevel = Logger::levelToStr(options.m_level); + try { + m_helper->emitLogOutput(strLevel, log, options.m_processName ? *options.m_processName : getProcessName()); + } catch (...) { + // Give up forwarding output. + m_helper.reset(); + } + } + } +}; + SessionHelper::SessionHelper(GMainLoop *loop, const GDBusCXX::DBusConnectionPtr &conn, const boost::shared_ptr<ForkExecChild> &forkexec, - LogRedirect *parentLogger) : + const boost::shared_ptr<LogRedirect> &parentLogger) : GDBusCXX::DBusObjectHelper(conn, SessionCommon::HELPER_PATH, SessionCommon::HELPER_IFACE, @@ -42,7 +114,7 @@ SessionHelper::SessionHelper(GMainLoop *loop, m_loop(loop), m_conn(conn), m_forkexec(forkexec), - m_parentLogger(parentLogger), + m_logger(new SessionHelperLogger(parentLogger, boost::shared_ptr<SessionHelper>(this, NopDestructor()))), emitLogOutput(*this, "LogOutput"), emitSyncProgress(*this, "SyncProgress"), emitSourceProgress(*this, "SourceProgress"), @@ -68,52 +140,18 @@ SessionHelper::SessionHelper(GMainLoop *loop, add(emitPasswordRequest); add(emitMessage); add(emitShutdown); - activate(); - Logger::pushLogger(this); -} - -SessionHelper::~SessionHelper() -{ - Logger::popLogger(); } -static void dumpString(const std::string &output) +void SessionHelper::activate() { - fputs(output.c_str(), stdout); + GDBusCXX::DBusObjectHelper::activate(); + m_pushLogger.reset(m_logger); } -void SessionHelper::messagev(const MessageOptions &options, - const char *format, - va_list args) +SessionHelper::~SessionHelper() { - static bool dbg = getenv("SYNCEVOLUTION_DEBUG"); - - if (dbg) { - // let parent LogRedirect or utility function handle the output *in addition* to - // logging via D-Bus - va_list argsCopy; - va_copy(argsCopy, args); - if (m_parentLogger) { - m_parentLogger->messagev(options, format, argsCopy); - } else { - formatLines(options.m_level, DEBUG, - options.m_processName, - options.m_prefix, - format, argsCopy, - boost::bind(dumpString, _1)); - } - va_end(argsCopy); - } else { - // Only flush parent logger, to capture output sent to - // stdout/stderr by some library and send it via D-Bus - // (recursively!) before printing out own, new output. - m_parentLogger->flush(); - } - - // send to parent - string log = StringPrintfV(format, args); - string strLevel = Logger::levelToStr(options.m_level); - emitLogOutput(strLevel, log, options.m_processName ? *options.m_processName : getProcessName()); + m_pushLogger.reset(); + m_logger.reset(); } void SessionHelper::run() diff --git a/src/dbus/server/session-helper.h b/src/dbus/server/session-helper.h index 380ff209..e06b28fd 100644 --- a/src/dbus/server/session-helper.h +++ b/src/dbus/server/session-helper.h @@ -43,14 +43,14 @@ class ForkExecChild; * traditional syncevo-dbus-server did. */ class SessionHelper : public GDBusCXX::DBusObjectHelper, - private Logger, private boost::noncopyable { GMainLoop *m_loop; GDBusCXX::DBusConnectionPtr m_conn; boost::shared_ptr<ForkExecChild> m_forkexec; - LogRedirect *m_parentLogger; boost::function<bool ()> m_operation; + boost::shared_ptr<Logger> m_logger; + PushLogger<Logger> m_pushLogger; /** valid during doSync() */ boost::scoped_ptr<DBusSync> m_sync; @@ -81,18 +81,14 @@ class SessionHelper : public GDBusCXX::DBusObjectHelper, /** SessionHelper.PasswordResponse */ void passwordResponse(bool timedOut, bool aborted, const std::string &password); - // Logger implementation -> output via D-Bus emitLogOutput - virtual void messagev(const MessageOptions &options, - const char *format, - va_list args); - public: SessionHelper(GMainLoop *loop, const GDBusCXX::DBusConnectionPtr &conn, const boost::shared_ptr<ForkExecChild> &forkexec, - LogRedirect *parentLogger); + const boost::shared_ptr<LogRedirect> &parentLogger); ~SessionHelper(); + void activate(); void run(); GMainLoop *getLoop() const { return m_loop; } diff --git a/src/dbus/server/session.cpp b/src/dbus/server/session.cpp index 64819b63..acae499c 100644 --- a/src/dbus/server/session.cpp +++ b/src/dbus/server/session.cpp @@ -204,7 +204,7 @@ void Session::setNamedConfig(const std::string &configName, bool update, bool temporary, const ReadOperations::Config_t &config) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (m_runOperation != SessionCommon::OP_NULL) { string msg = StringPrintf("%s started, cannot change configuration at this time", runOpToString(m_runOperation).c_str()); SE_THROW_EXCEPTION(InvalidCall, msg); @@ -323,7 +323,7 @@ void Session::setNamedConfig(const std::string &configName, void Session::initServer(SharedBuffer data, const std::string &messageType) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); m_serverMode = true; m_initialMessage = data; m_initialMessageType = messageType; @@ -331,7 +331,7 @@ void Session::initServer(SharedBuffer data, const std::string &messageType) void Session::sync(const std::string &mode, const SessionCommon::SourceModes_t &sourceModes) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (m_runOperation == SessionCommon::OP_SYNC) { string msg = StringPrintf("%s started, cannot start again", runOpToString(m_runOperation).c_str()); SE_THROW_EXCEPTION(InvalidCall, msg); @@ -352,7 +352,7 @@ void Session::sync(const std::string &mode, const SessionCommon::SourceModes_t & void Session::sync2(const std::string &mode, const SessionCommon::SourceModes_t &sourceModes) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (!m_forkExecParent || !m_helper) { SE_THROW("syncing cannot continue, helper died"); } @@ -415,7 +415,7 @@ void Session::sync2(const std::string &mode, const SessionCommon::SourceModes_t void Session::abort() { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (m_runOperation != SessionCommon::OP_SYNC && m_runOperation != SessionCommon::OP_CMDLINE) { SE_THROW_EXCEPTION(InvalidCall, "sync not started, cannot abort at this time"); } @@ -436,7 +436,7 @@ void Session::abort() void Session::suspend() { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (m_runOperation != SessionCommon::OP_SYNC && m_runOperation != SessionCommon::OP_CMDLINE) { SE_THROW_EXCEPTION(InvalidCall, "sync not started, cannot suspend at this time"); } @@ -453,7 +453,7 @@ void Session::suspend() void Session::abortAsync(const SimpleResult &result) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (!m_forkExecParent) { result.done(); } else { @@ -470,7 +470,7 @@ void Session::getStatus(std::string &status, uint32_t &error, SourceStatuses_t &sources) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); status = syncStatusToString(m_syncStatus); if (m_stepIsWaiting) { status += ";waiting"; @@ -483,14 +483,14 @@ void Session::getStatus(std::string &status, void Session::getProgress(int32_t &progress, SourceProgresses_t &sources) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); progress = m_progData.getProgress(); sources = m_sourceProgress; } void Session::fireStatus(bool flush) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); std::string status; uint32_t error; SourceStatuses_t sources; @@ -507,7 +507,7 @@ void Session::fireStatus(bool flush) void Session::fireProgress(bool flush) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); int32_t progress; SourceProgresses_t sources; @@ -595,13 +595,13 @@ Session::Session(Server &server, void Session::passwordRequest(const std::string &descr, const ConfigPasswordKey &key) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); m_passwordRequest = m_server.passwordRequest(descr, key, m_me); } void Session::dbusResultCb(const std::string &operation, bool success, const std::string &error) throw() { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { SE_LOG_DEBUG(NULL, "%s helper call completed, %s", operation.c_str(), @@ -625,7 +625,7 @@ void Session::dbusResultCb(const std::string &operation, bool success, const std void Session::failureCb() throw() { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { if (m_status == SESSION_DONE) { // ignore errors that happen after session already closed, @@ -666,7 +666,7 @@ void Session::failureCb() throw() void Session::doneCb(bool success) throw() { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { if (m_status == SESSION_DONE) { return; @@ -730,7 +730,7 @@ static void raiseChildTermError(int status, const SimpleResult &result) void Session::runOperationAsync(SessionCommon::RunOperation op, const SuccessCb_t &helperReady) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); m_server.addSyncSession(this); m_runOperation = op; m_status = SESSION_RUNNING; @@ -743,7 +743,7 @@ void Session::runOperationAsync(SessionCommon::RunOperation op, void Session::useHelperAsync(const SimpleResult &result) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { if (m_helper) { // exists already, invoke callback directly @@ -810,9 +810,9 @@ void Session::messagev(const MessageOptions &options, { // log with session path and empty process name, // just like the syncevo-dbus-helper does - m_server.messagev(options, - format, args, - getPath(), ""); + m_server.message2DBus(options, + format, args, + getPath(), ""); } static void Logging2Server(Server &server, @@ -829,7 +829,7 @@ static void Logging2Server(Server &server, void Session::useHelper2(const SimpleResult &result, const boost::signals2::connection &c) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { // helper is running, don't call result.failed() when it quits // sometime in the future @@ -872,7 +872,7 @@ void Session::useHelper2(const SimpleResult &result, const boost::signals2::conn void Session::onConnect(const GDBusCXX::DBusConnectionPtr &conn) throw () { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { SE_LOG_DEBUG(NULL, "helper has connected"); m_helper.reset(new SessionProxy(conn)); @@ -891,7 +891,7 @@ void Session::onConnect(const GDBusCXX::DBusConnectionPtr &conn) throw () void Session::onQuit(int status) throw () { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { SE_LOG_DEBUG(NULL, "helper quit with return code %d, was %s", status, @@ -936,7 +936,7 @@ void Session::onQuit(int status) throw () void Session::onFailure(SyncMLStatus status, const std::string &explanation) throw () { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { SE_LOG_DEBUG(NULL, "helper failed, status code %d = %s, %s", status, @@ -949,7 +949,7 @@ void Session::onFailure(SyncMLStatus status, const std::string &explanation) thr void Session::onOutput(const char *buffer, size_t length) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); // treat null-bytes inside the buffer like line breaks size_t off = 0; do { @@ -960,7 +960,7 @@ void Session::onOutput(const char *buffer, size_t length) void Session::activateSession() { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (m_status != SESSION_IDLE) { SE_THROW("internal error, session changing from non-idle to active"); } @@ -981,7 +981,7 @@ void Session::activateSession() void Session::passwordResponse(bool timedOut, bool aborted, const std::string &password) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (m_helper) { // Ignore communicaton failures with helper here, // we'll notice that elsewhere @@ -994,7 +994,7 @@ void Session::passwordResponse(bool timedOut, bool aborted, const std::string &p void Session::syncProgress(sysync::TProgressEventEnum type, int32_t extra1, int32_t extra2, int32_t extra3) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); switch(type) { case sysync::PEV_CUSTOM_START: m_cmdlineOp = (RunOperation)extra1; @@ -1047,7 +1047,7 @@ void Session::sourceProgress(sysync::TProgressEventEnum type, SyncMode sourceSyncMode, int32_t extra1, int32_t extra2, int32_t extra3) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); // a command line operation can be many things, helper must have told us SessionCommon::RunOperation op = m_runOperation == SessionCommon::OP_CMDLINE ? m_cmdlineOp : @@ -1165,7 +1165,7 @@ void Session::sourceProgress(sysync::TProgressEventEnum type, bool Session::setFilters(SyncConfig &config) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); /** apply temporary configs to config */ config.setConfigFilter(true, "", m_syncFilter); // set all sources in the filter to config @@ -1177,7 +1177,7 @@ bool Session::setFilters(SyncConfig &config) void Session::setWaiting(bool isWaiting) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); // if stepInfo doesn't change, then ignore it to avoid duplicate status info if(m_stepIsWaiting != isWaiting) { m_stepIsWaiting = isWaiting; @@ -1187,7 +1187,7 @@ void Session::setWaiting(bool isWaiting) void Session::restore(const string &dir, bool before, const std::vector<std::string> &sources) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (m_runOperation == SessionCommon::OP_RESTORE) { string msg = StringPrintf("restore started, cannot restore again"); SE_THROW_EXCEPTION(InvalidCall, msg); @@ -1207,7 +1207,7 @@ void Session::restore(const string &dir, bool before, const std::vector<std::str void Session::restore2(const string &dir, bool before, const std::vector<std::string> &sources) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (!m_forkExecParent || !m_helper) { SE_THROW("syncing cannot continue, helper died"); } @@ -1219,7 +1219,7 @@ void Session::restore2(const string &dir, bool before, const std::vector<std::st void Session::execute(const vector<string> &args, const map<string, string> &vars) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (m_runOperation == SessionCommon::OP_CMDLINE) { SE_THROW_EXCEPTION(InvalidCall, "cmdline started, cannot start again"); } else if (m_runOperation != SessionCommon::OP_NULL) { @@ -1238,7 +1238,7 @@ void Session::execute(const vector<string> &args, const map<string, string> &var void Session::execute2(const vector<string> &args, const map<string, string> &vars) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); if (!m_forkExecParent || !m_helper) { SE_THROW("syncing cannot continue, helper died"); } @@ -1251,7 +1251,7 @@ void Session::execute2(const vector<string> &args, const map<string, string> &va /*Implementation of Session.CheckPresence */ void Session::checkPresence (string &status) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); vector<string> transport; m_server.checkPresence(m_configName, status, transport); } @@ -1260,7 +1260,7 @@ void Session::sendViaConnection(const DBusArray<uint8_t> buffer, const std::string &type, const std::string &url) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { boost::shared_ptr<Connection> connection = m_connection.lock(); @@ -1279,7 +1279,7 @@ void Session::sendViaConnection(const DBusArray<uint8_t> buffer, void Session::shutdownConnection() { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); try { boost::shared_ptr<Connection> connection = m_connection.lock(); @@ -1299,7 +1299,7 @@ void Session::shutdownConnection() void Session::storeMessage(const DBusArray<uint8_t> &message, const std::string &type) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); // ignore errors if (m_helper) { m_helper->m_storeMessage.start(message, type, @@ -1309,7 +1309,7 @@ void Session::storeMessage(const DBusArray<uint8_t> &message, void Session::connectionState(const std::string &error) { - Session::LoggingGuard guard(this); + PushLogger<Logger> guard(m_me); // ignore errors if (m_helper) { m_helper->m_connectionState.start(error, diff --git a/src/dbus/server/session.h b/src/dbus/server/session.h index c7053e69..f2062eb8 100644 --- a/src/dbus/server/session.h +++ b/src/dbus/server/session.h @@ -351,16 +351,6 @@ private: const char *format, va_list args); - class LoggingGuard { - public: - LoggingGuard(Session *session) { - Logger::pushLogger(session); - } - ~LoggingGuard() { - Logger::popLogger(); - } - }; - public: enum { PRI_CMDLINE = -10, diff --git a/src/dbus/server/sync-helper.cpp b/src/dbus/server/sync-helper.cpp index fc7d4fae..5ad57516 100644 --- a/src/dbus/server/sync-helper.cpp +++ b/src/dbus/server/sync-helper.cpp @@ -44,11 +44,12 @@ namespace { } void onConnect(const DBusConnectionPtr &conn, - LogRedirect *parentLogger, + const boost::shared_ptr<LogRedirect> &parentLogger, const boost::shared_ptr<ForkExecChild> &forkexec, boost::shared_ptr<SessionHelper> &helper) { helper.reset(new SessionHelper(loop, conn, forkexec, parentLogger)); + helper->activate(); } void onAbort() @@ -87,9 +88,11 @@ int main(int argc, char **argv, char **envp) // which are unaware of the SyncEvolution logging system. // Redirecting is useful to get such output into our // sync logfile, once we have one. - boost::scoped_ptr<LogRedirect> redirect; + boost::shared_ptr<LogRedirect> redirect; + PushLogger<LogRedirect> pushRedirect; if (!debug) { - redirect.reset(new LogRedirect(true)); + redirect.reset(new LogRedirect(LogRedirect::STDERR_AND_STDOUT)); + pushRedirect.reset(redirect); } setvbuf(stderr, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); @@ -110,7 +113,7 @@ int main(int argc, char **argv, char **envp) boost::shared_ptr<SessionHelper> helper; bool failed = false; - forkexec->m_onConnect.connect(boost::bind(onConnect, _1, redirect.get(), + forkexec->m_onConnect.connect(boost::bind(onConnect, _1, redirect, boost::cref(forkexec), boost::ref(helper))); forkexec->m_onFailure.connect(boost::bind(onFailure, _2, boost::ref(failed))); |