mirror of
https://github.com/gabime/spdlog.git
synced 2025-10-02 11:29:01 +08:00
pattern per sink and pattern caching
This commit is contained in:
@@ -39,7 +39,17 @@ public:
|
||||
void log(const details::log_msg &msg) override
|
||||
{
|
||||
const android_LogPriority priority = convert_to_android(msg.level);
|
||||
const char *msg_output = (use_raw_msg_ ? msg.raw.c_str() : msg.formatted.c_str());
|
||||
fmt::memory_buffer formatted;
|
||||
if (use_raw_msg_)
|
||||
{
|
||||
formatted.append(msg.raw.data(), msg.raw.data() + msg.raw.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
formatter_->format(msg, formatted);
|
||||
}
|
||||
formatted.push_back('\0');
|
||||
const char *msg_output = formatted.data();
|
||||
|
||||
// See system/core/liblog/logger_write.c for explanation of return value
|
||||
int ret = __android_log_write(priority, tag_.c_str(), msg_output);
|
||||
|
@@ -88,20 +88,23 @@ public:
|
||||
// Wrap the originally formatted message in color codes.
|
||||
// If color is not supported in the terminal, log as is instead.
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
|
||||
fmt::memory_buffer formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
|
||||
{
|
||||
// before color range
|
||||
print_range_(msg, 0, msg.color_range_start);
|
||||
print_range_(formatted, 0, msg.color_range_start);
|
||||
// in color range
|
||||
print_ccode_(colors_[msg.level]);
|
||||
print_range_(msg, msg.color_range_start, msg.color_range_end);
|
||||
print_range_(formatted, msg.color_range_start, msg.color_range_end);
|
||||
print_ccode_(reset);
|
||||
// after color range
|
||||
print_range_(msg, msg.color_range_end, msg.formatted.size());
|
||||
print_range_(formatted, msg.color_range_end, formatted.size());
|
||||
}
|
||||
else // no color
|
||||
{
|
||||
print_range_(msg, 0, msg.formatted.size());
|
||||
print_range_(formatted, 0, formatted.size());
|
||||
}
|
||||
fflush(target_file_);
|
||||
}
|
||||
@@ -117,9 +120,9 @@ private:
|
||||
{
|
||||
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
||||
}
|
||||
void print_range_(const details::log_msg &msg, size_t start, size_t end)
|
||||
void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end)
|
||||
{
|
||||
fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
||||
|
||||
FILE *target_file_;
|
||||
|
@@ -21,7 +21,20 @@ template<class Mutex>
|
||||
class base_sink : public sink
|
||||
{
|
||||
public:
|
||||
base_sink() = default;
|
||||
base_sink()
|
||||
: sink()
|
||||
{
|
||||
}
|
||||
|
||||
base_sink(const std::string &formatter_pattern)
|
||||
: sink(formatter_pattern)
|
||||
{
|
||||
}
|
||||
|
||||
base_sink(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
: sink(std::move(sink_formatter))
|
||||
{
|
||||
}
|
||||
|
||||
base_sink(const base_sink &) = delete;
|
||||
base_sink &operator=(const base_sink &) = delete;
|
||||
@@ -29,7 +42,9 @@ public:
|
||||
void log(const details::log_msg &msg) SPDLOG_FINAL override
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
sink_it_(msg);
|
||||
fmt::memory_buffer formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
sink_it_(msg, formatted);
|
||||
}
|
||||
|
||||
void flush() SPDLOG_FINAL override
|
||||
@@ -39,7 +54,7 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void sink_it_(const details::log_msg &msg) = 0;
|
||||
virtual void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) = 0;
|
||||
virtual void flush_() = 0;
|
||||
Mutex mutex_;
|
||||
};
|
||||
|
@@ -76,14 +76,14 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override
|
||||
{
|
||||
if (std::chrono::system_clock::now() >= rotation_tp_)
|
||||
{
|
||||
file_helper_.open(FileNameCalc::calc_filename(base_filename_));
|
||||
rotation_tp_ = next_rotation_tp_();
|
||||
}
|
||||
file_helper_.write(msg);
|
||||
file_helper_.write(formatted);
|
||||
}
|
||||
|
||||
void flush_() override
|
||||
|
@@ -17,12 +17,12 @@ template<class Mutex>
|
||||
class null_sink : public base_sink<Mutex>
|
||||
{
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &) override {}
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &) override {}
|
||||
|
||||
void flush_() override {}
|
||||
};
|
||||
|
||||
using null_sink_mt = null_sink<details::null_mutex>;
|
||||
using null_sink_mt = null_sink<std::mutex>;
|
||||
using null_sink_st = null_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
@@ -26,9 +26,9 @@ public:
|
||||
ostream_sink &operator=(const ostream_sink &) = delete;
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) override
|
||||
{
|
||||
ostream_.write(msg.formatted.data(), msg.formatted.size());
|
||||
ostream_.write(formatted.data(), formatted.size());
|
||||
if (force_flush_)
|
||||
ostream_.flush();
|
||||
}
|
||||
|
@@ -55,15 +55,15 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override
|
||||
{
|
||||
current_size_ += msg.formatted.size();
|
||||
current_size_ += formatted.size();
|
||||
if (current_size_ > max_size_)
|
||||
{
|
||||
rotate_();
|
||||
current_size_ = msg.formatted.size();
|
||||
current_size_ = formatted.size();
|
||||
}
|
||||
file_helper_.write(msg);
|
||||
file_helper_.write(formatted);
|
||||
}
|
||||
|
||||
void flush_() override
|
||||
|
@@ -33,9 +33,9 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override
|
||||
{
|
||||
file_helper_.write(msg);
|
||||
file_helper_.write(formatted);
|
||||
if (force_flush_)
|
||||
{
|
||||
file_helper_.flush();
|
||||
|
@@ -6,39 +6,62 @@
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/details/log_msg.h"
|
||||
#include "spdlog/formatter.h"
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
class sink
|
||||
{
|
||||
public:
|
||||
// default sink ctor with default pattern formatter
|
||||
sink()
|
||||
: formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter("%+")))
|
||||
{
|
||||
}
|
||||
|
||||
explicit sink(const std::string &formatter_pattern)
|
||||
: formatter_(std::unique_ptr<spdlog::formatter>(new pattern_formatter(formatter_pattern)))
|
||||
{
|
||||
}
|
||||
|
||||
// sink with custom formatter
|
||||
explicit sink(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
: formatter_(std::move(sink_formatter))
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~sink() = default;
|
||||
|
||||
virtual void log(const details::log_msg &msg) = 0;
|
||||
virtual void flush() = 0;
|
||||
|
||||
bool should_log(level::level_enum msg_level) const;
|
||||
void set_level(level::level_enum log_level);
|
||||
level::level_enum level() const;
|
||||
bool should_log(level::level_enum msg_level) const
|
||||
{
|
||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||
}
|
||||
void set_level(level::level_enum log_level)
|
||||
{
|
||||
level_.store(log_level);
|
||||
}
|
||||
level::level_enum level() const
|
||||
{
|
||||
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
private:
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
{
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
spdlog::formatter *formatter()
|
||||
{
|
||||
return formatter_.get();
|
||||
}
|
||||
|
||||
protected:
|
||||
level_t level_{level::trace};
|
||||
std::unique_ptr<spdlog::formatter> formatter_;
|
||||
};
|
||||
|
||||
inline bool sink::should_log(level::level_enum msg_level) const
|
||||
{
|
||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
inline void sink::set_level(level::level_enum log_level)
|
||||
{
|
||||
level_.store(log_level);
|
||||
}
|
||||
|
||||
inline level::level_enum sink::level() const
|
||||
{
|
||||
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
|
||||
}
|
||||
|
||||
} // namespace sinks
|
||||
} // namespace spdlog
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../fmt/fmt.h"
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/details/traits.h"
|
||||
@@ -63,22 +64,23 @@ public:
|
||||
void log(const details::log_msg &msg) SPDLOG_FINAL override
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
|
||||
fmt::memory_buffer formatted;
|
||||
formatter_->format(msg, formatted);
|
||||
if (msg.color_range_end > msg.color_range_start)
|
||||
{
|
||||
// before color range
|
||||
print_range_(msg, 0, msg.color_range_start);
|
||||
print_range_(formatted, 0, msg.color_range_start);
|
||||
|
||||
// in color range
|
||||
auto orig_attribs = set_console_attribs(colors_[msg.level]);
|
||||
print_range_(msg, msg.color_range_start, msg.color_range_end);
|
||||
print_range_(formatted, msg.color_range_start, msg.color_range_end);
|
||||
::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
|
||||
// after color range
|
||||
print_range_(msg, msg.color_range_end, msg.formatted.size());
|
||||
print_range_(formatted, msg.color_range_end, formatted.size());
|
||||
}
|
||||
else // print without colors if color range is invalid
|
||||
{
|
||||
print_range_(msg, 0, msg.formatted.size());
|
||||
print_range_(formatted, 0, formatted.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,10 +105,10 @@ private:
|
||||
}
|
||||
|
||||
// print a range of formatted message to console
|
||||
void print_range_(const details::log_msg &msg, size_t start, size_t end)
|
||||
void print_range_(const fmt::memory_buffer formatted, size_t start, size_t end)
|
||||
{
|
||||
auto size = static_cast<DWORD>(end - start);
|
||||
::WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
|
||||
::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr);
|
||||
}
|
||||
|
||||
HANDLE out_handle_;
|
||||
|
Reference in New Issue
Block a user