mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
streamline constructor logic
and improve test for rotate_on_open=true
This commit is contained in:
parent
cce1e36e26
commit
3925f8fa16
@ -36,9 +36,14 @@ public:
|
|||||||
, max_size_(max_size)
|
, max_size_(max_size)
|
||||||
, max_files_(max_files)
|
, max_files_(max_files)
|
||||||
{
|
{
|
||||||
file_helper_.open(calc_filename(base_filename_, 0));
|
|
||||||
if (rotate_on_open)
|
if (rotate_on_open)
|
||||||
rotate_();
|
{
|
||||||
|
rotate_(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
file_helper_.open(calc_filename(base_filename_, 0));
|
||||||
|
}
|
||||||
current_size_ = file_helper_.size(); // expensive. called only once
|
current_size_ = file_helper_.size(); // expensive. called only once
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +95,7 @@ private:
|
|||||||
// log.1.txt -> log.2.txt
|
// log.1.txt -> log.2.txt
|
||||||
// log.2.txt -> log.3.txt
|
// log.2.txt -> log.3.txt
|
||||||
// log.3.txt -> delete
|
// log.3.txt -> delete
|
||||||
void rotate_()
|
void rotate_(bool reopen=true)
|
||||||
{
|
{
|
||||||
using details::os::filename_to_str;
|
using details::os::filename_to_str;
|
||||||
file_helper_.close();
|
file_helper_.close();
|
||||||
@ -118,8 +123,15 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (reopen)
|
||||||
|
{
|
||||||
file_helper_.reopen(true);
|
file_helper_.reopen(true);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
file_helper_.open(base_filename_, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// delete the target if exists, and rename the src file to target
|
// delete the target if exists, and rename the src file to target
|
||||||
// return true on success, false otherwise.
|
// return true on success, false otherwise.
|
||||||
|
@ -38,7 +38,7 @@ TEST_CASE("flush_on", "[flush_on]]")
|
|||||||
REQUIRE(count_lines(filename) == 3);
|
REQUIRE(count_lines(filename) == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
TEST_CASE("rotating_file_logger", "[rotating_logger]]")
|
||||||
{
|
{
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
size_t max_size = 1024 * 10;
|
size_t max_size = 1024 * 10;
|
||||||
@ -55,12 +55,25 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
|||||||
REQUIRE(count_lines(filename) == 10);
|
REQUIRE(count_lines(filename) == 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
TEST_CASE("rotating_file_logger2", "[rotating_logger2]]")
|
||||||
{
|
{
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
size_t max_size = 1024 * 10;
|
size_t max_size = 1024 * 10;
|
||||||
std::string basename = "logs/rotating_log";
|
std::string basename = "logs/rotating_log";
|
||||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1, true);
|
|
||||||
|
{
|
||||||
|
// 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
|
||||||
|
// next logger can rename the first output file.
|
||||||
|
spdlog::drop(logger->name());
|
||||||
|
}
|
||||||
|
|
||||||
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 2, true);
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
{
|
{
|
||||||
logger->info("Test message {}", i);
|
logger->info("Test message {}", i);
|
||||||
|
Loading…
Reference in New Issue
Block a user