move underscores to the end of private members

This commit is contained in:
gabime 2018-06-10 22:59:17 +03:00
parent 7d975de193
commit a21594bec7
24 changed files with 281 additions and 288 deletions

View File

@ -3,25 +3,18 @@ CXX_FLAGS = -Wall -Wshadow -Wextra -pedantic -std=c++11 -pthread -I../include
CXX_RELEASE_FLAGS = -O3 -march=native
CXX_DEBUG_FLAGS= -g
all: example bench
debug: example-debug bench-debug
all: example
debug: example-debug
example: example.cpp
$(CXX) example.cpp -o example $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS)
bench: bench.cpp
$(CXX) bench.cpp -o bench $(CXX_FLAGS) $(CXX_RELEASE_FLAGS) $(CXXFLAGS)
example-debug: example.cpp
$(CXX) example.cpp -o example-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS)
bench-debug: bench.cpp
$(CXX) bench.cpp -o bench-debug $(CXX_FLAGS) $(CXX_DEBUG_FLAGS) $(CXXFLAGS)
clean:
rm -f *.o logs/*.txt example example-debug bench bench-debug
rm -f *.o logs/*.txt example example-debug
rebuild: clean all

View File

@ -43,15 +43,15 @@ public:
async_overflow_policy overflow_policy = async_overflow_policy::block_retry);
protected:
void _sink_it(details::log_msg &msg) override;
void _flush() override;
void sink_it_(details::log_msg &msg) override;
void flush_() override;
void _backend_log(details::log_msg &incoming_log_msg);
void _backend_flush();
void backend_log_(details::log_msg &incoming_log_msg);
void backend_flush_();
private:
std::weak_ptr<details::thread_pool> _thread_pool;
async_overflow_policy _overflow_policy;
std::weak_ptr<details::thread_pool> thread_pool_;
async_overflow_policy overflow_policy_;
};
} // namespace spdlog

View File

@ -193,10 +193,10 @@ using filename_t = std::string;
#define SPDLOG_CATCH_AND_HANDLE \
catch (const std::exception &ex) \
{ \
_err_handler(ex.what()); \
err_handler_(ex.what()); \
} \
catch (...) \
{ \
_err_handler("Unknown exeption in logger"); \
err_handler_("Unknown exeption in logger"); \
}
} // namespace spdlog

View File

@ -18,8 +18,8 @@ template<class It>
inline spdlog::async_logger::async_logger(const std::string &logger_name, const It &begin, const It &end,
std::weak_ptr<details::thread_pool> tp, async_overflow_policy overflow_policy)
: logger(logger_name, begin, end)
, _thread_pool(tp)
, _overflow_policy(overflow_policy)
, thread_pool_(tp)
, overflow_policy_(overflow_policy)
{
}
@ -36,14 +36,14 @@ inline spdlog::async_logger::async_logger(
}
// send the log message to the thread pool
inline void spdlog::async_logger::_sink_it(details::log_msg &msg)
inline void spdlog::async_logger::sink_it_(details::log_msg &msg)
{
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
_incr_msg_counter(msg);
#endif
if (auto pool_ptr = _thread_pool.lock())
if (auto pool_ptr = thread_pool_.lock())
{
pool_ptr->post_log(shared_from_this(), std::move(msg), _overflow_policy);
pool_ptr->post_log(shared_from_this(), std::move(msg), overflow_policy_);
}
else
{
@ -52,11 +52,11 @@ inline void spdlog::async_logger::_sink_it(details::log_msg &msg)
}
// send flush request to the thread pool
inline void spdlog::async_logger::_flush()
inline void spdlog::async_logger::flush_()
{
if (auto pool_ptr = _thread_pool.lock())
if (auto pool_ptr = thread_pool_.lock())
{
pool_ptr->post_flush(shared_from_this(), _overflow_policy);
pool_ptr->post_flush(shared_from_this(), overflow_policy_);
}
else
{
@ -67,12 +67,12 @@ inline void spdlog::async_logger::_flush()
//
// backend functions - called from the thread pool to do the actual job
//
inline void spdlog::async_logger::_backend_log(details::log_msg &incoming_log_msg)
inline void spdlog::async_logger::backend_log_(details::log_msg &incoming_log_msg)
{
try
{
_formatter->format(incoming_log_msg);
for (auto &s : _sinks)
formatter_->format(incoming_log_msg);
for (auto &s : sinks_)
{
if (s->should_log(incoming_log_msg.level))
{
@ -82,17 +82,17 @@ inline void spdlog::async_logger::_backend_log(details::log_msg &incoming_log_ms
}
SPDLOG_CATCH_AND_HANDLE
if (_should_flush(incoming_log_msg))
if (should_flush_(incoming_log_msg))
{
_backend_flush();
backend_flush_();
}
}
inline void spdlog::async_logger::_backend_flush()
inline void spdlog::async_logger::backend_flush_()
{
try
{
for (auto &sink : _sinks)
for (auto &sink : sinks_)
{
sink->flush();
}

View File

@ -12,15 +12,15 @@
// all other ctors will call this one
template<class It>
inline spdlog::logger::logger(std::string logger_name, const It &begin, const It &end)
: _name(std::move(logger_name))
, _sinks(begin, end)
, _formatter(std::make_shared<pattern_formatter>("%+"))
, _level(level::info)
, _flush_level(level::off)
, _last_err_time(0)
, _msg_counter(1) // message counter will start from 1. 0-message id will be reserved for controll messages
: name_(std::move(logger_name))
, sinks_(begin, end)
, formatter_(std::make_shared<pattern_formatter>("%+"))
, level_(level::info)
, flush_level_(level::off)
, last_err_time_(0)
, msg_counter_(1) // message counter will start from 1. 0-message id will be reserved for controll messages
{
_err_handler = [this](const std::string &msg) { this->_default_err_handler(msg); };
err_handler_ = [this](const std::string &msg) { this->default_err_handler_(msg); };
}
// ctor with sinks as init list
@ -39,12 +39,12 @@ inline spdlog::logger::~logger() = default;
inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
{
_formatter = std::move(msg_formatter);
formatter_ = std::move(msg_formatter);
}
inline void spdlog::logger::set_pattern(const std::string &pattern, pattern_time_type pattern_time)
{
_formatter = std::make_shared<pattern_formatter>(pattern, pattern_time);
formatter_ = std::make_shared<pattern_formatter>(pattern, pattern_time);
}
template<typename... Args>
@ -57,14 +57,14 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar
try
{
details::log_msg log_msg(&_name, lvl);
details::log_msg log_msg(&name_, lvl);
#if defined(SPDLOG_FMT_PRINTF)
fmt::printf(log_msg.raw, fmt, args...);
#else
log_msg.raw.write(fmt, args...);
#endif
_sink_it(log_msg);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
}
@ -78,9 +78,9 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
}
try
{
details::log_msg log_msg(&_name, lvl);
details::log_msg log_msg(&name_, lvl);
log_msg.raw << msg;
_sink_it(log_msg);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
}
@ -94,9 +94,9 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
}
try
{
details::log_msg log_msg(&_name, lvl);
details::log_msg log_msg(&name_, lvl);
log_msg.raw << msg;
_sink_it(log_msg);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
}
@ -237,64 +237,64 @@ inline void spdlog::logger::critical(const wchar_t *fmt, const Args &... args)
//
inline const std::string &spdlog::logger::name() const
{
return _name;
return name_;
}
inline void spdlog::logger::set_level(spdlog::level::level_enum log_level)
{
_level.store(log_level);
level_.store(log_level);
}
inline void spdlog::logger::set_error_handler(spdlog::log_err_handler err_handler)
{
_err_handler = std::move(err_handler);
err_handler_ = std::move(err_handler);
}
inline spdlog::log_err_handler spdlog::logger::error_handler()
{
return _err_handler;
return err_handler_;
}
inline void spdlog::logger::flush()
{
try
{
_flush();
flush_();
}
SPDLOG_CATCH_AND_HANDLE
}
inline void spdlog::logger::flush_on(level::level_enum log_level)
{
_flush_level.store(log_level);
flush_level_.store(log_level);
}
inline bool spdlog::logger::_should_flush(const details::log_msg &msg)
inline bool spdlog::logger::should_flush_(const details::log_msg &msg)
{
auto flush_level = _flush_level.load(std::memory_order_relaxed);
auto flush_level = flush_level_.load(std::memory_order_relaxed);
return (msg.level >= flush_level) && (msg.level != level::off);
}
inline spdlog::level::level_enum spdlog::logger::level() const
{
return static_cast<spdlog::level::level_enum>(_level.load(std::memory_order_relaxed));
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
}
inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) const
{
return msg_level >= _level.load(std::memory_order_relaxed);
return msg_level >= level_.load(std::memory_order_relaxed);
}
//
// protected virtual called at end of each user log call (if enabled) by the line_logger
//
inline void spdlog::logger::_sink_it(details::log_msg &msg)
inline void spdlog::logger::sink_it_(details::log_msg &msg)
{
#if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)
_incr_msg_counter(msg);
incr_msg_counter_(msg);
#endif
_formatter->format(msg);
for (auto &sink : _sinks)
formatter_->format(msg);
for (auto &sink : sinks_)
{
if (sink->should_log(msg.level))
{
@ -302,40 +302,40 @@ inline void spdlog::logger::_sink_it(details::log_msg &msg)
}
}
if (_should_flush(msg))
if (should_flush_(msg))
{
flush();
}
}
inline void spdlog::logger::_flush()
inline void spdlog::logger::flush_()
{
for (auto &sink : _sinks)
for (auto &sink : sinks_)
{
sink->flush();
}
}
inline void spdlog::logger::_default_err_handler(const std::string &msg)
inline void spdlog::logger::default_err_handler_(const std::string &msg)
{
auto now = time(nullptr);
if (now - _last_err_time < 60)
if (now - last_err_time_ < 60)
{
return;
}
_last_err_time = now;
last_err_time_ = now;
auto tm_time = details::os::localtime(now);
char date_buf[100];
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
}
inline void spdlog::logger::_incr_msg_counter(details::log_msg &msg)
inline void spdlog::logger::incr_msg_counter_(details::log_msg &msg)
{
msg.msg_id = _msg_counter.fetch_add(1, std::memory_order_relaxed);
msg.msg_id = msg_counter_.fetch_add(1, std::memory_order_relaxed);
}
inline const std::vector<spdlog::sink_ptr> &spdlog::logger::sinks() const
{
return _sinks;
return sinks_;
}

View File

@ -335,19 +335,19 @@ public:
}
private:
log_clock::time_point _last_update{std::chrono::seconds(0)};
int _offset_minutes{0};
std::mutex _mutex;
log_clock::time_point last_update_{std::chrono::seconds(0)};
int offset_minutes_{0};
std::mutex mutex_;
int get_cached_offset(const log_msg &msg, const std::tm &tm_time)
{
std::lock_guard<std::mutex> l(_mutex);
if (msg.time - _last_update >= cache_refresh)
std::lock_guard<std::mutex> l(mutex_);
if (msg.time - last_update_ >= cache_refresh)
{
_offset_minutes = os::utc_minutes_offset(tm_time);
_last_update = msg.time;
offset_minutes_ = os::utc_minutes_offset(tm_time);
last_update_ = msg.time;
}
return _offset_minutes;
return offset_minutes_;
}
};
@ -390,16 +390,16 @@ class ch_formatter SPDLOG_FINAL : public flag_formatter
{
public:
explicit ch_formatter(char ch)
: _ch(ch)
: ch_(ch)
{
}
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << _ch;
msg.formatted << ch_;
}
private:
char _ch;
char ch_;
};
// aggregate user chars to display as is
@ -410,15 +410,15 @@ public:
void add_ch(char ch)
{
_str += ch;
str_ += ch;
}
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << _str;
msg.formatted << str_;
}
private:
std::string _str;
std::string str_;
};
// mark the color range. expect it to be in the form of "%^colored text%$"
@ -493,8 +493,8 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
// pattern_formatter inline impl
///////////////////////////////////////////////////////////////////////////////
inline spdlog::pattern_formatter::pattern_formatter(const std::string &pattern, pattern_time_type pattern_time, std::string eol)
: _eol(std::move(eol))
, _pattern_time(pattern_time)
: eol_(std::move(eol))
, pattern_time_(pattern_time)
{
compile_pattern(pattern);
}
@ -509,7 +509,7 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string &patter
{
if (user_chars) // append user chars found so far
{
_formatters.push_back(std::move(user_chars));
formatters_.push_back(std::move(user_chars));
}
// if(
if (++it != end)
@ -532,7 +532,7 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string &patter
}
if (user_chars) // append raw chars found so far
{
_formatters.push_back(std::move(user_chars));
formatters_.push_back(std::move(user_chars));
}
}
inline void spdlog::pattern_formatter::handle_flag(char flag)
@ -541,149 +541,149 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
{
// logger name
case 'n':
_formatters.emplace_back(new details::name_formatter());
formatters_.emplace_back(new details::name_formatter());
break;
case 'l':
_formatters.emplace_back(new details::level_formatter());
formatters_.emplace_back(new details::level_formatter());
break;
case 'L':
_formatters.emplace_back(new details::short_level_formatter());
formatters_.emplace_back(new details::short_level_formatter());
break;
case ('t'):
_formatters.emplace_back(new details::t_formatter());
formatters_.emplace_back(new details::t_formatter());
break;
case ('v'):
_formatters.emplace_back(new details::v_formatter());
formatters_.emplace_back(new details::v_formatter());
break;
case ('a'):
_formatters.emplace_back(new details::a_formatter());
formatters_.emplace_back(new details::a_formatter());
break;
case ('A'):
_formatters.emplace_back(new details::A_formatter());
formatters_.emplace_back(new details::A_formatter());
break;
case ('b'):
case ('h'):
_formatters.emplace_back(new details::b_formatter());
formatters_.emplace_back(new details::b_formatter());
break;
case ('B'):
_formatters.emplace_back(new details::B_formatter());
formatters_.emplace_back(new details::B_formatter());
break;
case ('c'):
_formatters.emplace_back(new details::c_formatter());
formatters_.emplace_back(new details::c_formatter());
break;
case ('C'):
_formatters.emplace_back(new details::C_formatter());
formatters_.emplace_back(new details::C_formatter());
break;
case ('Y'):
_formatters.emplace_back(new details::Y_formatter());
formatters_.emplace_back(new details::Y_formatter());
break;
case ('D'):
case ('x'):
_formatters.emplace_back(new details::D_formatter());
formatters_.emplace_back(new details::D_formatter());
break;
case ('m'):
_formatters.emplace_back(new details::m_formatter());
formatters_.emplace_back(new details::m_formatter());
break;
case ('d'):
_formatters.emplace_back(new details::d_formatter());
formatters_.emplace_back(new details::d_formatter());
break;
case ('H'):
_formatters.emplace_back(new details::H_formatter());
formatters_.emplace_back(new details::H_formatter());
break;
case ('I'):
_formatters.emplace_back(new details::I_formatter());
formatters_.emplace_back(new details::I_formatter());
break;
case ('M'):
_formatters.emplace_back(new details::M_formatter());
formatters_.emplace_back(new details::M_formatter());
break;
case ('S'):
_formatters.emplace_back(new details::S_formatter());
formatters_.emplace_back(new details::S_formatter());
break;
case ('e'):
_formatters.emplace_back(new details::e_formatter());
formatters_.emplace_back(new details::e_formatter());
break;
case ('f'):
_formatters.emplace_back(new details::f_formatter());
formatters_.emplace_back(new details::f_formatter());
break;
case ('F'):
_formatters.emplace_back(new details::F_formatter());
formatters_.emplace_back(new details::F_formatter());
break;
case ('E'):
_formatters.emplace_back(new details::E_formatter());
formatters_.emplace_back(new details::E_formatter());
break;
case ('p'):
_formatters.emplace_back(new details::p_formatter());
formatters_.emplace_back(new details::p_formatter());
break;
case ('r'):
_formatters.emplace_back(new details::r_formatter());
formatters_.emplace_back(new details::r_formatter());
break;
case ('R'):
_formatters.emplace_back(new details::R_formatter());
formatters_.emplace_back(new details::R_formatter());
break;
case ('T'):
case ('X'):
_formatters.emplace_back(new details::T_formatter());
formatters_.emplace_back(new details::T_formatter());
break;
case ('z'):
_formatters.emplace_back(new details::z_formatter());
formatters_.emplace_back(new details::z_formatter());
break;
case ('+'):
_formatters.emplace_back(new details::full_formatter());
formatters_.emplace_back(new details::full_formatter());
break;
case ('P'):
_formatters.emplace_back(new details::pid_formatter());
formatters_.emplace_back(new details::pid_formatter());
break;
case ('i'):
_formatters.emplace_back(new details::i_formatter());
formatters_.emplace_back(new details::i_formatter());
break;
case ('^'):
_formatters.emplace_back(new details::color_start_formatter());
formatters_.emplace_back(new details::color_start_formatter());
break;
case ('$'):
_formatters.emplace_back(new details::color_stop_formatter());
formatters_.emplace_back(new details::color_stop_formatter());
break;
default: // Unknown flag appears as is
_formatters.emplace_back(new details::ch_formatter('%'));
_formatters.emplace_back(new details::ch_formatter(flag));
formatters_.emplace_back(new details::ch_formatter('%'));
formatters_.emplace_back(new details::ch_formatter(flag));
break;
}
}
inline std::tm spdlog::pattern_formatter::get_time(details::log_msg &msg)
{
if (_pattern_time == pattern_time_type::local)
if (pattern_time_ == pattern_time_type::local)
{
return details::os::localtime(log_clock::to_time_t(msg.time));
}
@ -698,10 +698,10 @@ inline void spdlog::pattern_formatter::format(details::log_msg &msg)
#else
std::tm tm_time;
#endif
for (auto &f : _formatters)
for (auto &f : formatters_)
{
f->format(msg, tm_time);
}
// write eol
msg.formatted << _eol;
msg.formatted << eol_;
}

View File

@ -170,7 +170,7 @@ private:
{
case async_msg_type::flush:
{
incoming_async_msg.worker_ptr->_backend_flush();
incoming_async_msg.worker_ptr->backend_flush_();
return true;
}
@ -183,7 +183,7 @@ private:
{
log_msg msg;
incoming_async_msg.to_log_msg(std::move(msg));
incoming_async_msg.worker_ptr->_backend_log(msg);
incoming_async_msg.worker_ptr->backend_log_(msg);
return true;
}
}

View File

@ -33,10 +33,9 @@ public:
void format(details::log_msg &msg) override;
private:
const std::string _eol;
const std::string _pattern;
const pattern_time_type _pattern_time;
std::vector<std::unique_ptr<details::flag_formatter>> _formatters;
const std::string eol_;
const pattern_time_type pattern_time_;
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
std::tm get_time(details::log_msg &msg);
void handle_flag(char flag);
void compile_pattern(const std::string &pattern);

View File

@ -124,25 +124,25 @@ public:
log_err_handler error_handler();
protected:
virtual void _sink_it(details::log_msg &msg);
virtual void _flush();
virtual void sink_it_(details::log_msg &msg);
virtual void flush_();
bool _should_flush(const details::log_msg &msg);
bool should_flush_(const details::log_msg &msg);
// default error handler: print the error to stderr with the max rate of 1 message/minute
void _default_err_handler(const std::string &msg);
void default_err_handler_(const std::string &msg);
// increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER))
void _incr_msg_counter(details::log_msg &msg);
void incr_msg_counter_(details::log_msg &msg);
const std::string _name;
std::vector<sink_ptr> _sinks;
formatter_ptr _formatter;
spdlog::level_t _level;
spdlog::level_t _flush_level;
log_err_handler _err_handler;
std::atomic<time_t> _last_err_time;
std::atomic<size_t> _msg_counter;
const std::string name_;
std::vector<sink_ptr> sinks_;
formatter_ptr formatter_;
spdlog::level_t level_;
spdlog::level_t flush_level_;
log_err_handler err_handler_;
std::atomic<time_t> last_err_time_;
std::atomic<size_t> msg_counter_;
};
} // namespace spdlog

View File

@ -31,23 +31,23 @@ class android_sink : public sink
{
public:
explicit android_sink(const std::string &tag = "spdlog", bool use_raw_msg = false)
: _tag(tag)
, _use_raw_msg(use_raw_msg)
: tag_(tag)
, use_raw_msg_(use_raw_msg)
{
}
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());
const char *msg_output = (use_raw_msg_ ? msg.raw.c_str() : msg.formatted.c_str());
// See system/core/liblog/logger_write.c for explanation of return value
int ret = __android_log_write(priority, _tag.c_str(), msg_output);
int ret = __android_log_write(priority, tag_.c_str(), msg_output);
int retry_count = 0;
while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES))
{
details::os::sleep_for_millis(5);
ret = __android_log_write(priority, _tag.c_str(), msg_output);
ret = __android_log_write(priority, tag_.c_str(), msg_output);
retry_count++;
}
@ -81,8 +81,8 @@ private:
}
}
std::string _tag;
bool _use_raw_msg;
std::string tag_;
bool use_raw_msg_;
};
} // namespace sinks

View File

@ -29,7 +29,7 @@ public:
using mutex_t = typename ConsoleMutexTrait::mutex_t;
ansicolor_sink()
: target_file_(StreamTrait::stream())
, _mutex(ConsoleMutexTrait::console_mutex())
, mutex_(ConsoleMutexTrait::console_mutex())
{
should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
@ -49,7 +49,7 @@ public:
void set_color(level::level_enum color_level, const std::string &color)
{
std::lock_guard<mutex_t> lock(_mutex);
std::lock_guard<mutex_t> lock(mutex_);
colors_[color_level] = color;
}
@ -87,43 +87,43 @@ 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);
std::lock_guard<mutex_t> lock(mutex_);
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_(msg, 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_ccode(reset);
print_ccode_(colors_[msg.level]);
print_range_(msg, 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_(msg, msg.color_range_end, msg.formatted.size());
}
else // no color
{
_print_range(msg, 0, msg.formatted.size());
print_range_(msg, 0, msg.formatted.size());
}
fflush(target_file_);
}
void flush() SPDLOG_FINAL override
{
std::lock_guard<mutex_t> lock(_mutex);
std::lock_guard<mutex_t> lock(mutex_);
fflush(target_file_);
}
private:
void _print_ccode(const std::string &color_code)
void print_ccode_(const std::string &color_code)
{
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 details::log_msg &msg, size_t start, size_t end)
{
fwrite(msg.formatted.data() + start, sizeof(char), end - start, target_file_);
}
FILE *target_file_;
mutex_t &_mutex;
mutex_t &mutex_;
bool should_do_colors_;
std::unordered_map<level::level_enum, std::string, level::level_hasher> colors_;

View File

@ -6,7 +6,7 @@
#pragma once
//
// base sink templated over a mutex (either dummy or real)
// concrete implementation should only override the _sink_it method.
// concrete implementation should only override the sink_it_ method.
// all locking is taken care of here so no locking needed by the implementers..
//
@ -28,20 +28,21 @@ public:
void log(const details::log_msg &msg) SPDLOG_FINAL override
{
std::lock_guard<Mutex> lock(_mutex);
_sink_it(msg);
std::lock_guard<Mutex> lock(mutex_);
sink_it_(msg);
}
void flush() SPDLOG_FINAL override
{
std::lock_guard<Mutex> lock(_mutex);
_flush();
std::lock_guard<Mutex> lock(mutex_);
flush_();
}
protected:
virtual void _sink_it(const details::log_msg &msg) = 0;
virtual void _flush() = 0;
Mutex _mutex;
virtual void sink_it_(const details::log_msg &msg) = 0;
virtual void flush_() = 0;
Mutex mutex_;
};
} // namespace sinks
} // namespace spdlog

View File

@ -63,42 +63,42 @@ class daily_file_sink SPDLOG_FINAL : public base_sink<Mutex>
public:
// create daily file sink which rotates on given time
daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute)
: _base_filename(std::move(base_filename))
, _rotation_h(rotation_hour)
, _rotation_m(rotation_minute)
: base_filename_(std::move(base_filename))
, rotation_h_(rotation_hour)
, rotation_m_(rotation_minute)
{
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
{
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
}
_rotation_tp = _next_rotation_tp();
_file_helper.open(FileNameCalc::calc_filename(_base_filename));
rotation_tp_ = next_rotation_tp_();
file_helper_.open(FileNameCalc::calc_filename(base_filename_));
}
protected:
void _sink_it(const details::log_msg &msg) override
void sink_it_(const details::log_msg &msg) override
{
if (std::chrono::system_clock::now() >= _rotation_tp)
if (std::chrono::system_clock::now() >= rotation_tp_)
{
_file_helper.open(FileNameCalc::calc_filename(_base_filename));
_rotation_tp = _next_rotation_tp();
file_helper_.open(FileNameCalc::calc_filename(base_filename_));
rotation_tp_ = next_rotation_tp_();
}
_file_helper.write(msg);
file_helper_.write(msg);
}
void _flush() override
void flush_() override
{
_file_helper.flush();
file_helper_.flush();
}
private:
std::chrono::system_clock::time_point _next_rotation_tp()
std::chrono::system_clock::time_point next_rotation_tp_()
{
auto now = std::chrono::system_clock::now();
time_t tnow = std::chrono::system_clock::to_time_t(now);
tm date = spdlog::details::os::localtime(tnow);
date.tm_hour = _rotation_h;
date.tm_min = _rotation_m;
date.tm_hour = rotation_h_;
date.tm_min = rotation_m_;
date.tm_sec = 0;
auto rotation_time = std::chrono::system_clock::from_time_t(std::mktime(&date));
if (rotation_time > now)
@ -108,11 +108,11 @@ private:
return {rotation_time + std::chrono::hours(24)};
}
filename_t _base_filename;
int _rotation_h;
int _rotation_m;
std::chrono::system_clock::time_point _rotation_tp;
details::file_helper _file_helper;
filename_t base_filename_;
int rotation_h_;
int rotation_m_;
std::chrono::system_clock::time_point rotation_tp_;
details::file_helper file_helper_;
};
using daily_file_sink_mt = daily_file_sink<std::mutex>;

View File

@ -23,18 +23,18 @@ class dist_sink : public base_sink<Mutex>
{
public:
explicit dist_sink()
: _sinks()
: sinks_()
{
}
dist_sink(const dist_sink &) = delete;
dist_sink &operator=(const dist_sink &) = delete;
protected:
std::vector<std::shared_ptr<sink>> _sinks;
std::vector<std::shared_ptr<sink>> sinks_;
void _sink_it(const details::log_msg &msg) override
void sink_it_(const details::log_msg &msg) override
{
for (auto &sink : _sinks)
for (auto &sink : sinks_)
{
if (sink->should_log(msg.level))
{
@ -43,23 +43,23 @@ protected:
}
}
void _flush() override
void flush_() override
{
for (auto &sink : _sinks)
for (auto &sink : sinks_)
sink->flush();
}
public:
void add_sink(std::shared_ptr<sink> sink)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
_sinks.push_back(sink);
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_.push_back(sink);
}
void remove_sink(std::shared_ptr<sink> sink)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end());
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
}
};

View File

@ -27,12 +27,12 @@ public:
explicit msvc_sink() {}
protected:
void _sink_it(const details::log_msg &msg) override
void sink_it_(const details::log_msg &msg) override
{
OutputDebugStringA(msg.formatted.c_str());
}
void _flush() override {}
void flush_() override {}
};
using msvc_sink_mt = msvc_sink<std::mutex>;

View File

@ -17,9 +17,9 @@ 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 &) override {}
void _flush() override {}
void flush_() override {}
};
using null_sink_mt = null_sink<details::null_mutex>;

View File

@ -18,28 +18,28 @@ class ostream_sink : public base_sink<Mutex>
{
public:
explicit ostream_sink(std::ostream &os, bool force_flush = false)
: _ostream(os)
, _force_flush(force_flush)
: ostream_(os)
, force_flush_(force_flush)
{
}
ostream_sink(const ostream_sink &) = delete;
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) override
{
_ostream.write(msg.formatted.data(), msg.formatted.size());
if (_force_flush)
_ostream.flush();
ostream_.write(msg.formatted.data(), msg.formatted.size());
if (force_flush_)
ostream_.flush();
}
void _flush() override
void flush_() override
{
_ostream.flush();
ostream_.flush();
}
std::ostream &_ostream;
bool _force_flush;
std::ostream &ostream_;
bool force_flush_;
};
using ostream_sink_mt = ostream_sink<std::mutex>;

View File

@ -28,12 +28,12 @@ class rotating_file_sink SPDLOG_FINAL : public base_sink<Mutex>
{
public:
rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files)
: _base_filename(std::move(base_filename))
, _max_size(max_size)
, _max_files(max_files)
: base_filename_(std::move(base_filename))
, max_size_(max_size)
, max_files_(max_files)
{
_file_helper.open(calc_filename(_base_filename, 0));
_current_size = _file_helper.size(); // expensive. called only once
file_helper_.open(calc_filename(base_filename_, 0));
current_size_ = file_helper_.size(); // expensive. called only once
}
// calc filename according to index and file extension if exists.
@ -55,20 +55,20 @@ public:
}
protected:
void _sink_it(const details::log_msg &msg) override
void sink_it_(const details::log_msg &msg) override
{
_current_size += msg.formatted.size();
if (_current_size > _max_size)
current_size_ += msg.formatted.size();
if (current_size_ > max_size_)
{
_rotate();
_current_size = msg.formatted.size();
rotate_();
current_size_ = msg.formatted.size();
}
_file_helper.write(msg);
file_helper_.write(msg);
}
void _flush() override
void flush_() override
{
_file_helper.flush();
file_helper_.flush();
}
private:
@ -77,14 +77,14 @@ private:
// log.1.txt -> log.2.txt
// log.2.txt -> log.3.txt
// log.3.txt -> delete
void _rotate()
void rotate_()
{
using details::os::filename_to_str;
_file_helper.close();
for (auto i = _max_files; i > 0; --i)
file_helper_.close();
for (auto i = max_files_; i > 0; --i)
{
filename_t src = calc_filename(_base_filename, i - 1);
filename_t target = calc_filename(_base_filename, i);
filename_t src = calc_filename(base_filename_, i - 1);
filename_t target = calc_filename(base_filename_, i);
if (details::file_helper::file_exists(target))
{
@ -98,14 +98,14 @@ private:
throw spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
}
}
_file_helper.reopen(true);
file_helper_.reopen(true);
}
filename_t _base_filename;
std::size_t _max_size;
std::size_t _max_files;
std::size_t _current_size;
details::file_helper _file_helper;
filename_t base_filename_;
std::size_t max_size_;
std::size_t max_files_;
std::size_t current_size_;
details::file_helper file_helper_;
};
using rotating_file_sink_mt = rotating_file_sink<std::mutex>;

View File

@ -22,34 +22,34 @@ class simple_file_sink SPDLOG_FINAL : public base_sink<Mutex>
{
public:
explicit simple_file_sink(const filename_t &filename, bool truncate = false)
: _force_flush(false)
: force_flush_(false)
{
_file_helper.open(filename, truncate);
file_helper_.open(filename, truncate);
}
void set_force_flush(bool force_flush)
{
_force_flush = force_flush;
force_flush_ = force_flush;
}
protected:
void _sink_it(const details::log_msg &msg) override
void sink_it_(const details::log_msg &msg) override
{
_file_helper.write(msg);
if (_force_flush)
file_helper_.write(msg);
if (force_flush_)
{
_file_helper.flush();
file_helper_.flush();
}
}
void _flush() override
void flush_() override
{
_file_helper.flush();
file_helper_.flush();
}
private:
details::file_helper _file_helper;
bool _force_flush;
details::file_helper file_helper_;
bool force_flush_;
};
using simple_file_sink_mt = simple_file_sink<std::mutex>;

View File

@ -22,22 +22,22 @@ public:
level::level_enum level() const;
private:
level_t _level{level::trace};
level_t level_{level::trace};
};
inline bool sink::should_log(level::level_enum msg_level) const
{
return msg_level >= _level.load(std::memory_order_relaxed);
return msg_level >= level_.load(std::memory_order_relaxed);
}
inline void sink::set_level(level::level_enum log_level)
{
_level.store(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));
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));
}
} // namespace sinks

View File

@ -23,8 +23,8 @@ class stdout_sink : public sink
public:
using mutex_t = typename ConsoleMutexTrait::mutex_t;
stdout_sink()
: _mutex(ConsoleMutexTrait::console_mutex())
, _file(StdoutTrait::stream())
: mutex_(ConsoleMutexTrait::console_mutex())
, file_(StdoutTrait::stream())
{
}
~stdout_sink() = default;
@ -34,20 +34,20 @@ public:
void log(const details::log_msg &msg) override
{
std::lock_guard<mutex_t> lock(_mutex);
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), _file);
std::lock_guard<mutex_t> lock(mutex_);
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), file_);
fflush(StdoutTrait::stream());
}
void flush() override
{
std::lock_guard<mutex_t> lock(_mutex);
std::lock_guard<mutex_t> lock(mutex_);
fflush(StdoutTrait::stream());
}
private:
mutex_t &_mutex;
FILE *_file;
mutex_t &mutex_;
FILE *file_;
};
using stdout_sink_mt = stdout_sink<details::console_stdout_trait, details::console_mutex_trait>;

View File

@ -26,18 +26,18 @@ class syslog_sink : public sink
public:
//
syslog_sink(const std::string &ident = "", int syslog_option = 0, int syslog_facility = LOG_USER)
: _ident(ident)
: ident_(ident)
{
_priorities[static_cast<size_t>(level::trace)] = LOG_DEBUG;
_priorities[static_cast<size_t>(level::debug)] = LOG_DEBUG;
_priorities[static_cast<size_t>(level::info)] = LOG_INFO;
_priorities[static_cast<size_t>(level::warn)] = LOG_WARNING;
_priorities[static_cast<size_t>(level::err)] = LOG_ERR;
_priorities[static_cast<size_t>(level::critical)] = LOG_CRIT;
_priorities[static_cast<size_t>(level::off)] = LOG_INFO;
priorities_[static_cast<size_t>(level::trace)] = LOG_DEBUG;
priorities_[static_cast<size_t>(level::debug)] = LOG_DEBUG;
priorities_[static_cast<size_t>(level::info)] = LOG_INFO;
priorities_[static_cast<size_t>(level::warn)] = LOG_WARNING;
priorities_[static_cast<size_t>(level::err)] = LOG_ERR;
priorities_[static_cast<size_t>(level::critical)] = LOG_CRIT;
priorities_[static_cast<size_t>(level::off)] = LOG_INFO;
// set ident to be program name if empty
::openlog(_ident.empty() ? nullptr : _ident.c_str(), syslog_option, syslog_facility);
::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility);
}
~syslog_sink() override
@ -56,16 +56,16 @@ public:
void flush() override {}
private:
std::array<int, 7> _priorities;
std::array<int, 7> priorities_;
// must store the ident because the man says openlog might use the pointer as is and not a string copy
const std::string _ident;
const std::string ident_;
//
// Simply maps spdlog's log level to syslog priority level.
//
int syslog_prio_from_level(const details::log_msg &msg) const
{
return _priorities[static_cast<size_t>(msg.level)];
return priorities_[static_cast<size_t>(msg.level)];
}
};
} // namespace sinks

View File

@ -34,7 +34,7 @@ public:
wincolor_sink()
: out_handle_(HandleTrait::handle())
, _mutex(ConsoleMutexTrait::console_mutex())
, mutex_(ConsoleMutexTrait::console_mutex())
{
colors_[level::trace] = WHITE;
colors_[level::debug] = CYAN;
@ -56,29 +56,29 @@ public:
// change the color for the given level
void set_color(level::level_enum level, WORD color)
{
std::lock_guard<mutex_t> lock(_mutex);
std::lock_guard<mutex_t> lock(mutex_);
colors_[level] = color;
}
void log(const details::log_msg &msg) SPDLOG_FINAL override
{
std::lock_guard<mutex_t> lock(_mutex);
std::lock_guard<mutex_t> lock(mutex_);
if (msg.color_range_end > msg.color_range_start)
{
// before color range
_print_range(msg, 0, msg.color_range_start);
print_range_(msg, 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_(msg, 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_(msg, msg.color_range_end, msg.formatted.size());
}
else // print without colors if color range is invalid
{
_print_range(msg, 0, msg.formatted.size());
print_range_(msg, 0, msg.formatted.size());
}
}
@ -103,14 +103,14 @@ 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 details::log_msg &msg, size_t start, size_t end)
{
auto size = static_cast<DWORD>(end - start);
::WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
}
HANDLE out_handle_;
mutex_t &_mutex;
mutex_t &mutex_;
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
};

View File

@ -35,13 +35,13 @@ public:
}
protected:
void _sink_it(const details::log_msg &) override
void sink_it_(const details::log_msg &) override
{
msg_counter_++;
std::this_thread::sleep_for(delay_);
}
void _flush() override
void flush_() override
{
flush_counter_++;
}