spdlog/include/spdlog/details/pattern_formatter_impl.h

708 lines
19 KiB
C
Raw Normal View History

2016-08-26 05:38:08 +08:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include "../details/log_msg.h"
#include "../details/os.h"
#include "../fmt/fmt.h"
2018-03-09 21:26:33 +08:00
#include "../formatter.h"
2016-08-26 05:38:08 +08:00
2018-03-09 21:26:33 +08:00
#include <array>
2016-08-26 05:38:08 +08:00
#include <chrono>
#include <ctime>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <utility>
#include <vector>
2018-03-17 18:47:46 +08:00
namespace spdlog {
namespace details {
2016-08-26 05:38:08 +08:00
class flag_formatter
{
public:
2018-02-25 08:25:15 +08:00
virtual ~flag_formatter() = default;
2018-03-09 21:26:33 +08:00
virtual void format(details::log_msg &msg, const std::tm &tm_time) = 0;
2016-08-26 05:38:08 +08:00
};
///////////////////////////////////////////////////////////////////////
// name & level pattern appenders
///////////////////////////////////////////////////////////////////////
2018-02-25 09:43:26 +08:00
class name_formatter : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << *msg.logger_name;
}
};
// log level appender
2018-02-25 09:43:26 +08:00
class level_formatter : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << level::to_str(msg.level);
}
};
// short log level appender
2018-02-25 09:43:26 +08:00
class short_level_formatter : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << level::to_short_str(msg.level);
}
};
///////////////////////////////////////////////////////////////////////
// Date time pattern appenders
///////////////////////////////////////////////////////////////////////
2018-03-09 21:26:33 +08:00
static const char *ampm(const tm &t)
2016-08-26 05:38:08 +08:00
{
return t.tm_hour >= 12 ? "PM" : "AM";
}
2018-03-09 21:26:33 +08:00
static int to12h(const tm &t)
2016-08-26 05:38:08 +08:00
{
return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour;
}
2018-03-09 21:26:33 +08:00
// Abbreviated weekday name
static const std::string days[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
2018-02-25 09:43:26 +08:00
class a_formatter : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << days[tm_time.tm_wday];
}
};
2018-03-09 21:26:33 +08:00
// Full weekday name
static const std::string full_days[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
2018-02-25 09:43:26 +08:00
class A_formatter : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << full_days[tm_time.tm_wday];
2016-08-26 05:38:08 +08:00
}
};
2018-03-09 21:26:33 +08:00
// Abbreviated month
static const std::string months[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
2018-02-25 09:43:26 +08:00
class b_formatter : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << months[tm_time.tm_mon];
2016-08-26 05:38:08 +08:00
}
};
2018-03-09 21:26:33 +08:00
// Full month name
static const std::string full_months[]{
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
2018-02-25 09:43:26 +08:00
class B_formatter : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << full_months[tm_time.tm_mon];
2016-08-26 05:38:08 +08:00
}
};
2018-03-09 21:26:33 +08:00
// write 2 ints separated by sep with padding of 2
static fmt::MemoryWriter &pad_n_join(fmt::MemoryWriter &w, int v1, int v2, char sep)
2016-08-26 05:38:08 +08:00
{
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0');
return w;
}
2018-03-09 21:26:33 +08:00
// write 3 ints separated by sep with padding of 2
static fmt::MemoryWriter &pad_n_join(fmt::MemoryWriter &w, int v1, int v2, int v3, char sep)
2016-08-26 05:38:08 +08:00
{
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0') << sep << fmt::pad(v3, 2, '0');
return w;
}
2018-03-09 21:26:33 +08:00
// Date and time representation (Thu Aug 23 15:35:46 2014)
2018-02-25 08:25:15 +08:00
class c_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' ';
2016-08-26 05:38:08 +08:00
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900;
}
};
// year - 2 digit
2018-02-25 08:25:15 +08:00
class C_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << fmt::pad(tm_time.tm_year % 100, 2, '0');
}
};
// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
2018-02-25 08:25:15 +08:00
class D_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
pad_n_join(msg.formatted, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_year % 100, '/');
}
};
// year - 4 digit
2018-02-25 08:25:15 +08:00
class Y_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << tm_time.tm_year + 1900;
}
};
// month 1-12
2018-02-25 08:25:15 +08:00
class m_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << fmt::pad(tm_time.tm_mon + 1, 2, '0');
}
};
// day of month 1-31
2018-02-25 08:25:15 +08:00
class d_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << fmt::pad(tm_time.tm_mday, 2, '0');
}
};
2018-02-25 09:43:26 +08:00
// hours in 24 format 0-23
2018-02-25 08:25:15 +08:00
class H_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << fmt::pad(tm_time.tm_hour, 2, '0');
}
};
2018-02-25 09:43:26 +08:00
// hours in 12 format 1-12
2018-02-25 08:25:15 +08:00
class I_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << fmt::pad(to12h(tm_time), 2, '0');
}
};
// minutes 0-59
2018-02-25 08:25:15 +08:00
class M_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << fmt::pad(tm_time.tm_min, 2, '0');
}
};
// seconds 0-59
2018-02-25 08:25:15 +08:00
class S_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << fmt::pad(tm_time.tm_sec, 2, '0');
}
};
// milliseconds
2018-02-25 08:25:15 +08:00
class e_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
msg.formatted << fmt::pad(static_cast<int>(millis), 3, '0');
}
};
// microseconds
2018-02-25 08:25:15 +08:00
class f_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
auto duration = msg.time.time_since_epoch();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000;
msg.formatted << fmt::pad(static_cast<int>(micros), 6, '0');
}
};
// nanoseconds
2018-02-25 08:25:15 +08:00
class F_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
auto duration = msg.time.time_since_epoch();
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000000000;
msg.formatted << fmt::pad(static_cast<int>(ns), 9, '0');
}
};
2018-02-25 08:25:15 +08:00
class E_formatter SPDLOG_FINAL : public flag_formatter
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
{
auto duration = msg.time.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
msg.formatted << seconds;
}
};
2016-08-26 05:38:08 +08:00
// AM/PM
2018-02-25 08:25:15 +08:00
class p_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << ampm(tm_time);
}
};
// 12 hour clock 02:55:02 pm
2018-02-25 08:25:15 +08:00
class r_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
pad_n_join(msg.formatted, to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << ampm(tm_time);
}
};
// 24-hour HH:MM time, equivalent to %H:%M
2018-02-25 08:25:15 +08:00
class R_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
2016-08-26 05:38:08 +08:00
}
};
// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
2018-02-25 08:25:15 +08:00
class T_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':');
}
};
// ISO 8601 offset from UTC in timezone (+-HH:MM)
2018-02-25 07:31:14 +08:00
class z_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
public:
const std::chrono::seconds cache_refresh = std::chrono::seconds(5);
2018-02-25 08:25:15 +08:00
z_formatter() = default;
2018-03-09 21:26:33 +08:00
z_formatter(const z_formatter &) = delete;
z_formatter &operator=(const z_formatter &) = delete;
2016-08-26 05:38:08 +08:00
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
#ifdef _WIN32
int total_minutes = get_cached_offset(msg, tm_time);
#else
// No need to chache under gcc,
// it is very fast (already stored in tm.tm_gmtoff)
int total_minutes = os::utc_minutes_offset(tm_time);
#endif
bool is_negative = total_minutes < 0;
char sign;
if (is_negative)
{
total_minutes = -total_minutes;
sign = '-';
}
else
{
sign = '+';
}
int h = total_minutes / 60;
int m = total_minutes % 60;
msg.formatted << sign;
pad_n_join(msg.formatted, h, m, ':');
}
2018-03-09 21:26:33 +08:00
2016-08-26 05:38:08 +08:00
private:
2018-03-09 21:26:33 +08:00
log_clock::time_point _last_update{std::chrono::seconds(0)};
int _offset_minutes{0};
2016-08-26 05:38:08 +08:00
std::mutex _mutex;
2018-03-09 21:26:33 +08:00
int get_cached_offset(const log_msg &msg, const std::tm &tm_time)
2016-08-26 05:38:08 +08:00
{
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;
}
return _offset_minutes;
}
};
// Thread id
2018-02-25 08:25:15 +08:00
class t_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << msg.thread_id;
}
};
2016-12-02 23:40:40 +08:00
// Current pid
2018-02-25 08:25:15 +08:00
class pid_formatter SPDLOG_FINAL : public flag_formatter
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
{
2016-12-02 23:40:40 +08:00
msg.formatted << details::os::pid();
}
};
2017-10-13 00:48:04 +08:00
// message counter formatter
2018-02-25 08:25:15 +08:00
class i_formatter SPDLOG_FINAL : public flag_formatter
2017-10-13 00:48:04 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2017-10-13 00:48:04 +08:00
{
msg.formatted << fmt::pad(msg.msg_id, 6, '0');
}
};
2016-08-26 05:38:08 +08:00
2018-02-25 08:25:15 +08:00
class v_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
}
};
2018-02-25 08:25:15 +08:00
class ch_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
public:
2018-03-09 21:26:33 +08:00
explicit ch_formatter(char ch)
: _ch(ch)
{
}
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << _ch;
}
2018-03-09 21:26:33 +08:00
2016-08-26 05:38:08 +08:00
private:
char _ch;
};
2018-03-09 21:26:33 +08:00
// aggregate user chars to display as is
2018-02-25 08:25:15 +08:00
class aggregate_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
public:
2018-02-25 08:25:15 +08:00
aggregate_formatter() = default;
2016-08-26 05:38:08 +08:00
void add_ch(char ch)
{
_str += ch;
}
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &) override
2016-08-26 05:38:08 +08:00
{
msg.formatted << _str;
}
2018-03-09 21:26:33 +08:00
2016-08-26 05:38:08 +08:00
private:
std::string _str;
};
2018-04-06 07:24:07 +08:00
// set the color range. expect it to be in the form of "%^colored text%$"
class start_color_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.color_range_start = msg.formatted.size();
}
};
class stop_color_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.color_range_end = msg.formatted.size();
}
};
2016-08-26 05:38:08 +08:00
// Full info formatter
2018-04-06 07:24:07 +08:00
2016-08-26 05:38:08 +08:00
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
2018-02-25 08:25:15 +08:00
class full_formatter SPDLOG_FINAL : public flag_formatter
2016-08-26 05:38:08 +08:00
{
2018-03-09 21:26:33 +08:00
void format(details::log_msg &msg, const std::tm &tm_time) override
2016-08-26 05:38:08 +08:00
{
#ifndef SPDLOG_NO_DATETIME
auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
/* Slower version(while still very fast - about 3.2 million lines/sec under 10 threads),
msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] {} ",
tm_time.tm_year + 1900,
tm_time.tm_mon + 1,
tm_time.tm_mday,
tm_time.tm_hour,
tm_time.tm_min,
tm_time.tm_sec,
static_cast<int>(millis),
msg.logger_name,
level::to_str(msg.level),
msg.raw.str());*/
// Faster (albeit uglier) way to format the line (5.6 million lines/sec under 10 threads)
msg.formatted << '[' << static_cast<unsigned int>(tm_time.tm_year + 1900) << '-'
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_mon + 1), 2, '0') << '-'
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_mday), 2, '0') << ' '
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_hour), 2, '0') << ':'
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_min), 2, '0') << ':'
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_sec), 2, '0') << '.'
<< fmt::pad(static_cast<unsigned int>(millis), 3, '0') << "] ";
2018-03-09 21:26:33 +08:00
// no datetime needed
2016-08-26 05:38:08 +08:00
#else
(void)tm_time;
#endif
#ifndef SPDLOG_NO_NAME
msg.formatted << '[' << *msg.logger_name << "] ";
#endif
2018-04-06 07:24:07 +08:00
const char *level_name = level::to_str(msg.level);
size_t level_name_size = strlen(level_name);
msg.formatted << '[' << fmt::StringRef(level_name, level_name_size) << "] ";
2016-08-26 05:38:08 +08:00
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
2018-04-06 07:24:07 +08:00
// wrap the level with color
msg.color_range_start = 37;
msg.color_range_end = 37 + level_name_size;
2016-08-26 05:38:08 +08:00
}
};
2018-03-17 18:47:46 +08:00
} // namespace details
} // namespace spdlog
2016-08-26 05:38:08 +08:00
///////////////////////////////////////////////////////////////////////////////
// pattern_formatter inline impl
///////////////////////////////////////////////////////////////////////////////
2018-03-09 21:26:33 +08:00
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)
2016-08-26 05:38:08 +08:00
{
compile_pattern(pattern);
2016-08-26 05:38:08 +08:00
}
2018-03-09 21:26:33 +08:00
inline void spdlog::pattern_formatter::compile_pattern(const std::string &pattern)
2016-08-26 05:38:08 +08:00
{
auto end = pattern.end();
std::unique_ptr<details::aggregate_formatter> user_chars;
for (auto it = pattern.begin(); it != end; ++it)
{
if (*it == '%')
{
2018-03-09 21:26:33 +08:00
if (user_chars) // append user chars found so far
{
2018-02-23 20:29:31 +08:00
_formatters.push_back(std::move(user_chars));
}
2018-04-06 07:24:07 +08:00
// if(
2016-08-26 05:38:08 +08:00
if (++it != end)
{
2016-08-26 05:38:08 +08:00
handle_flag(*it);
}
2016-08-26 05:38:08 +08:00
else
{
2016-08-26 05:38:08 +08:00
break;
}
2016-08-26 05:38:08 +08:00
}
else // chars not following the % sign should be displayed as is
{
if (!user_chars)
{
2016-08-26 05:38:08 +08:00
user_chars = std::unique_ptr<details::aggregate_formatter>(new details::aggregate_formatter());
}
2016-08-26 05:38:08 +08:00
user_chars->add_ch(*it);
}
}
2018-03-09 21:26:33 +08:00
if (user_chars) // append raw chars found so far
2016-08-26 05:38:08 +08:00
{
_formatters.push_back(std::move(user_chars));
}
}
inline void spdlog::pattern_formatter::handle_flag(char flag)
{
switch (flag)
{
2017-12-01 09:46:19 +08:00
// logger name
2018-02-23 20:29:31 +08:00
case 'n':
_formatters.emplace_back(new details::name_formatter());
2016-08-26 05:38:08 +08:00
break;
case 'l':
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::level_formatter());
2016-08-26 05:38:08 +08:00
break;
case 'L':
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::short_level_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('t'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::t_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('v'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::v_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('a'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::a_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('A'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::A_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('b'):
case ('h'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::b_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('B'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::B_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('c'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::c_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('C'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::C_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('Y'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::Y_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('D'):
case ('x'):
2016-08-26 05:38:08 +08:00
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::D_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('m'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::m_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('d'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::d_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('H'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::H_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('I'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::I_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('M'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::M_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('S'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::S_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('e'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::e_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('f'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::f_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('F'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::F_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('E'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::E_formatter());
break;
2018-03-09 21:26:33 +08:00
case ('p'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::p_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('r'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::r_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('R'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::R_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('T'):
case ('X'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::T_formatter());
2016-08-26 05:38:08 +08:00
break;
2018-03-09 21:26:33 +08:00
case ('z'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::z_formatter());
2016-08-26 05:38:08 +08:00
break;
case ('+'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::full_formatter());
2016-08-26 05:38:08 +08:00
break;
case ('P'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::pid_formatter());
break;
case ('i'):
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::i_formatter());
break;
2018-04-06 07:24:07 +08:00
case ('^'):
_formatters.emplace_back(new details::start_color_formatter());
break;
case ('$'):
_formatters.emplace_back(new details::stop_color_formatter());
break;
2018-03-09 21:26:33 +08:00
default: // Unknown flag appears as is
2018-02-23 20:29:31 +08:00
_formatters.emplace_back(new details::ch_formatter('%'));
_formatters.emplace_back(new details::ch_formatter(flag));
2016-08-26 05:38:08 +08:00
break;
}
}
2018-03-09 21:26:33 +08:00
inline std::tm spdlog::pattern_formatter::get_time(details::log_msg &msg)
2017-06-01 01:12:21 +08:00
{
2018-02-25 19:41:18 +08:00
if (_pattern_time == pattern_time_type::local)
{
return details::os::localtime(log_clock::to_time_t(msg.time));
2018-02-25 07:24:47 +08:00
}
return details::os::gmtime(log_clock::to_time_t(msg.time));
}
2016-08-26 05:38:08 +08:00
2018-03-09 21:26:33 +08:00
inline void spdlog::pattern_formatter::format(details::log_msg &msg)
2016-08-26 05:38:08 +08:00
{
#ifndef SPDLOG_NO_DATETIME
auto tm_time = get_time(msg);
2016-08-26 05:38:08 +08:00
#else
std::tm tm_time;
#endif
for (auto &f : _formatters)
{
f->format(msg, tm_time);
}
2018-03-09 21:26:33 +08:00
// write eol
2018-02-05 12:36:54 +08:00
msg.formatted.write(_eol.data(), _eol.size());
2016-08-26 05:38:08 +08:00
}