<commit_before>#include <blackhole/formatter/string.hpp>
#include <blackhole/log.hpp>
#include <blackhole/logger.hpp>
#include <blackhole/logger/wrapper.hpp>
#include <blackhole/record.hpp>
#include <blackhole/sink/stream.hpp>

#include "global.hpp"

using namespace blackhole;

namespace {

class logger_factory_t {
public:
    template<class Logger>
    static Logger create() {
        Logger logger;
        auto formatter = utils::make_unique<
            formatter::string_t
        >("[%(timestamp)s]: %(message)s");

        auto sink = utils::make_unique<
            sink::stream_t
        >(sink::stream_t::output_t::stdout);

        auto frontend = utils::make_unique<
            frontend_t<
                formatter::string_t,
                sink::stream_t
            >
        >(std::move(formatter), std::move(sink));

        logger.add_frontend(std::move(frontend));
        return logger;
    }
};

} // namespace

TEST(Keeper, Class) {
    verbose_logger_t<testing::level> log;
    wrapper_t<verbose_logger_t<testing::level>> wrapper(log, log::attributes_t({
        attribute::make("answer", 42)
    }));
    UNUSED(wrapper);
}

TEST(Keeper, Usage) {
    auto log = logger_factory_t::create<logger_base_t>();
    log.add_attribute(attribute::make("id", 100500));

    {
        wrapper_t<logger_base_t> wrapper(log, log::attributes_t({
            attribute::make("answer", 42)
        }));

        auto record = log.open_record();
        ASSERT_EQ(1, record.attributes.count("id"));
        EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);
        EXPECT_EQ(0, record.attributes.count("answer"));

        record = wrapper.open_record();
        ASSERT_EQ(1, record.attributes.count("id"));
        EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);
        ASSERT_EQ(1, record.attributes.count("answer"));
        EXPECT_EQ(log::attribute_value_t(42), record.attributes["answer"].value);
    }

    auto record = log.open_record();
    ASSERT_EQ(1, record.attributes.count("id"));
    EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);
    EXPECT_EQ(0, record.attributes.count("answer"));
}

TEST(Keeper, UsageWithVerboseLogger) {
    auto log = logger_factory_t::create<verbose_logger_t<testing::level>>();
    log.add_attribute(attribute::make("id", 100500));

    {
        wrapper_t<verbose_logger_t<testing::level>> wrapper(
            log,
            log::attributes_t({
                attribute::make("answer", 42)
            })
        );

        auto record = log.open_record(testing::info);
        ASSERT_EQ(1, record.attributes.count("id"));
        EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);

        ASSERT_EQ(1, record.attributes.count("severity"));
        EXPECT_EQ(log::attribute_value_t(testing::info),
                  record.attributes["severity"].value);

        EXPECT_EQ(0, record.attributes.count("answer"));

        record = wrapper.open_record(testing::info);
        ASSERT_EQ(1, record.attributes.count("id"));
        EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);

        ASSERT_EQ(1, record.attributes.count("severity"));
        EXPECT_EQ(log::attribute_value_t(testing::info),
                  record.attributes["severity"].value);

        ASSERT_EQ(1, record.attributes.count("answer"));
        EXPECT_EQ(log::attribute_value_t(42), record.attributes["answer"].value);
    }

    auto record = log.open_record(testing::info);
    ASSERT_EQ(1, record.attributes.count("id"));
    EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);

    ASSERT_EQ(1, record.attributes.count("severity"));
    EXPECT_EQ(log::attribute_value_t(testing::info),
              record.attributes["severity"].value);

    EXPECT_EQ(0, record.attributes.count("answer"));
}

