Commit abc0d439 authored by gabime's avatar gabime

astyle

parent 3826ac14
...@@ -439,7 +439,7 @@ inline int pid() ...@@ -439,7 +439,7 @@ inline int pid()
{ {
#ifdef _WIN32 #ifdef _WIN32
return static_cast<int>(::GetCurrentProcessId()); return static_cast<int>(::GetCurrentProcessId());
#else #else
return static_cast<int>(::getpid()); return static_cast<int>(::getpid());
#endif #endif
......
This diff is collapsed.
...@@ -13,59 +13,67 @@ ...@@ -13,59 +13,67 @@
#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. {
DummyStream(); // Suppress a bogus warning in MSVC.
// Hide all operator<< overloads from std::ostream. // Hide all operator<< overloads from std::ostream.
template <typename T> template <typename T>
typename EnableIf<sizeof(T) == 0>::type operator<<(const T &); typename EnableIf<sizeof(T) == 0>::type operator<<(const T &);
}; };
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.
...@@ -75,17 +83,18 @@ FMT_API void write(std::ostream &os, Writer &w); ...@@ -75,17 +83,18 @@ 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);
std::basic_ostream<Char> output(&format_buf); internal::FormatBuf<Char> format_buf(buffer);
output.exceptions(std::ios_base::failbit | std::ios_base::badbit); std::basic_ostream<Char> output(&format_buf);
output << value; output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
output << value;
BasicStringRef<Char> str(&buffer[0], buffer.size());
typedef internal::MakeArg< BasicFormatter<Char> > MakeArg; BasicStringRef<Char> str(&buffer[0], buffer.size());
format_str = f.format(format_str, MakeArg(str)); typedef internal::MakeArg< BasicFormatter<Char> > MakeArg;
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
......
...@@ -3,63 +3,63 @@ ...@@ -3,63 +3,63 @@
// log to str and return it // log to str and return it
static std::string log_to_str(const std::string& msg, std::shared_ptr<spdlog::formatter> formatter = nullptr) static std::string log_to_str(const std::string& msg, std::shared_ptr<spdlog::formatter> formatter = nullptr)
{ {
std::ostringstream oss; std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss); auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("pattern_tester", oss_sink); spdlog::logger oss_logger("pattern_tester", oss_sink);
oss_logger.set_level(spdlog::level::info); oss_logger.set_level(spdlog::level::info);
if (formatter) oss_logger.set_formatter(formatter); if (formatter) oss_logger.set_formatter(formatter);
oss_logger.info(msg); oss_logger.info(msg);
return oss.str(); return oss.str();
} }
TEST_CASE("custom eol", "[pattern_formatter]") TEST_CASE("custom eol", "[pattern_formatter]")
{ {
std::string msg = "Hello custom eol test"; std::string msg = "Hello custom eol test";
std::string eol = ";)"; std::string eol = ";)";
auto formatter = std::make_shared<spdlog::pattern_formatter>("%v", spdlog::pattern_time_type::local, ";)"); auto formatter = std::make_shared<spdlog::pattern_formatter>("%v", spdlog::pattern_time_type::local, ";)");
REQUIRE(log_to_str(msg, formatter) == msg + eol); REQUIRE(log_to_str(msg, formatter) == msg + eol);
} }
TEST_CASE("empty format", "[pattern_formatter]") TEST_CASE("empty format", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("", spdlog::pattern_time_type::local, ""); auto formatter = std::make_shared<spdlog::pattern_formatter>("", spdlog::pattern_time_type::local, "");
REQUIRE(log_to_str("Some message", formatter) == ""); REQUIRE(log_to_str("Some message", formatter) == "");
} }
TEST_CASE("empty format2", "[pattern_formatter]") TEST_CASE("empty format2", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("", spdlog::pattern_time_type::local, "\n"); auto formatter = std::make_shared<spdlog::pattern_formatter>("", spdlog::pattern_time_type::local, "\n");
REQUIRE(log_to_str("Some message", formatter) == "\n"); REQUIRE(log_to_str("Some message", formatter) == "\n");
} }
TEST_CASE("level", "[pattern_formatter]") TEST_CASE("level", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%l] %v", spdlog::pattern_time_type::local, "\n"); auto formatter = std::make_shared<spdlog::pattern_formatter>("[%l] %v", spdlog::pattern_time_type::local, "\n");
REQUIRE(log_to_str("Some message", formatter) == "[info] Some message\n"); REQUIRE(log_to_str("Some message", formatter) == "[info] Some message\n");
} }
TEST_CASE("short level", "[pattern_formatter]") TEST_CASE("short level", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%L] %v", spdlog::pattern_time_type::local, "\n"); auto formatter = std::make_shared<spdlog::pattern_formatter>("[%L] %v", spdlog::pattern_time_type::local, "\n");
REQUIRE(log_to_str("Some message", formatter) == "[I] Some message\n"); REQUIRE(log_to_str("Some message", formatter) == "[I] Some message\n");
} }
TEST_CASE("name", "[pattern_formatter]") TEST_CASE("name", "[pattern_formatter]")
{ {
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%n] %v", spdlog::pattern_time_type::local, "\n"); auto formatter = std::make_shared<spdlog::pattern_formatter>("[%n] %v", spdlog::pattern_time_type::local, "\n");
REQUIRE(log_to_str("Some message", formatter) == "[pattern_tester] Some message\n"); REQUIRE(log_to_str("Some message", formatter) == "[pattern_tester] Some message\n");
} }
TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") TEST_CASE("date MM/DD/YY ", "[pattern_formatter]")
{ {
using namespace::std::chrono; using namespace::std::chrono;
auto formatter = std::make_shared<spdlog::pattern_formatter>("%D %v", spdlog::pattern_time_type::local, "\n"); auto formatter = std::make_shared<spdlog::pattern_formatter>("%D %v", spdlog::pattern_time_type::local, "\n");
auto now_tm = spdlog::details::os::localtime(); auto now_tm = spdlog::details::os::localtime();
std::stringstream oss; std::stringstream oss;
oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << now_tm.tm_mday << "/" << (now_tm.tm_year + 1900) % 1000 << " Some message\n"; oss << std::setfill('0') << std::setw(2) << now_tm.tm_mon + 1 << "/" << now_tm.tm_mday << "/" << (now_tm.tm_year + 1900) % 1000 << " Some message\n";
REQUIRE(log_to_str("Some message", formatter) == oss.str()); REQUIRE(log_to_str("Some message", formatter) == oss.str());
} }
......
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