pull/1884/head
gabime 5 years ago
parent eacfe072a1
commit fe734a3072

@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.2)
if (${CMAKE_VERSION} VERSION_LESS 3.11) if (${CMAKE_VERSION} VERSION_LESS 3.11)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else () else ()
cmake_policy(VERSION 3.14) cmake_policy(VERSION 3.11)
endif () endif ()
enable_language(C) enable_language(C)
@ -34,7 +34,7 @@ endif ()
# Compiler config # Compiler config
# --------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------
if (NOT CMAKE_CXX_STANDARD) if (NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif () endif ()

@ -257,6 +257,7 @@ void android_example()
// Log patterns can contain custom flags. // Log patterns can contain custom flags.
// this will add custom flag '%*' which will be bound to a <my_formatter_flag> instance // this will add custom flag '%*' which will be bound to a <my_formatter_flag> instance
#include "spdlog/pattern_formatter.h" #include "spdlog/pattern_formatter.h"
#include <memory>
class my_formatter_flag : public spdlog::custom_flag_formatter class my_formatter_flag : public spdlog::custom_flag_formatter
{ {
public: public:
@ -268,15 +269,14 @@ public:
std::unique_ptr<custom_flag_formatter> clone() const override std::unique_ptr<custom_flag_formatter> clone() const override
{ {
return spdlog::details::make_unique<my_formatter_flag>(); return std::make_unique<my_formatter_flag>();
} }
}; };
void custom_flags_example() void custom_flags_example()
{ {
using spdlog::details::make_unique; // for pre c++14 auto formatter = std::make_unique<spdlog::pattern_formatter>();
auto formatter = make_unique<spdlog::pattern_formatter>();
formatter->add_flag<my_formatter_flag>('*').set_pattern("[%n] [%*] [%^%l%$] %v"); formatter->add_flag<my_formatter_flag>('*').set_pattern("[%n] [%*] [%^%l%$] %v");
spdlog::set_formatter(std::move(formatter)); spdlog::set_formatter(std::move(formatter));
} }

@ -9,7 +9,6 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <initializer_list> #include <initializer_list>
#include <memory>
#include <exception> #include <exception>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
@ -209,21 +208,4 @@ struct source_loc
const char *funcname{nullptr}; const char *funcname{nullptr};
}; };
namespace details {
// make_unique support for pre c++14
#if __cplusplus >= 201402L // C++14 and beyond
using std::make_unique;
#define SPDLOG_VALIDATE_FMT(f) FMT_STRING(f)
#else
#define SPDLOG_VALIDATE_FMT(f) FMT_STRING(f)
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&... args)
{
static_assert(!std::is_array<T>::value, "arrays not supported");
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#endif
} // namespace details
} // namespace spdlog } // namespace spdlog

@ -94,7 +94,7 @@ public:
template<typename T, typename... Args> template<typename T, typename... Args>
pattern_formatter &add_flag(char flag, const Args &... args) pattern_formatter &add_flag(char flag, const Args &... args)
{ {
custom_handlers_[flag] = details::make_unique<T>(args...); custom_handlers_[flag] = std::make_unique<T>(args...);
return *this; return *this;
} }
void set_pattern(std::string pattern); void set_pattern(std::string pattern);

@ -3,8 +3,8 @@
#pragma once #pragma once
#define SPDLOG_VER_MAJOR 1 #define SPDLOG_VER_MAJOR 2
#define SPDLOG_VER_MINOR 6 #define SPDLOG_VER_MINOR 0
#define SPDLOG_VER_PATCH 1 #define SPDLOG_VER_PATCH 0
#define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH) #define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH)

@ -10,7 +10,7 @@
template<typename Mutex> template<typename Mutex>
SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::base_sink() SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::base_sink()
: formatter_{details::make_unique<spdlog::pattern_formatter>()} : formatter_{std::make_unique<spdlog::pattern_formatter>()}
{} {}
template<typename Mutex> template<typename Mutex>
@ -49,7 +49,7 @@ void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_pt
template<typename Mutex> template<typename Mutex>
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::string &pattern) void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::string &pattern)
{ {
set_formatter_(details::make_unique<spdlog::pattern_formatter>(pattern)); set_formatter_(std::make_unique<spdlog::pattern_formatter>(pattern));
} }
template<typename Mutex> template<typename Mutex>

@ -6,6 +6,7 @@
#include <spdlog/details/backtracer.h> #include <spdlog/details/backtracer.h>
#include <spdlog/pattern_formatter.h> #include <spdlog/pattern_formatter.h>
#include <memory>
#include <cstdio> #include <cstdio>
namespace spdlog { namespace spdlog {
@ -96,7 +97,7 @@ SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type) SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
{ {
auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type); auto new_formatter = std::make_unique<pattern_formatter>(std::move(pattern), time_type);
set_formatter(std::move(new_formatter)); set_formatter(std::move(new_formatter));
} }

@ -1007,7 +1007,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type,
, last_log_secs_(0) , last_log_secs_(0)
{ {
std::memset(&cached_tm_, 0, sizeof(cached_tm_)); std::memset(&cached_tm_, 0, sizeof(cached_tm_));
formatters_.push_back(details::make_unique<details::full_formatter>(details::padding_info{})); formatters_.push_back(std::make_unique<details::full_formatter>(details::padding_info{}));
} }
SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const
@ -1017,7 +1017,7 @@ SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const
{ {
cloned_custom_formatters[it.first] = it.second->clone(); cloned_custom_formatters[it.first] = it.second->clone();
} }
return details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters)); return std::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters));
} }
SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest)
@ -1069,178 +1069,178 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
switch (flag) switch (flag)
{ {
case ('+'): // default formatter case ('+'): // default formatter
formatters_.push_back(details::make_unique<details::full_formatter>(padding)); formatters_.push_back(std::make_unique<details::full_formatter>(padding));
break; break;
case 'n': // logger name case 'n': // logger name
formatters_.push_back(details::make_unique<details::name_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::name_formatter<Padder>>(padding));
break; break;
case 'l': // level case 'l': // level
formatters_.push_back(details::make_unique<details::level_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::level_formatter<Padder>>(padding));
break; break;
case 'L': // short level case 'L': // short level
formatters_.push_back(details::make_unique<details::short_level_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::short_level_formatter<Padder>>(padding));
break; break;
case ('t'): // thread id case ('t'): // thread id
formatters_.push_back(details::make_unique<details::t_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::t_formatter<Padder>>(padding));
break; break;
case ('v'): // the message text case ('v'): // the message text
formatters_.push_back(details::make_unique<details::v_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::v_formatter<Padder>>(padding));
break; break;
case ('a'): // weekday case ('a'): // weekday
formatters_.push_back(details::make_unique<details::a_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::a_formatter<Padder>>(padding));
break; break;
case ('A'): // short weekday case ('A'): // short weekday
formatters_.push_back(details::make_unique<details::A_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::A_formatter<Padder>>(padding));
break; break;
case ('b'): case ('b'):
case ('h'): // month case ('h'): // month
formatters_.push_back(details::make_unique<details::b_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::b_formatter<Padder>>(padding));
break; break;
case ('B'): // short month case ('B'): // short month
formatters_.push_back(details::make_unique<details::B_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::B_formatter<Padder>>(padding));
break; break;
case ('c'): // datetime case ('c'): // datetime
formatters_.push_back(details::make_unique<details::c_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::c_formatter<Padder>>(padding));
break; break;
case ('C'): // year 2 digits case ('C'): // year 2 digits
formatters_.push_back(details::make_unique<details::C_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::C_formatter<Padder>>(padding));
break; break;
case ('Y'): // year 4 digits case ('Y'): // year 4 digits
formatters_.push_back(details::make_unique<details::Y_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::Y_formatter<Padder>>(padding));
break; break;
case ('D'): case ('D'):
case ('x'): // datetime MM/DD/YY case ('x'): // datetime MM/DD/YY
formatters_.push_back(details::make_unique<details::D_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::D_formatter<Padder>>(padding));
break; break;
case ('m'): // month 1-12 case ('m'): // month 1-12
formatters_.push_back(details::make_unique<details::m_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::m_formatter<Padder>>(padding));
break; break;
case ('d'): // day of month 1-31 case ('d'): // day of month 1-31
formatters_.push_back(details::make_unique<details::d_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::d_formatter<Padder>>(padding));
break; break;
case ('H'): // hours 24 case ('H'): // hours 24
formatters_.push_back(details::make_unique<details::H_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::H_formatter<Padder>>(padding));
break; break;
case ('I'): // hours 12 case ('I'): // hours 12
formatters_.push_back(details::make_unique<details::I_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::I_formatter<Padder>>(padding));
break; break;
case ('M'): // minutes case ('M'): // minutes
formatters_.push_back(details::make_unique<details::M_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::M_formatter<Padder>>(padding));
break; break;
case ('S'): // seconds case ('S'): // seconds
formatters_.push_back(details::make_unique<details::S_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::S_formatter<Padder>>(padding));
break; break;
case ('e'): // milliseconds case ('e'): // milliseconds
formatters_.push_back(details::make_unique<details::e_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::e_formatter<Padder>>(padding));
break; break;
case ('f'): // microseconds case ('f'): // microseconds
formatters_.push_back(details::make_unique<details::f_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::f_formatter<Padder>>(padding));
break; break;
case ('F'): // nanoseconds case ('F'): // nanoseconds
formatters_.push_back(details::make_unique<details::F_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::F_formatter<Padder>>(padding));
break; break;
case ('E'): // seconds since epoch case ('E'): // seconds since epoch
formatters_.push_back(details::make_unique<details::E_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::E_formatter<Padder>>(padding));
break; break;
case ('p'): // am/pm case ('p'): // am/pm
formatters_.push_back(details::make_unique<details::p_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::p_formatter<Padder>>(padding));
break; break;
case ('r'): // 12 hour clock 02:55:02 pm case ('r'): // 12 hour clock 02:55:02 pm
formatters_.push_back(details::make_unique<details::r_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::r_formatter<Padder>>(padding));
break; break;
case ('R'): // 24-hour HH:MM time case ('R'): // 24-hour HH:MM time
formatters_.push_back(details::make_unique<details::R_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::R_formatter<Padder>>(padding));
break; break;
case ('T'): case ('T'):
case ('X'): // ISO 8601 time format (HH:MM:SS) case ('X'): // ISO 8601 time format (HH:MM:SS)
formatters_.push_back(details::make_unique<details::T_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::T_formatter<Padder>>(padding));
break; break;
case ('z'): // timezone case ('z'): // timezone
formatters_.push_back(details::make_unique<details::z_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::z_formatter<Padder>>(padding));
break; break;
case ('P'): // pid case ('P'): // pid
formatters_.push_back(details::make_unique<details::pid_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::pid_formatter<Padder>>(padding));
break; break;
case ('^'): // color range start case ('^'): // color range start
formatters_.push_back(details::make_unique<details::color_start_formatter>(padding)); formatters_.push_back(std::make_unique<details::color_start_formatter>(padding));
break; break;
case ('$'): // color range end case ('$'): // color range end
formatters_.push_back(details::make_unique<details::color_stop_formatter>(padding)); formatters_.push_back(std::make_unique<details::color_stop_formatter>(padding));
break; break;
case ('@'): // source location (filename:filenumber) case ('@'): // source location (filename:filenumber)
formatters_.push_back(details::make_unique<details::source_location_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::source_location_formatter<Padder>>(padding));
break; break;
case ('s'): // short source filename - without directory name case ('s'): // short source filename - without directory name
formatters_.push_back(details::make_unique<details::short_filename_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::short_filename_formatter<Padder>>(padding));
break; break;
case ('g'): // full source filename case ('g'): // full source filename
formatters_.push_back(details::make_unique<details::source_filename_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::source_filename_formatter<Padder>>(padding));
break; break;
case ('#'): // source line number case ('#'): // source line number
formatters_.push_back(details::make_unique<details::source_linenum_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::source_linenum_formatter<Padder>>(padding));
break; break;
case ('!'): // source funcname case ('!'): // source funcname
formatters_.push_back(details::make_unique<details::source_funcname_formatter<Padder>>(padding)); formatters_.push_back(std::make_unique<details::source_funcname_formatter<Padder>>(padding));
break; break;
case ('%'): // % char case ('%'): // % char
formatters_.push_back(details::make_unique<details::ch_formatter>('%')); formatters_.push_back(std::make_unique<details::ch_formatter>('%'));
break; break;
case ('u'): // elapsed time since last log message in nanos case ('u'): // elapsed time since last log message in nanos
formatters_.push_back(details::make_unique<details::elapsed_formatter<Padder, std::chrono::nanoseconds>>(padding)); formatters_.push_back(std::make_unique<details::elapsed_formatter<Padder, std::chrono::nanoseconds>>(padding));
break; break;
case ('i'): // elapsed time since last log message in micros case ('i'): // elapsed time since last log message in micros
formatters_.push_back(details::make_unique<details::elapsed_formatter<Padder, std::chrono::microseconds>>(padding)); formatters_.push_back(std::make_unique<details::elapsed_formatter<Padder, std::chrono::microseconds>>(padding));
break; break;
case ('o'): // elapsed time since last log message in millis case ('o'): // elapsed time since last log message in millis
formatters_.push_back(details::make_unique<details::elapsed_formatter<Padder, std::chrono::milliseconds>>(padding)); formatters_.push_back(std::make_unique<details::elapsed_formatter<Padder, std::chrono::milliseconds>>(padding));
break; break;
case ('O'): // elapsed time since last log message in seconds case ('O'): // elapsed time since last log message in seconds
formatters_.push_back(details::make_unique<details::elapsed_formatter<Padder, std::chrono::seconds>>(padding)); formatters_.push_back(std::make_unique<details::elapsed_formatter<Padder, std::chrono::seconds>>(padding));
break; break;
default: // Unknown flag appears as is default: // Unknown flag appears as is
auto unknown_flag = details::make_unique<details::aggregate_formatter>(); auto unknown_flag = std::make_unique<details::aggregate_formatter>();
unknown_flag->add_ch('%'); unknown_flag->add_ch('%');
unknown_flag->add_ch(flag); unknown_flag->add_ch(flag);
formatters_.push_back((std::move(unknown_flag))); formatters_.push_back((std::move(unknown_flag)));
@ -1340,7 +1340,7 @@ SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &patter
{ {
if (!user_chars) if (!user_chars)
{ {
user_chars = details::make_unique<details::aggregate_formatter>(); user_chars = std::make_unique<details::aggregate_formatter>();
} }
user_chars->add_ch(*it); user_chars->add_ch(*it);
} }

@ -183,7 +183,7 @@ SPDLOG_INLINE void registry::flush_every(std::chrono::seconds interval)
{ {
std::lock_guard<std::mutex> lock(flusher_mutex_); std::lock_guard<std::mutex> lock(flusher_mutex_);
auto clbk = [this]() { this->flush_all(); }; auto clbk = [this]() { this->flush_all(); };
periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval); periodic_flusher_ = std::make_unique<periodic_worker>(clbk, interval);
} }
SPDLOG_INLINE void registry::set_error_handler(void (*handler)(const std::string &msg)) SPDLOG_INLINE void registry::set_error_handler(void (*handler)(const std::string &msg))

@ -18,7 +18,7 @@ template<typename ConsoleMutex>
SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file) SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
: mutex_(ConsoleMutex::mutex()) : mutex_(ConsoleMutex::mutex())
, file_(file) , file_(file)
, formatter_(details::make_unique<spdlog::pattern_formatter>()) , formatter_(std::make_unique<spdlog::pattern_formatter>())
{} {}
template<typename ConsoleMutex> template<typename ConsoleMutex>

Loading…
Cancel
Save