Merge branch 'gabime:v1.x' into feature-source_location

pull/2690/head
M. Galib Uludag 2 years ago committed by GitHub
commit 4babfe6e20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
.gitignore vendored

@ -75,6 +75,8 @@ spdlogConfigVersion.cmake
# idea # idea
.idea/ .idea/
.cache/
.vscode/
cmake-build-*/ cmake-build-*/
*.db *.db
*.ipch *.ipch

@ -12,7 +12,7 @@ CMake:
add_executable(example example.cpp) add_executable(example example.cpp)
target_link_libraries(example spdlog::spdlog) target_link_libraries(example spdlog::spdlog)
Or copy src/spdlog.cpp to your build tree and pass the -DSPDLOG_COMPILED_LIB to the compiler. Or copy files src/*.cpp to your build tree and pass the -DSPDLOG_COMPILED_LIB to the compiler.
Tested on: Tested on:
gcc 4.8.1 and above gcc 4.8.1 and above

@ -293,7 +293,7 @@ void multi_sink_example2()
template<> template<>
struct fmt::formatter<my_type> : fmt::formatter<std::string> struct fmt::formatter<my_type> : fmt::formatter<std::string>
{ {
auto format(my_type my, format_context &ctx) -> decltype(ctx.out()) auto format(my_type my, format_context &ctx) const -> decltype(ctx.out())
{ {
return format_to(ctx.out(), "[my_type i={}]", my.i); return format_to(ctx.out(), "[my_type i={}]", my.i);
} }

@ -31,6 +31,7 @@ void replace_default_logger_example();
#include "spdlog/cfg/env.h" // support for loading levels from the environment variable #include "spdlog/cfg/env.h" // support for loading levels from the environment variable
#include "spdlog/fmt/ostr.h" // support for user defined types #include "spdlog/fmt/ostr.h" // support for user defined types
int main(int, char *[]) int main(int, char *[])
{ {
// Log levels can be loaded from argv/env using "SPDLOG_LEVEL" // Log levels can be loaded from argv/env using "SPDLOG_LEVEL"
@ -183,6 +184,7 @@ void async_example()
// {:p} - don't print the position on each line start. // {:p} - don't print the position on each line start.
// {:n} - don't split the output to lines. // {:n} - don't split the output to lines.
#if !defined SPDLOG_USE_STD_FORMAT || defined(_MSC_VER)
#include "spdlog/fmt/bin_to_hex.h" #include "spdlog/fmt/bin_to_hex.h"
void binary_example() void binary_example()
{ {
@ -200,6 +202,11 @@ void binary_example()
// logger->info("hexdump style: {:a}", spdlog::to_hex(buf)); // logger->info("hexdump style: {:a}", spdlog::to_hex(buf));
// logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20)); // logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20));
} }
#else
void binary_example() {
// not supported with std::format yet
}
#endif
// Log a vector of numbers // Log a vector of numbers
#ifndef SPDLOG_USE_STD_FORMAT #ifndef SPDLOG_USE_STD_FORMAT
@ -287,7 +294,7 @@ struct fmt::formatter<my_type> : fmt::formatter<std::string>
template<> template<>
struct std::formatter<my_type> : std::formatter<std::string> struct std::formatter<my_type> : std::formatter<std::string>
{ {
auto format(my_type my, format_context &ctx) -> decltype(ctx.out()) auto format(my_type my, format_context &ctx) const -> decltype(ctx.out())
{ {
return format_to(ctx.out(), "[my_type i={}]", my.i); return format_to(ctx.out(), "[my_type i={}]", my.i);
} }

@ -83,7 +83,7 @@ public:
{}; {};
ZeroMemory(&hints, sizeof(hints)); ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET; // IPv4 hints.ai_family = AF_UNSPEC; // To work with IPv4, IPv6, and so on
hints.ai_socktype = SOCK_STREAM; // TCP hints.ai_socktype = SOCK_STREAM; // TCP
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
hints.ai_protocol = 0; hints.ai_protocol = 0;

@ -58,7 +58,7 @@ public:
struct addrinfo hints struct addrinfo hints
{}; {};
memset(&hints, 0, sizeof(struct addrinfo)); memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; // IPv4 hints.ai_family = AF_UNSPEC; // To work with IPv4, IPv6, and so on
hints.ai_socktype = SOCK_STREAM; // TCP hints.ai_socktype = SOCK_STREAM; // TCP
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
hints.ai_protocol = 0; hints.ai_protocol = 0;

@ -12,7 +12,7 @@
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/details/os.h> #include <spdlog/details/os.h>
#include <cstring>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>

@ -156,7 +156,7 @@ struct formatter<spdlog::details::dump_info<T>, char>
// format the given bytes range as hex // format the given bytes range as hex
template<typename FormatContext, typename Container> template<typename FormatContext, typename Container>
auto format(const spdlog::details::dump_info<Container> &the_range, FormatContext &ctx) -> decltype(ctx.out()) auto format(const spdlog::details::dump_info<Container> &the_range, FormatContext &ctx) const -> decltype(ctx.out())
{ {
SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF"; SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF";
SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef"; SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef";
@ -232,7 +232,7 @@ struct formatter<spdlog::details::dump_info<T>, char>
// put newline(and position header) // put newline(and position header)
template<typename It> template<typename It>
void put_newline(It inserter, std::size_t pos) void put_newline(It inserter, std::size_t pos) const
{ {
#ifdef _WIN32 #ifdef _WIN32
*inserter++ = '\r'; *inserter++ = '\r';

@ -56,6 +56,10 @@ protected:
// See system/core/liblog/logger_write.c for explanation of return value // See system/core/liblog/logger_write.c for explanation of return value
int ret = android_log(priority, tag_.c_str(), msg_output); int ret = android_log(priority, tag_.c_str(), msg_output);
if (ret == -EPERM)
{
return; // !__android_log_is_loggable
}
int retry_count = 0; int retry_count = 0;
while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES))
{ {

@ -30,10 +30,14 @@ template<typename Mutex>
class qt_sink : public base_sink<Mutex> class qt_sink : public base_sink<Mutex>
{ {
public: public:
qt_sink(QObject *qt_object, const std::string &meta_method) qt_sink(QObject *qt_object, std::string meta_method)
: qt_object_(qt_object)
, meta_method_(std::move(meta_method))
{ {
qt_object_ = qt_object; if (!qt_object_)
meta_method_ = meta_method; {
throw_spdlog_ex("qt_sink: qt_object is null");
}
} }
~qt_sink() ~qt_sink()
@ -46,7 +50,7 @@ protected:
{ {
memory_buf_t formatted; memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->format(msg, formatted);
string_view_t str = string_view_t(formatted.data(), formatted.size()); const string_view_t str = string_view_t(formatted.data(), formatted.size());
QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection, QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection,
Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed())); Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed()));
} }
@ -68,7 +72,8 @@ private:
{ {
public: public:
qt_color_sink(QTextEdit *qt_text_edit, int max_lines) qt_color_sink(QTextEdit *qt_text_edit, int max_lines)
: qt_text_edit_(qt_text_edit), max_lines_(max_lines) : qt_text_edit_(qt_text_edit)
, max_lines_(max_lines)
{ {
if (!qt_text_edit_) if (!qt_text_edit_)
{ {
@ -131,17 +136,16 @@ private:
protected: protected:
struct invoke_params struct invoke_params
{ {
invoke_params(int max_lines, QTextEdit *q_text_edit, QString payload, QTextCharFormat default_color, invoke_params(int max_lines, QTextEdit *q_text_edit, QString payload, QTextCharFormat default_color, QTextCharFormat level_color,
QTextCharFormat level_color, int color_range_start, int color_range_end) int color_range_start, int color_range_end)
: max_lines(max_lines), : max_lines(max_lines)
q_text_edit(q_text_edit), , q_text_edit(q_text_edit)
payload(std::move(payload)), , payload(std::move(payload))
default_color(default_color), , default_color(default_color)
level_color(level_color), , level_color(level_color)
color_range_start(color_range_start), , color_range_start(color_range_start)
color_range_end(color_range_end) , color_range_end(color_range_end)
{ {}
}
int max_lines; int max_lines;
QTextEdit *q_text_edit; QTextEdit *q_text_edit;
QString payload; QString payload;
@ -150,10 +154,9 @@ private:
int color_range_start; int color_range_start;
int color_range_end; int color_range_end;
}; };
void sink_it_(const details::log_msg &msg) override void sink_it_(const details::log_msg &msg) override
{ {
using this_type = qt_color_sink<Mutex>;
memory_buf_t formatted; memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->format(msg, formatted);
@ -161,20 +164,16 @@ private:
// apply the color to the color range in the formatted message. // apply the color to the color range in the formatted message.
auto payload = QString::fromLatin1(str.data(), static_cast<int>(str.size())); auto payload = QString::fromLatin1(str.data(), static_cast<int>(str.size()));
invoke_params params { invoke_params params{max_lines_, // max lines
max_lines_, // max lines
qt_text_edit_, // text edit to append to qt_text_edit_, // text edit to append to
std::move(payload), // text to append std::move(payload), // text to append
default_color_, // default color default_color_, // default color
colors_.at(msg.level), // color to apply colors_.at(msg.level), // color to apply
msg.color_range_start, // color range start static_cast<int>(msg.color_range_start), // color range start
msg.color_range_end}; // color range end static_cast<int>(msg.color_range_end)}; // color range end
QMetaObject::invokeMethod( QMetaObject::invokeMethod(
qt_text_edit_, qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection);
[params]() {invoke_method_(params);},
Qt::AutoConnection);
} }
void flush_() override {} void flush_() override {}
@ -226,6 +225,7 @@ private:
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include <mutex> #include <mutex>
using qt_sink_mt = qt_sink<std::mutex>; using qt_sink_mt = qt_sink<std::mutex>;
using qt_sink_st = qt_sink<details::null_mutex>; using qt_sink_st = qt_sink<details::null_mutex>;
using qt_color_sink_mt = qt_color_sink<std::mutex>; using qt_color_sink_mt = qt_color_sink<std::mutex>;

@ -61,7 +61,7 @@ template<>
struct formatter<spdlog::stopwatch> : formatter<double> struct formatter<spdlog::stopwatch> : formatter<double>
{ {
template<typename FormatContext> template<typename FormatContext>
auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out()) auto format(const spdlog::stopwatch &sw, FormatContext &ctx) const -> decltype(ctx.out())
{ {
return formatter<double>::format(sw.elapsed().count(), ctx); return formatter<double>::format(sw.elapsed().count(), ctx);
} }

@ -4,7 +4,7 @@
#pragma once #pragma once
#define SPDLOG_VER_MAJOR 1 #define SPDLOG_VER_MAJOR 1
#define SPDLOG_VER_MINOR 11 #define SPDLOG_VER_MINOR 12
#define SPDLOG_VER_PATCH 0 #define SPDLOG_VER_PATCH 0
#define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH) #define SPDLOG_VERSION (SPDLOG_VER_MAJOR * 10000 + SPDLOG_VER_MINOR * 100 + SPDLOG_VER_PATCH)

@ -54,6 +54,10 @@ if(systemd_FOUND)
list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp) list(APPEND SPDLOG_UTESTS_SOURCES test_systemd.cpp)
endif() endif()
if(NOT SPDLOG_USE_STD_FORMAT)
list(APPEND SPDLOG_UTESTS_SOURCES test_bin_to_hex.cpp)
endif()
enable_testing() enable_testing()
function(spdlog_prepare_test test_target spdlog_lib) function(spdlog_prepare_test test_target spdlog_lib)

@ -0,0 +1,93 @@
#include "includes.h"
#include "test_sink.h"
#include "spdlog/fmt/bin_to_hex.h"
TEST_CASE("to_hex", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_upper", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{:X}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_no_delimiter", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{:sX}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_show_ascii", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 8));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_different_size_per_line", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 10));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol)));
oss_logger.info("{:Xs}", spdlog::to_hex(v, 10));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 6));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B ...A.K" + std::string(spdlog::details::os::default_eol) + "0006: FFFF .." +
std::string(spdlog::details::os::default_eol)));
oss_logger.info("{:Xs}", spdlog::to_hex(v, 6));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B" + std::string(spdlog::details::os::default_eol) + "0006: FFFF" +
std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_no_ascii", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
oss_logger.info("{:Xs}", spdlog::to_hex(v, 8));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
oss_logger.info("{:Xsna}", spdlog::to_hex(v, 8));
REQUIRE(ends_with(oss.str(), "090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
}

@ -1,6 +1,5 @@
#include "includes.h" #include "includes.h"
#include "test_sink.h" #include "test_sink.h"
#include "spdlog/fmt/bin_to_hex.h"
template<class T> template<class T>
std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info) std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info)
@ -140,96 +139,6 @@ TEST_CASE("clone async", "[clone]")
spdlog::drop_all(); spdlog::drop_all();
} }
TEST_CASE("to_hex", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_upper", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{:X}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_no_delimiter", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
oss_logger.info("{:sX}", spdlog::to_hex(v));
auto output = oss.str();
REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_show_ascii", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 8));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_different_size_per_line", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 10));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol)));
oss_logger.info("{:Xs}", spdlog::to_hex(v, 10));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
oss_logger.info("{:Xsa}", spdlog::to_hex(v, 6));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B ...A.K" + std::string(spdlog::details::os::default_eol) + "0006: FFFF .." +
std::string(spdlog::details::os::default_eol)));
oss_logger.info("{:Xs}", spdlog::to_hex(v, 6));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B" + std::string(spdlog::details::os::default_eol) + "0006: FFFF" +
std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("to_hex_no_ascii", "[to_hex]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::logger oss_logger("oss", oss_sink);
std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
oss_logger.info("{:Xs}", spdlog::to_hex(v, 8));
REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
oss_logger.info("{:Xsna}", spdlog::to_hex(v, 8));
REQUIRE(ends_with(oss.str(), "090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
}
TEST_CASE("default logger API", "[default logger]") TEST_CASE("default logger API", "[default logger]")
{ {
std::ostringstream oss; std::ostringstream oss;

Loading…
Cancel
Save