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