aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/test/bufferedlogskiptest.cpp
blob: ddb2634e7460dec41227b2da0c74f49156cb4cb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/log/bufferedlogger.h>
#include <vespa/log/internal.h>

#include <fstream>
#include <iostream>
#include <unistd.h>
#include <cstdlib>

LOG_SETUP("bufferedlogskiptest");

using namespace std::literals::chrono_literals;

/** Test timer returning just a given time. Used in tests to fake time. */
struct TestTimer : public ns_log::Timer {
    uint64_t & _time;
    TestTimer(uint64_t & timeVar) : _time(timeVar) { }
    ns_log::system_time getTimestamp() const noexcept override {
        return ns_log::system_time(std::chrono::microseconds(_time));
    }
};

std::string readFile(const std::string& file) {
    std::ostringstream ost;
    std::ifstream is(file.c_str());
    std::string line;
    while (std::getline(is, line)) {
        std::string::size_type pos = line.find('\t');
        if (pos == std::string::npos) continue;
        std::string::size_type pos2 = line.find('\t', pos + 1);
        if (pos2 == std::string::npos) continue;
        std::string result = line.substr(0, pos)
                           + "\tlocalhost"
                           + line.substr(pos2);
            // Ignore debug entries. (Log adds some itself with timestamp we
            // can't control)
        if (result.find("\tdebug\t") == std::string::npos) {
            ost << result << "\n";
        }
    }
    return ost.str();
}

void testSkipBufferOnDebug(const std::string& file, uint64_t & timer)
{
    std::cerr << "testSkipBufferOnDebug ...\n";
    LOGBM(info, "Starting up, using logfile %s", file.c_str());

    timer = 200 * 1000000;
    for (uint32_t i=0; i<10; ++i) {
        LOGBP(info, "Message");
        timer += 1;
        LOGBM(info, "Message");
        timer += 1;
        LOGBT(info, "Message", "Message");
        timer += 1;
    }

    std::string result(readFile(file));
    std::string expected(readFile("bufferedlogskiptest.skipped.log"));

    if (result != expected) {
        std::cerr << "Failed "
                  << "testSkipBufferOnDebug\n";
        [[maybe_unused]] int system_result =
        system(("diff -u " + file
                    + " bufferedlogskiptest.skipped.log").c_str());
        std::_Exit(EXIT_FAILURE);
    }
    unlink(file.c_str());
}

void reset(uint64_t & timer) {
    timer = 0;
    ns_log::BufferedLogger::instance().setMaxCacheSize(10);
    ns_log::BufferedLogger::instance().setMaxEntryAge(300);
    ns_log::BufferedLogger::instance().setCountFactor(5);
}

int
main(int argc, char **argv)
{
    if (argc != 2) {
        std::cerr << "bufferedlogskiptest must be called with one argument\n";
        return EXIT_FAILURE;
    }
    ns_log::Logger::fakePid = true;
    uint64_t timer;
    ns_log_logger.setTimer(std::make_unique<TestTimer>(timer));
    ns_log::BufferedLogger::instance().setTimer(std::make_unique<TestTimer>(timer));

    reset(timer);
    testSkipBufferOnDebug(argv[1], timer);

    return EXIT_SUCCESS;
}