Public Member Functions | Private Member Functions | Private Attributes | List of all members
spdlog::pattern_formatter Class Referencefinal

#include <pattern_formatter.h>

Inheritance diagram for spdlog::pattern_formatter:
spdlog::formatter

Public Member Functions

 pattern_formatter (std::string pattern, pattern_time_type time_type=pattern_time_type::local, std::string eol=spdlog::details::os::default_eol)
 
 pattern_formatter (pattern_time_type time_type=pattern_time_type::local, std::string eol=spdlog::details::os::default_eol)
 
 pattern_formatter (const pattern_formatter &other)=delete
 
pattern_formatteroperator= (const pattern_formatter &other)=delete
 
std::unique_ptr< formatterclone () const override
 
void format (const details::log_msg &msg, fmt::memory_buffer &dest) override
 
- Public Member Functions inherited from spdlog::formatter
virtual ~formatter ()=default
 

Private Member Functions

std::tm get_time_ (const details::log_msg &msg)
 
void handle_flag_ (char flag, details::padding_info padding)
 
details::padding_info handle_padspec_ (std::string::const_iterator &it, std::string::const_iterator end)
 
void compile_pattern_ (const std::string &pattern)
 

Private Attributes

std::string pattern_
 
std::string eol_
 
pattern_time_type pattern_time_type_
 
std::tm cached_tm_
 
std::chrono::seconds last_log_secs_
 
std::vector< std::unique_ptr< details::flag_formatter > > formatters_
 

Detailed Description

Definition at line 1014 of file pattern_formatter.h.

Constructor & Destructor Documentation

spdlog::pattern_formatter::pattern_formatter ( std::string  pattern,
pattern_time_type  time_type = pattern_time_type::local,
std::string  eol = spdlog::details::os::default_eol 
)
inlineexplicit

Definition at line 1017 of file pattern_formatter.h.

1020  , eol_(std::move(eol))
1021  , pattern_time_type_(time_type)
1022  , last_log_secs_(0)
1023  {
1024  std::memset(&cached_tm_, 0, sizeof(cached_tm_));
1026  }
pattern_time_type pattern_time_type_
std::chrono::seconds last_log_secs_
#define eol
def move(depos, offset)
Definition: depos.py:107
std::string pattern
Definition: regex_t.cc:33
void compile_pattern_(const std::string &pattern)
spdlog::pattern_formatter::pattern_formatter ( pattern_time_type  time_type = pattern_time_type::local,
std::string  eol = spdlog::details::os::default_eol 
)
inlineexplicit

Definition at line 1029 of file pattern_formatter.h.

1030  : pattern_("%+")
1031  , eol_(std::move(eol))
1032  , pattern_time_type_(time_type)
1033  , last_log_secs_(0)
1034  {
1035  std::memset(&cached_tm_, 0, sizeof(cached_tm_));
1036  formatters_.push_back(details::make_unique<details::full_formatter>(details::padding_info{}));
1037  }
pattern_time_type pattern_time_type_
std::chrono::seconds last_log_secs_
std::vector< std::unique_ptr< details::flag_formatter > > formatters_
#define eol
def move(depos, offset)
Definition: depos.py:107
spdlog::pattern_formatter::pattern_formatter ( const pattern_formatter other)
delete

Member Function Documentation

std::unique_ptr<formatter> spdlog::pattern_formatter::clone ( ) const
inlineoverridevirtual

Implements spdlog::formatter.

Definition at line 1042 of file pattern_formatter.h.

1043  {
1044  return details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_);
1045  }
pattern_time_type pattern_time_type_
void spdlog::pattern_formatter::compile_pattern_ ( const std::string pattern)
inlineprivate

Definition at line 1296 of file pattern_formatter.h.

1297  {
1298  auto end = pattern.end();
1299  std::unique_ptr<details::aggregate_formatter> user_chars;
1300  formatters_.clear();
1301  for (auto it = pattern.begin(); it != end; ++it)
1302  {
1303  if (*it == '%')
1304  {
1305  if (user_chars) // append user chars found so far
1306  {
1307  formatters_.push_back(std::move(user_chars));
1308  }
1309 
1310  auto padding = handle_padspec_(++it, end);
1311 
1312  if (it != end)
1313  {
1314  handle_flag_(*it, padding);
1315  }
1316  else
1317  {
1318  break;
1319  }
1320  }
1321  else // chars not following the % sign should be displayed as is
1322  {
1323  if (!user_chars)
1324  {
1325  user_chars = details::make_unique<details::aggregate_formatter>();
1326  }
1327  user_chars->add_ch(*it);
1328  }
1329  }
1330  if (user_chars) // append raw chars found so far
1331  {
1332  formatters_.push_back(std::move(user_chars));
1333  }
1334  }
std::vector< std::unique_ptr< details::flag_formatter > > formatters_
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
def move(depos, offset)
Definition: depos.py:107
details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end)
std::string pattern
Definition: regex_t.cc:33
void handle_flag_(char flag, details::padding_info padding)
void spdlog::pattern_formatter::format ( const details::log_msg msg,
fmt::memory_buffer dest 
)
inlineoverridevirtual

