Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_log.cpp « tests - github.com/gabime/spdlog.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5067ba2a9361cec5eed12eb718f4c5e7ea94dffd (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * 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();
    std::string filename = "logs/simple_log";

    auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename);
    logger->set_pattern("%v");

#if !defined(SPDLOG_FMT_PRINTF)
    logger->info("Test message {}", 1);
    logger->info("Test message {}", 2);
#else
    logger->info("Test message %d", 1);
    logger->info("Test message %d", 2);
#endif
    logger->flush();
    REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n"));
    REQUIRE(count_lines(filename) == 2);
}

TEST_CASE("flush_on", "[flush_on]]")
{
    prepare_logdir();
    std::string filename = "logs/simple_log";

    auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename);
    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);

#if !defined(SPDLOG_FMT_PRINTF)
    logger->info("Test message {}", 1);
    logger->info("Test message {}", 2);
#else
    logger->info("Test message %d", 1);
    logger->info("Test message %d", 2);
#endif
    logger->flush();
    REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n"));
    REQUIRE(count_lines(filename) == 3);
}

TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
{
    prepare_logdir();
    std::string basename = "logs/rotating_log_A";
    auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 0);

    for (int i = 0; i < 10; ++i)
    {
#if !defined(SPDLOG_FMT_PRINTF)
        logger->info("Test message {}", i);
#else
        logger->info("Test message %d", i);
#endif
    }

    logger->flush();
    auto filename = basename;
    REQUIRE(count_lines(filename) == 10);
}

TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
{
    prepare_logdir();
	size_t max_size = 10 * 1024;
    std::string basename = "logs/rotating_log.txt";
    auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1);
    for (int i = 0; i < 10; ++i)
        logger->info("Test message {}", i);

    logger->flush();
    auto filename = basename;
    REQUIRE(count_lines(filename) == 10);
    for (int i = 0; i < 1000; i++)
    {
#if !defined(SPDLOG_FMT_PRINTF)
        logger->info("Test message {}", i);
#else
        logger->info("Test message %d", i);
#endif
    }

    logger->flush();
    REQUIRE(get_filesize(filename) <= max_size);
	auto filename1 = "logs/rotating_log.1.txt";
    REQUIRE(get_filesize(filename1) <= max_size);
}

TEST_CASE("daily_logger", "[daily_logger]]")
{
    prepare_logdir();
    // calculate filename (time based)
    std::string basename = "logs/daily_log";
    std::tm tm = spdlog::details::os::localtime();
    fmt::MemoryWriter w;
    w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min);

    auto logger = spdlog::daily_logger_mt("logger", basename, 0, 0);
    logger->flush_on(spdlog::level::info);
    for (int i = 0; i < 10; ++i)
    {
#if !defined(SPDLOG_FMT_PRINTF)
        logger->info("Test message {}", i);
#else
        logger->info("Test message %d", i);
#endif
    }

    auto filename = w.str();
    REQUIRE(count_lines(filename) == 10);
}

TEST_CASE("daily_logger with dateonly calculator", "[daily_logger_dateonly]]")
{
    using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::dateonly_daily_file_name_calculator>;

    prepare_logdir();
    // calculate filename (time based)
    std::string basename = "logs/daily_dateonly";
    std::tm tm = spdlog::details::os::localtime();
    fmt::MemoryWriter w;
    w.write("{}_{:04d}-{:02d}-{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);

    auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
    for (int i = 0; i < 10; ++i)
    {
#if !defined(SPDLOG_FMT_PRINTF)
        logger->info("Test message {}", i);
#else
        logger->info("Test message %d", i);
#endif
    }
    logger->flush();
    auto filename = w.str();
    REQUIRE(count_lines(filename) == 10);
}

struct custom_daily_file_name_calculator
{
    static spdlog::filename_t calc_filename(const spdlog::filename_t &basename)
    {
        std::tm tm = spdlog::details::os::localtime();
        fmt::MemoryWriter w;
        w.write("{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
        return w.str();
    }
};

TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
{
    using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>;

    prepare_logdir();
    // calculate filename (time based)
    std::string basename = "logs/daily_dateonly";
    std::tm tm = spdlog::details::os::localtime();
    fmt::MemoryWriter w;
    w.write("{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);

    auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
    for (int i = 0; i < 10; ++i)
    {
#if !defined(SPDLOG_FMT_PRINTF)
        logger->info("Test message {}", i);
#else
        logger->info("Test message %d", i);
#endif
    }

    logger->flush();
    auto filename = w.str();
    REQUIRE(count_lines(filename) == 10);
}

/*
 * File name calculations
 */

TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]]")
{
    auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 3);
    REQUIRE(filename == "rotated.3.txt");
}

TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]]")
{
    auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated", 3);
    REQUIRE(filename == "rotated.3");
}

TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]")
{
    auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 0);
    REQUIRE(filename == "rotated.txt");
}

// regex supported only from gcc 4.9 and above
#if defined(_MSC_VER) || !(__GNUC__ <= 4 && __GNUC_MINOR__ < 9)
#include <regex>
TEST_CASE("daily_file_sink::default_daily_file_name_calculator1", "[daily_file_sink]]")
{
    // daily_YYYY-MM-DD_hh-mm.txt
    auto filename = spdlog::sinks::default_daily_file_name_calculator::calc_filename("daily.txt");
    std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9].txt$)");
    std::smatch match;
    REQUIRE(std::regex_match(filename, match, re));
}

TEST_CASE("daily_file_sink::default_daily_file_name_calculator2", "[daily_file_sink]]")
{
    // daily_YYYY-MM-DD_hh-mm.txt
    auto filename = spdlog::sinks::default_daily_file_name_calculator::calc_filename("daily");
    std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9]$)");
    std::smatch match;
    REQUIRE(std::regex_match(filename, match, re));
}

TEST_CASE("daily_file_sink::dateonly_daily_file_name_calculator", "[daily_file_sink]]")
{
    // daily_YYYY-MM-DD_hh-mm.txt
    auto filename = spdlog::sinks::dateonly_daily_file_name_calculator::calc_filename("daily.txt");
    // 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));
}
#endif