TEST(Keeper, MacroUsage) {
    auto log = logger_factory_t::create<verbose_logger_t<testing::level>>();
    log.add_attribute(attribute::make("id", 100500));<commit_msg>[Aux] Renaming.<commit_after>#include <blackhole/formatter/string.hpp>
#include <blackhole/log.hpp>
#include <blackhole/logger.hpp>
#include <blackhole/logger/wrapper.hpp>
#include <blackhole/record.hpp>
#include <blackhole/sink/stream.hpp>

#include "global.hpp"

using namespace blackhole;

namespace {

class logger_factory_t {
public:
    template<class Logger>
    static Logger create() {
        Logger logger;
        auto formatter = utils::make_unique<
            formatter::string_t
        >("[%(timestamp)s]: %(message)s");

        auto sink = utils::make_unique<
            sink::stream_t
        >(sink::stream_t::output_t::stdout);

        auto frontend = utils::make_unique<
            frontend_t<
                formatter::string_t,
                sink::stream_t
            >
        >(std::move(formatter), std::move(sink));

        logger.add_frontend(std::move(frontend));
        return logger;
    }
};

} // namespace

TEST(Wrapper, Class) {
    verbose_logger_t<testing::level> log;
    wrapper_t<verbose_logger_t<testing::level>> wrapper(log, log::attributes_t({
        attribute::make("answer", 42)
    }));
    UNUSED(wrapper);
}

TEST(Wrapper, Usage) {
    auto log = logger_factory_t::create<logger_base_t>();
    log.add_attribute(attribute::make("id", 100500));

    {
        wrapper_t<logger_base_t> wrapper(log, log::attributes_t({
            attribute::make("answer", 42)
        }));

        auto record = log.open_record();
        ASSERT_EQ(1, record.attributes.count("id"));
        EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);
        EXPECT_EQ(0, record.attributes.count("answer"));

        record = wrapper.open_record();
        ASSERT_EQ(1, record.attributes.count("id"));
        EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);
        ASSERT_EQ(1, record.attributes.count("answer"));
        EXPECT_EQ(log::attribute_value_t(42), record.attributes["answer"].value);
    }

    auto record = log.open_record();
    ASSERT_EQ(1, record.attributes.count("id"));
    EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);
    EXPECT_EQ(0, record.attributes.count("answer"));
}

TEST(Wrapper, UsageWithVerboseLogger) {
    auto log = logger_factory_t::create<verbose_logger_t<testing::level>>();
    log.add_attribute(attribute::make("id", 100500));

    {
        wrapper_t<verbose_logger_t<testing::level>> wrapper(
            log,
            log::attributes_t({
                attribute::make("answer", 42)
            })
        );

        auto record = log.open_record(testing::info);
        ASSERT_EQ(1, record.attributes.count("id"));
        EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);

        ASSERT_EQ(1, record.attributes.count("severity"));
        EXPECT_EQ(log::attribute_value_t(testing::info),
                  record.attributes["severity"].value);

        EXPECT_EQ(0, record.attributes.count("answer"));

        record = wrapper.open_record(testing::info);
        ASSERT_EQ(1, record.attributes.count("id"));
        EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);

        ASSERT_EQ(1, record.attributes.count("severity"));
        EXPECT_EQ(log::attribute_value_t(testing::info),
                  record.attributes["severity"].value);

        ASSERT_EQ(1, record.attributes.count("answer"));
        EXPECT_EQ(log::attribute_value_t(42), record.attributes["answer"].value);
    }

    auto record = log.open_record(testing::info);
    ASSERT_EQ(1, record.attributes.count("id"));
    EXPECT_EQ(log::attribute_value_t(100500), record.attributes["id"].value);

    ASSERT_EQ(1, record.attributes.count("severity"));
    EXPECT_EQ(log::attribute_value_t(testing::info),
              record.attributes["severity"].value);

    EXPECT_EQ(0, record.attributes.count("answer"));
}

TEST(Wrapper, MacroUsage) {
    auto log = logger_factory_t::create<verbose_logger_t<testing::level>>();
    log.add_attribute(attribute::make("id", 100500));
{'fraction_non_alphanumeric': 0.15674407114624506, 'fraction_numerical': 0.01951581027667984, 'mean_word_length': 3.5361344537815125, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2810858', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2468, 'n_words': 322}