Implements spdlog::formatter.

Definition at line 1047 of file pattern_formatter.h.

1048  {
1049 #ifndef SPDLOG_NO_DATETIME
1050  auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
1051  if (secs != last_log_secs_)
1052  {
1054  last_log_secs_ = secs;
1055  }
1056 #endif
1057  for (auto &f : formatters_)
1058  {
1059  f->format(msg, cached_tm_, dest);
1060  }
1061  // write eol
1063  }
std::chrono::seconds last_log_secs_
void msg(const char *fmt,...)
Definition: message.cpp:107
std::vector< std::unique_ptr< details::flag_formatter > > formatters_
second seconds
Alias for common language habits.
Definition: spacetime.h:83
void append_string_view(spdlog::string_view_t view, fmt::basic_memory_buffer< char, Buffer_Size > &dest)
Definition: fmt_helper.h:30
std::tm get_time_(const details::log_msg &msg)
std::tm spdlog::pattern_formatter::get_time_ ( const details::log_msg msg)
inlineprivate

Definition at line 1074 of file pattern_formatter.h.

1075  {
1077  {
1078  return details::os::localtime(log_clock::to_time_t(msg.time));
1079  }
1080  return details::os::gmtime(log_clock::to_time_t(msg.time));
1081  }
pattern_time_type pattern_time_type_
std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT
Definition: os.h:92
void msg(const char *fmt,...)
Definition: message.cpp:107
std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT
Definition: os.h:73
void spdlog::pattern_formatter::handle_flag_ ( char  flag,
details::padding_info  padding 
)
inlineprivate

Definition at line 1083 of file pattern_formatter.h.

1084  {
1085  switch (flag)
1086  {
1087 
1088  case ('+'): // default formatter
1089  formatters_.push_back(details::make_unique<details::full_formatter>(padding));
1090  break;
1091 
1092  case 'n': // logger name
1093  formatters_.push_back(details::make_unique<details::name_formatter>(padding));
1094  break;
1095 
1096  case 'l': // level
1097  formatters_.push_back(details::make_unique<details::level_formatter>(padding));
1098  break;
1099 
1100  case 'L': // short level
1101  formatters_.push_back(details::make_unique<details::short_level_formatter>(padding));
1102  break;
1103 
1104  case ('t'): // thread id
1105  formatters_.push_back(details::make_unique<details::t_formatter>(padding));
1106  break;
1107 
1108  case ('v'): // the message text
1109  formatters_.push_back(details::make_unique<details::v_formatter>(padding));
1110  break;
1111 
1112  case ('a'): // weekday
1113  formatters_.push_back(details::make_unique<details::a_formatter>(padding));
1114  break;
1115 
1116  case ('A'): // short weekday
1117  formatters_.push_back(details::make_unique<details::A_formatter>(padding));
1118  break;
1119 
1120  case ('b'):
1121  case ('h'): // month
1122  formatters_.push_back(details::make_unique<details::b_formatter>(padding));
1123  break;
1124 
1125  case ('B'): // short month
1126  formatters_.push_back(details::make_unique<details::B_formatter>(padding));
1127  break;
1128 
1129  case ('c'): // datetime
1130  formatters_.push_back(details::make_unique<details::c_formatter>(padding));
1131  break;
1132 
1133  case ('C'): // year 2 digits
1134  formatters_.push_back(details::make_unique<details::C_formatter>(padding));
1135  break;
1136 
1137  case ('Y'): // year 4 digits
1138  formatters_.push_back(details::make_unique<details::Y_formatter>(padding));
1139  break;
1140 
1141  case ('D'):
1142  case ('x'): // datetime MM/DD/YY
1143  formatters_.push_back(details::make_unique<details::D_formatter>(padding));
1144  break;
1145 
1146  case ('m'): // month 1-12
1147  formatters_.push_back(details::make_unique<details::m_formatter>(padding));
1148  break;
1149 
1150  case ('d'): // day of month 1-31
1151  formatters_.push_back(details::make_unique<details::d_formatter>(padding));
1152  break;
1153 
1154  case ('H'): // hours 24
1155  formatters_.push_back(details::make_unique<details::H_formatter>(padding));
1156  break;
1157 
1158  case ('I'): // hours 12
1159  formatters_.push_back(details::make_unique<details::I_formatter>(padding));
1160  break;
1161 
1162  case ('M'): // minutes
1163  formatters_.push_back(details::make_unique<details::M_formatter>(padding));
1164  break;
1165 
1166  case ('S'): // seconds
1167  formatters_.push_back(details::make_unique<details::S_formatter>(padding));
1168  break;
1169 
1170  case ('e'): // milliseconds
1171  formatters_.push_back(details::make_unique<details::e_formatter>(padding));
1172  break;
1173 
1174  case ('f'): // microseconds
1175  formatters_.push_back(details::make_unique<details::f_formatter>(padding));
1176  break;
1177 
1178  case ('F'): // nanoseconds
1179  formatters_.push_back(details::make_unique<details::F_formatter>(padding));
1180  break;
1181 
1182  case ('E'): // seconds since epoch
1183  formatters_.push_back(details::make_unique<details::E_formatter>(padding));
1184  break;
1185 
1186  case ('p'): // am/pm
1187  formatters_.push_back(details::make_unique<details::p_formatter>(padding));
1188  break;
1189 
1190  case ('r'): // 12 hour clock 02:55:02 pm
1191  formatters_.push_back(details::make_unique<details::r_formatter>(padding));
1192  break;
1193 
1194  case ('R'): // 24-hour HH:MM time
1195  formatters_.push_back(details::make_unique<details::R_formatter>(padding));
1196  break;
1197 
1198  case ('T'):
1199  case ('X'): // ISO 8601 time format (HH:MM:SS)
1200  formatters_.push_back(details::make_unique<details::T_formatter>(padding));
1201  break;
1202 
1203  case ('z'): // timezone
1204  formatters_.push_back(details::make_unique<details::z_formatter>(padding));
1205  break;
1206 
1207  case ('P'): // pid
1208  formatters_.push_back(details::make_unique<details::pid_formatter>(padding));
1209  break;
1210 
1211 #ifdef SPDLOG_ENABLE_MESSAGE_COUNTER
1212  case ('i'):
1213  formatters_.push_back(details::make_unique<details::i_formatter>(padding));
1214  break;
1215 #endif
1216  case ('^'): // color range start
1217  formatters_.push_back(details::make_unique<details::color_start_formatter>(padding));
1218  break;
1219 
1220  case ('$'): // color range end
1221  formatters_.push_back(details::make_unique<details::color_stop_formatter>(padding));
1222  break;
1223 
1224  case ('@'): // source location (filename:filenumber)
1225  formatters_.push_back(details::make_unique<details::source_location_formatter>(padding));
1226  break;
1227 
1228  case ('s'): // source filename
1229  formatters_.push_back(details::make_unique<details::source_filename_formatter>(padding));
1230  break;
1231 
1232  case ('#'): // source line number
1233  formatters_.push_back(details::make_unique<details::source_linenum_formatter>(padding));
1234  break;
1235 
1236  case ('!'): // source funcname
1237  formatters_.push_back(details::make_unique<details::source_funcname_formatter>(padding));
1238  break;
1239 
1240  case ('%'): // % char
1241  formatters_.push_back(details::make_unique<details::ch_formatter>('%'));
1242  break;
1243 
1244  default: // Unknown flag appears as is
1245  auto unknown_flag = details::make_unique<details::aggregate_formatter>();
1246  unknown_flag->add_ch('%');
1247  unknown_flag->add_ch(flag);
1248  formatters_.push_back((std::move(unknown_flag)));
1249  break;
1250  }
1251  }
std::vector< std::unique_ptr< details::flag_formatter > > formatters_
def move(depos, offset)
Definition: depos.py:107
details::padding_info spdlog::pattern_formatter::handle_padspec_ ( std::string::const_iterator it,
std::string::const_iterator  end 
)
inlineprivate

