From 04627982d0881a2bfab0e1668ab350544fd79ae5 Mon Sep 17 00:00:00 2001 From: Robert Swiecki Date: Thu, 7 Jun 2018 16:51:50 +0200 Subject: [PATCH] logs: use log file/level immediately --- cmdline.cc | 21 +++++++-------------- config.cc | 19 +++++++++---------- logs.cc | 20 ++++++++++++-------- logs.h | 3 ++- nsjail.h | 4 ---- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/cmdline.cc b/cmdline.cc index 50062ad..59be71f 100644 --- a/cmdline.cc +++ b/cmdline.cc @@ -383,7 +383,6 @@ std::unique_ptr parseArgs(int argc, char* argv[]) { nsjconf->cwd = "/"; nsjconf->port = 0; nsjconf->bindhost = "::"; - nsjconf->loglevel = logs::INFO; nsjconf->daemonize = false; nsjconf->tlimit = 0; nsjconf->max_cpus = 0; @@ -486,25 +485,22 @@ std::unique_ptr parseArgs(int argc, char* argv[]) { nsjconf->max_conns_per_ip = strtoul(optarg, NULL, 0); break; case 'l': - nsjconf->logfile = optarg; + logs::logFile(optarg); break; case 'L': - nsjconf->logfile = "/dev/fd/" + std::to_string(strtoimax(optarg, NULL, 10)); + logs::logFile(std::string("/dev/fd/") + optarg); break; case 'd': nsjconf->daemonize = true; break; case 'v': - nsjconf->loglevel = logs::DEBUG; - logs::logLevel(nsjconf->loglevel); + logs::logLevel(logs::DEBUG); break; case 'q': - nsjconf->loglevel = logs::WARNING; - logs::logLevel(nsjconf->loglevel); + logs::logLevel(logs::WARNING); break; case 'Q': - nsjconf->loglevel = logs::FATAL; - logs::logLevel(nsjconf->loglevel); + logs::logLevel(logs::FATAL); break; case 'e': nsjconf->keep_env = true; @@ -811,11 +807,8 @@ std::unique_ptr parseArgs(int argc, char* argv[]) { } } - if (nsjconf->daemonize && nsjconf->logfile.empty()) { - nsjconf->logfile = _LOG_DEFAULT_FILE; - } - if (!logs::initLog(nsjconf->logfile, nsjconf->loglevel)) { - return nullptr; + if (nsjconf->daemonize && !logs::logSet()) { + logs::logFile(_LOG_DEFAULT_FILE); } if (!setupMounts(nsjconf.get(), tmpfs_mounts, tmpfs_size)) { return nullptr; diff --git a/config.cc b/config.cc index e163caf..a476e6d 100644 --- a/config.cc +++ b/config.cc @@ -95,33 +95,32 @@ static bool configParseInternal(nsjconf_t* nsjconf, const nsjail::NsJailConfig& nsjconf->daemonize = njc.daemon(); if (njc.has_log_fd()) { - nsjconf->logfile = "/dev/fd/" + std::to_string(njc.log_fd()); + logs::logFile(std::string("/dev/fd/") + std::to_string(njc.log_fd())); } if (njc.has_log_file()) { - nsjconf->logfile = njc.log_file(); + logs::logFile(njc.log_file()); } if (njc.has_log_level()) { switch (njc.log_level()) { case nsjail::LogLevel::DEBUG: - nsjconf->loglevel = logs::DEBUG; + logs::logLevel(logs::DEBUG); break; case nsjail::LogLevel::INFO: - nsjconf->loglevel = logs::INFO; + logs::logLevel(logs::INFO); break; case nsjail::LogLevel::WARNING: - nsjconf->loglevel = logs::WARNING; + logs::logLevel(logs::WARNING); break; case nsjail::LogLevel::ERROR: - nsjconf->loglevel = logs::ERROR; + logs::logLevel(logs::ERROR); break; case nsjail::LogLevel::FATAL: - nsjconf->loglevel = logs::FATAL; + logs::logLevel(logs::FATAL); break; default: LOG_E("Unknown log_level: %d", njc.log_level()); return false; } - logs::logLevel(nsjconf->loglevel); } nsjconf->keep_env = njc.keep_env(); @@ -278,7 +277,7 @@ static void LogHandler( } bool parseFile(nsjconf_t* nsjconf, const char* file) { - LOG_I("Parsing configuration from '%s'", file); + LOG_D("Parsing configuration from '%s'", file); int fd = open(file, O_RDONLY | O_CLOEXEC); if (fd == -1) { @@ -302,8 +301,8 @@ bool parseFile(nsjconf_t* nsjconf, const char* file) { LOG_W("Couldn't parse the ProtoBuf"); return false; } - LOG_D("Parsed config:\n'%s'", nsc.DebugString().c_str()); + LOG_D("Parsed config:\n'%s'", nsc.DebugString().c_str()); return true; } diff --git a/logs.cc b/logs.cc index dcfb9b6..3fd0125 100644 --- a/logs.cc +++ b/logs.cc @@ -45,22 +45,32 @@ namespace logs { static int _log_fd = STDERR_FILENO; static bool _log_fd_isatty = true; static enum llevel_t _log_level = INFO; +static bool _log_set = false; __attribute__((constructor)) static void log_init(void) { _log_fd_isatty = isatty(_log_fd); } +bool logSet() { + return _log_set; +} + /* * Log to stderr by default. Use a dup()d fd, because in the future we'll associate the * connection socket with fd (0, 1, 2). */ -bool initLog(const std::string& logfile, llevel_t level) { + +void logLevel(enum llevel_t ll) { + _log_level = ll; +} + +void logFile(const std::string& logfile) { + _log_set = true; /* Close previous log_fd */ if (_log_fd > STDERR_FILENO) { close(_log_fd); _log_fd = STDERR_FILENO; } - _log_level = level; if (logfile.empty()) { _log_fd = fcntl(_log_fd, F_DUPFD_CLOEXEC, 0); } else { @@ -69,11 +79,9 @@ bool initLog(const std::string& logfile, llevel_t level) { _log_fd = STDERR_FILENO; _log_fd_isatty = (isatty(_log_fd) == 1 ? true : false); PLOG_E("Couldn't open logfile open('%s')", logfile.c_str()); - return false; } } _log_fd_isatty = (isatty(_log_fd) == 1 ? true : false); - return true; } void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) { @@ -135,8 +143,4 @@ void logStop(int sig) { LOG_I("Server stops due to fatal signal (%d) caught. Exiting", sig); } -void logLevel(enum llevel_t ll) { - _log_level = ll; -} - } // namespace logs diff --git a/logs.h b/logs.h index 8d5a1fe..27727ef 100644 --- a/logs.h +++ b/logs.h @@ -55,11 +55,12 @@ enum llevel_t { HELP_BOLD, }; -bool initLog(const std::string& logfile, llevel_t loglevel); void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) __attribute__((format(printf, 5, 6))); void logStop(int sig); void logLevel(enum llevel_t ll); +void logFile(const std::string& logfile); +bool logSet(); } // namespace logs diff --git a/nsjail.h b/nsjail.h index 132d7e5..69657a8 100644 --- a/nsjail.h +++ b/nsjail.h @@ -35,8 +35,6 @@ #include #include -#include "logs.h" - static const int nssigs[] = { SIGINT, SIGQUIT, @@ -93,8 +91,6 @@ struct nsjconf_t { std::string chroot; int port; std::string bindhost; - std::string logfile; - logs::llevel_t loglevel; bool daemonize; uint64_t tlimit; size_t max_cpus; -- 2.7.4