Verified Commit 02747f74 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add log modules for hermes and mercury

parent 093a819b
Loading
Loading
Loading
Loading
+26 −6
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <fmt/format.h>
#include <fmt/ostream.h>
#include <date/tz.h>
#include <hermes.hpp>

#ifdef GKFS_DEBUG_BUILD
#include <bitset>
@@ -38,14 +39,15 @@ enum class log_level : short {
    print_critical       = 1 << 3,
    print_errors         = 1 << 4,
    print_warnings       = 1 << 5,
    print_mercury        = 1 << 6,
    print_debug          = 1 << 7,
    print_hermes         = 1 << 6,
    print_mercury        = 1 << 7,
    print_debug          = 1 << 8,

    // for internal use
    print_none           = 0,
    print_all            = print_syscalls | print_syscalls_entry | print_info |
                           print_critical | print_errors | print_warnings |
                           print_mercury | print_debug,
                           print_hermes | print_mercury | print_debug,
    print_most           = print_all & ~print_syscalls_entry,
    print_help           = 1 << 10
};
@@ -100,6 +102,7 @@ static const auto constexpr info = log_level::print_info;
static const auto constexpr critical         = log_level::print_critical;
static const auto constexpr error            = log_level::print_errors;
static const auto constexpr warning          = log_level::print_warnings;
static const auto constexpr hermes           = log_level::print_hermes;
static const auto constexpr mercury          = log_level::print_mercury;
static const auto constexpr debug            = log_level::print_debug;
static const auto constexpr none             = log_level::print_none;
@@ -115,6 +118,7 @@ static const auto constexpr level_names =
        "critical",
        "error",
        "warning",
        "hermes",
        "mercury",
        "debug"
);
@@ -464,16 +468,18 @@ static_buffer::grow(std::size_t size) {

#define LOG(XXX, ...) LOG_##XXX(__VA_ARGS__)

#ifdef GKFS_DISABLE_LOGGING
#ifndef GKFS_ENABLE_LOGGING

#define LOG_INFO(...) do {} while(0);
#define LOG_WARNING(...) do {} while(0);
#define LOG_ERROR(...) do {} while(0);
#define LOG_CRITICAL(...) do {} while(0);
#define LOG_HERMES(...)  do {} while(0);
#define LOG_MERCURY(...) do {} while(0);
#define LOG_SYSCALL(...) do {} while(0);
#define LOG_DEBUG(...) do {} while(0);

#else // !GKFS_DISABLE_LOGGING
#else // !GKFS_ENABLE_LOGGING

#define LOG_INFO(...) do {                                              \
    if(gkfs::log::get_global_logger()) {                                \
@@ -503,6 +509,20 @@ static_buffer::grow(std::size_t size) {
    }                                                                   \
} while(0);

#define LOG_HERMES(...) do { \
    if(gkfs::log::get_global_logger()) {                                \
        gkfs::log::get_global_logger()->log(                            \
                gkfs::log::hermes, __func__, __LINE__, __VA_ARGS__);    \
    }                                                                   \
} while(0);

#define LOG_MERCURY(...) do { \
    if(gkfs::log::get_global_logger()) {                                \
        gkfs::log::get_global_logger()->log(                            \
                gkfs::log::mercury, __func__, __LINE__, __VA_ARGS__);   \
    }                                                                   \
} while(0);

#ifdef GKFS_DEBUG_BUILD