Definition at line 1256 of file pattern_formatter.h.

1257  {
1258  using details::padding_info;
1259  using details::scoped_pad;
1260  const size_t max_width = 128;
1261  if (it == end)
1262  {
1263  return padding_info{};
1264  }
1265 
1266  padding_info::pad_side side;
1267  switch (*it)
1268  {
1269  case '-':
1270  side = padding_info::right;
1271  ++it;
1272  break;
1273  case '=':
1274  side = padding_info::center;
1275  ++it;
1276  break;
1277  default:
1279  break;
1280  }
1281 
1282  if (it == end || !std::isdigit(static_cast<unsigned char>(*it)))
1283  {
1284  return padding_info{0, side};
1285  }
1286 
1287  auto width = static_cast<size_t>(*it - '0');
1288  for (++it; it != end && std::isdigit(static_cast<unsigned char>(*it)); ++it)
1289  {
1290  auto digit = static_cast<size_t>(*it - '0');
1291  width = width * 10 + digit;
1292  }
1293  return details::padding_info{std::min<size_t>(width, max_width), side};
1294  }
const double width
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
def center(depos, point)
Definition: depos.py:117
pattern_formatter& spdlog::pattern_formatter::operator= ( const pattern_formatter other)
delete

Member Data Documentation

std::tm spdlog::pattern_formatter::cached_tm_
private

Definition at line 1069 of file pattern_formatter.h.

std::string spdlog::pattern_formatter::eol_
private

Definition at line 1067 of file pattern_formatter.h.

std::vector<std::unique_ptr<details::flag_formatter> > spdlog::pattern_formatter::formatters_
private

Definition at line 1072 of file pattern_formatter.h.

std::chrono::seconds spdlog::pattern_formatter::last_log_secs_
private

Definition at line 1070 of file pattern_formatter.h.

std::string spdlog::pattern_formatter::pattern_
private

Definition at line 1066 of file pattern_formatter.h.

pattern_time_type spdlog::pattern_formatter::pattern_time_type_
private

Definition at line 1068 of file pattern_formatter.h.


The documentation for this class was generated from the following file: