9#include "../microfmt.hpp"
15#if MICROFMT_HAS_SOURCE_LOCATION
16template <
typename Location>
struct basic_source_loc_view {
18 bool show_function{
true};
19 bool file_basename_only{
true};
22#if MICROFMT_HAS_STD_SOURCE_LOCATION
25using std_source_loc_view = basic_source_loc_view<std::source_location>;
27[[nodiscard]]
constexpr std_source_loc_view source_loc(std::source_location loc = std::source_location::current(),
28 bool show_fn =
true,
bool basename_only =
true) noexcept {
29 return std_source_loc_view{loc, show_fn, basename_only};
34#if MICROFMT_HAS_BOOST_SOURCE_LOCATION
35using boost_source_loc_view = basic_source_loc_view<boost::source_location>;
37[[nodiscard]]
constexpr boost_source_loc_view source_loc(boost::source_location loc,
bool show_fn =
true,
38 bool basename_only =
true) noexcept {
39 return boost_source_loc_view{loc, show_fn, basename_only};
43#if MICROFMT_HAS_STD_SOURCE_LOCATION
44using source_loc_view = std_source_loc_view;
45#elif MICROFMT_HAS_BOOST_SOURCE_LOCATION
46using source_loc_view = boost_source_loc_view;
49template <
typename Location>
struct formatter<basic_source_loc_view<Location>> {
50 bool parse_short{
false};
53 constexpr void parse(format_parse_context &ctx)
noexcept {
54 for (
char c : ctx.spec()) {
55 if (c ==
's' || c ==
'S')
57 if (c ==
'f' || c ==
'F')
62 void format(
const basic_source_loc_view<Location> &sv,
const sink &out)
const noexcept {
63 microfmt::string_view file = sv.loc.file_name();
65 if (sv.file_basename_only) {
67 size_t last_slash = file.find_last_of(
"/\\");
68 if (last_slash != microfmt::string_view::npos) {
69 file = file.substr(last_slash + 1);
75 detail::format_unsigned<detail::radix::decimal>(out, sv.loc.line(),
false, 0);
77 const bool emit_fn = (sv.show_function && parse_fn && !parse_short);
80 out.write(sv.loc.function_name());
88#if MICROFMT_HAS_STD_SOURCE_LOCATION
89template <>
struct formatter<std::source_location> {
90 constexpr void parse(format_parse_context &)
noexcept {}
92 void format(
const std::source_location &loc,
const sink &out)
const noexcept {
93 const formatter<std_source_loc_view> f;
94 f.format(source_loc(loc), out);
100#if MICROFMT_HAS_BOOST_SOURCE_LOCATION
101template <>
struct formatter<boost::source_location> {
102 constexpr void parse(format_parse_context &)
noexcept {}
104 void format(
const boost::source_location &loc,
const sink &out)
const noexcept {
105 const formatter<boost_source_loc_view> f;
106 f.format(source_loc(loc), out);
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
RELOCO_CONSTEXPR20 buffer_sink< N > format(microfmt::string_view fmt, const Args &...args) noexcept
Definition microfmt.hpp:1786