Commit 0b0f6e57 authored by Ramon Nou's avatar Ramon Nou
Browse files

fix(client): support runtime log formats with C++20

parent 22af5e39
Loading
Loading
Loading
Loading
Loading
+3 −3
Changes for include/client/logging.hpp: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -412,7 +412,7 @@ struct logger {
    template <typename... Args>
    inline void
    log(log_level level, const char* const func, const int lineno,
        Args&&... args) {
        fmt::string_view format, Args&&... args) {

        if(!(level & log_mask_)) {
            return;
@@ -437,8 +437,8 @@ struct logger {
        }

        try {
            fmt::format_to(std::back_inserter(buffer),
                           std::forward<Args>(args)...);
            fmt::vformat_to(std::back_inserter(buffer), format,
                            fmt::make_format_args(args...));
        } catch(const fmt::format_error&) {
            // Logging must never terminate the intercepted application. This
            // fallback is especially important on failed RPC paths, where an
+27 −0
Changes for tests/unit/test_path.cpp: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,33 @@
#include <catch2/catch_all.hpp>
#include <client/path.hpp>
#include <client/preload.hpp>
#include <client/logging.hpp>
#include "helpers/helpers.hpp"

TEST_CASE("client logger accepts runtime formats", "[logging]") {
    helpers::temporary_directory directory;
    const auto path = directory.dirname() / "client.log";
    {
        gkfs::log::logger logger("info", path.string(), false, true
#ifdef GKFS_DEBUG_BUILD
                                ,
                                "", 0
#endif
        );
        const std::string format = "runtime {}";
        logger.log(gkfs::log::info, __func__, __LINE__, "literal {}", 42);
        logger.log(gkfs::log::info, __func__, __LINE__, format, 43);
        logger.log(gkfs::log::info, __func__, __LINE__, "no arguments");
        REQUIRE_NOTHROW(logger.log(gkfs::log::info, __func__, __LINE__,
                                  "malformed {"));
    }
    std::string contents;
    helpers::load_string_file(path, contents);
    REQUIRE(contents.find("literal 42") != std::string::npos);
    REQUIRE(contents.find("runtime 43") != std::string::npos);
    REQUIRE(contents.find("no arguments") != std::string::npos);
    REQUIRE(contents.find("[invalid log format]") != std::string::npos);
}

#define TEST_PAIR(p, s, s2)                                                    \
    REQUIRE(p.first == s);                                                     \