From 8c9a6fc02cb92bd079c705101a77ecec0f2d1202 Mon Sep 17 00:00:00 2001 From: fooinha Date: Tue, 16 Dec 2014 15:13:35 +0000 Subject: [PATCH 1/6] * openlog setup for syslog sink --- example/example.cpp | 5 ++- include/spdlog/details/format.h | 16 +++++--- .../spdlog/details/pattern_formatter_impl.h | 2 +- include/spdlog/details/spdlog_impl.h | 4 +- include/spdlog/sinks/syslog_sink.h | 41 ++++++++++++++++++- include/spdlog/spdlog.h | 2 +- 6 files changed, 56 insertions(+), 14 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 0cfa326f..a895fe3d 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -76,7 +76,7 @@ int main(int, char* []) // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous.. // Note: queue size must be power of 2! // - size_t max_q_size = 1048576; + size_t max_q_size = 1048576; spdlog::set_async_mode(max_q_size); auto async_file= spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); async_file->info() << "This is async log.." << "Should be very fast!"; @@ -85,7 +85,8 @@ int main(int, char* []) // syslog example // #ifdef __linux__ - auto syslog_logger = spd::syslog_logger("syslog"); + std::string ident = "my_ident"; + auto syslog_logger = spd::syslog_logger("syslog", ident, spd::sinks::syslog::option::PID | spd::sinks::syslog::option::PERROR, "mail" ); syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!"); #endif } diff --git a/include/spdlog/details/format.h b/include/spdlog/details/format.h index b74da1f2..3934f812 100644 --- a/include/spdlog/details/format.h +++ b/include/spdlog/details/format.h @@ -115,8 +115,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ TypeName(const TypeName&); \ void operator=(const TypeName&) -namespace spdlog { -namespace details { +namespace spdlog +{ +namespace details +{ namespace fmt { @@ -1054,7 +1056,7 @@ public: { default: assert(false); - // Fall through. + // Fall through. case Arg::INT: return FMT_DISPATCH(visit_int(arg.int_value)); case Arg::UINT: @@ -2223,7 +2225,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': @@ -2849,8 +2851,10 @@ fmt::print(format, args...); #define FMT_VARIADIC_W(ReturnType, func, ...) \ FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__) -namespace spdlog { -namespace details { +namespace spdlog +{ +namespace details +{ namespace fmt { FMT_VARIADIC(std::string, format, StringRef) diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 1b31ced4..7c44dd7f 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -458,7 +458,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; diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 6b559064..0199a8df 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -87,9 +87,9 @@ inline std::shared_ptr spdlog::stderr_logger_st(const std::strin #ifdef __linux__ // Create syslog logger -inline std::shared_ptr spdlog::syslog_logger(const std::string& logger_name) +inline std::shared_ptr spdlog::syslog_logger(const std::string& logger_name, const std::string& ident, int option , const std::string & facility) { - return create(logger_name); + return create(logger_name, ident, option, facility); } #endif diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 2aee3f7c..6a597d2d 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -27,6 +27,9 @@ #ifdef __linux__ #include + +#define SYSLOG_NAMES 1 + #include #include "./sink.h" #include "../common.h" @@ -37,6 +40,21 @@ namespace spdlog { namespace sinks { +namespace syslog +{ +namespace option +{ +typedef enum +{ + CONS = LOG_CONS, + NDELAY = LOG_NDELAY, + NOWAIT = LOG_NOWAIT, + ODELAY = LOG_ODELAY, + PERROR = LOG_PERROR, + PID = LOG_PID +} option_enum; +} +} /** * Sink that write to syslog using the `syscall()` library call. * @@ -45,8 +63,9 @@ namespace sinks class syslog_sink : public sink { public: - syslog_sink() + syslog_sink(const std::string& ident = "", int option = static_cast(syslog::option::PID), const std::string &facility = "user") { + _priorities[static_cast(level::TRACE)] = LOG_DEBUG; _priorities[static_cast(level::DEBUG)] = LOG_DEBUG; _priorities[static_cast(level::INFO)] = LOG_INFO; @@ -59,6 +78,8 @@ public: _priorities[static_cast(level::ALWAYS)] = LOG_INFO; _priorities[static_cast(level::OFF)] = LOG_INFO; + + ::openlog(ident.c_str(), option, syslog_facility_from_name(facility)); } virtual ~syslog_sink() = default; @@ -67,10 +88,11 @@ public: void log(const details::log_msg &msg) override { - syslog(syslog_prio_from_level(msg), "%s", msg.formatted.str().c_str()); + ::syslog(syslog_prio_from_level(msg), "%s", msg.formatted.str().c_str()); }; protected: + /** * Simply maps spdlog's log level to syslog priority level. */ @@ -81,6 +103,21 @@ protected: private: std::array _priorities; + + inline int syslog_facility_from_name (const std::string & name) + { + if (name.empty()) + return LOG_USER; + + for (int i = 0; facilitynames[i].c_name != NULL; ++i) + { + if (name == facilitynames[i].c_name) + return facilitynames[i].c_val; + } + + return LOG_USER; + + } }; } } diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 4841854e..7e285efd 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -96,7 +96,7 @@ std::shared_ptr stderr_logger_st(const std::string& logger_name); // Create a syslog logger // #ifdef __linux__ -std::shared_ptr syslog_logger(const std::string& logger_name); +std::shared_ptr syslog_logger(const std::string& logger_name, const std::string& ident, int option, const std::string &facility); #endif // From 235834b33ce6f73736f6ead1763ce87b429914db Mon Sep 17 00:00:00 2001 From: fooinha Date: Tue, 16 Dec 2014 15:17:21 +0000 Subject: [PATCH 2/6] * update README example --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 15643950..380f0c96 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,8 @@ int main(int, char* []) // syslog example // #ifdef __linux__ - auto syslog_logger = spd::syslog_logger("syslog"); + std::string ident = "my_app"; + auto syslog_logger = spd::syslog_logger("syslog", ident, spd::sinks::syslog::option::PID | spd::sinks::syslog::option::PERROR, "mail" ); syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!"); #endif } From 309fca8a725fb0631acd57a20f69403e304f2ea5 Mon Sep 17 00:00:00 2001 From: fooinha Date: Tue, 16 Dec 2014 15:28:23 +0000 Subject: [PATCH 3/6] Add a Makefile to main directory. It eases the project integration with some IDE applications. --- Makefile | 2 ++ example/Makefile | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..02355874 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +%: + make -C example $@ diff --git a/example/Makefile b/example/Makefile index 4208786a..1ded75f6 100644 --- a/example/Makefile +++ b/example/Makefile @@ -4,20 +4,20 @@ CXX_RELEASE_FLAGS = -O3 -flto CXX_DEBUG_FLAGS= -g -all: example bench -debug: example-debug bench-debug +all: test-example test-bench +debug: test-example-debug test-bench-debug -example: example.cpp +test-example: example.cpp $(CXX) example.cpp -o example $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -bench: bench.cpp +test-bench: bench.cpp $(CXX) bench.cpp -o bench $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -example-debug: example.cpp +test-example-debug: example.cpp $(CXX) example.cpp -o example-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) -bench-debug: bench.cpp +test-bench-debug: bench.cpp $(CXX) bench.cpp -o bench-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) From 375b88c19166750cee3b6c1753a7a8c71fb5e382 Mon Sep 17 00:00:00 2001 From: fooinha Date: Thu, 18 Dec 2014 15:47:43 +0000 Subject: [PATCH 4/6] Optional arguments for syslog_logger factory. --- include/spdlog/common.h | 29 ++++++++++++++++++++++++++++ include/spdlog/details/spdlog_impl.h | 3 +-- include/spdlog/sinks/syslog_sink.h | 17 ---------------- include/spdlog/spdlog.h | 3 ++- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 1be17d0d..1712ee56 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -27,13 +27,42 @@ #include #include +#ifdef __linux__ + +#define SYSLOG_NAMES 1 +#include +#endif + namespace spdlog { + class formatter; + namespace sinks { + class sink; + +#ifdef __linux__ +namespace syslog +{ +namespace option +{ +typedef enum +{ + CONS = LOG_CONS, + NDELAY = LOG_NDELAY, + NOWAIT = LOG_NOWAIT, + ODELAY = LOG_ODELAY, + PERROR = LOG_PERROR, + PID = LOG_PID +} option_enum; } +} +#endif + +} + // Common types across the lib using log_clock = std::chrono::system_clock; diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 0199a8df..c50cef89 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -87,11 +87,10 @@ inline std::shared_ptr spdlog::stderr_logger_st(const std::strin #ifdef __linux__ // Create syslog logger -inline std::shared_ptr spdlog::syslog_logger(const std::string& logger_name, const std::string& ident, int option , const std::string & facility) +inline std::shared_ptr spdlog::syslog_logger(const std::string& logger_name, const std::string& ident, int option, const std::string &facility) { return create(logger_name, ident, option, facility); } - #endif diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 6a597d2d..2270f184 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -28,9 +28,7 @@ #include -#define SYSLOG_NAMES 1 -#include #include "./sink.h" #include "../common.h" #include "../details/log_msg.h" @@ -40,21 +38,6 @@ namespace spdlog { namespace sinks { -namespace syslog -{ -namespace option -{ -typedef enum -{ - CONS = LOG_CONS, - NDELAY = LOG_NDELAY, - NOWAIT = LOG_NOWAIT, - ODELAY = LOG_ODELAY, - PERROR = LOG_PERROR, - PID = LOG_PID -} option_enum; -} -} /** * Sink that write to syslog using the `syscall()` library call. * diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 7e285efd..bb9b4980 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -96,7 +96,8 @@ std::shared_ptr stderr_logger_st(const std::string& logger_name); // Create a syslog logger // #ifdef __linux__ -std::shared_ptr syslog_logger(const std::string& logger_name, const std::string& ident, int option, const std::string &facility); + +std::shared_ptr syslog_logger(const std::string& logger_name, const std::string& ident = "", int option = static_cast(sinks::syslog::option::PID), const std::string &facility = "user"); #endif // From c44ca12d570ebef86df82beb80d41b31f2971372 Mon Sep 17 00:00:00 2001 From: fooinha Date: Thu, 18 Dec 2014 16:40:57 +0000 Subject: [PATCH 5/6] Call closelog in syslog sink's destructor --- include/spdlog/sinks/syslog_sink.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index 2270f184..d756b9fd 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -64,7 +64,9 @@ public: ::openlog(ident.c_str(), option, syslog_facility_from_name(facility)); } - virtual ~syslog_sink() = default; + virtual ~syslog_sink() { + ::closelog(); + } syslog_sink(const syslog_sink&) = delete; syslog_sink& operator=(const syslog_sink&) = delete; From 630e301e76a98d098dce921bd4763924974127c6 Mon Sep 17 00:00:00 2001 From: fooinha Date: Thu, 18 Dec 2014 16:48:42 +0000 Subject: [PATCH 6/6] Remove test- prefix from examples make targets --- Makefile | 2 -- example/Makefile | 12 ++++++------ 2 files changed, 6 insertions(+), 8 deletions(-) delete mode 100644 Makefile diff --git a/Makefile b/Makefile deleted file mode 100644 index 02355874..00000000 --- a/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -%: - make -C example $@ diff --git a/example/Makefile b/example/Makefile index 1ded75f6..42795358 100644 --- a/example/Makefile +++ b/example/Makefile @@ -4,20 +4,20 @@ CXX_RELEASE_FLAGS = -O3 -flto CXX_DEBUG_FLAGS= -g -all: test-example test-bench -debug: test-example-debug test-bench-debug +all: example bench +debug: example-debug bench-debug -test-example: example.cpp +example: example.cpp $(CXX) example.cpp -o example $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -test-bench: bench.cpp +bench: bench.cpp $(CXX) bench.cpp -o bench $(CXXFLAGS) $(CXX_RELEASE_FLAGS) -test-example-debug: example.cpp +example-debug: example.cpp $(CXX) example.cpp -o example-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS) -test-bench-debug: bench.cpp +bench-debug: bench.cpp $(CXX) bench.cpp -o bench-debug $(CXXFLAGS) $(CXX_DEBUG_FLAGS)