clang-format

This commit is contained in:
gabime
2018-03-09 15:26:33 +02:00
parent 461b5ef28a
commit a2653d409f
66 changed files with 2330 additions and 2587 deletions

View File

@@ -5,11 +5,12 @@
#pragma once
#include "../formatter.h"
#include "../details/log_msg.h"
#include "../details/os.h"
#include "../fmt/fmt.h"
#include "../formatter.h"
#include <array>
#include <chrono>
#include <ctime>
#include <memory>
@@ -18,17 +19,13 @@
#include <thread>
#include <utility>
#include <vector>
#include <array>
namespace spdlog
{
namespace details
{
namespace spdlog { namespace details {
class flag_formatter
{
public:
virtual ~flag_formatter() = default;
virtual void format(details::log_msg& msg, const std::tm& tm_time) = 0;
virtual void format(details::log_msg &msg, const std::tm &tm_time) = 0;
};
///////////////////////////////////////////////////////////////////////
@@ -36,7 +33,7 @@ public:
///////////////////////////////////////////////////////////////////////
class name_formatter : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << *msg.logger_name;
}
@@ -45,7 +42,7 @@ class name_formatter : public flag_formatter
// log level appender
class level_formatter : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << level::to_str(msg.level);
}
@@ -54,7 +51,7 @@ class level_formatter : public flag_formatter
// short log level appender
class short_level_formatter : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << level::to_short_str(msg.level);
}
@@ -64,74 +61,75 @@ class short_level_formatter : public flag_formatter
// Date time pattern appenders
///////////////////////////////////////////////////////////////////////
static const char* ampm(const tm& t)
static const char *ampm(const tm &t)
{
return t.tm_hour >= 12 ? "PM" : "AM";
}
static int to12h(const tm& t)
static int to12h(const tm &t)
{
return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour;
}
//Abbreviated weekday name
static const std::string days[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
// Abbreviated weekday name
static const std::string days[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
class a_formatter : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << days[tm_time.tm_wday];
}
};
//Full weekday name
static const std::string full_days[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
// Full weekday name
static const std::string full_days[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
class A_formatter : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << full_days[tm_time.tm_wday];
}
};
//Abbreviated month
static const std::string months[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
// Abbreviated month
static const std::string months[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
class b_formatter : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << months[tm_time.tm_mon];
}
};
//Full month name
static const std::string full_months[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
// Full month name
static const std::string full_months[]{
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
class B_formatter : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << full_months[tm_time.tm_mon];
}
};
//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)
// 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)
{
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0');
return w;
}
//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)
// 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)
{
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0') << sep << fmt::pad(v3, 2, '0');
return w;
}
//Date and time representation (Thu Aug 23 15:35:46 2014)
// Date and time representation (Thu Aug 23 15:35:46 2014)
class c_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' ';
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900;
@@ -141,7 +139,7 @@ class c_formatter SPDLOG_FINAL : public flag_formatter
// year - 2 digit
class C_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_year % 100, 2, '0');
}
@@ -150,7 +148,7 @@ class C_formatter SPDLOG_FINAL : public flag_formatter
// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01
class D_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
pad_n_join(msg.formatted, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_year % 100, '/');
}
@@ -159,7 +157,7 @@ class D_formatter SPDLOG_FINAL : public flag_formatter
// year - 4 digit
class Y_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << tm_time.tm_year + 1900;
}
@@ -168,7 +166,7 @@ class Y_formatter SPDLOG_FINAL : public flag_formatter
// month 1-12
class m_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_mon + 1, 2, '0');
}
@@ -177,7 +175,7 @@ class m_formatter SPDLOG_FINAL : public flag_formatter
// day of month 1-31
class d_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_mday, 2, '0');
}
@@ -186,7 +184,7 @@ class d_formatter SPDLOG_FINAL : public flag_formatter
// hours in 24 format 0-23
class H_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_hour, 2, '0');
}
@@ -195,7 +193,7 @@ class H_formatter SPDLOG_FINAL : public flag_formatter
// hours in 12 format 1-12
class I_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(to12h(tm_time), 2, '0');
}
@@ -204,7 +202,7 @@ class I_formatter SPDLOG_FINAL : public flag_formatter
// minutes 0-59
class M_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_min, 2, '0');
}
@@ -213,7 +211,7 @@ class M_formatter SPDLOG_FINAL : public flag_formatter
// seconds 0-59
class S_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_sec, 2, '0');
}
@@ -222,7 +220,7 @@ class S_formatter SPDLOG_FINAL : public flag_formatter
// milliseconds
class e_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
@@ -233,7 +231,7 @@ class e_formatter SPDLOG_FINAL : public flag_formatter
// microseconds
class f_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
auto duration = msg.time.time_since_epoch();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000;
@@ -244,7 +242,7 @@ class f_formatter SPDLOG_FINAL : public flag_formatter
// nanoseconds
class F_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
auto duration = msg.time.time_since_epoch();
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000000000;
@@ -254,7 +252,7 @@ class F_formatter SPDLOG_FINAL : public flag_formatter
class E_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
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();
@@ -265,7 +263,7 @@ class E_formatter SPDLOG_FINAL : public flag_formatter
// AM/PM
class p_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << ampm(tm_time);
}
@@ -274,7 +272,7 @@ class p_formatter SPDLOG_FINAL : public flag_formatter
// 12 hour clock 02:55:02 pm
class r_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
pad_n_join(msg.formatted, to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << ampm(tm_time);
}
@@ -283,7 +281,7 @@ class r_formatter SPDLOG_FINAL : public flag_formatter
// 24-hour HH:MM time, equivalent to %H:%M
class R_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
}
@@ -292,7 +290,7 @@ class R_formatter SPDLOG_FINAL : public flag_formatter
// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
class T_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':');
}
@@ -305,10 +303,10 @@ public:
const std::chrono::seconds cache_refresh = std::chrono::seconds(5);
z_formatter() = default;
z_formatter(const z_formatter&) = delete;
z_formatter& operator=(const z_formatter&) = delete;
z_formatter(const z_formatter &) = delete;
z_formatter &operator=(const z_formatter &) = delete;
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
#ifdef _WIN32
int total_minutes = get_cached_offset(msg, tm_time);
@@ -334,12 +332,13 @@ public:
msg.formatted << sign;
pad_n_join(msg.formatted, h, m, ':');
}
private:
log_clock::time_point _last_update{ std::chrono::seconds(0) };
int _offset_minutes{ 0 };
log_clock::time_point _last_update{std::chrono::seconds(0)};
int _offset_minutes{0};
std::mutex _mutex;
int get_cached_offset(const log_msg& msg, const std::tm& tm_time)
int get_cached_offset(const log_msg &msg, const std::tm &tm_time)
{
std::lock_guard<std::mutex> l(_mutex);
if (msg.time - _last_update >= cache_refresh)
@@ -354,7 +353,7 @@ private:
// Thread id
class t_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << msg.thread_id;
}
@@ -363,7 +362,7 @@ class t_formatter SPDLOG_FINAL : public flag_formatter
// Current pid
class pid_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << details::os::pid();
}
@@ -372,7 +371,7 @@ class pid_formatter SPDLOG_FINAL : public flag_formatter
// message counter formatter
class i_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << fmt::pad(msg.msg_id, 6, '0');
}
@@ -380,7 +379,7 @@ class i_formatter SPDLOG_FINAL : public flag_formatter
class v_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
}
@@ -389,18 +388,20 @@ class v_formatter SPDLOG_FINAL : public flag_formatter
class ch_formatter SPDLOG_FINAL : public flag_formatter
{
public:
explicit ch_formatter(char ch): _ch(ch)
{}
void format(details::log_msg& msg, const std::tm&) override
explicit ch_formatter(char ch)
: _ch(ch)
{
}
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << _ch;
}
private:
char _ch;
};
//aggregate user chars to display as is
// aggregate user chars to display as is
class aggregate_formatter SPDLOG_FINAL : public flag_formatter
{
public:
@@ -410,10 +411,11 @@ public:
{
_str += ch;
}
void format(details::log_msg& msg, const std::tm&) override
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << _str;
}
private:
std::string _str;
};
@@ -422,7 +424,7 @@ private:
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
class full_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg& msg, const std::tm& tm_time) override
void format(details::log_msg &msg, const std::tm &tm_time) override
{
#ifndef SPDLOG_NO_DATETIME
auto duration = msg.time.time_since_epoch();
@@ -441,7 +443,6 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
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') << '-'
@@ -451,7 +452,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_sec), 2, '0') << '.'
<< fmt::pad(static_cast<unsigned int>(millis), 3, '0') << "] ";
//no datetime needed
// no datetime needed
#else
(void)tm_time;
#endif
@@ -465,21 +466,18 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
}
};
}
}
}} // namespace spdlog::details
///////////////////////////////////////////////////////////////////////////////
// pattern_formatter inline impl
///////////////////////////////////////////////////////////////////////////////
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)
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)
{
compile_pattern(pattern);
}
inline void spdlog::pattern_formatter::compile_pattern(const std::string& pattern)
inline void spdlog::pattern_formatter::compile_pattern(const std::string &pattern)
{
auto end = pattern.end();
std::unique_ptr<details::aggregate_formatter> user_chars;
@@ -487,7 +485,7 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string& patter
{
if (*it == '%')
{
if (user_chars) //append user chars found so far
if (user_chars) // append user chars found so far
_formatters.push_back(std::move(user_chars));
if (++it != end)
handle_flag(*it);
@@ -501,11 +499,10 @@ inline void spdlog::pattern_formatter::compile_pattern(const std::string& patter
user_chars->add_ch(*it);
}
}
if (user_chars) //append raw chars found so far
if (user_chars) // append raw chars found so far
{
_formatters.push_back(std::move(user_chars));
}
}
inline void spdlog::pattern_formatter::handle_flag(char flag)
{
@@ -524,105 +521,105 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
_formatters.emplace_back(new details::short_level_formatter());
break;
case('t'):
case ('t'):
_formatters.emplace_back(new details::t_formatter());
break;
case('v'):
case ('v'):
_formatters.emplace_back(new details::v_formatter());
break;
case('a'):
case ('a'):
_formatters.emplace_back(new details::a_formatter());
break;
case('A'):
case ('A'):
_formatters.emplace_back(new details::A_formatter());
break;
case('b'):
case('h'):
case ('b'):
case ('h'):
_formatters.emplace_back(new details::b_formatter());
break;
case('B'):
case ('B'):
_formatters.emplace_back(new details::B_formatter());
break;
case('c'):
case ('c'):
_formatters.emplace_back(new details::c_formatter());
break;
case('C'):
case ('C'):
_formatters.emplace_back(new details::C_formatter());
break;
case('Y'):
case ('Y'):
_formatters.emplace_back(new details::Y_formatter());
break;
case('D'):
case('x'):
case ('D'):
case ('x'):
_formatters.emplace_back(new details::D_formatter());
break;
case('m'):
case ('m'):
_formatters.emplace_back(new details::m_formatter());
break;
case('d'):
case ('d'):
_formatters.emplace_back(new details::d_formatter());
break;
case('H'):
case ('H'):
_formatters.emplace_back(new details::H_formatter());
break;
case('I'):
case ('I'):
_formatters.emplace_back(new details::I_formatter());
break;
case('M'):
case ('M'):
_formatters.emplace_back(new details::M_formatter());
break;
case('S'):
case ('S'):
_formatters.emplace_back(new details::S_formatter());
break;
case('e'):
case ('e'):
_formatters.emplace_back(new details::e_formatter());
break;
case('f'):
case ('f'):
_formatters.emplace_back(new details::f_formatter());
break;
case('F'):
case ('F'):
_formatters.emplace_back(new details::F_formatter());
break;
case('E'):
case ('E'):
_formatters.emplace_back(new details::E_formatter());
break;
case('p'):
case ('p'):
_formatters.emplace_back(new details::p_formatter());
break;
case('r'):
case ('r'):
_formatters.emplace_back(new details::r_formatter());
break;
case('R'):
case ('R'):
_formatters.emplace_back(new details::R_formatter());
break;
case('T'):
case('X'):
case ('T'):
case ('X'):
_formatters.emplace_back(new details::T_formatter());
break;
case('z'):
case ('z'):
_formatters.emplace_back(new details::z_formatter());
break;
@@ -634,19 +631,18 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
_formatters.emplace_back(new details::pid_formatter());
break;
case ('i'):
_formatters.emplace_back(new details::i_formatter());
break;
default: //Unknown flag appears as is
default: // Unknown flag appears as is
_formatters.emplace_back(new details::ch_formatter('%'));
_formatters.emplace_back(new details::ch_formatter(flag));
break;
}
}
inline std::tm spdlog::pattern_formatter::get_time(details::log_msg& msg)
inline std::tm spdlog::pattern_formatter::get_time(details::log_msg &msg)
{
if (_pattern_time == pattern_time_type::local)
{
@@ -655,7 +651,7 @@ inline std::tm spdlog::pattern_formatter::get_time(details::log_msg& msg)
return details::os::gmtime(log_clock::to_time_t(msg.time));
}
inline void spdlog::pattern_formatter::format(details::log_msg& msg)
inline void spdlog::pattern_formatter::format(details::log_msg &msg)
{
#ifndef SPDLOG_NO_DATETIME
@@ -667,6 +663,6 @@ inline void spdlog::pattern_formatter::format(details::log_msg& msg)
{
f->format(msg, tm_time);
}
//write eol
// write eol
msg.formatted.write(_eol.data(), _eol.size());
}