#define LOG_SYSCALL(...) do {                                           \
@@ -524,6 +544,6 @@ if(gkfs::log::get_global_logger()) { \
#define LOG_DEBUG(...) do {} while(0);

#endif // ! GKFS_DEBUG_BUILD
#endif // !GKFS_DISABLE_LOGGING
#endif // !GKFS_ENABLE_LOGGING

#endif // LIBGKFS_LOGGING_HPP
+5 −5
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ hook_internal(long syscall_number,
              long arg3, long arg4, long arg5,
              long *result) {

#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#if defined(GKFS_ENABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
	const long args[gkfs::syscall::MAX_ARGS] = {
	    arg0, arg1, arg2, arg3, arg4, arg5
	};
@@ -408,7 +408,7 @@ int hook(long syscall_number,
         long arg3, long arg4, long arg5,
         long *result) {

#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#if defined(GKFS_ENABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
	const long args[gkfs::syscall::MAX_ARGS] = {
	    arg0, arg1, arg2, arg3, arg4, arg5
	};
@@ -733,7 +733,7 @@ hook_forwarded_syscall(long syscall_number,
        return;
    }

#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#if defined(GKFS_ENABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
	const long args[gkfs::syscall::MAX_ARGS] = {
	    arg0, arg1, arg2, arg3, arg4, arg5
	};
@@ -754,7 +754,7 @@ hook_clone_at_child(unsigned long flags,
		            int* ctid,
		            long newtls) {

#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#if defined(GKFS_ENABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
    const long args[gkfs::syscall::MAX_ARGS] = {
        static_cast<long>(flags), 
        reinterpret_cast<long>(child_stack), 
@@ -782,7 +782,7 @@ hook_clone_at_parent(unsigned long flags,
 		             long newtls, 
 		             long returned_pid) {

#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#if defined(GKFS_ENABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
    const long args[gkfs::syscall::MAX_ARGS] = {
        static_cast<long>(flags), 
        reinterpret_cast<long>(child_stack), 
+88 −6
Original line number Diff line number Diff line
@@ -18,6 +18,10 @@
#include <date/tz.h>
#include <fmt/ostream.h>

#ifdef GKFS_ENABLE_LOGGING
#include <hermes/logging.hpp>
#endif

namespace gkfs {
namespace log {

@@ -118,22 +122,21 @@ static const auto constexpr max_help_text_rows =
log_level
process_log_options(const std::string gkfs_debug) {

#ifdef GKFS_DISABLE_LOGGING
#ifndef GKFS_ENABLE_LOGGING

    (void) gkfs_debug;
    logger::log_message(stdout, "warning: logging options ignored: "
                        "logging support was disabled in this build");
    return log::none;

#endif // ! GKFS_DISABLE_LOGGING
#endif // ! GKFS_ENABLE_LOGGING

    log_level dm = log::none;

    std::vector<std::string> tokens;

    // skip separating white spaces and commas
    boost::split(tokens, gkfs_debug, 
            [](char c) { return c == ' ' || c == ','; });
    boost::split(tokens, gkfs_debug, boost::is_any_of(" ,"));

    for(const auto& t : tokens) {

@@ -265,7 +268,85 @@ logger::logger(const std::string& opts,
        log_fd_ = fd;
    }

#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#ifdef GKFS_ENABLE_LOGGING
    const auto log_hermes_message = 
        [](const std::string& msg, hermes::log::level l, int severity, 
           const std::string& file, const std::string& func, int lineno) {

        const auto name = [](hermes::log::level l, int severity) {
            using namespace std::string_literals;

            switch(l) {
                case hermes::log::info:
                    return "info"s;
                case hermes::log::warning:
                    return "warning"s;
                case hermes::log::error:
                    return "error"s;
                case hermes::log::fatal:
                    return "fatal"s;
                case hermes::log::mercury:
                    return "mercury"s;
                default:
                    return "unknown"s;
            }
        };

        LOG(HERMES, "[{}] {}", name(l, severity), msg);
    };

#ifdef GKFS_DEBUG_BUILD
    const auto log_hermes_debug_message = 
        [this](const std::string& msg, hermes::log::level l, 
               int severity, const std::string& file, 
               const std::string& func, int lineno) {

        if(severity > debug_verbosity_) {
            return;
        }

        LOG(HERMES, "[debug{}] <{}():{}> {}", 
                (severity == 0 ? "" : std::to_string(severity + 1)), 
                func, lineno, msg);
    };
#endif // GKFS_DEBUG_BUILD

    const auto log_hg_message = 
        [](const std::string& msg, hermes::log::level l, int severity, 
           const std::string& file, const std::string& func, int lineno) {

        (void) l;

        // mercury message might contain one or more sub-messages 
        // separated by '\n'
        std::vector<std::string> sub_msgs;
        boost::split(sub_msgs, msg, boost::is_any_of("\n"), boost::token_compress_on);

        for(const auto& m : sub_msgs) {
            if(!m.empty()) {
                LOG(MERCURY, "{}", m);
            }
        }
    };

    // register log callbacks into hermes so that we can manage 
    // both its and mercury's log messages 
    hermes::log::logger::register_callback(
            hermes::log::info, log_hermes_message);
    hermes::log::logger::register_callback(
            hermes::log::warning, log_hermes_message);
    hermes::log::logger::register_callback(
            hermes::log::error, log_hermes_message);
    hermes::log::logger::register_callback(
            hermes::log::fatal, log_hermes_message);
#ifdef GKFS_DEBUG_BUILD
    hermes::log::logger::register_callback(
            hermes::log::debug, log_hermes_debug_message);
#endif
    hermes::log::logger::register_callback(
            hermes::log::mercury, log_hg_message);

#ifdef GKFS_DEBUG_BUILD
    // Finding the current timezone implies accessing OS files (i.e. syscalls),
    // but current_zone() doesn't actually retrieve the time zone but rather
    // provides a descriptor to it that is **atomically initialized** upon its 
@@ -284,7 +365,8 @@ logger::logger(const std::string& opts,
    // be removed if the date API ends up providing this functionality.
    using namespace date;
    timezone_->get_info(date::sys_days{January/1/1970});
#endif
#endif // GKFS_DEBUG_BUILD
#endif // GKFS_ENABLE_LOGGING
}

logger::~logger() {
+0 −21
Original line number Diff line number Diff line
@@ -69,24 +69,6 @@ static inline void exit_error_msg(int errcode, const string& msg) {
    ::exit(errcode);
}

int 
hg_log_function(FILE *stream, const char *fmt, ...) {

#ifdef GKFS_DISABLE_LOGGING
    (void) stream;
    (void) fmt;

    return 0;
#endif // GKFS_DISABLE_LOGGING

    va_list ap;
    ::va_start(ap, fmt);
    int n = gkfs::log::get_global_logger()->log(gkfs::log::mercury, fmt, ap);
    ::va_end(ap);

    return n;
}

/**
 * Initializes the Hermes client for a given transport prefix
 * @param transport_prefix
@@ -105,9 +87,6 @@ bool init_hermes_client(const std::string& transport_prefix) {
        ld_network_service = 
            std::make_unique<hermes::async_engine>(
                    hermes::get_transport_type(transport_prefix), opts);

        ld_network_service->set_mercury_log_function(::hg_log_function);

        ld_network_service->run();
    } catch (const std::exception& ex) {
        fmt::print(stderr, "Failed to initialize Hermes RPC client {}\n", 
+3 −3
Original line number Diff line number Diff line
@@ -234,7 +234,7 @@ int PreloadContext::register_internal_fd(int fd) {
    internal_fds_.reset(pos);


#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#if defined(GKFS_ENABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
    long args[gkfs::syscall::MAX_ARGS]{fd, pos + MIN_INTERNAL_FD, O_CLOEXEC};
#endif

@@ -255,7 +255,7 @@ int PreloadContext::register_internal_fd(int fd) {

    assert(::syscall_error_code(ifd) == 0);

#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#if defined(GKFS_ENABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
    long args2[gkfs::syscall::MAX_ARGS]{fd};
#endif

@@ -265,7 +265,7 @@ int PreloadContext::register_internal_fd(int fd) {
        gkfs::syscall::not_executed, 
        SYS_close, args2);

#if !defined(GKFS_DISABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
#if defined(GKFS_ENABLE_LOGGING) && defined(GKFS_DEBUG_BUILD)
    int rv = ::syscall_no_intercept(SYS_close, fd);
#else
    ::syscall_no_intercept(SYS_close, fd);