#1838 Add ANSI-colored rotating file sink.
This commit is contained in:
parent
1d7886a27a
commit
ea601c44a7
14
README.md
14
README.md
@ -43,7 +43,7 @@ see example [CMakeLists.txt](https://github.com/gabime/spdlog/blob/v1.x/example/
|
||||
* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
|
||||
* Multi/Single threaded loggers.
|
||||
* Various log targets:
|
||||
* Rotating log files.
|
||||
* Rotating log files (ANSI colors supported).
|
||||
* Daily log files.
|
||||
* Console logging (colors supported).
|
||||
* syslog.
|
||||
@ -129,6 +129,18 @@ void rotating_example()
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
#### ANSI-colored rotating files
|
||||
```c++
|
||||
#include "spdlog/sinks/ansicolor_rotating_file_sink.h"
|
||||
void ansicolor_rotating_example() {
|
||||
// Create an ANSI-colored file rotating logger with 10 MB size max and 3 rotated files.
|
||||
auto max_size = 1048576 * 10;
|
||||
auto max_files = 3;
|
||||
auto logger = spdlog::ansicolor_rotating_logger_mt("ansi_logger", "logs/ansicolor_rotating.txt", max_size, max_files);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
#### Daily files
|
||||
```c++
|
||||
|
@ -11,6 +11,7 @@ void load_levels_example();
|
||||
void stdout_logger_example();
|
||||
void basic_example();
|
||||
void rotating_example();
|
||||
void ansicolor_rotating_example();
|
||||
void daily_example();
|
||||
void callback_example();
|
||||
void async_example();
|
||||
@ -71,6 +72,7 @@ int main(int, char *[]) {
|
||||
stdout_logger_example();
|
||||
basic_example();
|
||||
rotating_example();
|
||||
ansicolor_rotating_example();
|
||||
daily_example();
|
||||
callback_example();
|
||||
async_example();
|
||||
@ -128,6 +130,13 @@ void rotating_example() {
|
||||
spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/ansicolor_rotating_file_sink.h"
|
||||
void ansicolor_rotating_example() {
|
||||
// Create an ANSI-colored file rotating logger with 10mb size max and 3 rotated files.
|
||||
auto ansicolor_rotating_logger = spdlog::ansicolor_rotating_logger_mt(
|
||||
"ansi_logger", "logs/ansicolor_rotating.txt", 1048576 * 10, 3);
|
||||
}
|
||||
|
||||
#include "spdlog/sinks/daily_file_sink.h"
|
||||
void daily_example() {
|
||||
// Create a daily logger - a new file is created every day on 2:30am.
|
||||
|
67
include/spdlog/sinks/ansicolor_rotating_file_sink-inl.h
Normal file
67
include/spdlog/sinks/ansicolor_rotating_file_sink-inl.h
Normal file
@ -0,0 +1,67 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef SPDLOG_HEADER_ONLY
|
||||
#include <spdlog/sinks/ansicolor_rotating_file_sink.h>
|
||||
#endif
|
||||
|
||||
#include <spdlog/common.h>
|
||||
|
||||
#include <spdlog/details/rotating_file.h>
|
||||
#include <spdlog/details/null_mutex.h>
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
template <typename Mutex>
|
||||
SPDLOG_INLINE ansicolor_rotating_file_sink<Mutex>::ansicolor_rotating_file_sink(
|
||||
filename_t base_filename,
|
||||
std::size_t max_size,
|
||||
std::size_t max_files,
|
||||
bool rotate_on_open,
|
||||
const file_event_handlers &event_handlers)
|
||||
: file_{base_filename, max_size, max_files, rotate_on_open, event_handlers} {}
|
||||
|
||||
template <typename Mutex>
|
||||
SPDLOG_INLINE void ansicolor_rotating_file_sink<Mutex>::set_color(level::level_enum color_level,
|
||||
string_view_t color) {
|
||||
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||
colors_.set_color(color_level, color);
|
||||
}
|
||||
|
||||
// calc filename according to index and file extension if exists.
|
||||
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
|
||||
template <typename Mutex>
|
||||
SPDLOG_INLINE filename_t
|
||||
ansicolor_rotating_file_sink<Mutex>::calc_filename(const filename_t &filename, std::size_t index) {
|
||||
return details::rotating_file::calc_filename(filename, index);
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
SPDLOG_INLINE filename_t ansicolor_rotating_file_sink<Mutex>::filename() {
|
||||
return file_.filename();
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
SPDLOG_INLINE void ansicolor_rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg) {
|
||||
msg.color_range_start = 0;
|
||||
msg.color_range_end = 0;
|
||||
memory_buf_t formatted;
|
||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||
for (const auto &range : colors_.ranges(msg, formatted)) {
|
||||
file_.write(range);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Mutex>
|
||||
SPDLOG_INLINE void ansicolor_rotating_file_sink<Mutex>::flush_() {
|
||||
file_.flush();
|
||||
}
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
81
include/spdlog/sinks/ansicolor_rotating_file_sink.h
Normal file
81
include/spdlog/sinks/ansicolor_rotating_file_sink.h
Normal file
@ -0,0 +1,81 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/details/ansicolors.h>
|
||||
#include <spdlog/details/rotating_file.h>
|
||||
#include <spdlog/details/null_mutex.h>
|
||||
#include <spdlog/details/synchronous_factory.h>
|
||||
#include <spdlog/sinks/base_sink.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
//
|
||||
// Rotating file sink based on size, with ansi colors.
|
||||
//
|
||||
template <typename Mutex>
|
||||
class ansicolor_rotating_file_sink final : public base_sink<Mutex> {
|
||||
public:
|
||||
ansicolor_rotating_file_sink(filename_t base_filename,
|
||||
std::size_t max_size,
|
||||
std::size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {});
|
||||
|
||||
void set_color(level::level_enum color_level, string_view_t color);
|
||||
|
||||
static filename_t calc_filename(const filename_t &filename, std::size_t index);
|
||||
filename_t filename();
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override;
|
||||
void flush_() override;
|
||||
|
||||
private:
|
||||
details::ansicolors colors_;
|
||||
details::rotating_file file_;
|
||||
};
|
||||
|
||||
using ansicolor_rotating_file_sink_mt = ansicolor_rotating_file_sink<std::mutex>;
|
||||
using ansicolor_rotating_file_sink_st = ansicolor_rotating_file_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
//
|
||||
// factory functions
|
||||
//
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> ansicolor_rotating_logger_mt(
|
||||
const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
size_t max_file_size,
|
||||
size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::ansicolor_rotating_file_sink_mt>(
|
||||
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
|
||||
}
|
||||
|
||||
template <typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> ansicolor_rotating_logger_st(
|
||||
const std::string &logger_name,
|
||||
const filename_t &filename,
|
||||
size_t max_file_size,
|
||||
size_t max_files,
|
||||
bool rotate_on_open = false,
|
||||
const file_event_handlers &event_handlers = {}) {
|
||||
return Factory::template create<sinks::ansicolor_rotating_file_sink_st>(
|
||||
logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers);
|
||||
}
|
||||
} // namespace spdlog
|
||||
|
||||
#ifdef SPDLOG_HEADER_ONLY
|
||||
#include "ansicolor_rotating_file_sink-inl.h"
|
||||
#endif
|
@ -18,3 +18,7 @@ template class SPDLOG_API spdlog::sinks::basic_file_sink<spdlog::details::null_m
|
||||
#include <spdlog/sinks/rotating_file_sink-inl.h>
|
||||
template class SPDLOG_API spdlog::sinks::rotating_file_sink<std::mutex>;
|
||||
template class SPDLOG_API spdlog::sinks::rotating_file_sink<spdlog::details::null_mutex>;
|
||||
|
||||
#include <spdlog/sinks/ansicolor_rotating_file_sink-inl.h>
|
||||
template class SPDLOG_API spdlog::sinks::ansicolor_rotating_file_sink<std::mutex>;
|
||||
template class SPDLOG_API spdlog::sinks::ansicolor_rotating_file_sink<spdlog::details::null_mutex>;
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "spdlog/sinks/null_sink.h"
|
||||
#include "spdlog/sinks/ostream_sink.h"
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
#include "spdlog/sinks/ansicolor_rotating_file_sink.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
#include "spdlog/sinks/msvc_sink.h"
|
||||
#include "spdlog/pattern_formatter.h"
|
||||
|
@ -101,3 +101,60 @@ TEST_CASE("rotating_file_logger3", "[rotating_logger]") {
|
||||
REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0),
|
||||
spdlog::spdlog_ex);
|
||||
}
|
||||
|
||||
TEST_CASE("ansicolor_rotating_file_logger1", "[ansicolor_rotating_logger]") {
|
||||
prepare_logdir();
|
||||
size_t max_size = 1024 * 10;
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
||||
auto logger = spdlog::ansicolor_rotating_logger_mt("logger", basename, max_size, 0);
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
require_message_count(ROTATING_LOG, 10);
|
||||
}
|
||||
|
||||
TEST_CASE("ansicolor_rotating_file_logger2", "[ansicolor_rotating_logger]") {
|
||||
prepare_logdir();
|
||||
size_t max_size = 1024 * 10;
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
||||
|
||||
{
|
||||
// make an initial logger to create the first output file
|
||||
auto logger = spdlog::ansicolor_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::ansicolor_rotating_logger_mt("logger", basename, max_size, 2, true);
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
|
||||
require_message_count(ROTATING_LOG, 10);
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
logger->info("Test message {}", i);
|
||||
}
|
||||
|
||||
logger->flush();
|
||||
REQUIRE(get_filesize(ROTATING_LOG) <= max_size);
|
||||
REQUIRE(get_filesize(ROTATING_LOG ".1") <= max_size);
|
||||
}
|
||||
|
||||
// test that passing max_size=0 throws
|
||||
TEST_CASE("ansicolor_rotating_file_logger3", "[ansicolor_rotating_logger]") {
|
||||
prepare_logdir();
|
||||
size_t max_size = 0;
|
||||
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
|
||||
REQUIRE_THROWS_AS(spdlog::ansicolor_rotating_logger_mt("logger", basename, max_size, 0),
|
||||
spdlog::spdlog_ex);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user