Commit 93be7713 authored by gabime's avatar gabime

astyle

parent 6ab2f0e0
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -13,57 +13,65 @@ ...@@ -13,57 +13,65 @@
#include "format.h" #include "format.h"
#include <ostream> #include <ostream>
namespace fmt { namespace fmt
{
namespace internal { namespace internal
{
template <class Char> template <class Char>
class FormatBuf : public std::basic_streambuf<Char> { class FormatBuf : public std::basic_streambuf<Char>
private: {
typedef typename std::basic_streambuf<Char>::int_type int_type; private:
typedef typename std::basic_streambuf<Char>::traits_type traits_type; typedef typename std::basic_streambuf<Char>::int_type int_type;
typedef typename std::basic_streambuf<Char>::traits_type traits_type;
Buffer<Char> &buffer_;
Buffer<Char> &buffer_;
public:
FormatBuf(Buffer<Char> &buffer) : buffer_(buffer) {} public:
FormatBuf(Buffer<Char> &buffer) : buffer_(buffer) {}
protected:
// The put-area is actually always empty. This makes the implementation protected:
// simpler and has the advantage that the streambuf and the buffer are always // The put-area is actually always empty. This makes the implementation
// in sync and sputc never writes into uninitialized memory. The obvious // simpler and has the advantage that the streambuf and the buffer are always
// disadvantage is that each call to sputc always results in a (virtual) call // in sync and sputc never writes into uninitialized memory. The obvious
// to overflow. There is no disadvantage here for sputn since this always // disadvantage is that each call to sputc always results in a (virtual) call
// results in a call to xsputn. // to overflow. There is no disadvantage here for sputn since this always
// results in a call to xsputn.
int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE {
if (!traits_type::eq_int_type(ch, traits_type::eof())) int_type overflow(int_type ch = traits_type::eof()) FMT_OVERRIDE
buffer_.push_back(static_cast<Char>(ch)); {
return ch; if (!traits_type::eq_int_type(ch, traits_type::eof()))
} buffer_.push_back(static_cast<Char>(ch));
return ch;
std::streamsize xsputn(const Char *s, std::streamsize count) FMT_OVERRIDE { }
buffer_.append(s, s + count);
return count; std::streamsize xsputn(const Char *s, std::streamsize count) FMT_OVERRIDE
} {
buffer_.append(s, s + count);
return count;
}
}; };
Yes &convert(std::ostream &); Yes &convert(std::ostream &);
struct DummyStream : std::ostream { struct DummyStream : std::ostream
DummyStream(); // Suppress a bogus warning in MSVC. {
// Hide all operator<< overloads from std::ostream. DummyStream(); // Suppress a bogus warning in MSVC.
void operator<<(Null<>); // Hide all operator<< overloads from std::ostream.
void operator<<(Null<>);
}; };
No &operator<<(std::ostream &, int); No &operator<<(std::ostream &, int);
template<typename T> template<typename T>
struct ConvertToIntImpl<T, true> { struct ConvertToIntImpl<T, true>
// Convert to int only if T doesn't have an overloaded operator<<. {
enum { // Convert to int only if T doesn't have an overloaded operator<<.
value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No) enum
}; {
value = sizeof(convert(get<DummyStream>() << get<T>())) == sizeof(No)
};
}; };
// Write the content of w to os. // Write the content of w to os.
...@@ -73,16 +81,17 @@ FMT_API void write(std::ostream &os, Writer &w); ...@@ -73,16 +81,17 @@ FMT_API void write(std::ostream &os, Writer &w);
// Formats a value. // Formats a value.
template <typename Char, typename ArgFormatter_, typename T> template <typename Char, typename ArgFormatter_, typename T>
void format_arg(BasicFormatter<Char, ArgFormatter_> &f, void format_arg(BasicFormatter<Char, ArgFormatter_> &f,
const Char *&format_str, const T &value) { const Char *&format_str, const T &value)
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; {
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
internal::FormatBuf<Char> format_buf(buffer); internal::FormatBuf<Char> format_buf(buffer);
std::basic_ostream<Char> output(&format_buf); std::basic_ostream<Char> output(&format_buf);
output << value; output << value;
BasicStringRef<Char> str(&buffer[0], buffer.size()); BasicStringRef<Char> str(&buffer[0], buffer.size());
typedef internal::MakeArg< BasicFormatter<Char> > MakeArg; typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
format_str = f.format(format_str, MakeArg(str)); format_str = f.format(format_str, MakeArg(str));
} }
/** /**
......
This diff is collapsed.
This diff is collapsed.
...@@ -19,120 +19,160 @@ ...@@ -19,120 +19,160 @@
# pragma warning(disable: 4996) // "deprecated" functions # pragma warning(disable: 4996) // "deprecated" functions
#endif #endif
namespace fmt { namespace fmt
{
template <typename ArgFormatter> template <typename ArgFormatter>
void format_arg(BasicFormatter<char, ArgFormatter> &f, void format_arg(BasicFormatter<char, ArgFormatter> &f,
const char *&format_str, const std::tm &tm) { const char *&format_str, const std::tm &tm)
if (*format_str == ':') {
++format_str; if (*format_str == ':')
const char *end = format_str; ++format_str;
while (*end && *end != '}') const char *end = format_str;
++end; while (*end && *end != '}')
if (*end != '}') ++end;
FMT_THROW(FormatError("missing '}' in format string")); if (*end != '}')
internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> format; FMT_THROW(FormatError("missing '}' in format string"));
format.append(format_str, end + 1); internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> format;
format[format.size() - 1] = '\0'; format.append(format_str, end + 1);
Buffer<char> &buffer = f.writer().buffer(); format[format.size() - 1] = '\0';
std::size_t start = buffer.size(); Buffer<char> &buffer = f.writer().buffer();
for (;;) { std::size_t start = buffer.size();
std::size_t size = buffer.capacity() - start; for (;;)
std::size_t count = std::strftime(&buffer[start], size, &format[0], &tm); {
if (count != 0) { std::size_t size = buffer.capacity() - start;
buffer.resize(start + count); std::size_t count = std::strftime(&buffer[start], size, &format[0], &tm);
break; if (count != 0)
{
buffer.resize(start + count);
break;
}
if (size >= format.size() * 256)
{
// If the buffer is 256 times larger than the format string, assume
// that `strftime` gives an empty result. There doesn't seem to be a
// better way to distinguish the two cases:
// https://github.com/fmtlib/fmt/issues/367
break;
}
const std::size_t MIN_GROWTH = 10;
buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
} }
if (size >= format.size() * 256) { format_str = end + 1;
// If the buffer is 256 times larger than the format string, assume
// that `strftime` gives an empty result. There doesn't seem to be a
// better way to distinguish the two cases:
// https://github.com/fmtlib/fmt/issues/367
break;
}
const std::size_t MIN_GROWTH = 10;
buffer.reserve(buffer.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
}
format_str = end + 1;
} }
namespace internal{ namespace internal
inline Null<> localtime_r(...) { return Null<>(); } {
inline Null<> localtime_s(...) { return Null<>(); } inline Null<> localtime_r(...)
inline Null<> gmtime_r(...) { return Null<>(); } {
inline Null<> gmtime_s(...) { return Null<>(); } return Null<>();
}
inline Null<> localtime_s(...)
{
return Null<>();
}
inline Null<> gmtime_r(...)
{
return Null<>();
}
inline Null<> gmtime_s(...)
{
return Null<>();
}
} }
// Thread-safe replacement for std::localtime // Thread-safe replacement for std::localtime
inline std::tm localtime(std::time_t time) { inline std::tm localtime(std::time_t time)
struct LocalTime { {
std::time_t time_; struct LocalTime
std::tm tm_; {
std::time_t time_;
LocalTime(std::time_t t): time_(t) {} std::tm tm_;
bool run() { LocalTime(std::time_t t): time_(t) {}
using namespace fmt::internal;
return handle(localtime_r(&time_, &tm_)); bool run()
} {
using namespace fmt::internal;
bool handle(std::tm *tm) { return tm != FMT_NULL; } return handle(localtime_r(&time_, &tm_));
}
bool handle(internal::Null<>) {
using namespace fmt::internal; bool handle(std::tm *tm)
return fallback(localtime_s(&tm_, &time_)); {
} return tm != FMT_NULL;
}
bool fallback(int res) { return res == 0; }
bool handle(internal::Null<>)
bool fallback(internal::Null<>) { {
using namespace fmt::internal; using namespace fmt::internal;
std::tm *tm = std::localtime(&time_); return fallback(localtime_s(&tm_, &time_));
if (tm) tm_ = *tm; }
return tm != FMT_NULL;
} bool fallback(int res)
}; {
LocalTime lt(time); return res == 0;
if (lt.run()) }
return lt.tm_;
// Too big time values may be unsupported. bool fallback(internal::Null<>)
FMT_THROW(fmt::FormatError("time_t value out of range")); {
return std::tm(); using namespace fmt::internal;
std::tm *tm = std::localtime(&time_);
if (tm) tm_ = *tm;
return tm != FMT_NULL;
}
};
LocalTime lt(time);
if (lt.run())
return lt.tm_;
// Too big time values may be unsupported.
FMT_THROW(fmt::FormatError("time_t value out of range"));
return std::tm();
} }
// Thread-safe replacement for std::gmtime // Thread-safe replacement for std::gmtime
inline std::tm gmtime(std::time_t time) { inline std::tm gmtime(std::time_t time)
struct GMTime { {
std::time_t time_; struct GMTime
std::tm tm_; {
std::time_t time_;
GMTime(std::time_t t): time_(t) {} std::tm tm_;
bool run() { GMTime(std::time_t t): time_(t) {}
using namespace fmt::internal;
return handle(gmtime_r(&time_, &tm_)); bool run()
} {
using namespace fmt::internal;
bool handle(std::tm *tm) { return tm != FMT_NULL; } return handle(gmtime_r(&time_, &tm_));
}
bool handle(internal::Null<>) {
using namespace fmt::internal; bool handle(std::tm *tm)
return fallback(gmtime_s(&tm_, &time_)); {
} return tm != FMT_NULL;
}
bool fallback(int res) { return res == 0; }
bool handle(internal::Null<>)
bool fallback(internal::Null<>) { {
std::tm *tm = std::gmtime(&time_); using namespace fmt::internal;
if (tm != FMT_NULL) tm_ = *tm; return fallback(gmtime_s(&tm_, &time_));
return tm != FMT_NULL; }
}
}; bool fallback(int res)
GMTime gt(time); {
if (gt.run()) return res == 0;
return gt.tm_; }
// Too big time values may be unsupported.
FMT_THROW(fmt::FormatError("time_t value out of range")); bool fallback(internal::Null<>)
return std::tm(); {
std::tm *tm = std::gmtime(&time_);
if (tm != FMT_NULL) tm_ = *tm;
return tm != FMT_NULL;
}
};
GMTime gt(time);
if (gt.run())
return gt.tm_;
// Too big time values may be unsupported.
FMT_THROW(fmt::FormatError("time_t value out of range"));
return std::tm();
} }
} //namespace fmt } //namespace fmt
......
...@@ -169,11 +169,11 @@ void drop_all(); ...@@ -169,11 +169,11 @@ void drop_all();
#define SPDLOG_STR_H(x) #x #define SPDLOG_STR_H(x) #x
#define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x) #define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x)
#ifdef _MSC_VER #ifdef _MSC_VER
#define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__)
#define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) #define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__)
#else #else
#define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__)
#define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) #define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__)
#endif #endif
#else #else
#define SPDLOG_TRACE(logger, ...) #define SPDLOG_TRACE(logger, ...)
......
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
#include "catch.hpp" #include "catch.hpp"
#include "utils.h" #include "utils.h"
#define SPDLOG_TRACE_ON #define SPDLOG_TRACE_ON
#define SPDLOG_DEBUG_ON #define SPDLOG_DEBUG_ON
#include "../include/spdlog/spdlog.h" #include "../include/spdlog/spdlog.h"
#include "../include/spdlog/sinks/null_sink.h" #include "../include/spdlog/sinks/null_sink.h"
......
...@@ -6,45 +6,45 @@ ...@@ -6,45 +6,45 @@
TEST_CASE("debug and trace w/o format string", "[macros]]") TEST_CASE("debug and trace w/o format string", "[macros]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log"; std::string filename = "logs/simple_log";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->set_level(spdlog::level::trace); logger->set_level(spdlog::level::trace);
SPDLOG_TRACE(logger, "Test message 1"); SPDLOG_TRACE(logger, "Test message 1");
//SPDLOG_DEBUG(logger, "Test message 2"); //SPDLOG_DEBUG(logger, "Test message 2");
SPDLOG_DEBUG(logger, "Test message 2"); SPDLOG_DEBUG(logger, "Test message 2");
logger->flush(); logger->flush();
REQUIRE(ends_with(file_contents(filename), "Test message 2\n")); REQUIRE(ends_with(file_contents(filename), "Test message 2\n"));
REQUIRE(count_lines(filename) == 2); REQUIRE(count_lines(filename) == 2);
} }
TEST_CASE("debug and trace with format strings", "[macros]]") TEST_CASE("debug and trace with format strings", "[macros]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log"; std::string filename = "logs/simple_log";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename); auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->set_level(spdlog::level::trace); logger->set_level(spdlog::level::trace);
#if !defined(SPDLOG_FMT_PRINTF) #if !defined(SPDLOG_FMT_PRINTF)
SPDLOG_TRACE(logger, "Test message {}", 1); SPDLOG_TRACE(logger, "Test message {}", 1);
//SPDLOG_DEBUG(logger, "Test message 2"); //SPDLOG_DEBUG(logger, "Test message 2");
SPDLOG_DEBUG(logger, "Test message {}", 222); SPDLOG_DEBUG(logger, "Test message {}", 222);
#else #else
SPDLOG_TRACE(logger, "Test message %d", 1); SPDLOG_TRACE(logger, "Test message %d", 1);
//SPDLOG_DEBUG(logger, "Test message 2"); //SPDLOG_DEBUG(logger, "Test message 2");
SPDLOG_DEBUG(logger, "Test message %d", 222); SPDLOG_DEBUG(logger, "Test message %d", 222);
#endif #endif
logger->flush(); logger->flush();
REQUIRE(ends_with(file_contents(filename), "Test message 222\n")); REQUIRE(ends_with(file_contents(filename), "Test message 222\n"));
REQUIRE(count_lines(filename) == 2); REQUIRE(count_lines(filename) == 2);
} }
...@@ -49,8 +49,8 @@ std::size_t get_filesize(const std::string& filename) ...@@ -49,8 +49,8 @@ std::size_t get_filesize(const std::string& filename)
// source: https://stackoverflow.com/a/2072890/192001 // source: https://stackoverflow.com/a/2072890/192001
bool ends_with(std::string const & value, std::string const & ending) bool ends_with(std::string const & value, std::string const & ending)
{ {
if (ending.size() > value.size()) return false; if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment