diff --git a/bench/g2log-bench-mt.cpp b/bench/g2log-bench-mt.cpp index 60660ecf..9e32c3f4 100644 --- a/bench/g2log-bench-mt.cpp +++ b/bench/g2log-bench-mt.cpp @@ -19,7 +19,7 @@ int main(int argc, char* argv[]) g2LogWorker g2log(argv[0], "logs"); g2::initializeLogging(&g2log); - + std::atomic msg_counter {0}; vector threads; diff --git a/bench/g2log-bench.cpp b/bench/g2log-bench.cpp index d06131b8..f716940a 100644 --- a/bench/g2log-bench.cpp +++ b/bench/g2log-bench.cpp @@ -5,10 +5,10 @@ int main(int, char* argv[]) { int howmany = 1000000; - - g2LogWorker g2log(argv[0], "logs"); + + g2LogWorker g2log(argv[0], "logs"); g2::initializeLogging(&g2log); - + for(int i = 0 ; i < howmany; ++i) LOG(INFO) << "g2log message # " << i << ": This is some text for your pleasure"; diff --git a/bench/spdlog-bench-async.cpp b/bench/spdlog-bench-async.cpp index 9054cb6e..9ec78472 100644 --- a/bench/spdlog-bench-async.cpp +++ b/bench/spdlog-bench-async.cpp @@ -6,13 +6,13 @@ int main(int, char* []) { int howmany = 1000000; namespace spd = spdlog; - spd::set_async_mode(2500, std::chrono::seconds(0)); + spd::set_async_mode(2500, std::chrono::seconds(0)); ///Create a file rotating logger with 5mb size max and 3 rotated files auto logger = spd::rotating_logger_mt("file_logger", "logs/spd-sample", 10 *1024 * 1024 , 5); logger->set_pattern("[%Y-%b-%d %T.%e]: %v"); for(int i = 0 ; i < howmany; ++i) logger->info() << "spdlog message #" << i << ": This is some text for your pleasure"; - spd::stop(); + spd::stop(); return 0; } diff --git a/bench/spdlog-bench-mt-async.cpp b/bench/spdlog-bench-mt-async.cpp index 17c72a8c..58027020 100644 --- a/bench/spdlog-bench-mt-async.cpp +++ b/bench/spdlog-bench-mt-async.cpp @@ -17,7 +17,7 @@ int main(int argc, char* argv[]) int howmany = 1000000; namespace spd = spdlog; - spd::set_async_mode(2500, std::chrono::seconds(0)); + spd::set_async_mode(2500, std::chrono::seconds(0)); ///Create a file rotating logger with 5mb size max and 3 rotated files auto logger = spd::rotating_logger_mt("file_logger", "logs/spd-sample", 10 *1024 * 1024 , 5); @@ -45,6 +45,6 @@ int main(int argc, char* argv[]) t.join(); }; - spd::stop(); + spd::stop(); return 0; } diff --git a/example/bench.cpp b/example/bench.cpp index 31787af7..70d2ef9d 100644 --- a/example/bench.cpp +++ b/example/bench.cpp @@ -52,7 +52,7 @@ int main(int argc, char* argv[]) int howmany = 1000000; int threads = 10; - bool auto_flush = false; + bool auto_flush = false; int file_size = 30 * 1024 * 1024; int rotating_files = 5; @@ -79,22 +79,22 @@ int main(int argc, char* argv[]) cout << "*******************************************************************************\n"; auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt", file_size, rotating_files, auto_flush); - bench_mt(howmany, rotating_mt, threads); + bench_mt(howmany, rotating_mt, threads); auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt", auto_flush); bench_mt(howmany, daily_mt, threads); bench(howmany, spdlog::create("null_mt")); - + cout << "\n*******************************************************************************\n"; cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations, auto_flush=" << auto_flush << endl; - cout << "*******************************************************************************\n"; + cout << "*******************************************************************************\n"; + - spdlog::set_async_mode(2500); auto daily_st_async = spdlog::daily_logger_st("daily_async", "logs/daily_async", auto_flush); - bench_mt(howmany, daily_st_async, threads); - + bench_mt(howmany, daily_st_async, threads); + spdlog::stop(); } diff --git a/include/spdlog/details/format.h b/include/spdlog/details/format.h index 5e1644c2..208efe54 100644 --- a/include/spdlog/details/format.h +++ b/include/spdlog/details/format.h @@ -112,7 +112,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. TypeName(const TypeName&); \ void operator=(const TypeName&) -namespace fmt { +namespace fmt +{ // Fix the warning about long long on older versions of GCC // that don't support the diagnostic pragma. @@ -161,7 +162,8 @@ format(std::string("{}"), 42); \endrst */ template -class BasicStringRef { +class BasicStringRef +{ private: const Char *data_; std::size_t size_; @@ -188,28 +190,33 @@ public: /** Converts a string reference to an `std::string` object. */ - operator std::basic_string() const { + operator std::basic_string() const + { return std::basic_string(data_, size()); } /** Returns the pointer to a C string. */ - const Char *c_str() const { + const Char *c_str() const + { return data_; } /** Returns the string size. */ - std::size_t size() const { + std::size_t size() const + { return size_; } - friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) { + friend bool operator==(BasicStringRef lhs, BasicStringRef rhs) + { return lhs.data_ == rhs.data_; } - friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) { + friend bool operator!=(BasicStringRef lhs, BasicStringRef rhs) + { return lhs.data_ != rhs.data_; } }; @@ -220,13 +227,15 @@ typedef BasicStringRef WStringRef; /** A formatting error such as invalid format string. */ -class FormatError : public std::runtime_error { +class FormatError : public std::runtime_error +{ public: explicit FormatError(StringRef message) : std::runtime_error(message.c_str()) {} }; -namespace internal { +namespace internal +{ // The number of characters to store in the MemoryBuffer object itself // to avoid dynamic memory allocation. @@ -235,19 +244,22 @@ enum { INLINE_BUFFER_SIZE = 500 }; #if _SECURE_SCL // Use checked iterator to avoid warnings on MSVC. template -inline stdext::checked_array_iterator make_ptr(T *ptr, std::size_t size) { +inline stdext::checked_array_iterator make_ptr(T *ptr, std::size_t size) +{ return stdext::checked_array_iterator(ptr, size); } #else template -inline T *make_ptr(T *ptr, std::size_t) { +inline T *make_ptr(T *ptr, std::size_t) +{ return ptr; } #endif // A buffer for POD types. It supports a subset of std::vector's operations. template -class Buffer { +class Buffer +{ private: FMT_DISALLOW_COPY_AND_ASSIGN(Buffer); @@ -265,33 +277,39 @@ public: virtual ~Buffer() {} // Returns the size of this buffer. - std::size_t size() const { + std::size_t size() const + { return size_; } // Returns the capacity of this buffer. - std::size_t capacity() const { + std::size_t capacity() const + { return capacity_; } // Resizes the buffer. If T is a POD type new elements are not initialized. - void resize(std::size_t new_size) { + void resize(std::size_t new_size) + { if (new_size > capacity_) grow(new_size); size_ = new_size; } // Reserves space to store at least capacity elements. - void reserve(std::size_t capacity) { + void reserve(std::size_t capacity) + { if (capacity > capacity_) grow(capacity); } - void clear() FMT_NOEXCEPT(true) { + void clear() FMT_NOEXCEPT(true) + { size_ = 0; } - void push_back(const T &value) { + void push_back(const T &value) + { if (size_ == capacity_) grow(size_ + 1); ptr_[size_++] = value; @@ -300,16 +318,19 @@ public: // Appends data to the end of the buffer. void append(const T *begin, const T *end); - T &operator[](std::size_t index) { + T &operator[](std::size_t index) + { return ptr_[index]; } - const T &operator[](std::size_t index) const { + const T &operator[](std::size_t index) const + { return ptr_[index]; } }; template -void Buffer::append(const T *begin, const T *end) { +void Buffer::append(const T *begin, const T *end) +{ std::ptrdiff_t num_elements = end - begin; if (size_ + num_elements > capacity_) grow(size_ + num_elements); @@ -320,12 +341,14 @@ void Buffer::append(const T *begin, const T *end) { // A memory buffer for POD types with the first SIZE elements stored in // the object itself. template > -class MemoryBuffer : private Allocator, public Buffer { +class MemoryBuffer : private Allocator, public Buffer +{ private: T data_[SIZE]; // Free memory allocated by the buffer. - void free() { + void free() + { if (this->ptr_ != data_) this->deallocate(this->ptr_, this->capacity_); } @@ -335,24 +358,28 @@ protected: public: explicit MemoryBuffer(const Allocator &alloc = Allocator()) : Allocator(alloc), Buffer(data_, SIZE) {} - ~MemoryBuffer() { + ~MemoryBuffer() + { free(); } #if FMT_USE_RVALUE_REFERENCES private: // Move data from other to this buffer. - void move(MemoryBuffer &other) { + void move(MemoryBuffer &other) + { Allocator &this_alloc = *this, &other_alloc = other; this_alloc = std::move(other_alloc); this->size_ = other.size_; this->capacity_ = other.capacity_; - if (other.ptr_ == other.data_) { + if (other.ptr_ == other.data_) + { this->ptr_ = data_; std::copy(other.data_, other.data_ + this->size_, make_ptr(data_, this->capacity_)); } - else { + else + { this->ptr_ = other.ptr_; // Set pointer to the inline array so that delete is not called // when freeing. @@ -361,11 +388,13 @@ private: } public: - MemoryBuffer(MemoryBuffer &&other) { + MemoryBuffer(MemoryBuffer &&other) + { move(other); } - MemoryBuffer &operator=(MemoryBuffer &&other) { + MemoryBuffer &operator=(MemoryBuffer &&other) + { assert(this != &other); free(); move(other); @@ -374,13 +403,15 @@ public: #endif // Returns a copy of the allocator associated with this buffer. - Allocator get_allocator() const { + Allocator get_allocator() const + { return *this; } }; template -void MemoryBuffer::grow(std::size_t size) { +void MemoryBuffer::grow(std::size_t size) +{ std::size_t new_capacity = (std::max)(size, this->capacity_ + this->capacity_ / 2); T *new_ptr = this->allocate(new_capacity); @@ -400,7 +431,8 @@ void MemoryBuffer::grow(std::size_t size) { #ifndef _MSC_VER // Portable version of signbit. -inline int getsign(double x) { +inline int getsign(double x) +{ // When compiled in C++11 mode signbit is no longer a macro but a function // defined in namespace std and the macro is undefined. # ifdef signbit @@ -412,22 +444,27 @@ inline int getsign(double x) { // Portable version of isinf. # ifdef isinf -inline int isinfinity(double x) { +inline int isinfinity(double x) +{ return isinf(x); } -inline int isinfinity(long double x) { +inline int isinfinity(long double x) +{ return isinf(x); } # else -inline int isinfinity(double x) { +inline int isinfinity(double x) +{ return std::isinf(x); } -inline int isinfinity(long double x) { +inline int isinfinity(long double x) +{ return std::isinf(x); } # endif #else -inline int getsign(double value) { +inline int getsign(double value) +{ if (value < 0) return 1; if (value == value) return 0; int dec = 0, sign = 0; @@ -435,23 +472,27 @@ inline int getsign(double value) { _ecvt_s(buffer, sizeof(buffer), value, 0, &dec, &sign); return sign; } -inline int isinfinity(double x) { +inline int isinfinity(double x) +{ return !_finite(x); } #endif template -struct IsLongDouble { +struct IsLongDouble +{ enum { VALUE = 0 }; }; template <> -struct IsLongDouble { +struct IsLongDouble +{ enum { VALUE = 1 }; }; template -class BasicCharTraits { +class BasicCharTraits +{ public: #if _SECURE_SCL typedef stdext::checked_array_iterator CharPtr; @@ -464,7 +505,8 @@ template class CharTraits; template <> -class CharTraits : public BasicCharTraits { +class CharTraits : public BasicCharTraits +{ private: // Conversion from wchar_t to char is not allowed. static char convert(wchar_t); @@ -472,7 +514,8 @@ private: public: typedef const wchar_t *UnsupportedStrType; - static char convert(char value) { + static char convert(char value) + { return value; } @@ -483,14 +526,17 @@ public: }; template <> -class CharTraits : public BasicCharTraits { +class CharTraits : public BasicCharTraits +{ public: typedef const char *UnsupportedStrType; - static wchar_t convert(char value) { + static wchar_t convert(char value) + { return value; } - static wchar_t convert(wchar_t value) { + static wchar_t convert(wchar_t value) + { return value; } @@ -501,17 +547,21 @@ public: // Checks if a number is negative - used to avoid warnings. template -struct SignChecker { +struct SignChecker +{ template - static bool is_negative(T value) { + static bool is_negative(T value) + { return value < 0; } }; template <> -struct SignChecker { +struct SignChecker +{ template - static bool is_negative(T) { + static bool is_negative(T) + { return false; } }; @@ -519,23 +569,27 @@ struct SignChecker { // Returns true if value is negative, false otherwise. // Same as (value < 0) but doesn't produce warnings if T is an unsigned type. template -inline bool is_negative(T value) { +inline bool is_negative(T value) +{ return SignChecker::is_signed>::is_negative(value); } // Selects uint32_t if FitsIn32Bits is true, uint64_t otherwise. template -struct TypeSelector { +struct TypeSelector +{ typedef uint32_t Type; }; template <> -struct TypeSelector { +struct TypeSelector +{ typedef uint64_t Type; }; template -struct IntTraits { +struct IntTraits +{ // Smallest of uint32_t and uint64_t that is large enough to represent // all values of T. typedef typename @@ -544,7 +598,8 @@ struct IntTraits { // MakeUnsigned::Type gives an unsigned type corresponding to integer type T. template -struct MakeUnsigned { +struct MakeUnsigned +{ typedef T Type; }; @@ -567,7 +622,8 @@ extern const uint64_t POWERS_OF_10_64[]; #if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clzll) // Returns the number of decimal digits in n. Leading zeros are not counted // except for n == 0 in which case count_digits returns 1. -inline unsigned count_digits(uint64_t n) { +inline unsigned count_digits(uint64_t n) +{ // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits. unsigned t = (64 - __builtin_clzll(n | 1)) * 1233 >> 12; @@ -575,16 +631,19 @@ inline unsigned count_digits(uint64_t n) { } # if FMT_GCC_VERSION >= 400 || FMT_HAS_BUILTIN(__builtin_clz) // Optional version of count_digits for better performance on 32-bit platforms. -inline unsigned count_digits(uint32_t n) { +inline unsigned count_digits(uint32_t n) +{ uint32_t t = (32 - __builtin_clz(n | 1)) * 1233 >> 12; return t - (n < POWERS_OF_10_32[t]) + 1; } # endif #else // Fallback version of count_digits used when __builtin_clz is not available. -inline unsigned count_digits(uint64_t n) { +inline unsigned count_digits(uint64_t n) +{ unsigned count = 1; - for (;;) { + for (;;) + { // Integer division is slow so do it for a group of four digits instead // of for every digit. The idea comes from the talk by Alexandrescu // "Three Optimization Tips for C++". See speed-test for a comparison. @@ -602,9 +661,11 @@ extern const char DIGITS[]; // Formats a decimal unsigned integer value writing into buffer. template -inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) { +inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) +{ --num_digits; - while (value >= 100) { + while (value >= 100) + { // Integer division is slow so do it for a group of two digits instead // of for every digit. The idea comes from the talk by Alexandrescu // "Three Optimization Tips for C++". See speed-test for a comparison. @@ -614,7 +675,8 @@ inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) { buffer[num_digits - 1] = DIGITS[index]; num_digits -= 2; } - if (value < 10) { + if (value < 10) + { *buffer = static_cast('0' + value); return; } @@ -626,45 +688,55 @@ inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) { #ifdef _WIN32 // A converter from UTF-8 to UTF-16. // It is only provided for Windows since other systems support UTF-8 natively. -class UTF8ToUTF16 { +class UTF8ToUTF16 +{ private: MemoryBuffer buffer_; public: explicit UTF8ToUTF16(StringRef s); - operator WStringRef() const { + operator WStringRef() const + { return WStringRef(&buffer_[0], size()); } - size_t size() const { + size_t size() const + { return buffer_.size() - 1; } - const wchar_t *c_str() const { + const wchar_t *c_str() const + { return &buffer_[0]; } - std::wstring str() const { + std::wstring str() const + { return std::wstring(&buffer_[0], size()); } }; // A converter from UTF-16 to UTF-8. // It is only provided for Windows since other systems support UTF-8 natively. -class UTF16ToUTF8 { +class UTF16ToUTF8 +{ private: MemoryBuffer buffer_; public: UTF16ToUTF8() {} explicit UTF16ToUTF8(WStringRef s); - operator StringRef() const { + operator StringRef() const + { return StringRef(&buffer_[0], size()); } - size_t size() const { + size_t size() const + { return buffer_.size() - 1; } - const char *c_str() const { + const char *c_str() const + { return &buffer_[0]; } - std::string str() const { + std::string str() const + { return std::string(&buffer_[0], size()); } @@ -686,20 +758,24 @@ void format_windows_error(fmt::Writer &out, int error_code, // Computes max(Arg, 1) at compile time. It is used to avoid errors about // allocating an array of 0 size. template -struct NonZero { +struct NonZero +{ enum { VALUE = Arg }; }; template <> -struct NonZero<0> { +struct NonZero<0> +{ enum { VALUE = 1 }; }; // The value of a formatting argument. It is a POD type to allow storage in // internal::MemoryBuffer. -struct Value { +struct Value +{ template - struct StringValue { + struct StringValue + { const Char *value; std::size_t size; }; @@ -707,12 +783,14 @@ struct Value { typedef void(*FormatFunc)( void *formatter, const void *arg, void *format_str_ptr); - struct CustomValue { + struct CustomValue + { const void *value; FormatFunc format; }; - union { + union + { int int_value; unsigned uint_value; LongLong long_long_value; @@ -728,8 +806,10 @@ struct Value { }; }; -struct Arg : Value { - enum Type { +struct Arg : Value +{ + enum Type + { NONE, // Integer types should go first, INT, UINT, LONG_LONG, ULONG_LONG, CHAR, LAST_INTEGER_TYPE = CHAR, @@ -742,7 +822,8 @@ struct Arg : Value { // Makes a Value object from any type. template -class MakeValue : public Value { +class MakeValue : public Value +{ private: // The following two methods are private to disallow formatting of // arbitrary pointers. If you want to output a pointer cast it to @@ -754,12 +835,14 @@ private: template MakeValue(T *value); - void set_string(StringRef str) { + void set_string(StringRef str) + { string.value = str.c_str(); string.size = str.size(); } - void set_string(WStringRef str) { + void set_string(WStringRef str) + { CharTraits::convert(wchar_t()); wstring.value = str.c_str(); wstring.size = str.size(); @@ -768,7 +851,8 @@ private: // Formats an argument of a custom type, such as a user-defined class. template static void format_custom_arg( - void *formatter, const void *arg, void *format_str_ptr) { + void *formatter, const void *arg, void *format_str_ptr) + { format(*static_cast*>(formatter), *static_cast(format_str_ptr), *static_cast(arg)); @@ -787,7 +871,8 @@ public: FMT_MAKE_VALUE(int, int_value, INT) FMT_MAKE_VALUE(unsigned, uint_value, UINT) - MakeValue(long value) { + MakeValue(long value) + { // To minimize the number of types we need to deal with, long is // translated either to int or to long long depending on its size. if (sizeof(long) == sizeof(int)) @@ -795,17 +880,20 @@ public: else long_long_value = value; } - static uint64_t type(long) { + static uint64_t type(long) + { return sizeof(long) == sizeof(int) ? Arg::INT : Arg::LONG_LONG; } - MakeValue(unsigned long value) { + MakeValue(unsigned long value) + { if (sizeof(unsigned long) == sizeof(unsigned)) uint_value = static_cast(value); else ulong_long_value = value; } - static uint64_t type(unsigned long) { + static uint64_t type(unsigned long) + { return sizeof(unsigned long) == sizeof(unsigned) ? Arg::UINT : Arg::ULONG_LONG; } @@ -819,10 +907,12 @@ public: FMT_MAKE_VALUE(unsigned char, int_value, CHAR) FMT_MAKE_VALUE(char, int_value, CHAR) - MakeValue(wchar_t value) { + MakeValue(wchar_t value) + { int_value = internal::CharTraits::convert(value); } - static uint64_t type(wchar_t) { + static uint64_t type(wchar_t) + { return Arg::CHAR; } @@ -846,12 +936,14 @@ public: FMT_MAKE_VALUE(const void *, pointer, POINTER) template - MakeValue(const T &value) { + MakeValue(const T &value) + { custom.value = &value; custom.format = &format_custom_arg; } template - static uint64_t type(const T &) { + static uint64_t type(const T &) + { return Arg::CUSTOM; } }; @@ -879,61 +971,78 @@ public: // ArgVisitor uses the curiously recurring template pattern: // http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern template -class ArgVisitor { +class ArgVisitor +{ public: - Result visit_unhandled_arg() { + Result visit_unhandled_arg() + { return Result(); } - Result visit_int(int value) { + Result visit_int(int value) + { return FMT_DISPATCH(visit_any_int(value)); } - Result visit_long_long(LongLong value) { + Result visit_long_long(LongLong value) + { return FMT_DISPATCH(visit_any_int(value)); } - Result visit_uint(unsigned value) { + Result visit_uint(unsigned value) + { return FMT_DISPATCH(visit_any_int(value)); } - Result visit_ulong_long(ULongLong value) { + Result visit_ulong_long(ULongLong value) + { return FMT_DISPATCH(visit_any_int(value)); } - Result visit_char(int value) { + Result visit_char(int value) + { return FMT_DISPATCH(visit_any_int(value)); } template - Result visit_any_int(T) { + Result visit_any_int(T) + { return FMT_DISPATCH(visit_unhandled_arg()); } - Result visit_double(double value) { + Result visit_double(double value) + { return FMT_DISPATCH(visit_any_double(value)); } - Result visit_long_double(long double value) { + Result visit_long_double(long double value) + { return FMT_DISPATCH(visit_any_double(value)); } template - Result visit_any_double(T) { + Result visit_any_double(T) + { return FMT_DISPATCH(visit_unhandled_arg()); } - Result visit_string(Arg::StringValue) { + Result visit_string(Arg::StringValue) + { return FMT_DISPATCH(visit_unhandled_arg()); } - Result visit_wstring(Arg::StringValue) { + Result visit_wstring(Arg::StringValue) + { return FMT_DISPATCH(visit_unhandled_arg()); } - Result visit_pointer(const void *) { + Result visit_pointer(const void *) + { return FMT_DISPATCH(visit_unhandled_arg()); } - Result visit_custom(Arg::CustomValue) { + Result visit_custom(Arg::CustomValue) + { return FMT_DISPATCH(visit_unhandled_arg()); } - Result visit(const Arg &arg) { - switch (arg.type) { + Result visit(const Arg &arg) + { + switch (arg.type) + { default: assert(false); - // Fall through. + // Fall through. case Arg::INT: return FMT_DISPATCH(visit_int(arg.int_value)); case Arg::UINT: @@ -948,7 +1057,8 @@ public: return FMT_DISPATCH(visit_long_double(arg.long_double_value)); case Arg::CHAR: return FMT_DISPATCH(visit_char(arg.int_value)); - case Arg::CSTRING: { + case Arg::CSTRING: + { Value::StringValue str = arg.string; str.size = 0; return FMT_DISPATCH(visit_string(str)); @@ -965,7 +1075,8 @@ public: } }; -class RuntimeError : public std::runtime_error { +class RuntimeError : public std::runtime_error +{ protected: RuntimeError() : std::runtime_error("") {} }; @@ -977,7 +1088,8 @@ class ArgFormatter; /** An argument list. */ -class ArgList { +class ArgList +{ private: uint64_t types_; const internal::Value *values_; @@ -993,10 +1105,12 @@ public: /** Returns the argument at specified index. */ - internal::Arg operator[](unsigned index) const { + internal::Arg operator[](unsigned index) const + { using internal::Arg; Arg arg; - if (index >= MAX_ARGS) { + if (index >= MAX_ARGS) + { arg.type = Arg::NONE; return arg; } @@ -1005,7 +1119,8 @@ public: Arg::Type type = static_cast((types_ & (mask << shift)) >> shift); arg.type = type; - if (type != Arg::NONE) { + if (type != Arg::NONE) + { internal::Value &value = arg; value = values_[index]; } @@ -1015,9 +1130,11 @@ public: struct FormatSpec; -namespace internal { +namespace internal +{ -class FormatterBase { +class FormatterBase +{ private: ArgList args_; int next_arg_index_; @@ -1026,7 +1143,8 @@ private: Arg do_get_arg(unsigned arg_index, const char *&error); protected: - void set_args(const ArgList &args) { + void set_args(const ArgList &args) + { args_ = args; next_arg_index_ = 0; } @@ -1039,7 +1157,8 @@ protected: Arg get_arg(unsigned arg_index, const char *&error); template - void write(BasicWriter &w, const Char *start, const Char *end) { + void write(BasicWriter &w, const Char *start, const Char *end) + { if (start != end) w << BasicStringRef(start, end - start); } @@ -1047,7 +1166,8 @@ protected: // A printf formatter. template -class PrintfFormatter : private FormatterBase { +class PrintfFormatter : private FormatterBase +{ private: void parse_flags(FormatSpec &spec, const Char *&s); @@ -1067,7 +1187,8 @@ public: // A formatter. template -class BasicFormatter : private internal::FormatterBase { +class BasicFormatter : private internal::FormatterBase +{ private: BasicWriter &writer_; const Char *start_; @@ -1078,7 +1199,8 @@ private: public: explicit BasicFormatter(BasicWriter &w) : writer_(w) {} - BasicWriter &writer() { + BasicWriter &writer() + { return writer_; } @@ -1087,12 +1209,14 @@ public: const Char *format(const Char *&format_str, const internal::Arg &arg); }; -enum Alignment { +enum Alignment +{ ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC }; // Flags. -enum { +enum +{ SIGN_FLAG = 1, PLUS_FLAG = 2, MINUS_FLAG = 4, HASH_FLAG = 8, CHAR_FLAG = 0x10 // Argument has char type - used in error reporting. }; @@ -1102,29 +1226,37 @@ struct EmptySpec {}; // A type specifier. template -struct TypeSpec : EmptySpec { - Alignment align() const { +struct TypeSpec : EmptySpec +{ + Alignment align() const + { return ALIGN_DEFAULT; } - unsigned width() const { + unsigned width() const + { return 0; } - int precision() const { + int precision() const + { return -1; } - bool flag(unsigned) const { + bool flag(unsigned) const + { return false; } - char type() const { + char type() const + { return TYPE; } - char fill() const { + char fill() const + { return ' '; } }; // A width specifier. -struct WidthSpec { +struct WidthSpec +{ unsigned width_; // Fill is always wchar_t and cast to char if necessary to avoid having // two specialization of WidthSpec and its subclasses. @@ -1132,45 +1264,54 @@ struct WidthSpec { WidthSpec(unsigned width, wchar_t fill) : width_(width), fill_(fill) {} - unsigned width() const { + unsigned width() const + { return width_; } - wchar_t fill() const { + wchar_t fill() const + { return fill_; } }; // An alignment specifier. -struct AlignSpec : WidthSpec { +struct AlignSpec : WidthSpec +{ Alignment align_; AlignSpec(unsigned width, wchar_t fill, Alignment align = ALIGN_DEFAULT) : WidthSpec(width, fill), align_(align) {} - Alignment align() const { + Alignment align() const + { return align_; } - int precision() const { + int precision() const + { return -1; } }; // An alignment and type specifier. template -struct AlignTypeSpec : AlignSpec { +struct AlignTypeSpec : AlignSpec +{ AlignTypeSpec(unsigned width, wchar_t fill) : AlignSpec(width, fill) {} - bool flag(unsigned) const { + bool flag(unsigned) const + { return false; } - char type() const { + char type() const + { return TYPE; } }; // A full format specifier. -struct FormatSpec : AlignSpec { +struct FormatSpec : AlignSpec +{ unsigned flags_; int precision_; char type_; @@ -1179,20 +1320,24 @@ struct FormatSpec : AlignSpec { unsigned width = 0, char type = 0, wchar_t fill = ' ') : AlignSpec(width, fill), flags_(0), precision_(-1), type_(type) {} - bool flag(unsigned f) const { + bool flag(unsigned f) const + { return (flags_ & f) != 0; } - int precision() const { + int precision() const + { return precision_; } - char type() const { + char type() const + { return type_; } }; // An integer format specifier. template , typename Char = char> -class IntFormatSpec : public SpecT { +class IntFormatSpec : public SpecT +{ private: T value_; @@ -1200,14 +1345,16 @@ public: IntFormatSpec(T value, const SpecT &spec = SpecT()) : SpecT(spec), value_(value) {} - T value() const { + T value() const + { return value_; } }; // A string format specifier. template -class StrFormatSpec : public AlignSpec { +class StrFormatSpec : public AlignSpec +{ private: const T *str_; @@ -1215,7 +1362,8 @@ public: StrFormatSpec(const T *str, unsigned width, wchar_t fill) : AlignSpec(width, fill), str_(str) {} - const T *str() const { + const T *str() const + { return str_; } }; @@ -1330,12 +1478,14 @@ std::string s = str(MemoryWriter() << pad("abc", 8)); */ template inline StrFormatSpec pad( - const Char *str, unsigned width, Char fill = ' ') { + const Char *str, unsigned width, Char fill = ' ') +{ return StrFormatSpec(str, width, fill); } inline StrFormatSpec pad( - const wchar_t *str, unsigned width, char fill = ' ') { + const wchar_t *str, unsigned width, char fill = ' ') +{ return StrFormatSpec(str, width, fill); } @@ -1358,24 +1508,29 @@ inline StrFormatSpec pad( # define FMT_GEN14(f) FMT_GEN13(f), f(13) # define FMT_GEN15(f) FMT_GEN14(f), f(14) -namespace internal { -inline uint64_t make_type() { +namespace internal +{ +inline uint64_t make_type() +{ return 0; } template -inline uint64_t make_type(const T &arg) { +inline uint64_t make_type(const T &arg) +{ return MakeValue::type(arg); } #if FMT_USE_VARIADIC_TEMPLATES template -inline uint64_t make_type(const Arg &first, const Args & ... tail) { +inline uint64_t make_type(const Arg &first, const Args & ... tail) +{ return make_type(first) | (make_type(tail...) << 4); } #else -struct ArgType { +struct ArgType +{ uint64_t type; ArgType() : type(0) {} @@ -1386,7 +1541,8 @@ struct ArgType { # define FMT_ARG_TYPE_DEFAULT(n) ArgType t##n = ArgType() -inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { +inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) +{ return t0.type | (t1.type << 4) | (t2.type << 8) | (t3.type << 12) | (t4.type << 16) | (t5.type << 20) | (t6.type << 24) | (t7.type << 28) | (t8.type << 32) | (t9.type << 36) | (t10.type << 40) | (t11.type << 44) | @@ -1496,7 +1652,8 @@ inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { An error returned by an operating system or a language runtime, for example a file opening error. */ -class SystemError : public internal::RuntimeError { +class SystemError : public internal::RuntimeError +{ private: void init(int error_code, StringRef format_str, ArgList args); @@ -1517,12 +1674,14 @@ public: *error_code* is a system error code as given by ``errno``. \endrst */ - SystemError(int error_code, StringRef message) { + SystemError(int error_code, StringRef message) + { init(error_code, message, ArgList()); } FMT_VARIADIC_CTOR(SystemError, init, int, StringRef) - int error_code() const { + int error_code() const + { return error_code_; } }; @@ -1546,7 +1705,8 @@ You can use one of the following typedefs for common character types: \endrst */ template -class BasicWriter { +class BasicWriter +{ private: // Output buffer. internal::Buffer &buffer_; @@ -1557,11 +1717,13 @@ private: #if _SECURE_SCL // Returns pointer value. - static Char *get(CharPtr p) { + static Char *get(CharPtr p) + { return p.base(); } #else - static Char *get(Char *p) { + static Char *get(Char *p) + { return p; } #endif @@ -1573,7 +1735,8 @@ private: // Grows the buffer by n characters and returns a pointer to the newly // allocated area. - CharPtr grow_buffer(std::size_t n) { + CharPtr grow_buffer(std::size_t n) + { std::size_t size = buffer_.size(); buffer_.resize(size + n); return internal::make_ptr(&buffer_[size], n); @@ -1581,7 +1744,8 @@ private: // Prepare a buffer for integer formatting. CharPtr prepare_int_buffer(unsigned num_digits, - const EmptySpec &, const char *prefix, unsigned prefix_size) { + const EmptySpec &, const char *prefix, unsigned prefix_size) + { unsigned size = prefix_size + num_digits; CharPtr p = grow_buffer(size); std::copy(prefix, prefix + prefix_size, p); @@ -1633,7 +1797,8 @@ public: /** Returns the total number of characters written. */ - std::size_t size() const { + std::size_t size() const + { return buffer_.size(); } @@ -1641,7 +1806,8 @@ public: Returns a pointer to the output buffer content. No terminating null character is appended. */ - const Char *data() const FMT_NOEXCEPT(true) { + const Char *data() const FMT_NOEXCEPT(true) + { return &buffer_[0]; } @@ -1649,7 +1815,8 @@ public: Returns a pointer to the output buffer content with terminating null character appended. */ - const Char *c_str() const { + const Char *c_str() const + { std::size_t size = buffer_.size(); buffer_.reserve(size + 1); buffer_[size] = '\0'; @@ -1659,7 +1826,8 @@ public: /** Returns the content of the output buffer as an `std::string`. */ - std::basic_string str() const { + std::basic_string str() const + { return std::basic_string(&buffer_[0], buffer_.size()); } @@ -1688,35 +1856,43 @@ public: See also :ref:`syntax`. \endrst */ - void write(BasicStringRef format, ArgList args) { + void write(BasicStringRef format, ArgList args) + { BasicFormatter(*this).format(format, args); } FMT_VARIADIC_VOID(write, BasicStringRef) - BasicWriter &operator<<(int value) { + BasicWriter &operator<<(int value) + { return *this << IntFormatSpec(value); } - BasicWriter &operator<<(unsigned value) { + BasicWriter &operator<<(unsigned value) + { return *this << IntFormatSpec(value); } - BasicWriter &operator<<(long value) { + BasicWriter &operator<<(long value) + { return *this << IntFormatSpec(value); } - BasicWriter &operator<<(unsigned long value) { + BasicWriter &operator<<(unsigned long value) + { return *this << IntFormatSpec(value); } - BasicWriter &operator<<(LongLong value) { + BasicWriter &operator<<(LongLong value) + { return *this << IntFormatSpec(value); } /** Formats *value* and writes it to the stream. */ - BasicWriter &operator<<(ULongLong value) { + BasicWriter &operator<<(ULongLong value) + { return *this << IntFormatSpec(value); } - BasicWriter &operator<<(double value) { + BasicWriter &operator<<(double value) + { write_double(value, FormatSpec()); return *this; } @@ -1725,7 +1901,8 @@ public: Formats *value* using the general format for floating-point numbers (``'g'``) and writes it to the stream. */ - BasicWriter &operator<<(long double value) { + BasicWriter &operator<<(long double value) + { write_double(value, FormatSpec()); return *this; } @@ -1733,12 +1910,14 @@ public: /** Writes a character to the stream. */ - BasicWriter &operator<<(char value) { + BasicWriter &operator<<(char value) + { buffer_.push_back(value); return *this; } - BasicWriter &operator<<(wchar_t value) { + BasicWriter &operator<<(wchar_t value) + { buffer_.push_back(internal::CharTraits::convert(value)); return *this; } @@ -1746,28 +1925,32 @@ public: /** Writes *value* to the stream. */ - BasicWriter &operator<<(fmt::BasicStringRef value) { + BasicWriter &operator<<(fmt::BasicStringRef value) + { const Char *str = value.c_str(); buffer_.append(str, str + value.size()); return *this; } template - BasicWriter &operator<<(IntFormatSpec spec) { + BasicWriter &operator<<(IntFormatSpec spec) + { internal::CharTraits::convert(FillChar()); write_int(spec.value(), spec); return *this; } template - BasicWriter &operator<<(const StrFormatSpec &spec) { + BasicWriter &operator<<(const StrFormatSpec &spec) + { const StrChar *s = spec.str(); // TODO: error if fill is not convertible to Char write_str(s, std::char_traits::length(s), spec); return *this; } - void clear() FMT_NOEXCEPT(true) { + void clear() FMT_NOEXCEPT(true) + { buffer_.clear(); } }; @@ -1775,23 +1958,29 @@ public: template template typename BasicWriter::CharPtr BasicWriter::write_str( - const StrChar *s, std::size_t size, const AlignSpec &spec) { + const StrChar *s, std::size_t size, const AlignSpec &spec) +{ CharPtr out = CharPtr(); - if (spec.width() > size) { + if (spec.width() > size) + { out = grow_buffer(spec.width()); Char fill = static_cast(spec.fill()); - if (spec.align() == ALIGN_RIGHT) { + if (spec.align() == ALIGN_RIGHT) + { std::fill_n(out, spec.width() - size, fill); out += spec.width() - size; } - else if (spec.align() == ALIGN_CENTER) { + else if (spec.align() == ALIGN_CENTER) + { out = fill_padding(out, spec.width(), size, fill); } - else { + else + { std::fill_n(out + size, spec.width() - size, fill); } } - else { + else + { out = grow_buffer(size); } std::copy(s, s + size, out); @@ -1802,7 +1991,8 @@ template typename BasicWriter::CharPtr BasicWriter::fill_padding( CharPtr buffer, unsigned total_size, - std::size_t content_size, wchar_t fill) { + std::size_t content_size, wchar_t fill) +{ std::size_t padding = total_size - content_size; std::size_t left_padding = padding / 2; Char fill_char = static_cast(fill); @@ -1818,11 +2008,13 @@ template typename BasicWriter::CharPtr BasicWriter::prepare_int_buffer( unsigned num_digits, const Spec &spec, - const char *prefix, unsigned prefix_size) { + const char *prefix, unsigned prefix_size) +{ unsigned width = spec.width(); Alignment align = spec.align(); Char fill = static_cast(spec.fill()); - if (spec.precision() > static_cast(num_digits)) { + if (spec.precision() > static_cast(num_digits)) + { // Octal prefix '0' is counted as a digit, so ignore it if precision // is specified. if (prefix_size > 0 && prefix[prefix_size - 1] == '0') @@ -1833,44 +2025,53 @@ BasicWriter::prepare_int_buffer( return prepare_int_buffer(num_digits, subspec, prefix, prefix_size); buffer_.reserve(width); unsigned fill_size = width - number_size; - if (align != ALIGN_LEFT) { + if (align != ALIGN_LEFT) + { CharPtr p = grow_buffer(fill_size); std::fill(p, p + fill_size, fill); } CharPtr result = prepare_int_buffer( num_digits, subspec, prefix, prefix_size); - if (align == ALIGN_LEFT) { + if (align == ALIGN_LEFT) + { CharPtr p = grow_buffer(fill_size); std::fill(p, p + fill_size, fill); } return result; } unsigned size = prefix_size + num_digits; - if (width <= size) { + if (width <= size) + { CharPtr p = grow_buffer(size); std::copy(prefix, prefix + prefix_size, p); return p + size - 1; } CharPtr p = grow_buffer(width); CharPtr end = p + width; - if (align == ALIGN_LEFT) { + if (align == ALIGN_LEFT) + { std::copy(prefix, prefix + prefix_size, p); p += size; std::fill(p, end, fill); } - else if (align == ALIGN_CENTER) { + else if (align == ALIGN_CENTER) + { p = fill_padding(p, width, size, fill); std::copy(prefix, prefix + prefix_size, p); p += size; } - else { - if (align == ALIGN_NUMERIC) { - if (prefix_size != 0) { + else + { + if (align == ALIGN_NUMERIC) + { + if (prefix_size != 0) + { p = std::copy(prefix, prefix + prefix_size, p); size -= prefix_size; } } - else { + else + { std::copy(prefix, prefix + prefix_size, end - size); } std::fill(p, end - size, fill); @@ -1881,23 +2082,28 @@ BasicWriter::prepare_int_buffer( template template -void BasicWriter::write_int(T value, Spec spec) { +void BasicWriter::write_int(T value, Spec spec) +{ unsigned prefix_size = 0; typedef typename internal::IntTraits::MainType UnsignedType; UnsignedType abs_value = value; char prefix[4] = ""; - if (internal::is_negative(value)) { + if (internal::is_negative(value)) + { prefix[0] = '-'; ++prefix_size; abs_value = 0 - abs_value; } - else if (spec.flag(SIGN_FLAG)) { + else if (spec.flag(SIGN_FLAG)) + { prefix[0] = spec.flag(PLUS_FLAG) ? '+' : ' '; ++prefix_size; } - switch (spec.type()) { + switch (spec.type()) + { case 0: - case 'd': { + case 'd': + { unsigned num_digits = internal::count_digits(abs_value); CharPtr p = prepare_int_buffer( num_digits, spec, prefix, prefix_size) + 1 - num_digits; @@ -1905,57 +2111,74 @@ void BasicWriter::write_int(T value, Spec spec) { break; } case 'x': - case 'X': { + case 'X': + { UnsignedType n = abs_value; - if (spec.flag(HASH_FLAG)) { + if (spec.flag(HASH_FLAG)) + { prefix[prefix_size++] = '0'; prefix[prefix_size++] = spec.type(); } unsigned num_digits = 0; - do { + do + { ++num_digits; - } while ((n >>= 4) != 0); + } + while ((n >>= 4) != 0); Char *p = get(prepare_int_buffer( num_digits, spec, prefix, prefix_size)); n = abs_value; const char *digits = spec.type() == 'x' ? "0123456789abcdef" : "0123456789ABCDEF"; - do { + do + { *p-- = digits[n & 0xf]; - } while ((n >>= 4) != 0); + } + while ((n >>= 4) != 0); break; } case 'b': - case 'B': { + case 'B': + { UnsignedType n = abs_value; - if (spec.flag(HASH_FLAG)) { + if (spec.flag(HASH_FLAG)) + { prefix[prefix_size++] = '0'; prefix[prefix_size++] = spec.type(); } unsigned num_digits = 0; - do { + do + { ++num_digits; - } while ((n >>= 1) != 0); + } + while ((n >>= 1) != 0); Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); n = abs_value; - do { + do + { *p-- = '0' + (n & 1); - } while ((n >>= 1) != 0); + } + while ((n >>= 1) != 0); break; } - case 'o': { + case 'o': + { UnsignedType n = abs_value; if (spec.flag(HASH_FLAG)) prefix[prefix_size++] = '0'; unsigned num_digits = 0; - do { + do + { ++num_digits; - } while ((n >>= 3) != 0); + } + while ((n >>= 3) != 0); Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size)); n = abs_value; - do { + do + { *p-- = '0' + (n & 7); - } while ((n >>= 3) != 0); + } + while ((n >>= 3) != 0); break; } default: @@ -1968,11 +2191,13 @@ void BasicWriter::write_int(T value, Spec spec) { template template void BasicWriter::write_double( - T value, const FormatSpec &spec) { + T value, const FormatSpec &spec) +{ // Check type. char type = spec.type(); bool upper = false; - switch (type) { + switch (type) + { case 0: type = 'g'; break; @@ -1986,7 +2211,7 @@ void BasicWriter::write_double( // MSVC's printf doesn't support 'F'. type = 'f'; #endif - // Fall through. + // Fall through. case 'E': case 'G': case 'A': @@ -2000,20 +2225,24 @@ void BasicWriter::write_double( char sign = 0; // Use getsign instead of value < 0 because the latter is always // false for NaN. - if (internal::getsign(static_cast(value))) { + if (internal::getsign(static_cast(value))) + { sign = '-'; value = -value; } - else if (spec.flag(SIGN_FLAG)) { + else if (spec.flag(SIGN_FLAG)) + { sign = spec.flag(PLUS_FLAG) ? '+' : ' '; } - if (value != value) { + if (value != value) + { // Format NaN ourselves because sprintf's output is not consistent // across platforms. std::size_t size = 4; const char *nan = upper ? " NAN" : " nan"; - if (!sign) { + if (!sign) + { --size; ++nan; } @@ -2023,12 +2252,14 @@ void BasicWriter::write_double( return; } - if (internal::isinfinity(value)) { + if (internal::isinfinity(value)) + { // Format infinity ourselves because sprintf's output is not consistent // across platforms. std::size_t size = 4; const char *inf = upper ? " INF" : " inf"; - if (!sign) { + if (!sign) + { --size; ++inf; } @@ -2040,7 +2271,8 @@ void BasicWriter::write_double( std::size_t offset = buffer_.size(); unsigned width = spec.width(); - if (sign) { + if (sign) + { buffer_.reserve(buffer_.size() + (std::max)(width, 1u)); if (width > 0) --width; @@ -2055,16 +2287,19 @@ void BasicWriter::write_double( unsigned width_for_sprintf = width; if (spec.flag(HASH_FLAG)) *format_ptr++ = '#'; - if (spec.align() == ALIGN_CENTER) { + if (spec.align() == ALIGN_CENTER) + { width_for_sprintf = 0; } - else { + else + { if (spec.align() == ALIGN_LEFT) *format_ptr++ = '-'; if (width != 0) *format_ptr++ = '*'; } - if (spec.precision() >= 0) { + if (spec.precision() >= 0) + { *format_ptr++ = '.'; *format_ptr++ = '*'; } @@ -2075,13 +2310,15 @@ void BasicWriter::write_double( // Format using snprintf. Char fill = static_cast(spec.fill()); - for (;;) { + for (;;) + { std::size_t size = buffer_.capacity() - offset; #if _MSC_VER // MSVC's vsnprintf_s doesn't work with zero size, so reserve // space for at least one extra character to make the size non-zero. // Note that the buffer's capacity will increase by more than 1. - if (size == 0) { + if (size == 0) + { buffer_.reserve(offset + 1); size = buffer_.capacity() - offset; } @@ -2089,27 +2326,33 @@ void BasicWriter::write_double( Char *start = &buffer_[offset]; int n = internal::CharTraits::format_float( start, size, format, width_for_sprintf, spec.precision(), value); - if (n >= 0 && offset + n < buffer_.capacity()) { - if (sign) { + if (n >= 0 && offset + n < buffer_.capacity()) + { + if (sign) + { if ((spec.align() != ALIGN_RIGHT && spec.align() != ALIGN_DEFAULT) || - *start != ' ') { + *start != ' ') + { *(start - 1) = sign; sign = 0; } - else { + else + { *(start - 1) = fill; } ++n; } if (spec.align() == ALIGN_CENTER && - spec.width() > static_cast(n)) { + spec.width() > static_cast(n)) + { unsigned width = spec.width(); CharPtr p = grow_buffer(width); std::copy(p, p + n, p + (width - n) / 2); fill_padding(p, spec.width(), n, fill); return; } - if (spec.fill() != ' ' || sign) { + if (spec.fill() != ' ' || sign) + { while (*start == ' ') *start++ = fill; if (sign) @@ -2159,7 +2402,8 @@ accessed as a C string with ``out.c_str()``. \endrst */ template > -class BasicMemoryWriter : public BasicWriter { +class BasicMemoryWriter : public BasicWriter +{ private: internal::MemoryBuffer buffer_; @@ -2173,13 +2417,15 @@ public: object to it. */ BasicMemoryWriter(BasicMemoryWriter &&other) - : BasicWriter(buffer_), buffer_(std::move(other.buffer_)) { + : BasicWriter(buffer_), buffer_(std::move(other.buffer_)) + { } /** Moves the content of the other ``BasicMemoryWriter`` object to this one. */ - BasicMemoryWriter &operator=(BasicMemoryWriter &&other) { + BasicMemoryWriter &operator=(BasicMemoryWriter &&other) + { buffer_ = std::move(other.buffer_); return *this; } @@ -2191,7 +2437,8 @@ typedef BasicMemoryWriter WMemoryWriter; // Formats a value. template -void format(BasicFormatter &f, const Char *&format_str, const T &value) { +void format(BasicFormatter &f, const Char *&format_str, const T &value) +{ std::basic_ostringstream os; os << value; internal::Arg arg; @@ -2211,7 +2458,8 @@ void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT(true); /** A Windows error. */ -class WindowsError : public SystemError { +class WindowsError : public SystemError +{ private: void init(int error_code, StringRef format_str, ArgList args); @@ -2225,7 +2473,8 @@ public: *error_code* is a Windows error code as given by ``GetLastError``. \endrst */ - WindowsError(int error_code, StringRef message) { + WindowsError(int error_code, StringRef message) + { init(error_code, message, ArgList()); } FMT_VARIADIC_CTOR(WindowsError, init, int, StringRef) @@ -2256,13 +2505,15 @@ Formats arguments and returns the result as a string. std::string message = format("The answer is {}", 42); \endrst */ -inline std::string format(StringRef format_str, ArgList args) { +inline std::string format(StringRef format_str, ArgList args) +{ MemoryWriter w; w.write(format_str, args); return w.str(); } -inline std::wstring format(WStringRef format_str, ArgList args) { +inline std::wstring format(WStringRef format_str, ArgList args) +{ WMemoryWriter w; w.write(format_str, args); return w.str(); @@ -2302,7 +2553,8 @@ print(cerr, "Don't {}!", "panic"); void print(std::ostream &os, StringRef format_str, ArgList args); template -void printf(BasicWriter &w, BasicStringRef format, ArgList args) { +void printf(BasicWriter &w, BasicStringRef format, ArgList args) +{ internal::PrintfFormatter().format(w, format, args); } @@ -2315,7 +2567,8 @@ Formats arguments and returns the result as a string. std::string message = fmt::sprintf("The answer is %d", 42); \endrst */ -inline std::string sprintf(StringRef format, ArgList args) { +inline std::string sprintf(StringRef format, ArgList args) +{ MemoryWriter w; printf(w, format, args); return w.str(); @@ -2341,14 +2594,16 @@ Prints formatted data to ``stdout``. fmt::printf("Elapsed time: %.2f seconds", 1.23); \endrst */ -inline int printf(StringRef format, ArgList args) { +inline int printf(StringRef format, ArgList args) +{ return fprintf(stdout, format, args); } /** Fast integer formatter. */ -class FormatInt { +class FormatInt +{ private: // Buffer should be large enough to hold all digits (digits10 + 1), // a sign and a null character. @@ -2357,9 +2612,11 @@ private: char *str_; // Formats value in reverse and returns the number of digits. - char *format_decimal(ULongLong value) { + char *format_decimal(ULongLong value) + { char *buffer_end = buffer_ + BUFFER_SIZE - 1; - while (value >= 100) { + while (value >= 100) + { // Integer division is slow so do it for a group of two digits instead // of for every digit. The idea comes from the talk by Alexandrescu // "Three Optimization Tips for C++". See speed-test for a comparison. @@ -2368,7 +2625,8 @@ private: *--buffer_end = internal::DIGITS[index + 1]; *--buffer_end = internal::DIGITS[index]; } - if (value < 10) { + if (value < 10) + { *--buffer_end = static_cast('0' + value); return buffer_end; } @@ -2378,7 +2636,8 @@ private: return buffer_end; } - void FormatSigned(LongLong value) { + void FormatSigned(LongLong value) + { ULongLong abs_value = static_cast(value); bool negative = value < 0; if (negative) @@ -2389,13 +2648,16 @@ private: } public: - explicit FormatInt(int value) { + explicit FormatInt(int value) + { FormatSigned(value); } - explicit FormatInt(long value) { + explicit FormatInt(long value) + { FormatSigned(value); } - explicit FormatInt(LongLong value) { + explicit FormatInt(LongLong value) + { FormatSigned(value); } explicit FormatInt(unsigned value) : str_(format_decimal(value)) {} @@ -2405,7 +2667,8 @@ public: /** Returns the number of characters written to the output buffer. */ - std::size_t size() const { + std::size_t size() const + { return buffer_ - str_ + BUFFER_SIZE - 1; } @@ -2413,7 +2676,8 @@ public: Returns a pointer to the output buffer content. No terminating null character is appended. */ - const char *data() const { + const char *data() const + { return str_; } @@ -2421,7 +2685,8 @@ public: Returns a pointer to the output buffer content with terminating null character appended. */ - const char *c_str() const { + const char *c_str() const + { buffer_[BUFFER_SIZE - 1] = '\0'; return str_; } @@ -2429,7 +2694,8 @@ public: /** Returns the content of the output buffer as an `std::string`. */ - std::string str() const { + std::string str() const + { return std::string(str_, size()); } }; @@ -2438,14 +2704,18 @@ public: // a pointer to the end of the formatted string. This function doesn't // write a terminating null character. template -inline void format_decimal(char *&buffer, T value) { +inline void format_decimal(char *&buffer, T value) +{ typename internal::IntTraits::MainType abs_value = value; - if (internal::is_negative(value)) { + if (internal::is_negative(value)) + { *buffer++ = '-'; abs_value = 0 - abs_value; } - if (abs_value < 100) { - if (abs_value < 10) { + if (abs_value < 100) + { + if (abs_value < 10) + { *buffer++ = static_cast('0' + abs_value); return; } @@ -2565,7 +2835,8 @@ fmt::print(format, args...); #define FMT_VARIADIC_W(ReturnType, func, ...) \ FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__) -namespace fmt { +namespace fmt +{ FMT_VARIADIC(std::string, format, StringRef) FMT_VARIADIC_W(std::wstring, format, WStringRef) FMT_VARIADIC(void, print, StringRef) diff --git a/include/spdlog/details/mpcs_q.h b/include/spdlog/details/mpcs_q.h index acc62a75..058d6ee4 100644 --- a/include/spdlog/details/mpcs_q.h +++ b/include/spdlog/details/mpcs_q.h @@ -61,8 +61,10 @@ should not be interpreted as representing official policies, either expressed or #include -namespace spdlog { -namespace details { +namespace spdlog +{ +namespace details +{ template class mpsc_q { diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 69640a7b..aa0c4e8d 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -429,7 +429,7 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) { switch (flag) { - // logger name + // logger name case 'n': _formatters.push_back(std::unique_ptr(new details::name_formatter())); break;