Commit cfb87295 authored by Julius Athenstaedt's avatar Julius Athenstaedt
Browse files

LIBGKFS_LOG_PER_PROCESS environment variable can be used to

create logs in separate files for each process of the client.
parent c4d2a5a0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ static constexpr auto LOG_SYSCALL_FILTER = ADD_PREFIX("LOG_SYSCALL_FILTER");
#endif

static constexpr auto LOG_OUTPUT = ADD_PREFIX("LOG_OUTPUT");
static constexpr auto LOG_PER_PROCESS = ADD_PREFIX("LOG_PER_PROCESS");
static constexpr auto LOG_OUTPUT_TRUNC = ADD_PREFIX("LOG_OUTPUT_TRUNC");
static constexpr auto CWD = ADD_PREFIX("CWD");
static constexpr auto HOSTS_FILE = ADD_PREFIX("HOSTS_FILE");
+4 −3
Original line number Diff line number Diff line
@@ -273,7 +273,7 @@ protected:

struct logger {

    logger(const std::string& opts, const std::string& path, bool trunc
    logger(const std::string& opts, const std::string& path, int log_per_process, bool trunc
#ifdef GKFS_DEBUG_BUILD
           ,
           const std::string& filter, int verbosity
@@ -293,7 +293,7 @@ struct logger {

        static_buffer buffer;
        detail::format_timestamp_to(buffer, timezone_);
        fmt::format_to(buffer, "[{}] [{}] ", ::syscall_no_intercept(SYS_gettid),
        fmt::format_to(buffer, "[{}] [{}] ", log_process_id_,
                       lookup_level_name(level));

        if(!!(level & log::debug)) {
@@ -336,7 +336,7 @@ struct logger {

        static_buffer prefix;
        detail::format_timestamp_to(prefix);
        fmt::format_to(prefix, "[{}] [{}] ", ::syscall_no_intercept(SYS_gettid),
        fmt::format_to(prefix, "[{}] [{}] ", log_process_id_,
                       lookup_level_name(level));

        char buffer[max_buffer_size];
@@ -402,6 +402,7 @@ struct logger {
    }

    int log_fd_;
    int log_process_id_;
    log_level log_mask_;

#ifdef GKFS_DEBUG_BUILD
+13 −2
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
#include <client/env.hpp>
#include <client/make_array.hpp>
#include <regex>
#include <filesystem>

extern "C" {
#include <date/tz.h>
@@ -43,6 +44,8 @@ extern "C" {

#endif

namespace fs = std::filesystem;

namespace {
enum class split_str_mode {
    is_any_of,
@@ -280,7 +283,8 @@ process_log_filter(const std::string& log_filter) {

#endif // GKFS_DEBUG_BUILD

logger::logger(const std::string& opts, const std::string& path, bool trunc
logger::logger(const std::string& opts, const std::string& path,
               int log_per_process, bool trunc
#ifdef GKFS_DEBUG_BUILD
               ,
               const std::string& filter, int verbosity
@@ -290,6 +294,7 @@ logger::logger(const std::string& opts, const std::string& path, bool trunc

    /* use stderr by default */
    log_fd_ = 2;
    log_process_id_ = ::syscall_no_intercept(SYS_gettid);
    log_mask_ = process_log_options(opts);

#ifdef GKFS_DEBUG_BUILD
@@ -304,11 +309,17 @@ logger::logger(const std::string& opts, const std::string& path, bool trunc
            flags &= ~O_TRUNC;
        }

        std::string file_path = path;
        if(log_per_process) {
            fs::create_directories(path);
            file_path += "/" + std::to_string(log_process_id_);
        }

        // we use ::open() here rather than ::syscall_no_intercept(SYS_open)
        // because we want the call to be intercepted by our hooks, which
        // allows us to categorize the resulting fd as 'internal' and
        // relocate it to our private range
        int fd = ::open(path.c_str(), flags, 0600);
        int fd = ::open(file_path.c_str(), flags, 0600);

        if(fd == -1) {
            log(gkfs::log::error, __func__, __LINE__,
+4 −1
Original line number Diff line number Diff line
@@ -76,6 +76,9 @@ PreloadContext::init_logging() {
    const std::string log_output = gkfs::env::get_var(
            gkfs::env::LOG_OUTPUT, gkfs::config::log::client_log_path);

    const int log_per_process = std::atoi(
            gkfs::env::get_var(gkfs::env::LOG_PER_PROCESS, "0").c_str());

#ifdef GKFS_DEBUG_BUILD
    // atoi returns 0 if no int conversion can be performed, which works
    // for us since if the user provides a non-numeric value we can just treat
@@ -92,7 +95,7 @@ PreloadContext::init_logging() {

    const bool log_trunc = (!trunc_val.empty() && trunc_val[0] != '0');

    gkfs::log::create_global_logger(log_opts, log_output, log_trunc
    gkfs::log::create_global_logger(log_opts, log_output, log_per_process, log_trunc
#ifdef GKFS_DEBUG_BUILD
                                    ,
                                    log_filter, log_verbosity