mirror of https://github.com/gabime/spdlog.git
#1838 Add ANSI-colored rotating file sink.
parent
1d7886a27a
commit
ea601c44a7
@ -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
|
@ -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
|
Loading…
Reference in New Issue