spdlog/include/spdlog/details/pattern_formatter.h

772 lines
22 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
2018-06-15 19:15:35 +08:00
#include "spdlog/details/fmt_helper.h"
2018-04-29 06:43:42 +08:00
#include "spdlog/details/log_msg.h"
#include "spdlog/details/os.h"
#include "spdlog/fmt/fmt.h"
#include "spdlog/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 {
2018-06-12 23:48:22 +08:00
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-06-24 06:32:39 +08:00
virtual void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) = 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_str(*msg.logger_name, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
fmt_helper::append_c_str(level::to_c_str(msg.level), dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-07-04 14:59:26 +08:00
fmt_helper::append_c_str(level::to_short_c_str(msg.level), dest);
2016-08-26 05:38:08 +08:00
}
};
///////////////////////////////////////////////////////////////////////
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_str(days[tm_time.tm_wday], dest);
}
};
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_str(full_days[tm_time.tm_wday], dest);
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_str(months[tm_time.tm_mon], dest);
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_str(full_months[tm_time.tm_mon], dest);
2016-08-26 05:38:08 +08:00
}
};
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-07-10 02:07:44 +08:00
//fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday], months[tm_time.tm_mon], tm_time.tm_mday);
// date
fmt_helper::append_str(days[tm_time.tm_wday], dest);
dest.push_back(' ');
fmt_helper::append_str(months[tm_time.tm_mon], dest);
dest.push_back(' ');
fmt_helper::append_int(tm_time.tm_mday, dest);
dest.push_back(' ');
// time
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_hour, dest);
2018-06-24 06:32:39 +08:00
dest.push_back(':');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_min, dest);
2018-06-24 06:32:39 +08:00
dest.push_back(':');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_sec, dest);
2018-06-24 06:32:39 +08:00
dest.push_back(' ');
fmt_helper::append_int(tm_time.tm_year + 1900, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_year % 100, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_mon + 1, dest);
2018-06-24 06:32:39 +08:00
dest.push_back('/');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_mday, dest);
2018-06-24 06:32:39 +08:00
dest.push_back('/');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_year % 100, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_int(tm_time.tm_year + 1900, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_mon + 1, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_mday, dest);
2016-08-26 05:38:08 +08:00
}
};
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_hour, dest);
2016-08-26 05:38:08 +08:00
}
};
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(to12h(tm_time), dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_min, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_sec, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) 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;
2018-06-26 07:00:33 +08:00
fmt_helper::pad3(static_cast<int>(millis), dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) 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;
2018-07-04 06:41:05 +08:00
fmt_helper::pad6(static_cast<int>(micros), dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) 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;
2018-07-05 15:54:02 +08:00
fmt::format_to(dest, "{:09}", ns);
2016-08-26 05:38:08 +08:00
}
};
2018-02-25 08:25:15 +08:00
class E_formatter SPDLOG_FINAL : public flag_formatter
{
2018-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
{
auto duration = msg.time.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
2018-06-24 06:32:39 +08:00
fmt_helper::append_int(seconds, dest);
}
};
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
fmt_helper::append_c_str(ampm(tm_time), dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(to12h(tm_time), dest);
2018-06-24 06:32:39 +08:00
dest.push_back(':');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_min, dest);
2018-06-24 06:32:39 +08:00
dest.push_back(':');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_sec, dest);
dest.push_back(' ');
fmt_helper::append_c_str(ampm(tm_time), dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_hour, dest);
2018-06-24 06:32:39 +08:00
dest.push_back(':');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_min, dest);
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
// fmt::format_to(dest, "{:02}:{:02}:{:02}", tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_hour, dest);
2018-06-24 06:32:39 +08:00
dest.push_back(':');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_min, dest);
2018-06-24 06:32:39 +08:00
dest.push_back(':');
2018-06-26 07:00:33 +08:00
fmt_helper::pad2(tm_time.tm_sec, dest);
2016-08-26 05:38:08 +08:00
}
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) 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)
2018-06-24 06:32:39 +08:00
(void)(msg);
2016-08-26 05:38:08 +08:00
int total_minutes = os::utc_minutes_offset(tm_time);
#endif
bool is_negative = total_minutes < 0;
if (is_negative)
{
total_minutes = -total_minutes;
2018-07-08 06:41:32 +08:00
dest.push_back('-');
2016-08-26 05:38:08 +08:00
}
else
{
2018-07-08 06:41:32 +08:00
dest.push_back('+');
2016-08-26 05:38:08 +08:00
}
2018-07-08 21:26:25 +08:00
fmt_helper::pad2(total_minutes / 60, dest); // hours
2018-06-24 06:32:39 +08:00
dest.push_back(':');
2018-07-08 21:26:25 +08:00
fmt_helper::pad2(total_minutes % 60, dest); // minutes
2016-08-26 05:38:08 +08:00
}
2018-03-09 21:26:33 +08:00
2016-08-26 05:38:08 +08:00
private:
log_clock::time_point last_update_{std::chrono::seconds(0)};
2018-06-24 06:32:39 +08:00
#ifdef _WIN32
int offset_minutes_{0};
2016-08-26 05:38:08 +08:00
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
{
if (msg.time - last_update_ >= cache_refresh)
2016-08-26 05:38:08 +08:00
{
offset_minutes_ = os::utc_minutes_offset(tm_time);
last_update_ = msg.time;
2016-08-26 05:38:08 +08:00
}
return offset_minutes_;
2016-08-26 05:38:08 +08:00
}
2018-06-24 06:32:39 +08:00
#endif
2016-08-26 05:38:08 +08:00
};
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-07-04 06:41:05 +08:00
fmt_helper::pad6(msg.thread_id, dest);
2016-08-26 05:38:08 +08:00
}
};
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-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &, fmt::memory_buffer &dest) override
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_int(details::os::pid(), dest);
}
};
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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
2017-10-13 00:48:04 +08:00
{
2018-07-04 06:41:05 +08:00
fmt_helper::pad6(msg.msg_id, dest);
2017-10-13 00:48:04 +08:00
}
};
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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_buf(msg.raw, dest);
2016-08-26 05:38:08 +08:00
}
};
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)
2018-03-09 21:26:33 +08:00
{
}
2018-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
dest.push_back(ch_);
2016-08-26 05:38:08 +08:00
}
2018-03-09 21:26:33 +08:00
2016-08-26 05:38:08 +08:00
private:
char ch_;
2016-08-26 05:38:08 +08:00
};
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;
2016-08-26 05:38:08 +08:00
}
2018-06-24 06:32:39 +08:00
void format(const details::log_msg &, const std::tm &, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
2018-06-24 06:32:39 +08:00
fmt_helper::append_str(str_, dest);
2016-08-26 05:38:08 +08:00
}
2018-03-09 21:26:33 +08:00
2016-08-26 05:38:08 +08:00
private:
std::string str_;
2016-08-26 05:38:08 +08:00
};
2018-04-06 08:22:27 +08:00
// mark the color range. expect it to be in the form of "%^colored text%$"
class color_start_formatter SPDLOG_FINAL : public flag_formatter
2018-04-06 07:24:07 +08:00
{
2018-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
2018-04-06 07:24:07 +08:00
{
2018-06-24 06:32:39 +08:00
msg.color_range_start = dest.size();
2018-04-06 07:24:07 +08:00
}
};
2018-04-06 08:22:27 +08:00
class color_stop_formatter SPDLOG_FINAL : public flag_formatter
2018-04-06 07:24:07 +08:00
{
2018-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
2018-04-06 07:24:07 +08:00
{
2018-06-24 06:32:39 +08:00
msg.color_range_end = dest.size();
2018-04-06 07:24:07 +08:00
}
};
2016-08-26 05:38:08 +08:00
// Full info formatter
// 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-06-24 06:32:39 +08:00
void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) override
2016-08-26 05:38:08 +08:00
{
#ifndef SPDLOG_NO_DATETIME
2018-07-10 02:07:44 +08:00
// cache the date/time part for the next second.
2018-06-26 06:13:02 +08:00
auto duration = msg.time.time_since_epoch();
2018-07-10 02:07:44 +08:00
std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
if (cache_timestamp_ != seconds || cached_datetime_.size() == 0)
2018-06-24 06:32:39 +08:00
{
2018-07-10 02:07:44 +08:00
cached_datetime_.resize(0);
cached_datetime_.push_back('[');
fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_);
cached_datetime_.push_back('-');
2018-06-13 06:06:37 +08:00
2018-07-10 02:07:44 +08:00
fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_);
cached_datetime_.push_back('-');
2018-06-13 06:06:37 +08:00
2018-07-10 02:07:44 +08:00
fmt_helper::pad2(tm_time.tm_mday, cached_datetime_);
cached_datetime_.push_back(' ');
2018-06-13 06:06:37 +08:00
2018-07-10 02:07:44 +08:00
fmt_helper::pad2(tm_time.tm_hour, cached_datetime_);
cached_datetime_.push_back(':');
2018-06-13 06:06:37 +08:00
2018-07-10 02:07:44 +08:00
fmt_helper::pad2(tm_time.tm_min, cached_datetime_);
cached_datetime_.push_back(':');
2018-06-13 06:06:37 +08:00
2018-07-10 02:07:44 +08:00
fmt_helper::pad2(tm_time.tm_sec, cached_datetime_);
cached_datetime_.push_back('.');
2018-06-13 06:06:37 +08:00
2018-07-10 02:07:44 +08:00
cache_timestamp_ = seconds;
2018-06-24 06:32:39 +08:00
}
2018-07-10 02:07:44 +08:00
fmt_helper::append_buf(cached_datetime_, dest);
2016-08-26 05:38:08 +08:00
2018-07-10 02:07:44 +08:00
// cache the millis part for the next milli.
2018-06-24 06:32:39 +08:00
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
2018-07-10 02:07:44 +08:00
if(millis != millis_cache_timestamp_ || cached_millis_.size() == 0)
{
cached_millis_.resize(0);
2018-07-10 15:40:09 +08:00
fmt_helper::pad3(static_cast<int>(millis), cached_millis_);
2018-07-10 02:07:44 +08:00
cached_millis_.push_back(']');
cached_millis_.push_back(' ');
millis_cache_timestamp_ = millis;
}
fmt_helper::append_buf(cached_millis_, dest);
2018-06-24 06:32:39 +08:00
#else // no datetime needed
2016-08-26 05:38:08 +08:00
(void)tm_time;
#endif
#ifndef SPDLOG_NO_NAME
2018-06-24 06:32:39 +08:00
dest.push_back('[');
fmt_helper::append_str(*msg.logger_name, dest);
dest.push_back(']');
dest.push_back(' ');
2016-08-26 05:38:08 +08:00
#endif
2018-06-24 06:32:39 +08:00
dest.push_back('[');
// wrap the level name with color
2018-06-24 06:32:39 +08:00
msg.color_range_start = dest.size();
fmt_helper::append_c_str(level::to_c_str(msg.level), dest);
2018-06-24 06:32:39 +08:00
msg.color_range_end = dest.size();
dest.push_back(']');
dest.push_back(' ');
fmt_helper::append_buf(msg.raw, dest);
2016-08-26 05:38:08 +08:00
}
2018-06-24 06:32:39 +08:00
private:
2018-07-10 02:07:44 +08:00
std::chrono::seconds cache_timestamp_ {0};
std::chrono::milliseconds::rep millis_cache_timestamp_ {0};
fmt::basic_memory_buffer<char, 128> cached_datetime_;
fmt::basic_memory_buffer<char, 8> cached_millis_;
2016-08-26 05:38:08 +08:00
};
2018-03-17 18:47:46 +08:00
} // namespace details
2016-08-26 05:38:08 +08:00
2018-06-26 06:13:02 +08:00
class pattern_formatter SPDLOG_FINAL : public formatter
2016-08-26 05:38:08 +08:00
{
2018-06-26 06:13:02 +08:00
public:
explicit pattern_formatter(const std::string &pattern, pattern_time_type time_type = pattern_time_type::local,
2018-06-26 06:13:02 +08:00
std::string eol = spdlog::details::os::default_eol)
: eol_(std::move(eol))
, pattern_time_type_(time_type)
2018-06-26 07:00:33 +08:00
, last_log_secs_(0)
2016-08-26 05:38:08 +08:00
{
2018-06-26 07:00:33 +08:00
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
2018-07-07 19:04:31 +08:00
compile_pattern_(pattern);
2018-06-26 06:13:02 +08:00
}
2018-07-11 01:20:55 +08:00
pattern_formatter(const pattern_formatter &) = delete;
pattern_formatter &operator=(const pattern_formatter &) = delete;
2018-06-26 06:13:02 +08:00
void format(const details::log_msg &msg, fmt::memory_buffer &dest) override
{
#ifndef SPDLOG_NO_DATETIME
auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
if (secs != last_log_secs_)
2016-08-26 05:38:08 +08:00
{
2018-07-07 19:04:31 +08:00
cached_tm_ = get_time_(msg);
2018-06-26 06:13:02 +08:00
last_log_secs_ = secs;
2016-08-26 05:38:08 +08:00
}
2018-06-26 06:13:02 +08:00
#endif
for (auto &f : formatters_)
2016-08-26 05:38:08 +08:00
{
2018-06-26 06:13:02 +08:00
f->format(msg, cached_tm_, dest);
2016-08-26 05:38:08 +08:00
}
2018-06-26 06:13:02 +08:00
// write eol
details::fmt_helper::append_str(eol_, dest);
2016-08-26 05:38:08 +08:00
}
2018-06-26 06:13:02 +08:00
private:
const std::string eol_;
pattern_time_type pattern_time_type_;
2018-06-26 07:00:33 +08:00
std::tm cached_tm_;
2018-06-26 06:13:02 +08:00
std::chrono::seconds last_log_secs_;
2016-08-26 05:38:08 +08:00
2018-06-26 06:13:02 +08:00
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
2018-07-07 19:04:31 +08:00
std::tm get_time_(const details::log_msg &msg)
2018-06-26 06:13:02 +08:00
{
if (pattern_time_type_ == pattern_time_type::local)
2018-06-26 06:13:02 +08:00
{
return details::os::localtime(log_clock::to_time_t(msg.time));
}
return details::os::gmtime(log_clock::to_time_t(msg.time));
}
2018-06-26 07:00:33 +08:00
2018-07-07 19:04:31 +08:00
void handle_flag_(char flag)
2018-02-25 19:41:18 +08:00
{
2018-06-26 06:13:02 +08:00
switch (flag)
{
// logger name
case 'n':
formatters_.emplace_back(new details::name_formatter());
break;
case 'l':
formatters_.emplace_back(new details::level_formatter());
break;
case 'L':
formatters_.emplace_back(new details::short_level_formatter());
break;
case ('t'):
formatters_.emplace_back(new details::t_formatter());
break;
case ('v'):
formatters_.emplace_back(new details::v_formatter());
break;
case ('a'):
formatters_.emplace_back(new details::a_formatter());
break;
case ('A'):
formatters_.emplace_back(new details::A_formatter());
break;
case ('b'):
case ('h'):
formatters_.emplace_back(new details::b_formatter());
break;
case ('B'):
formatters_.emplace_back(new details::B_formatter());
break;
case ('c'):
formatters_.emplace_back(new details::c_formatter());
break;
case ('C'):
formatters_.emplace_back(new details::C_formatter());
break;
case ('Y'):
formatters_.emplace_back(new details::Y_formatter());
break;
case ('D'):
case ('x'):
formatters_.emplace_back(new details::D_formatter());
break;
case ('m'):
formatters_.emplace_back(new details::m_formatter());
break;
case ('d'):
formatters_.emplace_back(new details::d_formatter());
break;
case ('H'):
formatters_.emplace_back(new details::H_formatter());
break;
case ('I'):
formatters_.emplace_back(new details::I_formatter());
break;
case ('M'):
formatters_.emplace_back(new details::M_formatter());
break;
case ('S'):
formatters_.emplace_back(new details::S_formatter());
break;
case ('e'):
formatters_.emplace_back(new details::e_formatter());
break;
case ('f'):
formatters_.emplace_back(new details::f_formatter());
break;
case ('F'):
formatters_.emplace_back(new details::F_formatter());
break;
case ('E'):
formatters_.emplace_back(new details::E_formatter());
break;
case ('p'):
formatters_.emplace_back(new details::p_formatter());
break;
case ('r'):
formatters_.emplace_back(new details::r_formatter());
break;
case ('R'):
formatters_.emplace_back(new details::R_formatter());
break;
case ('T'):
case ('X'):
formatters_.emplace_back(new details::T_formatter());
break;
case ('z'):
formatters_.emplace_back(new details::z_formatter());
break;
case ('+'):
formatters_.emplace_back(new details::full_formatter());
break;
case ('P'):
formatters_.emplace_back(new details::pid_formatter());
break;
case ('i'):
formatters_.emplace_back(new details::i_formatter());
break;
case ('^'):
formatters_.emplace_back(new details::color_start_formatter());
break;
case ('$'):
formatters_.emplace_back(new details::color_stop_formatter());
break;
default: // Unknown flag appears as is
formatters_.emplace_back(new details::ch_formatter('%'));
formatters_.emplace_back(new details::ch_formatter(flag));
break;
}
2018-02-25 07:24:47 +08:00
}
2016-08-26 05:38:08 +08:00
2018-07-07 19:04:31 +08:00
void compile_pattern_(const std::string &pattern)
2016-08-26 05:38:08 +08:00
{
2018-06-26 06:13:02 +08:00
auto end = pattern.end();
std::unique_ptr<details::aggregate_formatter> user_chars;
for (auto it = pattern.begin(); it != end; ++it)
{
if (*it == '%')
{
if (user_chars) // append user chars found so far
{
formatters_.push_back(std::move(user_chars));
}
// if(
if (++it != end)
{
2018-07-07 19:04:31 +08:00
handle_flag_(*it);
2018-06-26 06:13:02 +08:00
}
else
{
break;
}
}
else // chars not following the % sign should be displayed as is
{
if (!user_chars)
{
user_chars = std::unique_ptr<details::aggregate_formatter>(new details::aggregate_formatter());
}
user_chars->add_ch(*it);
}
}
if (user_chars) // append raw chars found so far
{
formatters_.push_back(std::move(user_chars));
}
2016-08-26 05:38:08 +08:00
}
2018-06-26 06:13:02 +08:00
};
} // namespace spdlog