mirror of
https://github.com/gabime/spdlog.git
synced 2025-10-02 11:29:01 +08:00
Moved cpp files to inl.h
This commit is contained in:
93
include/spdlog/sinks/ansicolor_sink-inl.h
Normal file
93
include/spdlog/sinks/ansicolor_sink-inl.h
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "spdlog/details/os.h"
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::ansicolor_sink()
|
||||
: target_file_(TargetStream::stream())
|
||||
, mutex_(ConsoleMutex::mutex())
|
||||
|
||||
{
|
||||
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
|
||||
colors_[level::trace] = white;
|
||||
colors_[level::debug] = cyan;
|
||||
colors_[level::info] = green;
|
||||
colors_[level::warn] = yellow + bold;
|
||||
colors_[level::err] = red + bold;
|
||||
colors_[level::critical] = bold + on_red;
|
||||
colors_[level::off] = reset;
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::set_color(
|
||||
level::level_enum color_level, const std::string &color)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
colors_[color_level] = color;
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::log(const details::log_msg &msg)
|
||||
{
|
||||
// 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_(formatted, 0, msg.color_range_start);
|
||||
// in color range
|
||||
print_ccode_(colors_[msg.level]);
|
||||
print_range_(formatted, msg.color_range_start, msg.color_range_end);
|
||||
print_ccode_(reset);
|
||||
// after color range
|
||||
print_range_(formatted, msg.color_range_end, formatted.size());
|
||||
}
|
||||
else // no color
|
||||
{
|
||||
print_range_(formatted, 0, formatted.size());
|
||||
}
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::flush()
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
fflush(target_file_);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::set_pattern(const std::string &pattern)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::set_formatter(
|
||||
std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
{
|
||||
std::lock_guard<mutex_t> lock(mutex_);
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE bool spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::should_color()
|
||||
{
|
||||
return should_do_colors_;
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::print_ccode_(const std::string &color_code)
|
||||
{
|
||||
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
||||
}
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
SPDLOG_INLINE void spdlog::sinks::ansicolor_sink<TargetStream, ConsoleMutex>::print_range_(
|
||||
const fmt::memory_buffer &formatted, size_t start, size_t end)
|
||||
{
|
||||
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||
}
|
@@ -92,5 +92,5 @@ using ansicolor_stderr_sink_st = ansicolor_sink<details::console_stderr, details
|
||||
} // namespace spdlog
|
||||
|
||||
#ifndef SPDLOG_STATIC_LIB
|
||||
#include "spdlog/impl/ansicolor_sink.cpp"
|
||||
#include "ansicolor_sink-inl.h"
|
||||
#endif
|
42
include/spdlog/sinks/base_sink-inl.h
Normal file
42
include/spdlog/sinks/base_sink-inl.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/pattern_formatter.h"
|
||||
|
||||
template<typename Mutex>
|
||||
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::log(const details::log_msg &msg)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
sink_it_(msg);
|
||||
}
|
||||
|
||||
template<typename Mutex>
|
||||
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::flush()
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
flush_();
|
||||
}
|
||||
|
||||
template<typename Mutex>
|
||||
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern(const std::string &pattern)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
set_pattern_(pattern);
|
||||
}
|
||||
|
||||
template<typename Mutex>
|
||||
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
{
|
||||
std::lock_guard<Mutex> lock(mutex_);
|
||||
set_formatter_(std::move(sink_formatter));
|
||||
}
|
||||
|
||||
template<typename Mutex>
|
||||
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::string &pattern)
|
||||
{
|
||||
set_formatter_(details::make_unique<spdlog::pattern_formatter>(pattern));
|
||||
}
|
||||
|
||||
template<typename Mutex>
|
||||
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter)
|
||||
{
|
||||
formatter_ = std::move(sink_formatter);
|
||||
}
|
@@ -40,5 +40,5 @@ protected:
|
||||
} // namespace spdlog
|
||||
|
||||
#ifndef SPDLOG_STATIC_LIB
|
||||
#include "spdlog/impl/base_sink.cpp"
|
||||
#include "base_sink-inl.h"
|
||||
#endif
|
27
include/spdlog/sinks/sink-inl.h
Normal file
27
include/spdlog/sinks/sink-inl.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/details/pattern_formatter.h"
|
||||
|
||||
SPDLOG_INLINE spdlog::sinks::sink::sink()
|
||||
: formatter_{details::make_unique<spdlog::pattern_formatter>()}
|
||||
{
|
||||
}
|
||||
|
||||
SPDLOG_INLINE spdlog::sinks::sink::sink(std::unique_ptr<spdlog::formatter> formatter)
|
||||
: formatter_{std::move(formatter)}
|
||||
{
|
||||
}
|
||||
|
||||
SPDLOG_INLINE bool spdlog::sinks::sink::should_log(spdlog::level::level_enum msg_level) const
|
||||
{
|
||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE void spdlog::sinks::sink::set_level(level::level_enum log_level)
|
||||
{
|
||||
level_.store(log_level);
|
||||
}
|
||||
|
||||
SPDLOG_INLINE spdlog::level::level_enum spdlog::sinks::sink::level() const
|
||||
{
|
||||
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
|
||||
}
|
@@ -42,5 +42,5 @@ protected:
|
||||
} // namespace spdlog
|
||||
|
||||
#ifndef SPDLOG_STATIC_LIB
|
||||
#include "spdlog/impl/sink.cpp"
|
||||
#include "sink-inl.h"
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user