Commit 394f79e9 authored by gabime's avatar gabime

small refactoring

parent 595bbbd3
...@@ -59,6 +59,12 @@ public: ...@@ -59,6 +59,12 @@ public:
} }
} }
template<typename T>
static unsigned int count_digits(T n)
{
return fmt_helper::count_digits(n);
}
~scoped_padder() ~scoped_padder()
{ {
if (remaining_pad_ >= 0) if (remaining_pad_ >= 0)
...@@ -87,6 +93,9 @@ private: ...@@ -87,6 +93,9 @@ private:
struct null_scoped_padder struct null_scoped_padder
{ {
null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, memory_buf_t & /*dest*/) {} null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, memory_buf_t & /*dest*/) {}
template<typename T>
static unsigned int count_digits(T /* number */) { return 0;}
}; };
template<typename ScopedPadder> template<typename ScopedPadder>
...@@ -638,26 +647,12 @@ public: ...@@ -638,26 +647,12 @@ public:
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
{ {
const auto field_size = fmt_helper::count_digits(msg.thread_id); const auto field_size = ScopedPadder::count_digits(msg.thread_id);
ScopedPadder p(field_size, padinfo_, dest); ScopedPadder p(field_size, padinfo_, dest);
fmt_helper::append_int(msg.thread_id, dest); fmt_helper::append_int(msg.thread_id, dest);
} }
}; };
// If padding is not needed, there is no need to count the digits of the thread id
template<>
class t_formatter<null_scoped_padder> final : public flag_formatter
{
public:
explicit t_formatter(padding_info padinfo)
: flag_formatter(padinfo)
{}
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
{
fmt_helper::append_int(msg.thread_id, dest);
}
};
// Current pid // Current pid
template<typename ScopedPadder> template<typename ScopedPadder>
...@@ -671,28 +666,12 @@ public: ...@@ -671,28 +666,12 @@ public:
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override
{ {
const auto pid = static_cast<uint32_t>(details::os::pid()); const auto pid = static_cast<uint32_t>(details::os::pid());
auto field_size = fmt_helper::count_digits(pid); auto field_size = ScopedPadder::count_digits(pid);
ScopedPadder p(field_size, padinfo_, dest); ScopedPadder p(field_size, padinfo_, dest);
fmt_helper::append_int(pid, dest); fmt_helper::append_int(pid, dest);
} }
}; };
// If padding is not needed, there is no need to count the digits of the pid
template<>
class pid_formatter<null_scoped_padder> final : public flag_formatter
{
public:
explicit pid_formatter(padding_info padinfo)
: flag_formatter(padinfo)
{}
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override
{
const auto pid = static_cast<uint32_t>(details::os::pid());
fmt_helper::append_int(pid, dest);
}
};
template<typename ScopedPadder> template<typename ScopedPadder>
class v_formatter final : public flag_formatter class v_formatter final : public flag_formatter
{ {
...@@ -786,8 +765,16 @@ public: ...@@ -786,8 +765,16 @@ public:
return; return;
} }
size_t text_size = size_t text_size;
padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename) + fmt_helper::count_digits(msg.source.line) + 1 : 0; if(padinfo_.enabled())
{
// calc text size for padding based on "filename:line"
text_size = std::char_traits<char>::length(msg.source.filename) + ScopedPadder::count_digits(msg.source.line) + 1;
}
else
{
text_size = 0;
}
ScopedPadder p(text_size, padinfo_, dest); ScopedPadder p(text_size, padinfo_, dest);
fmt_helper::append_string_view(msg.source.filename, dest); fmt_helper::append_string_view(msg.source.filename, dest);
...@@ -859,7 +846,7 @@ public: ...@@ -859,7 +846,7 @@ public:
return; return;
} }
auto field_size = fmt_helper::count_digits(msg.source.line); auto field_size = ScopedPadder::count_digits(msg.source.line);
ScopedPadder p(field_size, padinfo_, dest); ScopedPadder p(field_size, padinfo_, dest);
fmt_helper::append_int(msg.source.line, dest); fmt_helper::append_int(msg.source.line, dest);
} }
...@@ -904,7 +891,7 @@ public: ...@@ -904,7 +891,7 @@ public:
auto delta_units = std::chrono::duration_cast<DurationUnits>(delta); auto delta_units = std::chrono::duration_cast<DurationUnits>(delta);
last_message_time_ = msg.time; last_message_time_ = msg.time;
auto delta_count = static_cast<size_t>(delta_units.count()); auto delta_count = static_cast<size_t>(delta_units.count());
auto n_digits = static_cast<size_t>(fmt_helper::count_digits(delta_count)); auto n_digits = static_cast<size_t>(ScopedPadder::count_digits(delta_count));
ScopedPadder p(n_digits, padinfo_, dest); ScopedPadder p(n_digits, padinfo_, dest);
fmt_helper::append_int(delta_count, dest); fmt_helper::append_int(delta_count, dest);
} }
...@@ -913,30 +900,6 @@ private: ...@@ -913,30 +900,6 @@ private:
log_clock::time_point last_message_time_; log_clock::time_point last_message_time_;
}; };
// If padding is not needed, there is no need to count the digits of the value
template<typename Units>
class elapsed_formatter<null_scoped_padder, Units> final : public flag_formatter
{
public:
using DurationUnits = Units;
explicit elapsed_formatter(padding_info padinfo)
: flag_formatter(padinfo)
, last_message_time_(log_clock::now())
{}
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
{
auto delta = (std::max)(msg.time - last_message_time_, log_clock::duration::zero());
auto delta_units = std::chrono::duration_cast<DurationUnits>(delta);
last_message_time_ = msg.time;
auto delta_count = static_cast<size_t>(delta_units.count());
fmt_helper::append_int(delta_count, dest);
}
private:
log_clock::time_point last_message_time_;
};
// Full info formatter // Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v // pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment