Commit a38ec381 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Merge branch '33-empty-log-file-when-running-in-daemon-mode' into 'master'

Resolve "Empty log file when running in daemon mode"

Closes #33

See merge request !14
parents b6adab6b a428f3e3
Loading
Loading
Loading
Loading
Loading
+4 −0
Original line number Original line Diff line number Diff line
@@ -131,6 +131,10 @@ public:
        return global_logger();
        return global_logger();
    }
    }


    static inline void destroy_global_logger() {
        global_logger().reset();
    }

// some macros to make it more convenient to use the global logger
// some macros to make it more convenient to use the global logger
#define LOGGER_INFO(...)                                    \
#define LOGGER_INFO(...)                                    \
do {                                                        \
do {                                                        \
+17 −2
Original line number Original line Diff line number Diff line
@@ -94,8 +94,23 @@ pid_t urd::daemonize() {
        return 0;
        return 0;
    }
    }


    // We need to destroy the global logger before calling fork. Otherwise the
    // logger will not function properly since its internal thread will not
    // be duplicated by fork(). Furthermore, if we don't destroy pre-fork()
    // and attempt to replace it post-fork(), the logger destructor will attempt
    // to join the (now invalid) thread and end up blocking forever. To avoid
    // this (and since we want to be able to output messages from all 
    // processes), we destroy it now and recreate it post-fork() both in the
    // parent process and in the child.
    logger::destroy_global_logger();

    /* Fork off the parent process */
    /* Fork off the parent process */
    if((pid = fork()) < 0) {
    pid = fork();

    // re-initialize logging facilities (post-fork)
    init_logger();

    if(pid < 0) {
        LOGGER_ERRNO("Failed to create child process");
        LOGGER_ERRNO("Failed to create child process");
        exit(EXIT_FAILURE);
        exit(EXIT_FAILURE);
    }
    }
@@ -1536,7 +1551,7 @@ urd_error urd::check_shutdown() {


int urd::run() {
int urd::run() {


    // initialize logging facilities
    // initialize logging facilities (pre-fork)
    init_logger();
    init_logger();


    // validate settings
    // validate settings