test_file_logging.cpp 5.81 KB
Newer Older
gabime's avatar
gabime committed
1 2 3 4 5 6 7 8
/*
 * This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
 */
#include "includes.h"

TEST_CASE("simple_file_logger", "[simple_logger]]")
{
    prepare_logdir();
9
    std::string filename = "logs/simple_log";
gabime's avatar
gabime committed
10

11
    auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
gabime's avatar
gabime committed
12 13 14 15
    logger->set_pattern("%v");

    logger->info("Test message {}", 1);
    logger->info("Test message {}", 2);
gabime's avatar
gabime committed
16

gabime's avatar
gabime committed
17 18 19
    logger->flush();
    REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n"));
    REQUIRE(count_lines(filename) == 2);
20 21 22 23
}

TEST_CASE("flush_on", "[flush_on]]")
{
gabime's avatar
gabime committed
24
    prepare_logdir();
25
    std::string filename = "logs/simple_log";
gabime's avatar
gabime committed
26

27
    auto logger = spdlog::create<spdlog::sinks::basic_file_sink_mt>("logger", filename);
gabime's avatar
gabime committed
28 29 30 31 32 33 34 35
    logger->set_pattern("%v");
    logger->set_level(spdlog::level::trace);
    logger->flush_on(spdlog::level::info);
    logger->trace("Should not be flushed");
    REQUIRE(count_lines(filename) == 0);

    logger->info("Test message {}", 1);
    logger->info("Test message {}", 2);
36

gabime's avatar
gabime committed
37 38
    REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n"));
    REQUIRE(count_lines(filename) == 3);
gabime's avatar
gabime committed
39 40
}

41
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
gabime's avatar
gabime committed
42 43
{
    prepare_logdir();
gabime's avatar
gabime committed
44
    size_t max_size = 1024 * 10;
gabime's avatar
gabime committed
45
    std::string basename = "logs/rotating_log";
46
    auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0);
gabime's avatar
gabime committed
47

gabime's avatar
gabime committed
48
    for (int i = 0; i < 10; ++i)
fogo's avatar
fogo committed
49
    {
gabime's avatar
gabime committed
50
        logger->info("Test message {}", i);
fogo's avatar
fogo committed
51
    }
gabime's avatar
gabime committed
52

gabime's avatar
gabime committed
53
    logger->flush();
54
    auto filename = basename;
gabime's avatar
gabime committed
55 56 57
    REQUIRE(count_lines(filename) == 10);
}

58
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
gabime's avatar
gabime committed
59 60
{
    prepare_logdir();
gabime's avatar
gabime committed
61
    size_t max_size = 1024 * 10;
gabime's avatar
gabime committed
62
    std::string basename = "logs/rotating_log";
63 64 65 66 67 68 69 70 71

    {
        // make an initial logger to create the first output file
        auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
        for (int i = 0; i < 10; ++i)
        {
            logger->info("Test message {}", i);
        }
        // drop causes the logger destructor to be called, which is required so the
gabime's avatar
gabime committed
72
        // next logger can rename the first output file.
73 74 75 76
        spdlog::drop(logger->name());
    }

    auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
gabime's avatar
gabime committed
77
    for (int i = 0; i < 10; ++i)
gabime's avatar
gabime committed
78
    {
gabime's avatar
gabime committed
79
        logger->info("Test message {}", i);
gabime's avatar
gabime committed
80
    }
gabime's avatar
gabime committed
81 82

    logger->flush();
83
    auto filename = basename;
gabime's avatar
gabime committed
84 85
    REQUIRE(count_lines(filename) == 10);
    for (int i = 0; i < 1000; i++)
fogo's avatar
fogo committed
86
    {
gabime's avatar
gabime committed
87

gabime's avatar
gabime committed
88
        logger->info("Test message {}", i);
fogo's avatar
fogo committed
89
    }
gabime's avatar
gabime committed
90 91

    logger->flush();
92
    REQUIRE(get_filesize(filename) <= max_size);
93
    auto filename1 = basename + ".1";
94
    REQUIRE(get_filesize(filename1) <= max_size);
gabime's avatar
gabime committed
95 96
}

97 98
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
{
99
    using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
gabime's avatar
gabime committed
100

101
    prepare_logdir();
gabime's avatar
gabime committed
102
    // calculate filename (time based)
103 104
    std::string basename = "logs/daily_dateonly";
    std::tm tm = spdlog::details::os::localtime();
105
    spdlog::memory_buf_t w;
gabime's avatar
gabime committed
106
    fmt::format_to(w, "{}_{:04d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
gabime's avatar
gabime committed
107

108
    auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
109
    for (int i = 0; i < 10; ++i)
fogo's avatar
fogo committed
110
    {
gabime's avatar
gabime committed
111

112
        logger->info("Test message {}", i);
fogo's avatar
fogo committed
113
    }
gabime's avatar
gabime committed
114
    logger->flush();
gabime's avatar
gabime committed
115
    auto filename = fmt::to_string(w);
116 117
    REQUIRE(count_lines(filename) == 10);
}
gabime's avatar
gabime committed
118

119 120
struct custom_daily_file_name_calculator
{
121
    static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm)
122
    {
123
        spdlog::memory_buf_t w;
124
        fmt::format_to(w, "{}{:04d}{:02d}{:02d}", basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday);
gabime's avatar
gabime committed
125
        return fmt::to_string(w);
126 127 128 129 130
    }
};

TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
{
gabime's avatar
gabime committed
131
    using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>;
gabime's avatar
gabime committed
132

133
    prepare_logdir();
gabime's avatar
gabime committed
134
    // calculate filename (time based)
135 136
    std::string basename = "logs/daily_dateonly";
    std::tm tm = spdlog::details::os::localtime();
137
    spdlog::memory_buf_t w;
gabime's avatar
gabime committed
138
    fmt::format_to(w, "{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
gabime's avatar
gabime committed
139

140
    auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
141
    for (int i = 0; i < 10; ++i)
fogo's avatar
fogo committed
142
    {
143
        logger->info("Test message {}", i);
fogo's avatar
fogo committed
144
    }
gabime's avatar
gabime committed
145

gabime's avatar
gabime committed
146
    logger->flush();
gabime's avatar
gabime committed
147
    auto filename = fmt::to_string(w);
148 149
    REQUIRE(count_lines(filename) == 10);
}
gabime's avatar
gabime committed
150

151 152 153 154 155 156
/*
 * File name calculations
 */

TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]]")
{
gabime's avatar
gabime committed
157 158
    auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 3);
    REQUIRE(filename == "rotated.3.txt");
159 160 161 162
}

TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]]")
{
gabime's avatar
gabime committed
163 164
    auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated", 3);
    REQUIRE(filename == "rotated.3");
165 166 167 168
}

TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]")
{
gabime's avatar
gabime committed
169 170
    auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 0);
    REQUIRE(filename == "rotated.txt");
171 172
}

173
// regex supported only from gcc 4.9 and above
gabime's avatar
gabime committed
174
#if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
gabime's avatar
gabime committed
175
#include <regex>
176

177
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]]")
178
{
gabime's avatar
gabime committed
179
    // daily_YYYY-MM-DD_hh-mm.txt
180
    auto filename = spdlog::sinks::daily_filename_calculator::calc_filename("daily.txt", spdlog::details::os::localtime());
gabime's avatar
gabime committed
181 182 183 184
    // date regex based on https://www.regular-expressions.info/dates.html
    std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)");
    std::smatch match;
    REQUIRE(std::regex_match(filename, match, re));
185
}
186
#endif