From 0f2d50bf1ab008fde0937ab0624e178673fa9454 Mon Sep 17 00:00:00 2001 From: Robert Swiecki Date: Thu, 7 Jun 2018 18:42:34 +0200 Subject: [PATCH] Log to a duplicate of stderr initially --- logs.cc | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/logs.cc b/logs.cc index 52dfd9f..ac9fd32 100644 --- a/logs.cc +++ b/logs.cc @@ -49,6 +49,10 @@ static bool _log_set = false; __attribute__((constructor)) static void log_init(void) { _log_fd_isatty = isatty(_log_fd); + _log_fd = fcntl(_log_fd, F_DUPFD_CLOEXEC, 0); + if (_log_fd == -1) { + _log_fd = STDERR_FILENO; + } } bool logSet() { @@ -71,17 +75,10 @@ void logFile(const std::string& logfile) { close(_log_fd); _log_fd = STDERR_FILENO; } - if (logfile.empty()) { - _log_fd = fcntl(_log_fd, F_DUPFD_CLOEXEC, 0); - if (_log_fd == -1) { - _log_fd = STDERR_FILENO; - } - } else { - if (TEMP_FAILURE_RETRY(_log_fd = open(logfile.c_str(), - O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640)) == -1) { - _log_fd = STDERR_FILENO; - PLOG_W("Couldn't open logfile open('%s')", logfile.c_str()); - } + if (TEMP_FAILURE_RETRY(_log_fd = open(logfile.c_str(), + O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640)) == -1) { + _log_fd = STDERR_FILENO; + PLOG_W("Couldn't open logfile open('%s')", logfile.c_str()); } _log_fd_isatty = (isatty(_log_fd) == 1); }