Commit 0c276bea authored by gabime's avatar gabime

astyle

parent 5a8cecdf
...@@ -64,9 +64,9 @@ public: ...@@ -64,9 +64,9 @@ public:
//Warning: this can potentialy last forever as we wait it to complete //Warning: this can potentialy last forever as we wait it to complete
void flush() override; void flush() override;
// Error handler // Error handler
virtual void set_error_handler(log_err_handler) override; virtual void set_error_handler(log_err_handler) override;
virtual log_err_handler error_handler() override; virtual log_err_handler error_handler() override;
protected: protected:
void _sink_it(details::log_msg& msg) override; void _sink_it(details::log_msg& msg) override;
......
This diff is collapsed.
...@@ -60,13 +60,13 @@ inline void spdlog::async_logger::flush() ...@@ -60,13 +60,13 @@ inline void spdlog::async_logger::flush()
// Error handler // Error handler
inline void spdlog::async_logger::set_error_handler(spdlog::log_err_handler err_handler) inline void spdlog::async_logger::set_error_handler(spdlog::log_err_handler err_handler)
{ {
_err_handler = err_handler; _err_handler = err_handler;
_async_log_helper->set_error_handler(err_handler); _async_log_helper->set_error_handler(err_handler);
} }
inline spdlog::log_err_handler spdlog::async_logger::error_handler() inline spdlog::log_err_handler spdlog::async_logger::error_handler()
{ {
return _err_handler; return _err_handler;
} }
......
This diff is collapsed.
...@@ -13,74 +13,85 @@ ...@@ -13,74 +13,85 @@
#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_;
Char *start_; Buffer<Char> &buffer_;
Char *start_;
public:
FormatBuf(Buffer<Char> &buffer) : buffer_(buffer), start_(&buffer[0]) { public:
this->setp(start_, start_ + buffer_.capacity()); FormatBuf(Buffer<Char> &buffer) : buffer_(buffer), start_(&buffer[0])
} {
this->setp(start_, start_ + buffer_.capacity());
int_type overflow(int_type ch = traits_type::eof()) {
if (!traits_type::eq_int_type(ch, traits_type::eof())) {
size_t buf_size = size();
buffer_.resize(buf_size);
buffer_.reserve(buf_size * 2);
start_ = &buffer_[0];
start_[buf_size] = traits_type::to_char_type(ch);
this->setp(start_+ buf_size + 1, start_ + buf_size * 2);
} }
return ch;
}
size_t size() const { int_type overflow(int_type ch = traits_type::eof())
return to_unsigned(this->pptr() - start_); {
} if (!traits_type::eq_int_type(ch, traits_type::eof()))
{
size_t buf_size = size();
buffer_.resize(buf_size);
buffer_.reserve(buf_size * 2);
start_ = &buffer_[0];
start_[buf_size] = traits_type::to_char_type(ch);
this->setp(start_+ buf_size + 1, start_ + buf_size * 2);
}
return ch;
}
size_t size() const
{
return to_unsigned(this->pptr() - start_);
}
}; };
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)
};
}; };
} // namespace internal } // namespace internal
// Formats a value. // Formats a value.
template <typename Char, typename ArgFormatter, typename T> template <typename Char, typename ArgFormatter, typename T>
void format(BasicFormatter<Char, ArgFormatter> &f, void format(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], format_buf.size()); BasicStringRef<Char> str(&buffer[0], format_buf.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.
...@@ -13,40 +13,45 @@ ...@@ -13,40 +13,45 @@
#include "format.h" #include "format.h"
#include <ctime> #include <ctime>
namespace fmt { namespace fmt
{
template <typename ArgFormatter> template <typename ArgFormatter>
void format(BasicFormatter<char, ArgFormatter> &f, void format(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;
} }
} }
......
...@@ -58,7 +58,7 @@ public: ...@@ -58,7 +58,7 @@ public:
const std::string& name() const; const std::string& name() const;
void set_pattern(const std::string&); void set_pattern(const std::string&);
void set_formatter(formatter_ptr); void set_formatter(formatter_ptr);
// automatically call flush() if message level >= log_level // automatically call flush() if message level >= log_level
void flush_on(level::level_enum log_level); void flush_on(level::level_enum log_level);
...@@ -66,9 +66,9 @@ public: ...@@ -66,9 +66,9 @@ public:
const std::vector<sink_ptr>& sinks() const; const std::vector<sink_ptr>& sinks() const;
// error handler // error handler
virtual void set_error_handler(log_err_handler); virtual void set_error_handler(log_err_handler);
virtual log_err_handler error_handler(); virtual log_err_handler error_handler();
protected: protected:
virtual void _sink_it(details::log_msg&); virtual void _sink_it(details::log_msg&);
......
...@@ -8,30 +8,30 @@ ...@@ -8,30 +8,30 @@
class failing_sink: public spdlog::sinks::sink class failing_sink: public spdlog::sinks::sink
{ {
void log(const spdlog::details::log_msg& msg) override void log(const spdlog::details::log_msg& msg) override
{ {
throw std::runtime_error("some error happened during log"); throw std::runtime_error("some error happened during log");
} }
void flush() void flush()
{} {}
}; };
TEST_CASE("default_error_handler", "[errors]]") TEST_CASE("default_error_handler", "[errors]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log.txt"; std::string filename = "logs/simple_log.txt";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
logger->set_pattern("%v"); logger->set_pattern("%v");
logger->info("Test message {} {}", 1); logger->info("Test message {} {}", 1);
logger->info("Test message {}", 2); logger->info("Test message {}", 2);
logger->flush(); logger->flush();
REQUIRE(file_contents(filename) == std::string("Test message 2\n")); REQUIRE(file_contents(filename) == std::string("Test message 2\n"));
REQUIRE(count_lines(filename) == 1); REQUIRE(count_lines(filename) == 1);
} }
...@@ -41,69 +41,73 @@ struct custom_ex ...@@ -41,69 +41,73 @@ struct custom_ex
{}; {};
TEST_CASE("custom_error_handler", "[errors]]") TEST_CASE("custom_error_handler", "[errors]]")
{ {
prepare_logdir(); prepare_logdir();
std::string filename = "logs/simple_log.txt"; std::string filename = "logs/simple_log.txt";
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
logger->flush_on(spdlog::level::info); logger->flush_on(spdlog::level::info);
logger->set_error_handler([=](const std::string& msg) { logger->set_error_handler([=](const std::string& msg)
throw custom_ex(); {
}); throw custom_ex();
logger->info("Good message #1"); });
REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex); logger->info("Good message #1");
logger->info("Good message #2"); REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex);
REQUIRE(count_lines(filename) == 2); logger->info("Good message #2");
REQUIRE(count_lines(filename) == 2);
} }
TEST_CASE("default_error_handler2", "[errors]]") TEST_CASE("default_error_handler2", "[errors]]")
{ {
auto logger = spdlog::create<failing_sink>("failed_logger"); auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string& msg) { logger->set_error_handler([=](const std::string& msg)
throw custom_ex(); {
}); throw custom_ex();
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex); });
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
} }
TEST_CASE("async_error_handler", "[errors]]") TEST_CASE("async_error_handler", "[errors]]")
{ {
prepare_logdir(); prepare_logdir();
std::string err_msg("log failed with some msg"); std::string err_msg("log failed with some msg");
spdlog::set_async_mode(128); spdlog::set_async_mode(128);
std::string filename = "logs/simple_async_log.txt"; std::string filename = "logs/simple_async_log.txt";
{ {
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true); auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
logger->set_error_handler([=](const std::string& msg) { logger->set_error_handler([=](const std::string& msg)
std::ofstream ofs("logs/custom_err.txt"); {
if (!ofs) throw std::runtime_error("Failed open logs/custom_err.txt"); std::ofstream ofs("logs/custom_err.txt");
ofs << err_msg; if (!ofs) throw std::runtime_error("Failed open logs/custom_err.txt");
}); ofs << err_msg;
logger->info("Good message #1"); });
logger->info("Bad format msg {} {}", "xxx"); logger->info("Good message #1");
logger->info("Good message #2"); logger->info("Bad format msg {} {}", "xxx");
spdlog::drop("logger"); //force logger to drain the queue and shutdown logger->info("Good message #2");
spdlog::set_sync_mode(); spdlog::drop("logger"); //force logger to drain the queue and shutdown
} spdlog::set_sync_mode();
REQUIRE(count_lines(filename) == 2); }
REQUIRE(file_contents("logs/custom_err.txt") == err_msg); REQUIRE(count_lines(filename) == 2);
REQUIRE(file_contents("logs/custom_err.txt") == err_msg);
} }
// Make sure async error handler is executed // Make sure async error handler is executed
TEST_CASE("async_error_handler2", "[errors]]") TEST_CASE("async_error_handler2", "[errors]]")
{ {
prepare_logdir(); prepare_logdir();
std::string err_msg("This is async handler error message"); std::string err_msg("This is async handler error message");
spdlog::set_async_mode(128); spdlog::set_async_mode(128);
{ {
auto logger = spdlog::create<failing_sink>("failed_logger"); auto logger = spdlog::create<failing_sink>("failed_logger");
logger->set_error_handler([=](const std::string& msg) { logger->set_error_handler([=](const std::string& msg)
std::ofstream ofs("logs/custom_err2.txt"); {
if (!ofs) throw std::runtime_error("Failed open logs/custom_err2.txt"); std::ofstream ofs("logs/custom_err2.txt");
ofs << err_msg; if (!ofs) throw std::runtime_error("Failed open logs/custom_err2.txt");
}); ofs << err_msg;
logger->info("Hello failure"); });
spdlog::drop("failed_logger"); //force logger to drain the queue and shutdown logger->info("Hello failure");
spdlog::set_sync_mode(); spdlog::drop("failed_logger"); //force logger to drain the queue and shutdown
} spdlog::set_sync_mode();
}
REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
REQUIRE(file_contents("logs/custom_err2.txt") == err_msg);
} }
...@@ -3,15 +3,15 @@ ...@@ -3,15 +3,15 @@
void prepare_logdir() void prepare_logdir()
{ {
spdlog::drop_all(); spdlog::drop_all();
#ifdef _WIN32 #ifdef _WIN32
system("if not exist logs mkdir logs"); system("if not exist logs mkdir logs");
system("del /F /Q logs\\*"); system("del /F /Q logs\\*");
#else #else
auto rv = system("mkdir -p logs"); auto rv = system("mkdir -p logs");
rv = system("rm -f logs/*"); rv = system("rm -f logs/*");
(void)rv; (void)rv;
#endif #endif
} }
......
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