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
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2018-10-22 04:48:11 +08:00
|
|
|
// name & level pattern appender
|
2016-08-26 05:38:08 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
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-10-30 07:23:17 +08:00
|
|
|
fmt_helper::append_string_view(*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
|
|
|
{
|
2018-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(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-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(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-08-13 15:30:02 +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
|
2018-07-23 05:13:52 +08:00
|
|
|
static const char *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-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(days[tm_time.tm_wday], dest);
|
2017-05-19 03:36:48 +08:00
|
|
|
}
|
|
|
|
};
|
2017-08-19 20:36:34 +08:00
|
|
|
|
2018-03-09 21:26:33 +08:00
|
|
|
// Full weekday name
|
2018-07-23 05:13:52 +08:00
|
|
|
static const char *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-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(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
|
2018-07-23 05:13:52 +08:00
|
|
|
static const char *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-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(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
|
2018-07-23 05:13:52 +08:00
|
|
|
static const char *full_months[]{
|
2018-03-09 21:26:33 +08:00
|
|
|
"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-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(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-09-26 19:33:37 +08:00
|
|
|
class c_formatter 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-22 04:48:07 +08:00
|
|
|
// fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday],
|
|
|
|
// months[tm_time.tm_mon], tm_time.tm_mday);
|
2018-07-10 02:07:44 +08:00
|
|
|
// date
|
2018-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(days[tm_time.tm_wday], dest);
|
2018-07-10 02:07:44 +08:00
|
|
|
dest.push_back(' ');
|
2018-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(months[tm_time.tm_mon], dest);
|
2018-07-10 02:07:44 +08:00
|
|
|
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-09-26 19:33:37 +08:00
|
|
|
class C_formatter 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-09-26 19:33:37 +08:00
|
|
|
class D_formatter 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-09-26 19:33:37 +08:00
|
|
|
class Y_formatter 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-09-26 19:33:37 +08:00
|
|
|
class m_formatter 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-09-26 19:33:37 +08:00
|
|
|
class d_formatter 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-09-26 19:33:37 +08:00
|
|
|
class H_formatter 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-09-26 19:33:37 +08:00
|
|
|
class I_formatter 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-09-26 19:33:37 +08:00
|
|
|
class M_formatter 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-09-26 19:33:37 +08:00
|
|
|
class S_formatter 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-09-26 19:33:37 +08:00
|
|
|
class e_formatter 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-26 05:20:31 +08:00
|
|
|
auto millis = fmt_helper::time_fraction<std::chrono::milliseconds>(msg.time);
|
2018-07-26 04:33:03 +08:00
|
|
|
fmt_helper::pad3(static_cast<int>(millis.count()), dest);
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// microseconds
|
2018-09-26 19:33:37 +08:00
|
|
|
class f_formatter 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-26 05:20:31 +08:00
|
|
|
auto micros = fmt_helper::time_fraction<std::chrono::microseconds>(msg.time);
|
2018-08-13 15:30:02 +08:00
|
|
|
fmt_helper::pad6(static_cast<size_t>(micros.count()), dest);
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// nanoseconds
|
2018-09-26 19:33:37 +08:00
|
|
|
class F_formatter 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-26 05:20:31 +08:00
|
|
|
auto ns = fmt_helper::time_fraction<std::chrono::nanoseconds>(msg.time);
|
|
|
|
fmt::format_to(dest, "{:09}", ns.count());
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-26 05:20:31 +08:00
|
|
|
// seconds since epoch
|
2018-09-26 19:33:37 +08:00
|
|
|
class E_formatter final : public flag_formatter
|
2017-09-18 10:11:23 +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-09-18 10:11:23 +08:00
|
|
|
{
|
|
|
|
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);
|
2017-09-18 10:11:23 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-26 05:38:08 +08:00
|
|
|
// AM/PM
|
2018-09-26 19:33:37 +08:00
|
|
|
class p_formatter 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-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(ampm(tm_time), dest);
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 12 hour clock 02:55:02 pm
|
2018-09-26 19:33:37 +08:00
|
|
|
class r_formatter 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);
|
2018-07-07 20:34:25 +08:00
|
|
|
dest.push_back(' ');
|
2018-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(ampm(tm_time), dest);
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 24-hour HH:MM time, equivalent to %H:%M
|
2018-09-26 19:33:37 +08:00
|
|
|
class R_formatter 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-09-26 19:33:37 +08:00
|
|
|
class T_formatter 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-22 04:48:07 +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-09-26 19:33:37 +08:00
|
|
|
class z_formatter 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:
|
2018-06-11 03:59:17 +08:00
|
|
|
log_clock::time_point last_update_{std::chrono::seconds(0)};
|
2018-06-24 06:32:39 +08:00
|
|
|
#ifdef _WIN32
|
2018-06-11 03:59:17 +08:00
|
|
|
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
|
|
|
{
|
2018-06-11 03:59:17 +08:00
|
|
|
if (msg.time - last_update_ >= cache_refresh)
|
2016-08-26 05:38:08 +08:00
|
|
|
{
|
2018-06-11 03:59:17 +08:00
|
|
|
offset_minutes_ = os::utc_minutes_offset(tm_time);
|
|
|
|
last_update_ = msg.time;
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
2018-06-11 03:59:17 +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
|
|
|
};
|
|
|
|
|
2016-12-02 23:09:00 +08:00
|
|
|
// Thread id
|
2018-09-26 19:33:37 +08:00
|
|
|
class t_formatter 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-09-26 19:33:37 +08:00
|
|
|
class pid_formatter final : public flag_formatter
|
2016-12-02 23:09:00 +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-12-02 23:09:00 +08:00
|
|
|
{
|
2018-06-24 06:32:39 +08:00
|
|
|
fmt_helper::append_int(details::os::pid(), dest);
|
2016-12-02 23:09:00 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-13 00:48:04 +08:00
|
|
|
// message counter formatter
|
2018-09-26 19:33:37 +08:00
|
|
|
class i_formatter 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-09-26 19:33:37 +08:00
|
|
|
class v_formatter 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-10-19 22:12:02 +08:00
|
|
|
fmt_helper::append_string_view(msg.payload, dest);
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-26 19:33:37 +08:00
|
|
|
class ch_formatter 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)
|
2018-06-11 03:59:17 +08:00
|
|
|
: 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:
|
2018-06-11 03:59:17 +08:00
|
|
|
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-09-26 19:33:37 +08:00
|
|
|
class aggregate_formatter 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)
|
|
|
|
{
|
2018-06-11 03:59:17 +08:00
|
|
|
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-10-19 07:15:50 +08:00
|
|
|
fmt_helper::append_string_view(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:
|
2018-06-11 03:59:17 +08:00
|
|
|
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%$"
|
2018-09-26 19:33:37 +08:00
|
|
|
class color_start_formatter 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-09-26 19:33:37 +08:00
|
|
|
class color_stop_formatter 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-09-26 19:33:37 +08:00
|
|
|
class full_formatter 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
|
|
|
{
|
2018-09-27 05:56:19 +08:00
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using std::chrono::milliseconds;
|
2018-09-27 07:03:12 +08:00
|
|
|
using std::chrono::seconds;
|
2018-09-27 05:56:19 +08:00
|
|
|
|
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-26 04:33:03 +08:00
|
|
|
auto secs = duration_cast<seconds>(duration);
|
2018-07-10 02:07:44 +08:00
|
|
|
|
2018-07-26 04:33:03 +08:00
|
|
|
if (cache_timestamp_ != secs || cached_datetime_.size() == 0)
|
2018-06-24 06:32:39 +08:00
|
|
|
{
|
2018-09-27 22:08:21 +08:00
|
|
|
cached_datetime_.clear();
|
2018-07-10 02:07:44 +08:00
|
|
|
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-26 04:33:03 +08:00
|
|
|
cache_timestamp_ = secs;
|
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-26 05:20:31 +08:00
|
|
|
auto millis = fmt_helper::time_fraction<milliseconds>(msg.time);
|
2018-07-26 04:33:03 +08:00
|
|
|
fmt_helper::pad3(static_cast<int>(millis.count()), dest);
|
|
|
|
dest.push_back(']');
|
|
|
|
dest.push_back(' ');
|
2018-07-10 02:07:44 +08:00
|
|
|
|
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-10-12 08:04:55 +08:00
|
|
|
if (!msg.logger_name->empty())
|
|
|
|
{
|
|
|
|
dest.push_back('[');
|
2018-10-19 07:15:50 +08:00
|
|
|
// fmt_helper::append_str(*msg.logger_name, dest);
|
|
|
|
fmt_helper::append_string_view(*msg.logger_name, dest);
|
2018-10-12 08:04:55 +08:00
|
|
|
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('[');
|
2018-04-08 23:27:18 +08:00
|
|
|
// wrap the level name with color
|
2018-06-24 06:32:39 +08:00
|
|
|
msg.color_range_start = dest.size();
|
2018-10-19 07:15:50 +08:00
|
|
|
// fmt_helper::append_string_view(level::to_c_str(msg.level), dest);
|
|
|
|
fmt_helper::append_string_view(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(' ');
|
2018-10-19 07:15:50 +08:00
|
|
|
// fmt_helper::append_string_view(msg.msg(), dest);
|
2018-10-19 22:12:02 +08:00
|
|
|
fmt_helper::append_string_view(msg.payload, dest);
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
2018-06-24 06:32:39 +08:00
|
|
|
|
|
|
|
private:
|
2018-07-11 04:53:00 +08:00
|
|
|
std::chrono::seconds cache_timestamp_{0};
|
2018-07-10 02:07:44 +08:00
|
|
|
fmt::basic_memory_buffer<char, 128> cached_datetime_;
|
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-09-26 19:33:37 +08:00
|
|
|
class pattern_formatter final : public formatter
|
2016-08-26 05:38:08 +08:00
|
|
|
{
|
2018-06-26 06:13:02 +08:00
|
|
|
public:
|
2018-07-23 05:13:52 +08:00
|
|
|
explicit pattern_formatter(
|
|
|
|
std::string pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol)
|
|
|
|
: pattern_(std::move(pattern))
|
|
|
|
, eol_(std::move(eol))
|
|
|
|
, pattern_time_type_(time_type)
|
|
|
|
, last_log_secs_(0)
|
|
|
|
{
|
|
|
|
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
|
|
|
compile_pattern_(pattern_);
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern_formatter(const pattern_formatter &other) = delete;
|
|
|
|
pattern_formatter &operator=(const pattern_formatter &other) = delete;
|
|
|
|
|
2018-08-13 14:09:49 +08:00
|
|
|
std::unique_ptr<formatter> clone() const override
|
2018-07-23 05:13:52 +08:00
|
|
|
{
|
2018-09-27 04:48:36 +08:00
|
|
|
return details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_);
|
2018-07-23 05:13:52 +08:00
|
|
|
}
|
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
|
2018-10-30 07:14:42 +08:00
|
|
|
details::fmt_helper::append_string_view(eol_, dest);
|
2016-08-26 05:38:08 +08:00
|
|
|
}
|
|
|
|
|
2018-06-26 06:13:02 +08:00
|
|
|
private:
|
2018-07-23 05:13:52 +08:00
|
|
|
std::string pattern_;
|
2018-07-23 02:52:46 +08:00
|
|
|
std::string eol_;
|
2018-07-07 21:15:17 +08:00
|
|
|
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 21:15:17 +08:00
|
|
|
|
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
|
|
|
{
|
2018-07-07 21:15:17 +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)
|
|
|
|
{
|
2018-07-22 04:48:07 +08:00
|
|
|
// logger name
|
2018-06-26 06:13:02 +08:00
|
|
|
case 'n':
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::name_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::level_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'L':
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::short_level_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('t'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::t_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('v'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::v_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('a'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::a_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('A'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::A_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('b'):
|
|
|
|
case ('h'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::b_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('B'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::B_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
case ('c'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::c_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('C'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::C_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('Y'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::Y_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('D'):
|
|
|
|
case ('x'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::D_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('m'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::m_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('d'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::d_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('H'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::H_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('I'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::I_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('M'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::M_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('S'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::S_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('e'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::e_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('f'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::f_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
case ('F'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::F_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('E'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::E_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('p'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::p_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('r'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::r_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('R'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::R_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('T'):
|
|
|
|
case ('X'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::T_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('z'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::z_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('+'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::full_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('P'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::pid_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
2018-10-06 06:49:34 +08:00
|
|
|
#ifdef SPDLOG_ENABLE_MESSAGE_COUNTER
|
2018-06-26 06:13:02 +08:00
|
|
|
case ('i'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::i_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
2018-10-06 06:49:34 +08:00
|
|
|
#endif
|
2018-06-26 06:13:02 +08:00
|
|
|
case ('^'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::color_start_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ('$'):
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::color_stop_formatter>());
|
2018-06-26 06:13:02 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: // Unknown flag appears as is
|
2018-09-27 04:48:36 +08:00
|
|
|
formatters_.push_back(details::make_unique<details::ch_formatter>('%'));
|
|
|
|
formatters_.push_back(details::make_unique<details::ch_formatter>(flag));
|
2018-06-26 06:13:02 +08:00
|
|
|
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)
|
2018-07-23 05:13:52 +08:00
|
|
|
{
|
2018-06-26 06:13:02 +08:00
|
|
|
auto end = pattern.end();
|
|
|
|
std::unique_ptr<details::aggregate_formatter> user_chars;
|
2018-07-23 05:13:52 +08:00
|
|
|
formatters_.clear();
|
2018-06-26 06:13:02 +08:00
|
|
|
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 (++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)
|
|
|
|
{
|
2018-09-27 04:48:36 +08:00
|
|
|
user_chars = details::make_unique<details::aggregate_formatter>();
|
2018-06-26 06:13:02 +08:00
|
|
|
}
|
|
|
|
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
|