logs: use log file/level immediately
authorRobert Swiecki <robert@swiecki.net>
Thu, 7 Jun 2018 14:51:50 +0000 (16:51 +0200)
committerRobert Swiecki <robert@swiecki.net>
Thu, 7 Jun 2018 14:51:50 +0000 (16:51 +0200)
cmdline.cc
config.cc
logs.cc
logs.h
nsjail.h

index 50062ad0b19464173cc154def1b056ae98e82aa9..59be71f38a5956994ace54f72e88ef569a491469 100644 (file)
@@ -383,7 +383,6 @@ std::unique_ptr<nsjconf_t> 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<nsjconf_t> 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<nsjconf_t> 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;
index e163caf47d2d7f95878e96d9bd94ce0918824210..a476e6dc9c4d906e6d5a27e8bee089f743b1550e 100644 (file)
--- 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 dcfb9b62f67d3ad698bfe28be504837be3258a5b..3fd0125597c93a563e12cb2cb863a3c114a4b28d 100644 (file)
--- 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 8d5a1fe93b1629e68b2e3d58725d880a87ae49f0..27727efee370c3ff2ae198b3e1051a549c5de7a5 100644 (file)
--- 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
 
index 132d7e53011689a092698343e7d49581d0075611..69657a8e5937240a9e5bc71291a6e58e68dbb96a 100644 (file)
--- a/nsjail.h
+++ b/nsjail.h
@@ -35,8 +35,6 @@
 #include <string>
 #include <vector>
 
-#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;