mirror of
https://github.com/gabime/spdlog.git
synced 2025-04-01 02:42:41 +08:00
Refactored padding handling in formatter using templates
This commit is contained in:
parent
54456aee9e
commit
4b0267910c
@ -85,8 +85,7 @@ private:
|
|||||||
|
|
||||||
struct null_scoped_padder
|
struct null_scoped_padder
|
||||||
{
|
{
|
||||||
null_scoped_padder(size_t /*wrapped_size*/, const padding_info &/*padinfo*/, fmt::memory_buffer &/*dest*/)
|
null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, fmt::memory_buffer & /*dest*/) {}
|
||||||
{}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
@ -434,7 +433,6 @@ public:
|
|||||||
const size_t field_size = 3;
|
const size_t field_size = 3;
|
||||||
ScopedPadder p(field_size, padinfo_, dest);
|
ScopedPadder p(field_size, padinfo_, dest);
|
||||||
fmt_helper::pad3(static_cast<uint32_t>(millis.count()), dest);
|
fmt_helper::pad3(static_cast<uint32_t>(millis.count()), dest);
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -454,7 +452,6 @@ public:
|
|||||||
const size_t field_size = 6;
|
const size_t field_size = 6;
|
||||||
ScopedPadder p(field_size, padinfo_, dest);
|
ScopedPadder p(field_size, padinfo_, dest);
|
||||||
fmt_helper::pad6(static_cast<size_t>(micros.count()), dest);
|
fmt_helper::pad6(static_cast<size_t>(micros.count()), dest);
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -579,7 +576,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// ISO 8601 offset from UTC in timezone (+-HH:MM)
|
// ISO 8601 offset from UTC in timezone (+-HH:MM)
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class z_formatter final : public flag_formatter
|
class z_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -639,7 +636,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Thread id
|
// Thread id
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class t_formatter final : public flag_formatter
|
class t_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -766,9 +763,8 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t text_size = padinfo_.enabled() ?
|
size_t text_size =
|
||||||
std::char_traits<char>::length(msg.source.filename) + fmt_helper::count_digits(msg.source.line) + 1:
|
padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename) + fmt_helper::count_digits(msg.source.line) + 1 : 0;
|
||||||
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);
|
||||||
@ -792,7 +788,7 @@ public:
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
size_t text_size = padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename): 0;
|
size_t text_size = padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename) : 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);
|
||||||
}
|
}
|
||||||
@ -825,7 +821,7 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class source_linenum_formatter final : public flag_formatter
|
class source_linenum_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -1043,8 +1039,7 @@ SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
|
|||||||
return details::os::gmtime(log_clock::to_time_t(msg.time));
|
return details::os::gmtime(log_clock::to_time_t(msg.time));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Padder>
|
||||||
template <typename Padder>
|
|
||||||
SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding)
|
SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding)
|
||||||
{
|
{
|
||||||
switch (flag)
|
switch (flag)
|
||||||
@ -1291,7 +1286,7 @@ SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &patter
|
|||||||
|
|
||||||
if (it != end)
|
if (it != end)
|
||||||
{
|
{
|
||||||
if(padding.enabled())
|
if (padding.enabled())
|
||||||
{
|
{
|
||||||
handle_flag_<details::scoped_padder>(*it, padding);
|
handle_flag_<details::scoped_padder>(*it, padding);
|
||||||
}
|
}
|
||||||
@ -1299,7 +1294,6 @@ SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &patter
|
|||||||
{
|
{
|
||||||
handle_flag_<details::null_scoped_padder>(*it, padding);
|
handle_flag_<details::null_scoped_padder>(*it, padding);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -82,7 +82,7 @@ private:
|
|||||||
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
||||||
|
|
||||||
std::tm get_time_(const details::log_msg &msg);
|
std::tm get_time_(const details::log_msg &msg);
|
||||||
template <typename Padder>
|
template<typename Padder>
|
||||||
void handle_flag_(char flag, details::padding_info padding);
|
void handle_flag_(char flag, details::padding_info padding);
|
||||||
|
|
||||||
// Extract given pad spec (e.g. %8X)
|
// Extract given pad spec (e.g. %8X)
|
||||||
|
Loading…
Reference in New Issue
Block a user