#1838 Add ANSI-colored non-rotating file sink.

pull/3112/head
Patrick Rotsaert 1 year ago
parent ea601c44a7
commit acae7a76ad

@ -117,6 +117,22 @@ void basic_logfile_example()
}
```
---
#### ANSI-colored file logger
```c++
#include "spdlog/sinks/ansicolor_file_sink.h"
void ansicolor_logfile_example()
{
try
{
auto logger = spdlog::ansicolor_logger_mt("ansi_logger", "logs/ansicolor-log.txt");
}
catch (const spdlog::spdlog_ex &ex)
{
std::cout << "Log init failed: " << ex.what() << std::endl;
}
}
```
---
#### Rotating files
```c++
#include "spdlog/sinks/rotating_file_sink.h"

@ -10,6 +10,7 @@
void load_levels_example();
void stdout_logger_example();
void basic_example();
void ansicolor_example();
void rotating_example();
void ansicolor_rotating_example();
void daily_example();
@ -71,6 +72,7 @@ int main(int, char *[]) {
try {
stdout_logger_example();
basic_example();
ansicolor_example();
rotating_example();
ansicolor_rotating_example();
daily_example();
@ -123,6 +125,12 @@ void basic_example() {
auto my_logger = spdlog::basic_logger_mt("file_logger", "logs/basic-log.txt", true);
}
#include "spdlog/sinks/ansicolor_file_sink.h"
void ansicolor_example() {
// Create ANSI-colored file logger (not rotated).
auto my_logger = spdlog::ansicolor_logger_mt("ansi_logger", "logs/ansicolor-log.txt", true);
}
#include "spdlog/sinks/rotating_file_sink.h"
void rotating_example() {
// Create a file rotating logger with 5mb size max and 3 rotated files.

@ -10,6 +10,41 @@
namespace spdlog {
namespace details {
// Formatting codes
constexpr const char* ansicolors::reset;
constexpr const char* ansicolors::bold;
constexpr const char* ansicolors::dark;
constexpr const char* ansicolors::underline;
constexpr const char* ansicolors::blink;
constexpr const char* ansicolors::reverse;
constexpr const char* ansicolors::concealed;
constexpr const char* ansicolors::clear_line;
// Foreground colors
constexpr const char* ansicolors::black;
constexpr const char* ansicolors::red;
constexpr const char* ansicolors::green;
constexpr const char* ansicolors::yellow;
constexpr const char* ansicolors::blue;
constexpr const char* ansicolors::magenta;
constexpr const char* ansicolors::cyan;
constexpr const char* ansicolors::white;
/// Background colors
constexpr const char* ansicolors::on_black;
constexpr const char* ansicolors::on_red;
constexpr const char* ansicolors::on_green;
constexpr const char* ansicolors::on_yellow;
constexpr const char* ansicolors::on_blue;
constexpr const char* ansicolors::on_magenta;
constexpr const char* ansicolors::on_cyan;
constexpr const char* ansicolors::on_white;
/// Bold colors
constexpr const char* ansicolors::yellow_bold;
constexpr const char* ansicolors::red_bold;
constexpr const char* ansicolors::bold_on_red;
SPDLOG_INLINE ansicolors::ansicolors() {
colors_.at(level::trace) = to_string_(white);
colors_.at(level::debug) = to_string_(cyan);
@ -25,7 +60,7 @@ SPDLOG_INLINE void ansicolors::set_color(level::level_enum color_level, string_v
}
SPDLOG_INLINE std::vector<string_view_t> ansicolors::ranges(
const details::log_msg &msg, const memory_buf_t &formatted_msg) const {
const details::log_msg& msg, const memory_buf_t& formatted_msg) const {
std::vector<string_view_t> result{};
if (msg.color_range_end > msg.color_range_start) {
// before color range
@ -49,7 +84,7 @@ SPDLOG_INLINE std::vector<string_view_t> ansicolors::ranges(
return result;
}
SPDLOG_INLINE std::string ansicolors::to_string_(const string_view_t &sv) {
SPDLOG_INLINE std::string ansicolors::to_string_(const string_view_t& sv) {
return std::string(sv.data(), sv.size());
}

@ -0,0 +1,52 @@
// 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_file_sink.h>
#endif
#include <spdlog/common.h>
#include <spdlog/details/os.h>
namespace spdlog {
namespace sinks {
template <typename Mutex>
SPDLOG_INLINE ansicolor_file_sink<Mutex>::ansicolor_file_sink(
const filename_t &filename, bool truncate, const file_event_handlers &event_handlers)
: file_helper_{event_handlers} {
file_helper_.open(filename, truncate);
}
template <typename Mutex>
SPDLOG_INLINE void ansicolor_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);
}
template <typename Mutex>
SPDLOG_INLINE const filename_t &ansicolor_file_sink<Mutex>::filename() const {
return file_helper_.filename();
}
template <typename Mutex>
SPDLOG_INLINE void ansicolor_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_helper_.write(range);
}
}
template <typename Mutex>
SPDLOG_INLINE void ansicolor_file_sink<Mutex>::flush_() {
file_helper_.flush();
}
} // namespace sinks
} // namespace spdlog

@ -0,0 +1,68 @@
// 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/file_helper.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/details/synchronous_factory.h>
#include <spdlog/sinks/base_sink.h>
#include <mutex>
#include <string>
namespace spdlog {
namespace sinks {
/*
* ANSI-colored file sink with single file as target
*/
template <typename Mutex>
class ansicolor_file_sink final : public base_sink<Mutex> {
public:
explicit ansicolor_file_sink(const filename_t &filename,
bool truncate = false,
const file_event_handlers &event_handlers = {});
void set_color(level::level_enum color_level, string_view_t color);
const filename_t &filename() const;
protected:
void sink_it_(const details::log_msg &msg) override;
void flush_() override;
private:
details::ansicolors colors_;
details::file_helper file_helper_;
};
using ansicolor_file_sink_mt = ansicolor_file_sink<std::mutex>;
using ansicolor_file_sink_st = ansicolor_file_sink<details::null_mutex>;
} // namespace sinks
//
// factory functions
//
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> ansicolor_logger_mt(const std::string &logger_name,
const filename_t &filename,
bool truncate = false,
const file_event_handlers &event_handlers = {}) {
return Factory::template create<sinks::ansicolor_file_sink_mt>(logger_name, filename, truncate,
event_handlers);
}
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> ansicolor_logger_st(const std::string &logger_name,
const filename_t &filename,
bool truncate = false,
const file_event_handlers &event_handlers = {}) {
return Factory::template create<sinks::ansicolor_file_sink_st>(logger_name, filename, truncate,
event_handlers);
}
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "ansicolor_file_sink-inl.h"
#endif

@ -15,6 +15,10 @@
template class SPDLOG_API spdlog::sinks::basic_file_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::basic_file_sink<spdlog::details::null_mutex>;
#include <spdlog/sinks/ansicolor_file_sink-inl.h>
template class SPDLOG_API spdlog::sinks::ansicolor_file_sink<std::mutex>;
template class SPDLOG_API spdlog::sinks::ansicolor_file_sink<spdlog::details::null_mutex>;
#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>;

@ -28,6 +28,7 @@
#include "spdlog/details/fmt_helper.h"
#include "spdlog/mdc.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/ansicolor_file_sink.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/null_sink.h"
#include "spdlog/sinks/ostream_sink.h"

@ -45,6 +45,33 @@ TEST_CASE("flush_on", "[flush_on]") {
default_eol, default_eol, default_eol));
}
TEST_CASE("ansicolor_file_logger", "[ansicolor_logger]") {
prepare_logdir();
spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
// auto logger = spdlog::details::make_unique<spdlog::sinks::ansicolor_file_sink_mt>(filename);
auto logger = spdlog::create<spdlog::sinks::ansicolor_file_sink_mt>("logger", filename);
logger->set_pattern("[%^+++%$] %v");
auto sink =
std::dynamic_pointer_cast<spdlog::sinks::ansicolor_file_sink_mt>(logger->sinks().front());
sink->set_color(spdlog::level::info, spdlog::details::ansicolors::blue);
logger->info("Test message {}", 1);
logger->info("Test message {}", 2);
logger->flush();
require_message_count(SIMPLE_LOG, 2);
using spdlog::details::os::default_eol;
REQUIRE(file_contents(SIMPLE_LOG) ==
spdlog::fmt_lib::format(
"[{ansi_blue}+++{ansi_reset}] Test message 1{eol}"
"[{ansi_blue}+++{ansi_reset}] Test message 2{eol}",
spdlog::fmt_lib::arg("ansi_blue", spdlog::details::ansicolors::blue),
spdlog::fmt_lib::arg("ansi_reset", spdlog::details::ansicolors::reset),
spdlog::fmt_lib::arg("eol", default_eol)));
}
TEST_CASE("rotating_file_logger1", "[rotating_logger]") {
prepare_logdir();
size_t max_size = 1024 * 10;

Loading…
Cancel
Save