file_log.cpp 4.67 KB
Newer Older
gabime's avatar
gabime committed
1 2 3 4 5 6 7 8 9
/*
 * 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();
10
    std::string filename = "logs/simple_log";
gabime's avatar
gabime committed
11 12 13 14 15 16 17 18 19 20

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


    logger->info("Test message {}", 1);
    logger->info("Test message {}", 2);
    logger->flush();
    REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n"));
    REQUIRE(count_lines(filename) == 2);
21 22
}

gabime's avatar
gabime committed
23

24 25
TEST_CASE("flush_on", "[flush_on]]")
{
gabime's avatar
gabime committed
26
    prepare_logdir();
27
    std::string filename = "logs/simple_log";
gabime's avatar
gabime committed
28 29 30 31 32 33 34 35 36 37 38 39 40

    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);

    logger->info("Test message {}", 1);
    logger->info("Test message {}", 2);
    logger->flush();
    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
41 42 43 44 45 46
}

TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
{
    prepare_logdir();
    std::string basename = "logs/rotating_log";
47
    auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 0);
gabime's avatar
gabime committed
48

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

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


TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
{
    prepare_logdir();
    std::string basename = "logs/rotating_log";
62
    auto logger = spdlog::rotating_logger_mt("logger", basename, 1024, 1);
gabime's avatar
gabime committed
63 64 65 66
    for (int i = 0; i < 10; ++i)
        logger->info("Test message {}", i);

    logger->flush();
67
    auto filename = basename;
gabime's avatar
gabime committed
68 69 70 71 72 73
    REQUIRE(count_lines(filename) == 10);
    for (int i = 0; i < 1000; i++)
        logger->info("Test message {}", i);

    logger->flush();
    REQUIRE(get_filesize(filename) <= 1024);
74
    auto filename1 = basename + ".1";
gabime's avatar
gabime committed
75 76 77 78 79 80 81 82 83 84 85
    REQUIRE(get_filesize(filename1) <= 1024);
}


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;
86
    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);
gabime's avatar
gabime committed
87

88
    auto logger = spdlog::daily_logger_mt("logger", basename, 0, 0);
gabime's avatar
gabime committed
89
    logger->flush_on(spdlog::level::info);
gabime's avatar
gabime committed
90 91 92 93 94 95 96 97
    for (int i = 0; i < 10; ++i)
        logger->info("Test message {}", i);

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


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

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

111
    auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
112 113
    for (int i = 0; i < 10; ++i)
        logger->info("Test message {}", i);
gabime's avatar
gabime committed
114
    logger->flush();
115 116 117
    auto filename = w.str();
    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)
122 123 124
    {
        std::tm tm = spdlog::details::os::localtime();
        fmt::MemoryWriter w;
125
        w.write("{}{:04d}{:02d}{:02d}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
126 127 128 129 130 131 132
        return w.str();
    }
};

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

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

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

gabime's avatar
gabime committed
147 148
    logger->flush();
    auto filename = w.str();
149 150
    REQUIRE(count_lines(filename) == 10);
}
gabime's avatar
gabime committed
151