From 4d98a14cb16850287d70ebff3172534642421e56 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 22 Dec 2019 22:54:31 +0200 Subject: [PATCH 01/81] Updated fmt.cpp to 6.1.2 --- src/fmt.cpp | 169 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 149 insertions(+), 20 deletions(-) diff --git a/src/fmt.cpp b/src/fmt.cpp index 6d7ff0cc..973dcad5 100644 --- a/src/fmt.cpp +++ b/src/fmt.cpp @@ -9,45 +9,174 @@ #if !defined(SPDLOG_FMT_EXTERNAL) #include "spdlog/fmt/bundled/format-inl.h" + FMT_BEGIN_NAMESPACE -template struct FMT_API internal::basic_data; + namespace internal { + + template + int format_float(char* buf, std::size_t size, const char* format, int precision, + T value) { +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + if (precision > 100000) + throw std::runtime_error( + "fuzz mode - avoid large allocation inside snprintf"); +#endif + // Suppress the warning about nonliteral format string. + auto snprintf_ptr = FMT_SNPRINTF; + return precision < 0 ? snprintf_ptr(buf, size, format, value) + : snprintf_ptr(buf, size, format, precision, value); + } + struct sprintf_specs { + int precision; + char type; + bool alt : 1; + + template + constexpr sprintf_specs(basic_format_specs specs) + : precision(specs.precision), type(specs.type), alt(specs.alt) {} + + constexpr bool has_precision() const { return precision >= 0; } + }; + +// This is deprecated and is kept only to preserve ABI compatibility. + template + char* sprintf_format(Double value, internal::buffer& buf, + sprintf_specs specs) { + // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail. + FMT_ASSERT(buf.capacity() != 0, "empty buffer"); + + // Build format string. + enum { max_format_size = 10 }; // longest format: %#-*.*Lg + char format[max_format_size]; + char* format_ptr = format; + *format_ptr++ = '%'; + if (specs.alt || !specs.type) *format_ptr++ = '#'; + if (specs.precision >= 0) { + *format_ptr++ = '.'; + *format_ptr++ = '*'; + } + if (std::is_same::value) *format_ptr++ = 'L'; + + char type = specs.type; + + if (type == '%') + type = 'f'; + else if (type == 0 || type == 'n') + type = 'g'; +#if FMT_MSC_VER + if (type == 'F') { + // MSVC's printf doesn't support 'F'. + type = 'f'; + } +#endif + *format_ptr++ = type; + *format_ptr = '\0'; + + // Format using snprintf. + char* start = nullptr; + char* decimal_point_pos = nullptr; + for (;;) { + std::size_t buffer_size = buf.capacity(); + start = &buf[0]; + int result = + format_float(start, buffer_size, format, specs.precision, value); + if (result >= 0) { + unsigned n = internal::to_unsigned(result); + if (n < buf.capacity()) { + // Find the decimal point. + auto p = buf.data(), end = p + n; + if (*p == '+' || *p == '-') ++p; + if (specs.type != 'a' && specs.type != 'A') { + while (p < end && *p >= '0' && *p <= '9') ++p; + if (p < end && *p != 'e' && *p != 'E') { + decimal_point_pos = p; + if (!specs.type) { + // Keep only one trailing zero after the decimal point. + ++p; + if (*p == '0') ++p; + while (p != end && *p >= '1' && *p <= '9') ++p; + char* where = p; + while (p != end && *p == '0') ++p; + if (p == end || *p < '0' || *p > '9') { + if (p != end) std::memmove(where, p, to_unsigned(end - p)); + n -= static_cast(p - where); + } + } + } + } + buf.resize(n); + break; // The buffer is large enough - continue with formatting. + } + buf.reserve(n + 1); + } else { + // If result is negative we ask to increase the capacity by at least 1, + // but as std::vector, the buffer grows exponentially. + buf.reserve(buf.capacity() + 1); + } + } + return decimal_point_pos; + } + } // namespace internal + + template FMT_API char* internal::sprintf_format(double, internal::buffer&, + sprintf_specs); + template FMT_API char* internal::sprintf_format(long double, + internal::buffer&, + sprintf_specs); + + template struct FMT_API internal::basic_data; // Workaround a bug in MSVC2013 that prevents instantiation of format_float. -int (*instantiate_format_float)(double, int, internal::float_specs, internal::buffer &) = internal::format_float; + int (*instantiate_format_float)(double, int, internal::float_specs, + internal::buffer&) = + internal::format_float; #ifndef FMT_STATIC_THOUSANDS_SEPARATOR -template FMT_API internal::locale_ref::locale_ref(const std::locale &loc); -template FMT_API std::locale internal::locale_ref::get() const; + template FMT_API internal::locale_ref::locale_ref(const std::locale& loc); + template FMT_API std::locale internal::locale_ref::get() const; #endif // Explicit instantiations for char. -template FMT_API std::string internal::grouping_impl(locale_ref); -template FMT_API char internal::thousands_sep_impl(locale_ref); -template FMT_API char internal::decimal_point_impl(locale_ref); + template FMT_API std::string internal::grouping_impl(locale_ref); + template FMT_API char internal::thousands_sep_impl(locale_ref); + template FMT_API char internal::decimal_point_impl(locale_ref); -template FMT_API void internal::buffer::append(const char *, const char *); + template FMT_API void internal::buffer::append(const char*, const char*); -template FMT_API void internal::arg_map::init(const basic_format_args &args); + template FMT_API void internal::arg_map::init( + const basic_format_args& args); -template FMT_API std::string internal::vformat(string_view, basic_format_args); + template FMT_API std::string internal::vformat( + string_view, basic_format_args); -template FMT_API format_context::iterator internal::vformat_to(internal::buffer &, string_view, basic_format_args); + template FMT_API format_context::iterator internal::vformat_to( + internal::buffer&, string_view, basic_format_args); -template FMT_API int internal::snprintf_float(double, int, internal::float_specs, internal::buffer &); -template FMT_API int internal::snprintf_float(long double, int, internal::float_specs, internal::buffer &); -template FMT_API int internal::format_float(double, int, internal::float_specs, internal::buffer &); -template FMT_API int internal::format_float(long double, int, internal::float_specs, internal::buffer &); + template FMT_API int internal::snprintf_float(double, int, + internal::float_specs, + internal::buffer&); + template FMT_API int internal::snprintf_float(long double, int, + internal::float_specs, + internal::buffer&); + template FMT_API int internal::format_float(double, int, internal::float_specs, + internal::buffer&); + template FMT_API int internal::format_float(long double, int, + internal::float_specs, + internal::buffer&); // Explicit instantiations for wchar_t. -template FMT_API std::string internal::grouping_impl(locale_ref); -template FMT_API wchar_t internal::thousands_sep_impl(locale_ref); -template FMT_API wchar_t internal::decimal_point_impl(locale_ref); + template FMT_API std::string internal::grouping_impl(locale_ref); + template FMT_API wchar_t internal::thousands_sep_impl(locale_ref); + template FMT_API wchar_t internal::decimal_point_impl(locale_ref); -template FMT_API void internal::buffer::append(const wchar_t *, const wchar_t *); + template FMT_API void internal::buffer::append(const wchar_t*, + const wchar_t*); -template FMT_API std::wstring internal::vformat(wstring_view, basic_format_args); + template FMT_API std::wstring internal::vformat( + wstring_view, basic_format_args); FMT_END_NAMESPACE + #endif From 8e69c6e492be1dcd034fdca6933540da16762163 Mon Sep 17 00:00:00 2001 From: Ahmed Sagdati Date: Mon, 23 Dec 2019 15:57:16 +0100 Subject: [PATCH 02/81] Fix invalid meson option --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 1ab1e0a7..3460188d 100644 --- a/meson.build +++ b/meson.build @@ -148,7 +148,7 @@ endif # --- Conditionally add subdirs --- # ------------------------------------- -if get_option('enable_tests') or get_option('enable_tests-ho') +if get_option('enable_tests') or get_option('enable_tests_ho') subdir('tests') endif From aac085a9bec955fb4542398580a77556ff419c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Da=C5=84ko?= Date: Wed, 1 Jan 2020 23:54:22 +0100 Subject: [PATCH 03/81] meson: add fallback to fmt dependency Now `fmt` library can be used as subproject which helps with cross compilation. --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 3460188d..8116b23c 100644 --- a/meson.build +++ b/meson.build @@ -23,7 +23,7 @@ if get_option('external_fmt') if not meson.version().version_compare('>=0.49.0') warning('Finding fmt can fail with meson versions before 0.49.0') endif - dep_list += dependency('fmt') + dep_list += dependency('fmt', fallback : ['fmt', 'fmt_dep']) compile_args += '-DSPDLOG_FMT_EXTERNAL' endif From 286eb59081499d6d13699d2ebb15b88c8659de74 Mon Sep 17 00:00:00 2001 From: Carlos Gomes Martinho Date: Mon, 13 Jan 2020 08:40:01 +0100 Subject: [PATCH 04/81] docs: spdlog now available in conan center --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a2f79bea..c8f10569 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ $ cmake .. && make -j * Gentoo: `emerge dev-libs/spdlog` * Arch Linux: `yaourt -S spdlog-git` * vcpkg: `vcpkg install spdlog` -* conan: `spdlog/[>=1.4.1]@bincrafters/stable` +* conan: `spdlog/[>=1.4.1]` ## Features From 26bdf66659719871e625adfae643dd86ecac5e1d Mon Sep 17 00:00:00 2001 From: X Nephila Date: Tue, 14 Jan 2020 07:04:27 +0800 Subject: [PATCH 05/81] latest spdlog now available on MacPorts I am the maintainer of spdlog on MacPorts, now the latest version(1.4.2) of spdlog is available on MacPorts. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c8f10569..d200a4d5 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ $ cmake .. && make -j ## Package managers: * Homebrew: `brew install spdlog` +* MacPorts: `sudo port install spdlog` * FreeBSD: `cd /usr/ports/devel/spdlog/ && make install clean` * Fedora: `yum install spdlog` * Gentoo: `emerge dev-libs/spdlog` From 46fcd2e8448cf3717475913c6ee6d749703db1f1 Mon Sep 17 00:00:00 2001 From: weiy Date: Sun, 19 Jan 2020 13:46:43 +0800 Subject: [PATCH 06/81] add max files for rotating days --- include/spdlog/sinks/daily_file_sink.h | 32 +++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 40a37a71..de673932 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -66,8 +66,7 @@ public: if (max_files_ > 0) { - filenames_q_ = details::circular_q(static_cast(max_files_)); - filenames_q_.push_back(std::move(filename)); + init_filenames_q(); } } @@ -104,6 +103,27 @@ protected: } private: + void init_filenames_q() + { + using details::os::file_exists; + filenames_q_ = details::circular_q(static_cast(max_files_)); + std::vector filenames; + auto now = log_clock::now(); + while (filenames.size() < max_files_) + { + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + if (!file_exists(filename)) + { + break; + } + filenames.emplace_back(filename); + now -= std::chrono::hours(24); + } + for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) { + filenames_q_.push_back(std::move(*iter)); + } + } + tm now_tm(log_clock::time_point tp) { time_t tnow = log_clock::to_time_t(tp); @@ -167,15 +187,15 @@ using daily_file_sink_st = daily_file_sink; // template inline std::shared_ptr daily_logger_mt( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); } template inline std::shared_ptr daily_logger_st( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); } } // namespace spdlog From 34cc3419fa67d38f0a4918b40351c2d7e260c804 Mon Sep 17 00:00:00 2001 From: weiy Date: Sun, 19 Jan 2020 13:52:01 +0800 Subject: [PATCH 07/81] add max files for rotating days --- include/spdlog/sinks/daily_file_sink.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index de673932..294c81ac 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -105,14 +105,15 @@ protected: private: void init_filenames_q() { - using details::os::file_exists; + using details::os::path_exists; + filenames_q_ = details::circular_q(static_cast(max_files_)); std::vector filenames; auto now = log_clock::now(); while (filenames.size() < max_files_) { auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); - if (!file_exists(filename)) + if (!path_exists(filename)) { break; } From 85ea4297b9a77131849e57572f7a4e0027b1e8b1 Mon Sep 17 00:00:00 2001 From: weiy Date: Sun, 19 Jan 2020 17:29:17 +0800 Subject: [PATCH 08/81] update method name --- include/spdlog/sinks/daily_file_sink.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 294c81ac..f796f831 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -66,7 +66,7 @@ public: if (max_files_ > 0) { - init_filenames_q(); + init_filenames_q_(); } } @@ -103,7 +103,7 @@ protected: } private: - void init_filenames_q() + void init_filenames_q_() { using details::os::path_exists; From 1b4621962fd6787b0006ca641944aa5e60b570e5 Mon Sep 17 00:00:00 2001 From: Vitaly Lipatov Date: Thu, 30 Jan 2020 14:51:42 +0300 Subject: [PATCH 09/81] CMakeLists.txt: put GNUInstallDirs after set project language --- CMakeLists.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 508a0b65..4c414f0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,12 +6,16 @@ cmake_minimum_required(VERSION 3.2) #--------------------------------------------------------------------------------------- # Start spdlog project #--------------------------------------------------------------------------------------- -include(GNUInstallDirs) include(cmake/utils.cmake) include(cmake/ide.cmake) spdlog_extract_version() +project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX) +message(STATUS "Build spdlog: ${SPDLOG_VERSION}") + +include(GNUInstallDirs) + #--------------------------------------------------------------------------------------- # Set default build to release #--------------------------------------------------------------------------------------- @@ -19,8 +23,6 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE) endif() -project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX) -message(STATUS "Build spdlog: ${SPDLOG_VERSION}") #--------------------------------------------------------------------------------------- # Compiler config From 742df52236c427905d5184365b11dd9087d77f52 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 2 Feb 2020 14:16:57 +0200 Subject: [PATCH 10/81] Update license to add note about fmt lib dependecy --- LICENSE | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/LICENSE b/LICENSE index 4b43e064..0a67394e 100644 --- a/LICENSE +++ b/LICENSE @@ -2,6 +2,7 @@ The MIT License (MIT) Copyright (c) 2016 Gabi Melman. +This sowftare also uses the fmt library with MIT license ( Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights @@ -20,3 +21,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-- NOTE: Third party dependecy used by this library -- +This software uses the fmt lib (MIT License) https://github.com/fmtlib/fmt/blob/master/LICENSE.rst + + + From da2ff552c5ccd4d01964d29103a2c751fdbb3158 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 2 Feb 2020 14:20:24 +0200 Subject: [PATCH 11/81] Update LICENSE --- LICENSE | 4 ---- 1 file changed, 4 deletions(-) diff --git a/LICENSE b/LICENSE index 0a67394e..e1997558 100644 --- a/LICENSE +++ b/LICENSE @@ -2,7 +2,6 @@ The MIT License (MIT) Copyright (c) 2016 Gabi Melman. -This sowftare also uses the fmt library with MIT license ( Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights @@ -23,6 +22,3 @@ THE SOFTWARE. -- NOTE: Third party dependecy used by this library -- This software uses the fmt lib (MIT License) https://github.com/fmtlib/fmt/blob/master/LICENSE.rst - - - From 60853b5e54b134a4c9118cd5536af4a9b3b53983 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sun, 2 Feb 2020 14:20:44 +0200 Subject: [PATCH 12/81] Update LICENSE --- LICENSE | 1 + 1 file changed, 1 insertion(+) diff --git a/LICENSE b/LICENSE index e1997558..bb10b6e5 100644 --- a/LICENSE +++ b/LICENSE @@ -22,3 +22,4 @@ THE SOFTWARE. -- NOTE: Third party dependecy used by this library -- This software uses the fmt lib (MIT License) https://github.com/fmtlib/fmt/blob/master/LICENSE.rst + From 24173d5ebc39a52cf81f44a37895f545115e2313 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 3 Feb 2020 21:11:45 +0200 Subject: [PATCH 13/81] Update LICENSE --- LICENSE | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index bb10b6e5..1c712f33 100644 --- a/LICENSE +++ b/LICENSE @@ -20,6 +20,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --- NOTE: Third party dependecy used by this library -- -This software uses the fmt lib (MIT License) https://github.com/fmtlib/fmt/blob/master/LICENSE.rst +-- NOTE: Third party dependecy used by this sofware -- +This software uses and includes the fmt lib (MIT License), and users must comply to its license: https://github.com/fmtlib/fmt/blob/master/LICENSE.rst From bed56d3e52a4f16335be038cb691406287ce95e4 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 3 Feb 2020 21:12:12 +0200 Subject: [PATCH 14/81] Update LICENSE --- LICENSE | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 1c712f33..ab442ddc 100644 --- a/LICENSE +++ b/LICENSE @@ -21,5 +21,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -- NOTE: Third party dependecy used by this sofware -- -This software uses and includes the fmt lib (MIT License), and users must comply to its license: https://github.com/fmtlib/fmt/blob/master/LICENSE.rst +This software uses and includes the fmt lib (MIT License), +and users must comply to its license: https://github.com/fmtlib/fmt/blob/master/LICENSE.rst From 966d827d351edab31dac3f916a17849c07a4e3b9 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 3 Feb 2020 22:32:17 +0200 Subject: [PATCH 15/81] Update LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index ab442ddc..0526b0b6 100644 --- a/LICENSE +++ b/LICENSE @@ -21,6 +21,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -- NOTE: Third party dependecy used by this sofware -- -This software uses and includes the fmt lib (MIT License), +This software depends on the fmt lib (MIT License), and users must comply to its license: https://github.com/fmtlib/fmt/blob/master/LICENSE.rst From 622f5eb967de7f3228668012901a5ea3bfd38a8b Mon Sep 17 00:00:00 2001 From: Mario Emmenlauer Date: Fri, 7 Feb 2020 10:20:44 +0100 Subject: [PATCH 16/81] tests/utils.h: removed duplicate entry for count_lines() --- tests/utils.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/utils.h b/tests/utils.h index de553a9c..4f27f3bb 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -3,8 +3,6 @@ #include #include -std::size_t count_lines(const std::string &filename); - std::size_t count_files(const std::string &folder); void prepare_logdir(); From db1a2214270259c108ad0290c7d2264fd85ed1c1 Mon Sep 17 00:00:00 2001 From: bandana2004 Date: Tue, 4 Feb 2020 15:38:26 +0200 Subject: [PATCH 17/81] Add eventlog_sink for logging to Windows Event Log (local only). --- include/spdlog/sinks/eventlog_sink.h | 100 +++++++++ include/spdlog/sinks/eventlog_sink_win32.h | 246 +++++++++++++++++++++ tests/CMakeLists.txt | 1 + tests/test_eventlog.cpp | 66 ++++++ 4 files changed, 413 insertions(+) create mode 100644 include/spdlog/sinks/eventlog_sink.h create mode 100644 include/spdlog/sinks/eventlog_sink_win32.h create mode 100644 tests/test_eventlog.cpp diff --git a/include/spdlog/sinks/eventlog_sink.h b/include/spdlog/sinks/eventlog_sink.h new file mode 100644 index 00000000..5ea370cc --- /dev/null +++ b/include/spdlog/sinks/eventlog_sink.h @@ -0,0 +1,100 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +// Requires the following registry entries to be present, with the following modifications: +// 1. {app_name} should be replaced with your application name +// 2. {log_name} should be replaced with the specific log name and the key should be duplicated for +// each log used in the application + +/*--------------------------------------------------------------------------------------- + +Windows Registry Editor Version 5.00 + +[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\{app_name}] + +[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\{app_name}\{log_name}] +"TypesSupported"=dword:00000007 +"EventMessageFile"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,\ + 00,6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,\ + 5c,00,6d,00,73,00,63,00,6f,00,72,00,65,00,65,00,2e,00,64,00,6c,00,6c,00,00,\ + 00 + +-----------------------------------------------------------------------------------------*/ + +#pragma once + +#ifdef _WIN32 +#include +#endif + +#include +#include + +#include +#include + +namespace spdlog { +namespace sinks { +/* + * Windows Event Log sink + */ +template +class eventlog_sink : public base_sink + { + public: + explicit eventlog_sink( + std::string const& source, + std::string const& log = "Application", + std::string const& message_file_path = "%windir%\\System32\\mscoree.dll"); + + eventlog_sink(eventlog_sink const&) = delete; + eventlog_sink& operator=(eventlog_sink const&) = delete; + protected: + void sink_it_(const details::log_msg &msg) override; + void flush_() override {} + virtual void set_pattern_(const std::string &pattern) override; + virtual void set_formatter_(std::unique_ptr sink_formatter) override; + + private: + std::unique_ptr impl_; + }; + +#ifdef _WIN32 +#include "eventlog_sink_win32.h" +#else + +template +eventlog_sink::eventlog_sink(std::string const& source, std::string const& log, std::string const& message_file_path) + { + } + +template +void eventlog_sink::sink_it_(const details::log_msg &msg) + {} + + +#endif + +template +void eventlog_sink::set_pattern_(const std::string &pattern) +{ + if (impl_) + impl_->set_pattern(pattern); +} + +template +void eventlog_sink::set_formatter_(std::unique_ptr sink_formatter) +{ + if (impl_) + impl_->set_formatter(std::move(sink_formatter)); +} + +using eventlog_sink_mt = eventlog_sink; +using eventlog_sink_st = eventlog_sink; + +using windebug_sink_mt = eventlog_sink_mt; +using windebug_sink_st = eventlog_sink_st; + +} // namespace sinks +} // namespace spdlog + diff --git a/include/spdlog/sinks/eventlog_sink_win32.h b/include/spdlog/sinks/eventlog_sink_win32.h new file mode 100644 index 00000000..9e172804 --- /dev/null +++ b/include/spdlog/sinks/eventlog_sink_win32.h @@ -0,0 +1,246 @@ +// Copyright(c) 2020 bandana2004@gmail.com +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +namespace eventlog_sink_win32 { + +struct win32_error : public spdlog_ex +{ + static std::string format(std::string const& func, DWORD error = GetLastError()) + { + return fmt::format(func + " failed ({0})", error); + } + + static void report(char const* message) SPDLOG_NOEXCEPT + { + fprintf(stderr, "%s", message); + fflush(stderr); + } + + static void report(std::string const& message) SPDLOG_NOEXCEPT + { + report(message.c_str()); + } + + win32_error(std::string const& func_name, DWORD error = GetLastError()) + : spdlog_ex(format(func_name, error), error) + {} +}; + +struct sid +{ + static SID* duplicate(SID* p_src_sid) + { + if (!IsValidSid(p_src_sid)) + SPDLOG_THROW(spdlog_ex("sid::duplicate: invalid SID received")); + + DWORD sid_size = ::GetLengthSid(p_src_sid); + SID* p_dest_sid = (SID*) ::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sid_size); + + if (!p_dest_sid) + SPDLOG_THROW(win32_error("HeapAlloc")); + + if (!CopySid(sid_size, p_dest_sid, p_src_sid)) + { + sid::free(&p_dest_sid); + SPDLOG_THROW(win32_error("CopySid")); + } + + return p_dest_sid; + } + + static void free(SID** ppsid) + { + if (ppsid && *ppsid) + { + if (!HeapFree(GetProcessHeap(), 0, (LPVOID) *ppsid)) + { + SPDLOG_THROW(win32_error("HeapFree")); + } + } + + *ppsid = nullptr; + } + + static SID* get_current_user_sid() + { + struct process_token_t + { + HANDLE hToken_; + bool hasToken_; + + process_token_t(HANDLE process) + : hToken_(0) + , hasToken_(OpenProcessToken(process, TOKEN_QUERY, &hToken_)) + { + if (!hasToken_) + SPDLOG_THROW(win32_error("OpenProcessToken")); + } + + ~process_token_t() + { + if (hasToken_ && !CloseHandle(hToken_)) + win32_error::report(win32_error::format("CloseHandle")); + } + } current_process_token(GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here! + + // Get the required size + DWORD tusize = 0; + GetTokenInformation(current_process_token.hToken_, TokenUser, NULL, 0, &tusize); + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + SPDLOG_THROW(win32_error("GetTokenInformation")); + + std::vector buffer(tusize); + if (!GetTokenInformation(current_process_token.hToken_, TokenUser, (LPVOID) buffer.data(), tusize, &tusize)) + SPDLOG_THROW(win32_error("GetTokenInformation")); + + return sid::duplicate((SID *) ((TOKEN_USER*) buffer.data())->User.Sid); + } +}; + +struct eventlog +{ + static WORD get_event_type(details::log_msg const& msg) + { + WORD type = EVENTLOG_SUCCESS; + if (msg.level >= level::info) + { + type = EVENTLOG_INFORMATION_TYPE; + if (msg.level >= level::warn) + { + type = EVENTLOG_WARNING_TYPE; + if (msg.level >= level::err) + { + type = EVENTLOG_ERROR_TYPE; + } + } + } + return type; + } + + static WORD get_event_category(details::log_msg const& msg) + { + WORD category = 1; + if (msg.level >= level::debug) + { + category = 2; + if (msg.level >= level::info) + { + category = 3; + if (msg.level >= level::warn) + { + category = 4; + if (msg.level >= level::err) + { + category = 5; + if (msg.level >= level::critical) + { + category = 6; + } + } + } + } + } + return category; + } +}; + +class sink : public base_sink +{ +private: + HANDLE hEventLog_; + SID* current_user_sid_; + std::string source_; + std::string log_; + + void add_registry_info(std::string const& message_file_path) + { + std::string subkey("SYSTEM\\CurrentControlSet\\Services\\EventLog\\"); + subkey += log_ + "\\" + source_; + + ::HKEY hkey {}; + DWORD disposition {}; + long stat = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey.c_str(), 0, NULL, + REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, + &hkey, &disposition); + if (stat == ERROR_SUCCESS) + { + if (disposition == REG_CREATED_NEW_KEY && !message_file_path.empty()) + { + auto const expanded_message_file_path_length = ExpandEnvironmentStrings(message_file_path.c_str(), (LPSTR) &disposition, 0); + std::vectorexpanded_message_file_path(expanded_message_file_path_length + 1); + ExpandEnvironmentStrings(message_file_path.c_str(), expanded_message_file_path.data(), expanded_message_file_path_length); + + RegSetValueEx(hkey, "EventMessageFile", 0, REG_SZ, (LPBYTE) expanded_message_file_path.data(), expanded_message_file_path_length); + DWORD typesSupported = 7; + RegSetValueEx(hkey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &typesSupported, sizeof(DWORD)); + } + } + else + { + SPDLOG_THROW(win32_error("RegCreateKeyEx", stat)); + } + + RegCloseKey(hkey); + } + +protected: + void sink_it_(const details::log_msg &msg) override + { + memory_buf_t formatted; + formatter_->format(msg, formatted); + + auto formatted_string = fmt::to_string(formatted); + auto formatted_string_lpsz = formatted_string.c_str(); + + if (!ReportEvent(hEventLog_, eventlog::get_event_type(msg), eventlog::get_event_category(msg), 1000, current_user_sid_, 1, 0, &formatted_string_lpsz, nullptr)) + SPDLOG_THROW(win32_error("ReportEvent")); + } + + void flush_() override {} + +public: + sink( + std::string const& source, + std::string const& log, + std::string const& message_file_path) + : source_(source) + , log_(log) + { + if (!message_file_path.empty()) + add_registry_info(message_file_path); + + current_user_sid_ = sid::get_current_user_sid(); + hEventLog_ = RegisterEventSource(nullptr, source.c_str()); + if (!hEventLog_) + SPDLOG_THROW(win32_error("RegisterEventSource")); + } + + ~sink() + { + try + { + if (hEventLog_ && !DeregisterEventSource(hEventLog_)) + win32_error::report(win32_error::format("DeregisterEventSource")); + + sid::free(¤t_user_sid_); + } + catch (std::exception const& e) + { + win32_error::report(e.what()); + } + } +}; + +} // namespace eventlog_sink_win32 + +template +eventlog_sink::eventlog_sink(std::string const& source, std::string const& log, std::string const& message_file_path) + : impl_(std::make_unique(source, log, message_file_path)) +{} + +template +void eventlog_sink::sink_it_(const details::log_msg &msg) +{ + impl_->log(msg); +} + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3184e9b6..e5540ed8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,6 +12,7 @@ set(SPDLOG_UTESTS_SOURCES test_file_logging.cpp test_daily_logger.cpp test_misc.cpp + test_eventlog.cpp test_pattern_formatter.cpp test_async.cpp test_registry.cpp diff --git a/tests/test_eventlog.cpp b/tests/test_eventlog.cpp new file mode 100644 index 00000000..36dde56f --- /dev/null +++ b/tests/test_eventlog.cpp @@ -0,0 +1,66 @@ +#include "includes.h" +#include "test_sink.h" +#include "spdlog/fmt/bin_to_hex.h" + +#include "spdlog/sinks/eventlog_sink.h" + +#if _WIN32 + +static void test_single_print(std::function do_print, std::string const& expectedContents, WORD expectedEventType) +{ + do_print(expectedContents); + + using namespace std::chrono; + const auto expectedTimeGenerated = duration_cast(system_clock::now().time_since_epoch()).count(); + + struct handle_t + { + HANDLE handle_; + + ~handle_t() + { + if (handle_) + REQUIRE(CloseEventLog(handle_)); + } + } eventLog {OpenEventLog(nullptr, "my_source")}; + + REQUIRE(eventLog.handle_); + + DWORD readBytes {}, sizeNeeded {}; + auto ok = ReadEventLog(eventLog.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &readBytes, 0, &readBytes, &sizeNeeded); + REQUIRE(!ok); + REQUIRE(GetLastError() == ERROR_INSUFFICIENT_BUFFER); + + std::vector recordBuffer(sizeNeeded); + PEVENTLOGRECORD record = (PEVENTLOGRECORD)recordBuffer.data(); + + ok = ReadEventLog(eventLog.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, sizeNeeded, &readBytes, &sizeNeeded); + REQUIRE(ok); + + REQUIRE(record->NumStrings == 1); + REQUIRE(record->EventType == expectedEventType); + REQUIRE(record->TimeGenerated == expectedTimeGenerated); + + std::string message_in_log(((char*) record + record->StringOffset)); + REQUIRE(message_in_log == expectedContents + "\r\n"); +} + +TEST_CASE("eventlog", "[eventlog]") +{ + using namespace spdlog; + + auto test_sink = std::make_shared("my_source", "my_spd_log"); + + spdlog::logger test_logger("eventlog", test_sink); + test_logger.set_level(level::trace); + + test_sink->set_pattern("%v"); + + test_single_print([&test_logger] (std::string const& msg) { test_logger.debug(msg); }, "my debug message", EVENTLOG_SUCCESS); + test_single_print([&test_logger] (std::string const& msg) { test_logger.info(msg); }, "my info message", EVENTLOG_INFORMATION_TYPE); + test_single_print([&test_logger] (std::string const& msg) { test_logger.warn(msg); }, "my warn message", EVENTLOG_WARNING_TYPE); + test_single_print([&test_logger] (std::string const& msg) { test_logger.error(msg); }, "my error message", EVENTLOG_ERROR_TYPE); + test_single_print([&test_logger] (std::string const& msg) { test_logger.critical(msg); }, "my critical message", EVENTLOG_ERROR_TYPE); +} + +#endif \ No newline at end of file From eb234bbf91dbbcdac5be5fb0bf7f599b7bb7c76f Mon Sep 17 00:00:00 2001 From: tt4g Date: Fri, 7 Feb 2020 22:20:54 +0900 Subject: [PATCH 18/81] Don't run spdlog-utests and spdlog-utests-ho in parallel spdlog-utests and spdlog-utests-ho use the same log output directory, so some tests will randomly fail when run in parallel. --- tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3184e9b6..904b4561 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -47,6 +47,7 @@ function(spdlog_prepare_test test_target spdlog_lib) spdlog_enable_sanitizer(${test_target}) endif() add_test(NAME ${test_target} COMMAND ${test_target}) + set_tests_properties(${test_target} PROPERTIES RUN_SERIAL ON) endfunction() # The compiled library tests From a8d6e60ec68ca7609706e076102aca758be05280 Mon Sep 17 00:00:00 2001 From: "Michael R. Crusoe" Date: Fri, 7 Feb 2020 15:39:26 +0100 Subject: [PATCH 19/81] Enable running the tests against an installed copy of spdlog --- tests/CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 904b4561..ce22954f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,12 @@ +cmake_minimum_required(VERSION 3.2) + project(spdlog_utests CXX) +if(NOT TARGET spdlog) + # Stand-alone build + find_package(spdlog REQUIRED) +endif() + include(../cmake/utils.cmake) find_package(PkgConfig) From 8d06df97757bb865d184546c9f993a4e53d9f523 Mon Sep 17 00:00:00 2001 From: "Michael R. Crusoe" Date: Fri, 7 Feb 2020 16:09:16 +0100 Subject: [PATCH 20/81] Enable building & running the example against an installed copy of spdlog --- example/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 458ca952..a2330174 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -4,6 +4,8 @@ cmake_minimum_required(VERSION 3.1) project(spdlog_examples CXX) +include(../cmake/utils.cmake) + if(NOT TARGET spdlog) # Stand-alone build find_package(spdlog REQUIRED) From a9c3630d1bb2760546470845c34fcc1dc04bb561 Mon Sep 17 00:00:00 2001 From: dominicpoeschko <45942148+dominicpoeschko@users.noreply.github.com> Date: Fri, 7 Feb 2020 17:59:11 +0100 Subject: [PATCH 21/81] Properly handling SPDLOG_PREVENT_CHILD_FD Using the SPDLOG_PREVENT_CHILD_FD option there where still a race when a other thread was using fork and exec in between the call to fopen and fcntl. Using open and O_CLOEXEC when possible prevents this race. I have no idea if this problem exists on Windows. --- include/spdlog/details/os-inl.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index 8473eb09..b306e318 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -153,10 +153,26 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename *fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); #endif #else // unix +#if defined(SPDLOG_PREVENT_CHILD_FD) && (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) + // prevent child processes from inheriting log file descriptors direcly on open + // so there is no race with a possible fork and exec + const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC; + const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644)); + if(fd == -1) + { + return false; + } + *fp = ::fdopen(fd, mode.c_str()); + if (*fp == nullptr) + { + ::close(fd); + } +#else *fp = ::fopen((filename.c_str()), mode.c_str()); #endif +#endif -#ifdef SPDLOG_PREVENT_CHILD_FD +#if defined(SPDLOG_PREVENT_CHILD_FD) && !(defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) // prevent child processes from inheriting log file descriptors if (*fp != nullptr) { From 25b10dc26469762d27517829b9d61d42199d901c Mon Sep 17 00:00:00 2001 From: dominicpoeschko <45942148+dominicpoeschko@users.noreply.github.com> Date: Sat, 8 Feb 2020 11:11:04 +0100 Subject: [PATCH 22/81] additional log overload calling log with a string_view as msg called ``` template void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args) ``` instead of ``` template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) ``` which lead to an unnecessary call to fmt::format --- include/spdlog/logger.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 43f39600..ed7d2361 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -143,6 +143,11 @@ public: // T can be statically converted to string_view template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) + { + log(loc,lvl, string_view_t{msg}); + } + + void log(source_loc loc, level::level_enum lvl, string_view_t msg) { bool log_enabled = should_log(lvl); bool traceback_enabled = tracer_.enabled(); From 033fe9f133092dd8d13cdd52a882a7c291a4a48d Mon Sep 17 00:00:00 2001 From: dominic Date: Sat, 8 Feb 2020 12:15:05 +0100 Subject: [PATCH 23/81] Properly handling SPDLOG_PREVENT_CHILD_FD Removed check for posix version so that missing O_CLOEXEC leads to compiler error. Removed extra function since it hat no real purpose anymore. Error behavior between Windows and Unix now equivalent. --- include/spdlog/details/os-inl.h | 39 ++++++++++----------------------- include/spdlog/details/os.h | 4 ---- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index b306e318..ccc53ff2 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -126,23 +126,6 @@ SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT return gmtime(now_t); } -#ifdef SPDLOG_PREVENT_CHILD_FD -SPDLOG_INLINE void prevent_child_fd(FILE *f) -{ -#ifdef _WIN32 - auto file_handle = reinterpret_cast(_get_osfhandle(::_fileno(f))); - if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) - SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno)); -#else - auto fd = ::fileno(f); - if (::fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) - { - SPDLOG_THROW(spdlog_ex("fcntl with FD_CLOEXEC failed", errno)); - } -#endif -} -#endif // SPDLOG_PREVENT_CHILD_FD - // fopen_s on non windows for writing SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) { @@ -152,10 +135,19 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename #else *fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); #endif +#if defined(SPDLOG_PREVENT_CHILD_FD) + if (*fp != nullptr) + { + auto file_handle = reinterpret_cast(_get_osfhandle(::_fileno(*fp))); + if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) + { + :fclose(*fp); + *fp = nullptr; + } + } +#endif #else // unix -#if defined(SPDLOG_PREVENT_CHILD_FD) && (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) - // prevent child processes from inheriting log file descriptors direcly on open - // so there is no race with a possible fork and exec +#if defined(SPDLOG_PREVENT_CHILD_FD) const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC; const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644)); if(fd == -1) @@ -172,13 +164,6 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename #endif #endif -#if defined(SPDLOG_PREVENT_CHILD_FD) && !(defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) - // prevent child processes from inheriting log file descriptors - if (*fp != nullptr) - { - prevent_child_fd(*fp); - } -#endif return *fp == nullptr; } diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 0894a6c0..8a038b26 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -38,10 +38,6 @@ static const char folder_sep = '\\'; SPDLOG_CONSTEXPR static const char folder_sep = '/'; #endif -#ifdef SPDLOG_PREVENT_CHILD_FD -void prevent_child_fd(FILE *f); -#endif - // fopen_s on non windows for writing bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode); From 4bece787c82f35af73de5645d0f4f6e7e465fae9 Mon Sep 17 00:00:00 2001 From: bandana2004 Date: Sun, 9 Feb 2020 11:52:26 +0200 Subject: [PATCH 24/81] Refactor Event Log sink --- include/spdlog/sinks/eventlog_sink.h | 100 ------ include/spdlog/sinks/eventlog_sink_win32.h | 246 -------------- include/spdlog/sinks/win_eventlog_sink.h | 357 +++++++++++++++++++++ tests/test_eventlog.cpp | 11 +- 4 files changed, 364 insertions(+), 350 deletions(-) delete mode 100644 include/spdlog/sinks/eventlog_sink.h delete mode 100644 include/spdlog/sinks/eventlog_sink_win32.h create mode 100644 include/spdlog/sinks/win_eventlog_sink.h diff --git a/include/spdlog/sinks/eventlog_sink.h b/include/spdlog/sinks/eventlog_sink.h deleted file mode 100644 index 5ea370cc..00000000 --- a/include/spdlog/sinks/eventlog_sink.h +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -// Requires the following registry entries to be present, with the following modifications: -// 1. {app_name} should be replaced with your application name -// 2. {log_name} should be replaced with the specific log name and the key should be duplicated for -// each log used in the application - -/*--------------------------------------------------------------------------------------- - -Windows Registry Editor Version 5.00 - -[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\{app_name}] - -[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\{app_name}\{log_name}] -"TypesSupported"=dword:00000007 -"EventMessageFile"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,\ - 00,6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,\ - 5c,00,6d,00,73,00,63,00,6f,00,72,00,65,00,65,00,2e,00,64,00,6c,00,6c,00,00,\ - 00 - ------------------------------------------------------------------------------------------*/ - -#pragma once - -#ifdef _WIN32 -#include -#endif - -#include -#include - -#include -#include - -namespace spdlog { -namespace sinks { -/* - * Windows Event Log sink - */ -template -class eventlog_sink : public base_sink - { - public: - explicit eventlog_sink( - std::string const& source, - std::string const& log = "Application", - std::string const& message_file_path = "%windir%\\System32\\mscoree.dll"); - - eventlog_sink(eventlog_sink const&) = delete; - eventlog_sink& operator=(eventlog_sink const&) = delete; - protected: - void sink_it_(const details::log_msg &msg) override; - void flush_() override {} - virtual void set_pattern_(const std::string &pattern) override; - virtual void set_formatter_(std::unique_ptr sink_formatter) override; - - private: - std::unique_ptr impl_; - }; - -#ifdef _WIN32 -#include "eventlog_sink_win32.h" -#else - -template -eventlog_sink::eventlog_sink(std::string const& source, std::string const& log, std::string const& message_file_path) - { - } - -template -void eventlog_sink::sink_it_(const details::log_msg &msg) - {} - - -#endif - -template -void eventlog_sink::set_pattern_(const std::string &pattern) -{ - if (impl_) - impl_->set_pattern(pattern); -} - -template -void eventlog_sink::set_formatter_(std::unique_ptr sink_formatter) -{ - if (impl_) - impl_->set_formatter(std::move(sink_formatter)); -} - -using eventlog_sink_mt = eventlog_sink; -using eventlog_sink_st = eventlog_sink; - -using windebug_sink_mt = eventlog_sink_mt; -using windebug_sink_st = eventlog_sink_st; - -} // namespace sinks -} // namespace spdlog - diff --git a/include/spdlog/sinks/eventlog_sink_win32.h b/include/spdlog/sinks/eventlog_sink_win32.h deleted file mode 100644 index 9e172804..00000000 --- a/include/spdlog/sinks/eventlog_sink_win32.h +++ /dev/null @@ -1,246 +0,0 @@ -// Copyright(c) 2020 bandana2004@gmail.com -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -namespace eventlog_sink_win32 { - -struct win32_error : public spdlog_ex -{ - static std::string format(std::string const& func, DWORD error = GetLastError()) - { - return fmt::format(func + " failed ({0})", error); - } - - static void report(char const* message) SPDLOG_NOEXCEPT - { - fprintf(stderr, "%s", message); - fflush(stderr); - } - - static void report(std::string const& message) SPDLOG_NOEXCEPT - { - report(message.c_str()); - } - - win32_error(std::string const& func_name, DWORD error = GetLastError()) - : spdlog_ex(format(func_name, error), error) - {} -}; - -struct sid -{ - static SID* duplicate(SID* p_src_sid) - { - if (!IsValidSid(p_src_sid)) - SPDLOG_THROW(spdlog_ex("sid::duplicate: invalid SID received")); - - DWORD sid_size = ::GetLengthSid(p_src_sid); - SID* p_dest_sid = (SID*) ::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sid_size); - - if (!p_dest_sid) - SPDLOG_THROW(win32_error("HeapAlloc")); - - if (!CopySid(sid_size, p_dest_sid, p_src_sid)) - { - sid::free(&p_dest_sid); - SPDLOG_THROW(win32_error("CopySid")); - } - - return p_dest_sid; - } - - static void free(SID** ppsid) - { - if (ppsid && *ppsid) - { - if (!HeapFree(GetProcessHeap(), 0, (LPVOID) *ppsid)) - { - SPDLOG_THROW(win32_error("HeapFree")); - } - } - - *ppsid = nullptr; - } - - static SID* get_current_user_sid() - { - struct process_token_t - { - HANDLE hToken_; - bool hasToken_; - - process_token_t(HANDLE process) - : hToken_(0) - , hasToken_(OpenProcessToken(process, TOKEN_QUERY, &hToken_)) - { - if (!hasToken_) - SPDLOG_THROW(win32_error("OpenProcessToken")); - } - - ~process_token_t() - { - if (hasToken_ && !CloseHandle(hToken_)) - win32_error::report(win32_error::format("CloseHandle")); - } - } current_process_token(GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here! - - // Get the required size - DWORD tusize = 0; - GetTokenInformation(current_process_token.hToken_, TokenUser, NULL, 0, &tusize); - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) - SPDLOG_THROW(win32_error("GetTokenInformation")); - - std::vector buffer(tusize); - if (!GetTokenInformation(current_process_token.hToken_, TokenUser, (LPVOID) buffer.data(), tusize, &tusize)) - SPDLOG_THROW(win32_error("GetTokenInformation")); - - return sid::duplicate((SID *) ((TOKEN_USER*) buffer.data())->User.Sid); - } -}; - -struct eventlog -{ - static WORD get_event_type(details::log_msg const& msg) - { - WORD type = EVENTLOG_SUCCESS; - if (msg.level >= level::info) - { - type = EVENTLOG_INFORMATION_TYPE; - if (msg.level >= level::warn) - { - type = EVENTLOG_WARNING_TYPE; - if (msg.level >= level::err) - { - type = EVENTLOG_ERROR_TYPE; - } - } - } - return type; - } - - static WORD get_event_category(details::log_msg const& msg) - { - WORD category = 1; - if (msg.level >= level::debug) - { - category = 2; - if (msg.level >= level::info) - { - category = 3; - if (msg.level >= level::warn) - { - category = 4; - if (msg.level >= level::err) - { - category = 5; - if (msg.level >= level::critical) - { - category = 6; - } - } - } - } - } - return category; - } -}; - -class sink : public base_sink -{ -private: - HANDLE hEventLog_; - SID* current_user_sid_; - std::string source_; - std::string log_; - - void add_registry_info(std::string const& message_file_path) - { - std::string subkey("SYSTEM\\CurrentControlSet\\Services\\EventLog\\"); - subkey += log_ + "\\" + source_; - - ::HKEY hkey {}; - DWORD disposition {}; - long stat = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey.c_str(), 0, NULL, - REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, - &hkey, &disposition); - if (stat == ERROR_SUCCESS) - { - if (disposition == REG_CREATED_NEW_KEY && !message_file_path.empty()) - { - auto const expanded_message_file_path_length = ExpandEnvironmentStrings(message_file_path.c_str(), (LPSTR) &disposition, 0); - std::vectorexpanded_message_file_path(expanded_message_file_path_length + 1); - ExpandEnvironmentStrings(message_file_path.c_str(), expanded_message_file_path.data(), expanded_message_file_path_length); - - RegSetValueEx(hkey, "EventMessageFile", 0, REG_SZ, (LPBYTE) expanded_message_file_path.data(), expanded_message_file_path_length); - DWORD typesSupported = 7; - RegSetValueEx(hkey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &typesSupported, sizeof(DWORD)); - } - } - else - { - SPDLOG_THROW(win32_error("RegCreateKeyEx", stat)); - } - - RegCloseKey(hkey); - } - -protected: - void sink_it_(const details::log_msg &msg) override - { - memory_buf_t formatted; - formatter_->format(msg, formatted); - - auto formatted_string = fmt::to_string(formatted); - auto formatted_string_lpsz = formatted_string.c_str(); - - if (!ReportEvent(hEventLog_, eventlog::get_event_type(msg), eventlog::get_event_category(msg), 1000, current_user_sid_, 1, 0, &formatted_string_lpsz, nullptr)) - SPDLOG_THROW(win32_error("ReportEvent")); - } - - void flush_() override {} - -public: - sink( - std::string const& source, - std::string const& log, - std::string const& message_file_path) - : source_(source) - , log_(log) - { - if (!message_file_path.empty()) - add_registry_info(message_file_path); - - current_user_sid_ = sid::get_current_user_sid(); - hEventLog_ = RegisterEventSource(nullptr, source.c_str()); - if (!hEventLog_) - SPDLOG_THROW(win32_error("RegisterEventSource")); - } - - ~sink() - { - try - { - if (hEventLog_ && !DeregisterEventSource(hEventLog_)) - win32_error::report(win32_error::format("DeregisterEventSource")); - - sid::free(¤t_user_sid_); - } - catch (std::exception const& e) - { - win32_error::report(e.what()); - } - } -}; - -} // namespace eventlog_sink_win32 - -template -eventlog_sink::eventlog_sink(std::string const& source, std::string const& log, std::string const& message_file_path) - : impl_(std::make_unique(source, log, message_file_path)) -{} - -template -void eventlog_sink::sink_it_(const details::log_msg &msg) -{ - impl_->log(msg); -} - diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h new file mode 100644 index 00000000..78a22027 --- /dev/null +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -0,0 +1,357 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +// Writing to Windows Event Log requires the registry entries below to be present, with the following modifications: +// 1. {log_name} should be replaced with your log name (e.g. your application name) +// 2. {source_name} should be replaced with the specific source name and the key should be duplicated for +// each source used in the application +// +// Since typically modifications of this kind require elevation, it's better to do it as a part of setup procedure. +// However, the win_eventlog_sink constructor can do it for you in runtime if you set the message_file_path parameter to +// win_eventlog_sink::DEFAULT_MESSAGE_FILE +// +// You can also specify a custom message file if needed. +// Please refer to Event Log functions descriptions in MSDN for more details on custom message files. + +/*--------------------------------------------------------------------------------------- + +Windows Registry Editor Version 5.00 + +[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\{log_name}] + +[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\{log_name}\{source_name}] +"TypesSupported"=dword:00000007 +"EventMessageFile"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,\ + 00,6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,\ + 5c,00,6d,00,73,00,63,00,6f,00,72,00,65,00,65,00,2e,00,64,00,6c,00,6c,00,00,\ + 00 + +-----------------------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include +#include + +#include +#include + +namespace spdlog { +namespace sinks { + +namespace win_eventlog { + +namespace internal +{ + +struct utils +{ + /** Reports a message to stderr */ + static void report(char const* message) SPDLOG_NOEXCEPT + { + fprintf(stderr, "%s", message); + fflush(stderr); + } + + /** Reports a message to stderr */ + static void report(std::string const& message) SPDLOG_NOEXCEPT + { + report(message.c_str()); + } + + /** Runs a func and reports an exception to stderr if any */ + template + static Result report_exceptions(std::function func, Result defaultValue) + { + try + { + return func(); + } + catch (std::exception const& e) + { + report(e.what()); + return defaultValue; + } + } + + /** Runs a func and reports an exception to stderr if any */ + static void report_exceptions(std::function func) + { + report_exceptions([&func] () { func(); return 0; }, 0); + } +}; + +/** Windows error */ +struct win32_error : public spdlog_ex +{ + /** Formats an error report line: "user-message: error-code (system message)" */ + static std::string format(std::string const& user_message, DWORD error_code = GetLastError()) + { + std::string system_message; + + LPSTR format_message_result {}; + auto format_message_succeeded = FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, + error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &format_message_result, 0, nullptr); + + if (format_message_succeeded && format_message_result) + { + system_message = fmt::format(" ({})", format_message_result); + } + + if (format_message_result) + LocalFree((HLOCAL) format_message_result); + + return fmt::format("{}: {}{}", user_message, error_code, system_message); + } + + win32_error(std::string const& func_name, DWORD error = GetLastError()) + : spdlog_ex(format(func_name, error)) + {} +}; + +/** Wrapper for security identifiers (SID) on Windows */ +struct sid_t +{ + std::vector buffer_; + +public: + sid_t() + {} + + /** creates a wrapped SID copy */ + static sid_t duplicate_sid(PSID psid) + { + if (!IsValidSid(psid)) + SPDLOG_THROW(spdlog_ex("sid_t::sid_t(): invalid SID received")); + + auto const sid_length {::GetLengthSid(psid)}; + + sid_t result; + result.buffer_.resize(sid_length); + if (!CopySid(sid_length, (PSID) result.as_sid(), psid)) + SPDLOG_THROW(win32_error("CopySid")); + + return result; + } + + /** Retrieves pointer to the internal buffer contents as SID* */ + SID * as_sid() const + { + return buffer_.empty() ? nullptr : (SID *) buffer_.data(); + } + + /** Get SID for the current user */ + static sid_t get_current_user_sid() + { + /* create and init RAII holder for process token */ + struct process_token_t + { + HANDLE hToken_; + bool hasToken_; + + process_token_t(HANDLE process) + : hToken_(0) + , hasToken_(OpenProcessToken(process, TOKEN_QUERY, &hToken_)) + { + if (!hasToken_) + SPDLOG_THROW(win32_error("OpenProcessToken")); + } + + ~process_token_t() + { + if (hasToken_ && !CloseHandle(hToken_)) + utils::report(win32_error::format("CloseHandle")); + } + } current_process_token(GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here! + + // Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size + DWORD tusize = 0; + GetTokenInformation(current_process_token.hToken_, TokenUser, NULL, 0, &tusize); + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + SPDLOG_THROW(win32_error("GetTokenInformation")); + + // get user token + std::vector buffer(tusize); + if (!GetTokenInformation(current_process_token.hToken_, TokenUser, (LPVOID) buffer.data(), tusize, &tusize)) + SPDLOG_THROW(win32_error("GetTokenInformation")); + + // create a wrapper of the SID data as stored in the user token + return sid_t::duplicate_sid(((TOKEN_USER*) buffer.data())->User.Sid); + } +}; + +struct eventlog +{ + static WORD get_event_type(details::log_msg const& msg) + { + switch (msg.level) + { + case level::trace: + case level::debug: + return EVENTLOG_SUCCESS; + + case level::info: + return EVENTLOG_INFORMATION_TYPE; + + case level::warn: + return EVENTLOG_WARNING_TYPE; + + case level::err: + case level::critical: + case level::off: + return EVENTLOG_ERROR_TYPE; + + default: + // should be unreachable + SPDLOG_THROW(std::logic_error(fmt::format("Unsupported log level {}", msg.level))); + } + } + + static WORD get_event_category(details::log_msg const& msg) + { + return (WORD) msg.level; + } +}; + +} // namespace internal + + +/* + * Windows Event Log sink + */ +template +class win_eventlog_sink : public base_sink +{ +private: + HANDLE hEventLog_ {NULL}; + internal::sid_t current_user_sid_; + std::string source_; + WORD event_id_ {DEFAULT_EVENT_ID}; + + HANDLE event_log_handle() + { + if (!hEventLog_) + { + hEventLog_ = RegisterEventSource(nullptr, source_.c_str()); + if (!hEventLog_ || hEventLog_ == (HANDLE) ERROR_ACCESS_DENIED) + SPDLOG_THROW(internal::win32_error("RegisterEventSource")); + } + + return hEventLog_; + } + +protected: + void sink_it_(const details::log_msg &msg) override + { + using namespace internal; + + memory_buf_t formatted; + formatter_->format(msg, formatted); + + auto formatted_string = fmt::to_string(formatted); + auto formatted_string_lpsz = formatted_string.c_str(); + + bool succeeded = ReportEvent( + event_log_handle(), + eventlog::get_event_type(msg), + eventlog::get_event_category(msg), + event_id_, + current_user_sid_.as_sid(), + 1, + 0, + &formatted_string_lpsz, + nullptr); + + if (!succeeded) + SPDLOG_THROW(win32_error("ReportEvent")); + } + + void flush_() override {} + +public: + static const std::string DEFAULT_MESSAGE_FILE; + static const WORD DEFAULT_EVENT_ID {1000}; + + win_eventlog_sink( std::string const& source ) + : source_(source) + { + using namespace internal; + + utils::report_exceptions([this] () { + current_user_sid_ = sid_t::get_current_user_sid(); + }); + } + + ~win_eventlog_sink() + { + using namespace internal; + utils::report_exceptions([this]() + { + if (hEventLog_ && !DeregisterEventSource(hEventLog_)) + utils::report(win32_error::format("DeregisterEventSource")); + }); + } + + /** + Register the log source in the Windows registry. + + Requires elevation on Windows Vista and later. + */ + void add_registry_info(std::string const& log = "Application", std::string const& message_file_path = DEFAULT_MESSAGE_FILE, WORD event_id = DEFAULT_EVENT_ID) + { + using namespace internal; + + event_id_ = event_id; + + std::string subkey = fmt::format("SYSTEM\\CurrentControlSet\\Services\\EventLog\\{}\\{}", log, source_); + + ::HKEY hkey {}; + DWORD disposition {}; + long stat = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey.c_str(), 0, NULL, + REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, + &hkey, &disposition); + if (stat == ERROR_SUCCESS) + { + if (disposition == REG_CREATED_NEW_KEY && !message_file_path.empty()) + { + auto const expanded_message_file_path_length = ExpandEnvironmentStrings(message_file_path.c_str(), (LPSTR) &disposition, 0); + if (!expanded_message_file_path_length) + SPDLOG_THROW(win32_error("ExpandEnvironmentStrings")); + + std::vectorexpanded_message_file_path(expanded_message_file_path_length); + ExpandEnvironmentStrings(message_file_path.c_str(), expanded_message_file_path.data(), expanded_message_file_path_length); // this can't fail if the preivous ExpandEnvironmentStrings succeeded + + stat = RegSetValueEx(hkey, "EventMessageFile", 0, REG_SZ, (LPBYTE) expanded_message_file_path.data(), expanded_message_file_path_length); + if (stat != ERROR_SUCCESS) + SPDLOG_THROW(win32_error("RegSetValueEx", stat)); + + DWORD typesSupported = 7; + stat = RegSetValueEx(hkey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &typesSupported, sizeof(DWORD)); + if (stat != ERROR_SUCCESS) + SPDLOG_THROW(win32_error("RegSetValueEx", stat)); + } + } + else + { + SPDLOG_THROW(win32_error("RegCreateKeyEx", stat)); + } + + RegCloseKey(hkey); + } + +}; + +template +const std::string win_eventlog_sink::DEFAULT_MESSAGE_FILE = "%systemroot%\\system32\\mscoree.dll"; + +} // namespace win_eventlog + +using win_eventlog_sink_mt = win_eventlog::win_eventlog_sink; +using win_eventlog_sink_st = win_eventlog::win_eventlog_sink; + +} // namespace sinks +} // namespace spdlog + diff --git a/tests/test_eventlog.cpp b/tests/test_eventlog.cpp index 36dde56f..a8e06255 100644 --- a/tests/test_eventlog.cpp +++ b/tests/test_eventlog.cpp @@ -2,10 +2,13 @@ #include "test_sink.h" #include "spdlog/fmt/bin_to_hex.h" -#include "spdlog/sinks/eventlog_sink.h" - #if _WIN32 +#include "spdlog/sinks/win_eventlog_sink.h" + +static const LPCSTR TEST_LOG = "my log"; +static const LPCSTR TEST_SOURCE = "my source"; + static void test_single_print(std::function do_print, std::string const& expectedContents, WORD expectedEventType) { do_print(expectedContents); @@ -22,7 +25,7 @@ static void test_single_print(std::function do_print, if (handle_) REQUIRE(CloseEventLog(handle_)); } - } eventLog {OpenEventLog(nullptr, "my_source")}; + } eventLog {OpenEventLog(nullptr, TEST_SOURCE)}; REQUIRE(eventLog.handle_); @@ -49,7 +52,7 @@ TEST_CASE("eventlog", "[eventlog]") { using namespace spdlog; - auto test_sink = std::make_shared("my_source", "my_spd_log"); + auto test_sink = std::make_shared(TEST_SOURCE); spdlog::logger test_logger("eventlog", test_sink); test_logger.set_level(level::trace); From 2435f46d066a86b9aaaae8c309a17cb47ec3fce9 Mon Sep 17 00:00:00 2001 From: bandana2004 Date: Sun, 9 Feb 2020 18:04:38 +0200 Subject: [PATCH 25/81] fix registry key handle leak in win_eventlog_sink::add_registry_info --- include/spdlog/sinks/win_eventlog_sink.h | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index 78a22027..b2ec3dbd 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -306,13 +306,23 @@ public: event_id_ = event_id; - std::string subkey = fmt::format("SYSTEM\\CurrentControlSet\\Services\\EventLog\\{}\\{}", log, source_); + std::string logSourceRegKeyName = fmt::format("SYSTEM\\CurrentControlSet\\Services\\EventLog\\{}\\{}", log, source_); + + struct hkey_t + { + ::HKEY handle_ {}; + + ~hkey_t() + { + if (handle_) + RegCloseKey(handle_); + } + } logSourceRegKey; - ::HKEY hkey {}; DWORD disposition {}; - long stat = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey.c_str(), 0, NULL, + long stat = RegCreateKeyEx(HKEY_LOCAL_MACHINE, logSourceRegKeyName.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, - &hkey, &disposition); + &logSourceRegKey.handle_, &disposition); if (stat == ERROR_SUCCESS) { if (disposition == REG_CREATED_NEW_KEY && !message_file_path.empty()) @@ -324,12 +334,12 @@ public: std::vectorexpanded_message_file_path(expanded_message_file_path_length); ExpandEnvironmentStrings(message_file_path.c_str(), expanded_message_file_path.data(), expanded_message_file_path_length); // this can't fail if the preivous ExpandEnvironmentStrings succeeded - stat = RegSetValueEx(hkey, "EventMessageFile", 0, REG_SZ, (LPBYTE) expanded_message_file_path.data(), expanded_message_file_path_length); + stat = RegSetValueEx(logSourceRegKey.handle_, "EventMessageFile", 0, REG_SZ, (LPBYTE) expanded_message_file_path.data(), expanded_message_file_path_length); if (stat != ERROR_SUCCESS) SPDLOG_THROW(win32_error("RegSetValueEx", stat)); DWORD typesSupported = 7; - stat = RegSetValueEx(hkey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &typesSupported, sizeof(DWORD)); + stat = RegSetValueEx(logSourceRegKey.handle_, "TypesSupported", 0, REG_DWORD, (LPBYTE) &typesSupported, sizeof(DWORD)); if (stat != ERROR_SUCCESS) SPDLOG_THROW(win32_error("RegSetValueEx", stat)); } @@ -338,8 +348,6 @@ public: { SPDLOG_THROW(win32_error("RegCreateKeyEx", stat)); } - - RegCloseKey(hkey); } }; From d8701890b2a6625f708dcdf79fcd795e0b3e920c Mon Sep 17 00:00:00 2001 From: bandana2004 Date: Sun, 9 Feb 2020 18:12:54 +0200 Subject: [PATCH 26/81] cleanup win_eventlog_sink --- include/spdlog/sinks/win_eventlog_sink.h | 39 ++++++------------------ 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index b2ec3dbd..28716fdd 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -60,27 +60,6 @@ struct utils { report(message.c_str()); } - - /** Runs a func and reports an exception to stderr if any */ - template - static Result report_exceptions(std::function func, Result defaultValue) - { - try - { - return func(); - } - catch (std::exception const& e) - { - report(e.what()); - return defaultValue; - } - } - - /** Runs a func and reports an exception to stderr if any */ - static void report_exceptions(std::function func) - { - report_exceptions([&func] () { func(); return 0; }, 0); - } }; /** Windows error */ @@ -279,20 +258,22 @@ public: : source_(source) { using namespace internal; - - utils::report_exceptions([this] () { + try + { current_user_sid_ = sid_t::get_current_user_sid(); - }); + } + catch (std::exception const& e) + { + utils::report(e.what()); + } } ~win_eventlog_sink() { using namespace internal; - utils::report_exceptions([this]() - { - if (hEventLog_ && !DeregisterEventSource(hEventLog_)) - utils::report(win32_error::format("DeregisterEventSource")); - }); + + if (hEventLog_ && !DeregisterEventSource(hEventLog_)) + utils::report(win32_error::format("DeregisterEventSource")); } /** From 67a8ecf2bf5f253efbbcdc3dbd671510eba8ef32 Mon Sep 17 00:00:00 2001 From: bandana2004 Date: Sun, 9 Feb 2020 22:59:02 +0200 Subject: [PATCH 27/81] Remove registration and stderr reporting from win_eventlog_sink --- include/spdlog/sinks/win_eventlog_sink.h | 109 ++++------------------- 1 file changed, 15 insertions(+), 94 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index 28716fdd..03267086 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -7,8 +7,8 @@ // each source used in the application // // Since typically modifications of this kind require elevation, it's better to do it as a part of setup procedure. -// However, the win_eventlog_sink constructor can do it for you in runtime if you set the message_file_path parameter to -// win_eventlog_sink::DEFAULT_MESSAGE_FILE +// The snippet below uses mscoree.dll as the message file as it exists on most of the Windows systems anyway and +// happens to contain the needed resource. // // You can also specify a custom message file if needed. // Please refer to Event Log functions descriptions in MSDN for more details on custom message files. @@ -46,22 +46,6 @@ namespace win_eventlog { namespace internal { -struct utils -{ - /** Reports a message to stderr */ - static void report(char const* message) SPDLOG_NOEXCEPT - { - fprintf(stderr, "%s", message); - fflush(stderr); - } - - /** Reports a message to stderr */ - static void report(std::string const& message) SPDLOG_NOEXCEPT - { - report(message.c_str()); - } -}; - /** Windows error */ struct win32_error : public spdlog_ex { @@ -141,8 +125,8 @@ public: ~process_token_t() { - if (hasToken_ && !CloseHandle(hToken_)) - utils::report(win32_error::format("CloseHandle")); + if (hasToken_) + CloseHandle(hToken_); } } current_process_token(GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here! @@ -205,10 +189,10 @@ template class win_eventlog_sink : public base_sink { private: - HANDLE hEventLog_ {NULL}; + HANDLE hEventLog_ {NULL}; internal::sid_t current_user_sid_; - std::string source_; - WORD event_id_ {DEFAULT_EVENT_ID}; + std::string source_; + WORD event_id_; HANDLE event_log_handle() { @@ -251,91 +235,28 @@ protected: void flush_() override {} public: - static const std::string DEFAULT_MESSAGE_FILE; - static const WORD DEFAULT_EVENT_ID {1000}; - - win_eventlog_sink( std::string const& source ) + win_eventlog_sink(std::string const& source, WORD event_id = 1000 /* according to mscoree.dll */) : source_(source) + , event_id_ (event_id) { - using namespace internal; try { - current_user_sid_ = sid_t::get_current_user_sid(); + current_user_sid_ = internal::sid_t::get_current_user_sid(); } - catch (std::exception const& e) + catch (...) { - utils::report(e.what()); + // get_current_user_sid() is unlikely to fail and if it does, we can still proceed without + // current_user_sid but in the event log the record will have no user name } } ~win_eventlog_sink() { - using namespace internal; - - if (hEventLog_ && !DeregisterEventSource(hEventLog_)) - utils::report(win32_error::format("DeregisterEventSource")); + if (hEventLog_) + DeregisterEventSource(hEventLog_); } - - /** - Register the log source in the Windows registry. - - Requires elevation on Windows Vista and later. - */ - void add_registry_info(std::string const& log = "Application", std::string const& message_file_path = DEFAULT_MESSAGE_FILE, WORD event_id = DEFAULT_EVENT_ID) - { - using namespace internal; - - event_id_ = event_id; - - std::string logSourceRegKeyName = fmt::format("SYSTEM\\CurrentControlSet\\Services\\EventLog\\{}\\{}", log, source_); - - struct hkey_t - { - ::HKEY handle_ {}; - - ~hkey_t() - { - if (handle_) - RegCloseKey(handle_); - } - } logSourceRegKey; - - DWORD disposition {}; - long stat = RegCreateKeyEx(HKEY_LOCAL_MACHINE, logSourceRegKeyName.c_str(), 0, NULL, - REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, - &logSourceRegKey.handle_, &disposition); - if (stat == ERROR_SUCCESS) - { - if (disposition == REG_CREATED_NEW_KEY && !message_file_path.empty()) - { - auto const expanded_message_file_path_length = ExpandEnvironmentStrings(message_file_path.c_str(), (LPSTR) &disposition, 0); - if (!expanded_message_file_path_length) - SPDLOG_THROW(win32_error("ExpandEnvironmentStrings")); - - std::vectorexpanded_message_file_path(expanded_message_file_path_length); - ExpandEnvironmentStrings(message_file_path.c_str(), expanded_message_file_path.data(), expanded_message_file_path_length); // this can't fail if the preivous ExpandEnvironmentStrings succeeded - - stat = RegSetValueEx(logSourceRegKey.handle_, "EventMessageFile", 0, REG_SZ, (LPBYTE) expanded_message_file_path.data(), expanded_message_file_path_length); - if (stat != ERROR_SUCCESS) - SPDLOG_THROW(win32_error("RegSetValueEx", stat)); - - DWORD typesSupported = 7; - stat = RegSetValueEx(logSourceRegKey.handle_, "TypesSupported", 0, REG_DWORD, (LPBYTE) &typesSupported, sizeof(DWORD)); - if (stat != ERROR_SUCCESS) - SPDLOG_THROW(win32_error("RegSetValueEx", stat)); - } - } - else - { - SPDLOG_THROW(win32_error("RegCreateKeyEx", stat)); - } - } - }; -template -const std::string win_eventlog_sink::DEFAULT_MESSAGE_FILE = "%systemroot%\\system32\\mscoree.dll"; - } // namespace win_eventlog using win_eventlog_sink_mt = win_eventlog::win_eventlog_sink; From 640921cd3f5d61f07eb62a5cc0b11def823b5054 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 00:37:45 +0200 Subject: [PATCH 28/81] Optimize win_eventlog to avoid string allocation --- include/spdlog/sinks/win_eventlog_sink.h | 54 ++++++++++++++---------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index 03267086..fa9165f2 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -37,6 +37,7 @@ Windows Registry Editor Version 5.00 #include #include +#include namespace spdlog { namespace sinks { @@ -55,7 +56,7 @@ struct win32_error : public spdlog_ex std::string system_message; LPSTR format_message_result {}; - auto format_message_succeeded = FormatMessage( + auto format_message_succeeded = ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &format_message_result, 0, nullptr); @@ -65,7 +66,9 @@ struct win32_error : public spdlog_ex } if (format_message_result) - LocalFree((HLOCAL) format_message_result); + { + LocalFree((HLOCAL)format_message_result); + } return fmt::format("{}: {}{}", user_message, error_code, system_message); } @@ -87,15 +90,19 @@ public: /** creates a wrapped SID copy */ static sid_t duplicate_sid(PSID psid) { - if (!IsValidSid(psid)) + if (!::IsValidSid(psid)) + { SPDLOG_THROW(spdlog_ex("sid_t::sid_t(): invalid SID received")); + } auto const sid_length {::GetLengthSid(psid)}; sid_t result; result.buffer_.resize(sid_length); - if (!CopySid(sid_length, (PSID) result.as_sid(), psid)) + if (!::CopySid(sid_length, (PSID)result.as_sid(), psid)) + { SPDLOG_THROW(win32_error("CopySid")); + } return result; } @@ -112,34 +119,36 @@ public: /* create and init RAII holder for process token */ struct process_token_t { - HANDLE hToken_; - bool hasToken_; - - process_token_t(HANDLE process) - : hToken_(0) - , hasToken_(OpenProcessToken(process, TOKEN_QUERY, &hToken_)) + HANDLE token_handle_= INVALID_HANDLE_VALUE; + explicit process_token_t(HANDLE process) { - if (!hasToken_) + if (!::OpenProcessToken(process, TOKEN_QUERY, &token_handle_)) + { SPDLOG_THROW(win32_error("OpenProcessToken")); + } } ~process_token_t() { - if (hasToken_) - CloseHandle(hToken_); + ::CloseHandle(token_handle_); } - } current_process_token(GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here! + + } current_process_token(::GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here! // Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size DWORD tusize = 0; - GetTokenInformation(current_process_token.hToken_, TokenUser, NULL, 0, &tusize); - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + ::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize); + if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) + { SPDLOG_THROW(win32_error("GetTokenInformation")); + } // get user token - std::vector buffer(tusize); - if (!GetTokenInformation(current_process_token.hToken_, TokenUser, (LPVOID) buffer.data(), tusize, &tusize)) + std::vector buffer(static_cast(tusize)); + if (!GetTokenInformation(current_process_token.token_handle_, TokenUser, (LPVOID)buffer.data(), tusize, &tusize)) + { SPDLOG_THROW(win32_error("GetTokenInformation")); + } // create a wrapper of the SID data as stored in the user token return sid_t::duplicate_sid(((TOKEN_USER*) buffer.data())->User.Sid); @@ -213,10 +222,9 @@ protected: memory_buf_t formatted; formatter_->format(msg, formatted); - - auto formatted_string = fmt::to_string(formatted); - auto formatted_string_lpsz = formatted_string.c_str(); - + formatted.push_back('\0'); + LPCSTR lp_str = static_cast(formatted.data()); + bool succeeded = ReportEvent( event_log_handle(), eventlog::get_event_type(msg), @@ -225,7 +233,7 @@ protected: current_user_sid_.as_sid(), 1, 0, - &formatted_string_lpsz, + &lp_str, nullptr); if (!succeeded) From 90454a93b215032964bb72756ecae4ed711e19b7 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 00:37:48 +0200 Subject: [PATCH 29/81] Update test_eventlog.cpp --- tests/test_eventlog.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/test_eventlog.cpp b/tests/test_eventlog.cpp index a8e06255..4a3d3fce 100644 --- a/tests/test_eventlog.cpp +++ b/tests/test_eventlog.cpp @@ -1,20 +1,17 @@ #include "includes.h" #include "test_sink.h" -#include "spdlog/fmt/bin_to_hex.h" #if _WIN32 #include "spdlog/sinks/win_eventlog_sink.h" -static const LPCSTR TEST_LOG = "my log"; -static const LPCSTR TEST_SOURCE = "my source"; +static const LPCSTR TEST_SOURCE = "spdlog_test"; -static void test_single_print(std::function do_print, std::string const& expectedContents, WORD expectedEventType) +static void test_single_print(std::function do_log, std::string const& expected_contents, WORD expected_ev_type) { - do_print(expectedContents); - using namespace std::chrono; - const auto expectedTimeGenerated = duration_cast(system_clock::now().time_since_epoch()).count(); + do_log(expected_contents); + const auto expected_time_generated = duration_cast(system_clock::now().time_since_epoch()).count(); struct handle_t { @@ -23,29 +20,31 @@ static void test_single_print(std::function do_print, ~handle_t() { if (handle_) + { REQUIRE(CloseEventLog(handle_)); + } } - } eventLog {OpenEventLog(nullptr, TEST_SOURCE)}; + } event_log {::OpenEventLog(nullptr, TEST_SOURCE)}; - REQUIRE(eventLog.handle_); + REQUIRE(event_log.handle_); - DWORD readBytes {}, sizeNeeded {}; - auto ok = ReadEventLog(eventLog.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &readBytes, 0, &readBytes, &sizeNeeded); + DWORD read_bytes {}, size_needed{}; + auto ok = ::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed); REQUIRE(!ok); - REQUIRE(GetLastError() == ERROR_INSUFFICIENT_BUFFER); + REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER); - std::vector recordBuffer(sizeNeeded); - PEVENTLOGRECORD record = (PEVENTLOGRECORD)recordBuffer.data(); + std::vector record_buffer(size_needed); + PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data(); - ok = ReadEventLog(eventLog.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, sizeNeeded, &readBytes, &sizeNeeded); + ok = ::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed); REQUIRE(ok); REQUIRE(record->NumStrings == 1); - REQUIRE(record->EventType == expectedEventType); - REQUIRE(record->TimeGenerated == expectedTimeGenerated); + REQUIRE(record->EventType == expected_ev_type); + REQUIRE(record->TimeGenerated == expected_time_generated); std::string message_in_log(((char*) record + record->StringOffset)); - REQUIRE(message_in_log == expectedContents + "\r\n"); + REQUIRE(message_in_log == expected_contents + spdlog::details::os::default_eol); } TEST_CASE("eventlog", "[eventlog]") @@ -59,6 +58,7 @@ TEST_CASE("eventlog", "[eventlog]") test_sink->set_pattern("%v"); + test_single_print([&test_logger] (std::string const& msg) { test_logger.trace(msg); }, "my trace message", EVENTLOG_SUCCESS); test_single_print([&test_logger] (std::string const& msg) { test_logger.debug(msg); }, "my debug message", EVENTLOG_SUCCESS); test_single_print([&test_logger] (std::string const& msg) { test_logger.info(msg); }, "my info message", EVENTLOG_INFORMATION_TYPE); test_single_print([&test_logger] (std::string const& msg) { test_logger.warn(msg); }, "my warn message", EVENTLOG_WARNING_TYPE); From 601bdfb1b4294a8e478811daaffa6a013777647c Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 00:53:01 +0200 Subject: [PATCH 30/81] Minor cleanup --- include/spdlog/sinks/win_eventlog_sink.h | 12 ++++++++---- tests/test_eventlog.cpp | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index fa9165f2..21325bf7 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -145,7 +145,7 @@ public: // get user token std::vector buffer(static_cast(tusize)); - if (!GetTokenInformation(current_process_token.token_handle_, TokenUser, (LPVOID)buffer.data(), tusize, &tusize)) + if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser, (LPVOID)buffer.data(), tusize, &tusize)) { SPDLOG_THROW(win32_error("GetTokenInformation")); } @@ -207,9 +207,11 @@ private: { if (!hEventLog_) { - hEventLog_ = RegisterEventSource(nullptr, source_.c_str()); - if (!hEventLog_ || hEventLog_ == (HANDLE) ERROR_ACCESS_DENIED) + hEventLog_ = ::RegisterEventSource(nullptr, source_.c_str()); + if (!hEventLog_ || hEventLog_ == (HANDLE)ERROR_ACCESS_DENIED) + { SPDLOG_THROW(internal::win32_error("RegisterEventSource")); + } } return hEventLog_; @@ -225,7 +227,7 @@ protected: formatted.push_back('\0'); LPCSTR lp_str = static_cast(formatted.data()); - bool succeeded = ReportEvent( + auto succeeded = ::ReportEvent( event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), @@ -237,7 +239,9 @@ protected: nullptr); if (!succeeded) + { SPDLOG_THROW(win32_error("ReportEvent")); + } } void flush_() override {} diff --git a/tests/test_eventlog.cpp b/tests/test_eventlog.cpp index 4a3d3fce..114f2cb7 100644 --- a/tests/test_eventlog.cpp +++ b/tests/test_eventlog.cpp @@ -1,8 +1,8 @@ +#if _WIN32 + #include "includes.h" #include "test_sink.h" -#if _WIN32 - #include "spdlog/sinks/win_eventlog_sink.h" static const LPCSTR TEST_SOURCE = "spdlog_test"; @@ -66,4 +66,4 @@ TEST_CASE("eventlog", "[eventlog]") test_single_print([&test_logger] (std::string const& msg) { test_logger.critical(msg); }, "my critical message", EVENTLOG_ERROR_TYPE); } -#endif \ No newline at end of file +#endif //_WIN32 \ No newline at end of file From 4f3224321414619cdcab732fb1f395e78a44e7ba Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 00:54:43 +0200 Subject: [PATCH 31/81] Update comment --- include/spdlog/sinks/win_eventlog_sink.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index 21325bf7..b67cfc6e 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -2,8 +2,8 @@ // Distributed under the MIT License (http://opensource.org/licenses/MIT) // Writing to Windows Event Log requires the registry entries below to be present, with the following modifications: -// 1. {log_name} should be replaced with your log name (e.g. your application name) -// 2. {source_name} should be replaced with the specific source name and the key should be duplicated for +// 1. should be replaced with your log name (e.g. your application name) +// 2. should be replaced with the specific source name and the key should be duplicated for // each source used in the application // // Since typically modifications of this kind require elevation, it's better to do it as a part of setup procedure. @@ -17,9 +17,9 @@ Windows Registry Editor Version 5.00 -[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\{log_name}] +[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\] -[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\{log_name}\{source_name}] +[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\\] "TypesSupported"=dword:00000007 "EventMessageFile"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,\ 00,6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,\ From e2789531912a5c6ab28a90387f97c52963eec08a Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 10 Feb 2020 02:23:25 +0200 Subject: [PATCH 32/81] Update win_eventlog_sink.h --- include/spdlog/sinks/win_eventlog_sink.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index b67cfc6e..51bfca9f 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -137,10 +137,9 @@ public: // Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size DWORD tusize = 0; - ::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize); - if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) + if(::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize)) { - SPDLOG_THROW(win32_error("GetTokenInformation")); + SPDLOG_THROW(win32_error("GetTokenInformation should fail")); } // get user token From 58a5e654f94e8e0fda1f3ec1ea596451a3079692 Mon Sep 17 00:00:00 2001 From: Mario Emmenlauer Date: Fri, 7 Feb 2020 17:41:50 +0100 Subject: [PATCH 33/81] tests/utils.cpp: Use binary mode for reading test results, to preserve EOL --- tests/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/utils.cpp b/tests/utils.cpp index 255a4fec..cdfdbaca 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -21,7 +21,7 @@ void prepare_logdir() std::string file_contents(const std::string &filename) { - std::ifstream ifs(filename); + std::ifstream ifs(filename, std::ios_base::binary); if (!ifs) { throw std::runtime_error("Failed open file "); From 87acec6a91ec02a68d05d217f9d154ea3e0cbc39 Mon Sep 17 00:00:00 2001 From: Mario Emmenlauer Date: Tue, 4 Feb 2020 22:23:36 +0100 Subject: [PATCH 34/81] Make tests support empty SPDLOG_EOL with new helper method require_message_count() --- tests/test_async.cpp | 7 ++++--- tests/test_daily_logger.cpp | 8 +++----- tests/test_errors.cpp | 7 ++++--- tests/test_file_logging.cpp | 17 +++++++++++------ tests/test_macros.cpp | 7 ++++--- tests/utils.cpp | 9 +++++++++ tests/utils.h | 2 ++ 7 files changed, 37 insertions(+), 20 deletions(-) diff --git a/tests/test_async.cpp b/tests/test_async.cpp index 166ac21e..7b859a13 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -169,9 +169,10 @@ TEST_CASE("to_file", "[async]") } } - REQUIRE(count_lines(filename) == messages); + require_message_count(filename, messages); auto contents = file_contents(filename); - REQUIRE(ends_with(contents, std::string("Hello message #1023\n"))); + using spdlog::details::os::default_eol; + REQUIRE(ends_with(contents, fmt::format("Hello message #1023{}", default_eol))); } TEST_CASE("to_file multi-workers", "[async]") @@ -191,5 +192,5 @@ TEST_CASE("to_file multi-workers", "[async]") } } - REQUIRE(count_lines(filename) == messages); + require_message_count(filename, messages); } diff --git a/tests/test_daily_logger.cpp b/tests/test_daily_logger.cpp index cf2002a3..996fe4ee 100644 --- a/tests/test_daily_logger.cpp +++ b/tests/test_daily_logger.cpp @@ -24,7 +24,7 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") logger->flush(); auto filename = fmt::to_string(w); - REQUIRE(count_lines(filename) == 10); + require_message_count(filename, 10); } struct custom_daily_file_name_calculator @@ -55,12 +55,10 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger]") logger->info("Test message {}", i); } - logger-> - - flush(); + logger->flush(); auto filename = fmt::to_string(w); - REQUIRE(count_lines(filename) == 10); + require_message_count(filename, 10); } /* diff --git a/tests/test_errors.cpp b/tests/test_errors.cpp index 4fc40594..613a9012 100644 --- a/tests/test_errors.cpp +++ b/tests/test_errors.cpp @@ -34,7 +34,8 @@ TEST_CASE("default_error_handler", "[errors]]") logger->info("Test message {}", 2); logger->flush(); - REQUIRE(file_contents(filename) == std::string("Test message 2\n")); + using spdlog::details::os::default_eol; + REQUIRE(file_contents(filename) == fmt::format("Test message 2{}", default_eol)); REQUIRE(count_lines(filename) == 1); } @@ -51,7 +52,7 @@ TEST_CASE("custom_error_handler", "[errors]]") REQUIRE_THROWS_AS(logger->info("Bad format msg {} {}", "xxx"), custom_ex); logger->info("Good message #2"); - REQUIRE(count_lines(filename) == 2); + require_message_count(filename, 2); } TEST_CASE("default_error_handler2", "[errors]]") @@ -93,7 +94,7 @@ TEST_CASE("async_error_handler", "[errors]]") spdlog::drop("logger"); // force logger to drain the queue and shutdown } spdlog::init_thread_pool(128, 1); - REQUIRE(count_lines(filename) == 2); + require_message_count(filename, 2); REQUIRE(file_contents("test_logs/custom_err.txt") == err_msg); } diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index a8f9b2bb..0a1f3ffc 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -15,8 +15,10 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") logger->info("Test message {}", 2); logger->flush(); - REQUIRE(file_contents(filename) == std::string("Test message 1\nTest message 2\n")); - REQUIRE(count_lines(filename) == 2); + require_message_count(filename, 2); + using spdlog::details::os::default_eol; + REQUIRE(file_contents(filename) == fmt::format("Test message 1{}Test message 2{}", + default_eol, default_eol)); } TEST_CASE("flush_on", "[flush_on]]") @@ -34,8 +36,10 @@ TEST_CASE("flush_on", "[flush_on]]") logger->info("Test message {}", 1); logger->info("Test message {}", 2); - REQUIRE(file_contents(filename) == std::string("Should not be flushed\nTest message 1\nTest message 2\n")); - REQUIRE(count_lines(filename) == 3); + require_message_count(filename, 3); + using spdlog::details::os::default_eol; + REQUIRE(file_contents(filename) == fmt::format("Should not be flushed{}Test message 1{}Test message 2{}", + default_eol, default_eol, default_eol)); } TEST_CASE("rotating_file_logger1", "[rotating_logger]]") @@ -52,7 +56,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]") logger->flush(); auto filename = basename; - REQUIRE(count_lines(filename) == 10); + require_message_count(filename, 10); } TEST_CASE("rotating_file_logger2", "[rotating_logger]]") @@ -81,7 +85,8 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") logger->flush(); auto filename = basename; - REQUIRE(count_lines(filename) == 10); + require_message_count(filename, 10); + for (int i = 0; i < 1000; i++) { diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 4c621bb3..37f5c892 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -22,7 +22,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") SPDLOG_LOGGER_DEBUG(logger, "Test message 2"); logger->flush(); - REQUIRE(ends_with(file_contents(filename), "Test message 2\n")); + using spdlog::details::os::default_eol; + REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 2{}", default_eol))); REQUIRE(count_lines(filename) == 1); spdlog::set_default_logger(logger); @@ -31,8 +32,8 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") SPDLOG_DEBUG("Test message {}", 4); logger->flush(); - REQUIRE(ends_with(file_contents(filename), "Test message 4\n")); - REQUIRE(count_lines(filename) == 2); + require_message_count(filename, 2); + REQUIRE(ends_with(file_contents(filename), fmt::format("Test message 4{}", default_eol))); } TEST_CASE("disable param evaluation", "[macros]") diff --git a/tests/utils.cpp b/tests/utils.cpp index cdfdbaca..08c19c8a 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -44,6 +44,15 @@ std::size_t count_lines(const std::string &filename) return counter; } +void require_message_count(const std::string &filename, const std::size_t messages) +{ + if (strlen(spdlog::details::os::default_eol) == 0) { + REQUIRE(count_lines(filename) == 1); + } else { + REQUIRE(count_lines(filename) == messages); + } +} + std::size_t get_filesize(const std::string &filename) { std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary); diff --git a/tests/utils.h b/tests/utils.h index de553a9c..00bb9cc8 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -13,6 +13,8 @@ std::string file_contents(const std::string &filename); std::size_t count_lines(const std::string &filename); +void require_message_count(const std::string &filename, const std::size_t messages); + std::size_t get_filesize(const std::string &filename); bool ends_with(std::string const &value, std::string const &ending); \ No newline at end of file From 346b9ae5a1bfdf038cd5721c734b300cbef40db8 Mon Sep 17 00:00:00 2001 From: "v.reshetnikov" Date: Mon, 10 Feb 2020 14:24:17 +0300 Subject: [PATCH 35/81] tcp_sink implementation for fluentbit --- include/spdlog/tcp_sink.h | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 include/spdlog/tcp_sink.h diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h new file mode 100644 index 00000000..a52a9991 --- /dev/null +++ b/include/spdlog/tcp_sink.h @@ -0,0 +1,47 @@ + +#include +#include +#include +#include +#include +using namespace std; +template +class tcp_sink : public spdlog::sinks::base_sink +{ +public: + tcp_sink(std::string address,int port) + { + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + printf("\n Socket creation error \n"); + } + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + if(inet_pton(AF_INET, address.c_str(), &serv_addr.sin_addr)<=0) + { + printf("\nInvalid address/ Address not supported \n"); + } + if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + { + printf("\nConnection Failed \n"); + } + + } +protected: + void sink_it_(const spdlog::details::log_msg& msg) override + { + spdlog::memory_buf_t formatted; + spdlog::sinks::base_sink::formatter_->format(msg, formatted); + std::string message = fmt::to_string(formatted); + send(sock , message.c_str() , message.size() , 0 ); + } + + void flush_() override + { + } +private: + int sock; + struct sockaddr_in serv_addr; +}; +using tcp_sink_mt = tcp_sink; +using tcp_sink_st = tcp_sink; From ccad4ae04f23c871fdbad8fae61dd97333e58e8e Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Mon, 10 Feb 2020 14:46:39 +0300 Subject: [PATCH 36/81] Resolve modification requests --- include/spdlog/tcp_sink.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h index a52a9991..36ec4324 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/tcp_sink.h @@ -13,17 +13,17 @@ public: { if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - printf("\n Socket creation error \n"); + SPDLOG_THROW(spdlog_ex("Socket creation error", errno)); } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port); if(inet_pton(AF_INET, address.c_str(), &serv_addr.sin_addr)<=0) { - printf("\nInvalid address/ Address not supported \n"); + SPDLOG_THROW(spdlog_ex("Invalid address/ Address not supported", errno)); } if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { - printf("\nConnection Failed \n"); + SPDLOG_THROW(spdlog_ex("Connection Failed", errno)); } } @@ -32,8 +32,7 @@ protected: { spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); - std::string message = fmt::to_string(formatted); - send(sock , message.c_str() , message.size() , 0 ); + send(sock , formatted.data() , formatted.size() , 0 ); } void flush_() override From 3aa94a099778714256179f0f652c15e539a37bce Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Mon, 10 Feb 2020 14:58:54 +0300 Subject: [PATCH 37/81] Added send function verification + licence --- include/spdlog/tcp_sink.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h index 36ec4324..1c44d644 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/tcp_sink.h @@ -1,3 +1,5 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) #include #include @@ -32,7 +34,9 @@ protected: { spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); - send(sock , formatted.data() , formatted.size() , 0 ); + int res = send(sock , formatted.data() , formatted.size() , 0 ); + if(res < 0) + SPDLOG_THROW(spdlog_ex("Message Send Failed", errno)); } void flush_() override From 4bb623a0a3fda0b987c5e9454f20837cfa674c85 Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Mon, 10 Feb 2020 15:08:51 +0300 Subject: [PATCH 38/81] removed unneccessary namespace std --- include/spdlog/tcp_sink.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h index 1c44d644..dff51853 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/tcp_sink.h @@ -3,10 +3,11 @@ #include #include +#include #include #include #include -using namespace std; + template class tcp_sink : public spdlog::sinks::base_sink { @@ -15,17 +16,17 @@ public: { if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - SPDLOG_THROW(spdlog_ex("Socket creation error", errno)); + SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno)); } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port); if(inet_pton(AF_INET, address.c_str(), &serv_addr.sin_addr)<=0) { - SPDLOG_THROW(spdlog_ex("Invalid address/ Address not supported", errno)); + SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno)); } if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { - SPDLOG_THROW(spdlog_ex("Connection Failed", errno)); + SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno)); } } @@ -35,8 +36,8 @@ protected: spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); int res = send(sock , formatted.data() , formatted.size() , 0 ); - if(res < 0) - SPDLOG_THROW(spdlog_ex("Message Send Failed", errno)); + if(res < 0) + SPDLOG_THROW(spdlog::spdlog_ex("Message Send Failed", errno)); } void flush_() override From d96d8c49ac646dee98c9d97ddeb4e0f88d1f534b Mon Sep 17 00:00:00 2001 From: Vyacheslav Date: Mon, 10 Feb 2020 15:13:39 +0300 Subject: [PATCH 39/81] Code Style naming --- include/spdlog/tcp_sink.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/tcp_sink.h index dff51853..f581b71d 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/tcp_sink.h @@ -14,17 +14,17 @@ class tcp_sink : public spdlog::sinks::base_sink public: tcp_sink(std::string address,int port) { - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) + if ((sock_ = socket(AF_INET, SOCK_STREAM, 0)) < 0) { SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno)); } - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(port); - if(inet_pton(AF_INET, address.c_str(), &serv_addr.sin_addr)<=0) + serv_addr_.sin_family = AF_INET; + serv_addr_.sin_port = htons(port); + if(inet_pton(AF_INET, address.c_str(), &serv_addr_.sin_addr)<=0) { SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno)); } - if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + if (connect(sock_, (struct sockaddr *)&serv_addr_, sizeof(serv_addr_)) < 0) { SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno)); } @@ -35,7 +35,7 @@ protected: { spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); - int res = send(sock , formatted.data() , formatted.size() , 0 ); + int res = send(sock_ , formatted.data() , formatted.size() , 0 ); if(res < 0) SPDLOG_THROW(spdlog::spdlog_ex("Message Send Failed", errno)); } @@ -44,8 +44,8 @@ protected: { } private: - int sock; - struct sockaddr_in serv_addr; + int sock_; + struct sockaddr_in serv_addr_; }; using tcp_sink_mt = tcp_sink; using tcp_sink_st = tcp_sink; From 05cbdbc1ef772fa5b5d233151eb06a772dbb55bc Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 14:21:26 +0200 Subject: [PATCH 40/81] moved tcp sink to sinks --- include/spdlog/{ => sinks}/tcp_sink.h | 4 ++++ 1 file changed, 4 insertions(+) rename include/spdlog/{ => sinks}/tcp_sink.h (97%) diff --git a/include/spdlog/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h similarity index 97% rename from include/spdlog/tcp_sink.h rename to include/spdlog/sinks/tcp_sink.h index f581b71d..5ea3d662 100644 --- a/include/spdlog/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -8,6 +8,9 @@ #include #include +namespace spdlog { + namespace sinks { + template class tcp_sink : public spdlog::sinks::base_sink { @@ -49,3 +52,4 @@ private: }; using tcp_sink_mt = tcp_sink; using tcp_sink_st = tcp_sink; +}} From 66e86528627019a5225946ded367efdf9ec96cc2 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 14:34:00 +0200 Subject: [PATCH 41/81] Fix tcp_sink --- include/spdlog/sinks/tcp_sink.h | 35 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index 5ea3d662..961cc343 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -1,29 +1,30 @@ // Copyright(c) 2015-present, Gabi Melman & spdlog contributors. // Distributed under the MIT License (http://opensource.org/licenses/MIT) -#include +#pragma once + +#include #include -#include #include #include #include namespace spdlog { - namespace sinks { +namespace sinks { template -class tcp_sink : public spdlog::sinks::base_sink +class tcp_sink : public spdlog::sinks::base_sink { public: - tcp_sink(std::string address,int port) + tcp_sink(std::string address, int port) { if ((sock_ = socket(AF_INET, SOCK_STREAM, 0)) < 0) { SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno)); } serv_addr_.sin_family = AF_INET; - serv_addr_.sin_port = htons(port); - if(inet_pton(AF_INET, address.c_str(), &serv_addr_.sin_addr)<=0) + serv_addr_.sin_port = ::htons(static_cast(port)); + if (inet_pton(AF_INET, address.c_str(), &serv_addr_.sin_addr) <= 0) { SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno)); } @@ -31,25 +32,29 @@ public: { SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno)); } - } + protected: - void sink_it_(const spdlog::details::log_msg& msg) override + void sink_it_(const spdlog::details::log_msg &msg) override { spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); - int res = send(sock_ , formatted.data() , formatted.size() , 0 ); - if(res < 0) + auto res = ::send(sock_, formatted.data(), formatted.size(), 0); + if (res < 0) + { SPDLOG_THROW(spdlog::spdlog_ex("Message Send Failed", errno)); + } } - void flush_() override - { - } + void flush_() override {} + private: int sock_; struct sockaddr_in serv_addr_; }; + using tcp_sink_mt = tcp_sink; using tcp_sink_st = tcp_sink; -}} + +} // namespace sinks +} // namespace spdlog From 7f8169f0dae7b71988eec69f72281667dfbd57b8 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 15:02:48 +0200 Subject: [PATCH 42/81] Fixed tcp_sink to accept hostnames --- include/spdlog/sinks/tcp_sink.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index 961cc343..cf31d1fa 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace spdlog { namespace sinks { @@ -22,12 +23,16 @@ public: { SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno)); } - serv_addr_.sin_family = AF_INET; - serv_addr_.sin_port = ::htons(static_cast(port)); - if (inet_pton(AF_INET, address.c_str(), &serv_addr_.sin_addr) <= 0) + struct hostent *he = gethostbyname(address.c_str()); + if (he == nullptr) { - SPDLOG_THROW(spdlog::spdlog_ex("Invalid address/ Address not supported", errno)); + SPDLOG_THROW(spdlog::spdlog_ex("gethostbyname failed", errno)); } + + serv_addr_.sin_family = AF_INET; + + serv_addr_.sin_addr = *(struct in_addr *)(he->h_addr); + serv_addr_.sin_port = ::htons(static_cast(port)); if (connect(sock_, (struct sockaddr *)&serv_addr_, sizeof(serv_addr_)) < 0) { SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno)); @@ -48,6 +53,8 @@ protected: void flush_() override {} +private: + private: int sock_; struct sockaddr_in serv_addr_; From ad4fb1cf8498486a9df2b7c3a7c149cadb3b58a4 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 17:13:04 +0200 Subject: [PATCH 43/81] Fixed tcp sink --- include/spdlog/sinks/tcp_sink.h | 91 +++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 21 deletions(-) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index cf31d1fa..c22980eb 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -17,25 +17,16 @@ template class tcp_sink : public spdlog::sinks::base_sink { public: - tcp_sink(std::string address, int port) + tcp_sink(std::string host, int port) { - if ((sock_ = socket(AF_INET, SOCK_STREAM, 0)) < 0) - { - SPDLOG_THROW(spdlog::spdlog_ex("Socket creation error", errno)); - } - struct hostent *he = gethostbyname(address.c_str()); - if (he == nullptr) - { - SPDLOG_THROW(spdlog::spdlog_ex("gethostbyname failed", errno)); - } + sock_ = connect_to(host, port); + } - serv_addr_.sin_family = AF_INET; - - serv_addr_.sin_addr = *(struct in_addr *)(he->h_addr); - serv_addr_.sin_port = ::htons(static_cast(port)); - if (connect(sock_, (struct sockaddr *)&serv_addr_, sizeof(serv_addr_)) < 0) + ~tcp_sink() + { + if (sock_ != -1) { - SPDLOG_THROW(spdlog::spdlog_ex("Connection Failed", errno)); + ::close(sock_); } } @@ -44,20 +35,78 @@ protected: { spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); - auto res = ::send(sock_, formatted.data(), formatted.size(), 0); - if (res < 0) + size_t bytes_sent = 0; + while (bytes_sent < formatted.size()) { - SPDLOG_THROW(spdlog::spdlog_ex("Message Send Failed", errno)); + auto write_result = ::write(sock_, formatted.data(), formatted.size() - bytes_sent); + if (write_result < 0) + { + SPDLOG_THROW(spdlog::spdlog_ex("write(2) failed", errno)); + } + + if (write_result == 0) // (probably should not happen but in any case..) + { + break; + } + bytes_sent += static_cast(write_result); } } void flush_() override {} private: + // try to connect and return socket fd or throw on failure + int connect_to(const std::string &host, int port) + { + struct addrinfo hints; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_INET; // IPv4 + hints.ai_socktype = SOCK_STREAM; // TCP + hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value + hints.ai_protocol = 0; + + auto port_str = std::to_string(port); + struct addrinfo *addrinfo_result; + auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result); + if (rv != 0) + { + auto msg = fmt::format("::getaddrinfo failed: {}", gai_strerror(rv)); + SPDLOG_THROW(spdlog::spdlog_ex(msg)); + } + + // Try each address until we successfully connect(2). + int socket_rv = -1; + int last_errno = 0; + for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) + { + socket_rv = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (socket_rv == -1) + { + last_errno = errno; + continue; + } + rv = ::connect(socket_rv, rp->ai_addr, rp->ai_addrlen); + if (rv == 0) + { + break; + } + else + { + socket_rv = -1; + last_errno = errno; + ::close(socket_rv); + } + } + ::freeaddrinfo(addrinfo_result); + if (socket_rv == -1) + { + SPDLOG_THROW(spdlog::spdlog_ex("::connect failed", last_errno)); + } + return socket_rv; + } private: - int sock_; - struct sockaddr_in serv_addr_; + int sock_ = -1; }; using tcp_sink_mt = tcp_sink; From 5370443ece4b7a42282a4e780921818af3e18daa Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 17:13:41 +0200 Subject: [PATCH 44/81] clang-format --- include/spdlog/details/os-inl.h | 2 +- include/spdlog/logger.h | 2 +- include/spdlog/sinks/daily_file_sink.h | 3 +- include/spdlog/sinks/win_eventlog_sink.h | 108 ++++----- src/fmt.cpp | 265 ++++++++++++----------- tests/test_eventlog.cpp | 28 +-- tests/test_file_logging.cpp | 7 +- tests/utils.cpp | 7 +- 8 files changed, 211 insertions(+), 211 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index ccc53ff2..e83c2ce4 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -150,7 +150,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename #if defined(SPDLOG_PREVENT_CHILD_FD) const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC; const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644)); - if(fd == -1) + if (fd == -1) { return false; } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index ed7d2361..b78d8544 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -144,7 +144,7 @@ public: template::value, T>::type * = nullptr> void log(source_loc loc, level::level_enum lvl, const T &msg) { - log(loc,lvl, string_view_t{msg}); + log(loc, lvl, string_view_t{msg}); } void log(source_loc loc, level::level_enum lvl, string_view_t msg) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index f796f831..e122f10f 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -120,7 +120,8 @@ private: filenames.emplace_back(filename); now -= std::chrono::hours(24); } - for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) { + for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) + { filenames_q_.push_back(std::move(*iter)); } } diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index 51bfca9f..b594e0c9 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -5,12 +5,12 @@ // 1. should be replaced with your log name (e.g. your application name) // 2. should be replaced with the specific source name and the key should be duplicated for // each source used in the application -// +// // Since typically modifications of this kind require elevation, it's better to do it as a part of setup procedure. -// The snippet below uses mscoree.dll as the message file as it exists on most of the Windows systems anyway and +// The snippet below uses mscoree.dll as the message file as it exists on most of the Windows systems anyway and // happens to contain the needed resource. -// -// You can also specify a custom message file if needed. +// +// You can also specify a custom message file if needed. // Please refer to Event Log functions descriptions in MSDN for more details on custom message files. /*--------------------------------------------------------------------------------------- @@ -44,21 +44,20 @@ namespace sinks { namespace win_eventlog { -namespace internal -{ +namespace internal { /** Windows error */ struct win32_error : public spdlog_ex { /** Formats an error report line: "user-message: error-code (system message)" */ - static std::string format(std::string const& user_message, DWORD error_code = GetLastError()) + static std::string format(std::string const &user_message, DWORD error_code = GetLastError()) { std::string system_message; - LPSTR format_message_result {}; - auto format_message_succeeded = ::FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, - error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &format_message_result, 0, nullptr); + LPSTR format_message_result{}; + auto format_message_succeeded = + ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, + error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result, 0, nullptr); if (format_message_succeeded && format_message_result) { @@ -73,7 +72,7 @@ struct win32_error : public spdlog_ex return fmt::format("{}: {}{}", user_message, error_code, system_message); } - win32_error(std::string const& func_name, DWORD error = GetLastError()) + win32_error(std::string const &func_name, DWORD error = GetLastError()) : spdlog_ex(format(func_name, error)) {} }; @@ -84,8 +83,7 @@ struct sid_t std::vector buffer_; public: - sid_t() - {} + sid_t() {} /** creates a wrapped SID copy */ static sid_t duplicate_sid(PSID psid) @@ -95,7 +93,7 @@ public: SPDLOG_THROW(spdlog_ex("sid_t::sid_t(): invalid SID received")); } - auto const sid_length {::GetLengthSid(psid)}; + auto const sid_length{::GetLengthSid(psid)}; sid_t result; result.buffer_.resize(sid_length); @@ -108,9 +106,9 @@ public: } /** Retrieves pointer to the internal buffer contents as SID* */ - SID * as_sid() const + SID *as_sid() const { - return buffer_.empty() ? nullptr : (SID *) buffer_.data(); + return buffer_.empty() ? nullptr : (SID *)buffer_.data(); } /** Get SID for the current user */ @@ -119,11 +117,11 @@ public: /* create and init RAII holder for process token */ struct process_token_t { - HANDLE token_handle_= INVALID_HANDLE_VALUE; - explicit process_token_t(HANDLE process) + HANDLE token_handle_ = INVALID_HANDLE_VALUE; + explicit process_token_t(HANDLE process) { if (!::OpenProcessToken(process, TOKEN_QUERY, &token_handle_)) - { + { SPDLOG_THROW(win32_error("OpenProcessToken")); } } @@ -137,7 +135,7 @@ public: // Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size DWORD tusize = 0; - if(::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize)) + if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize)) { SPDLOG_THROW(win32_error("GetTokenInformation should fail")); } @@ -150,57 +148,56 @@ public: } // create a wrapper of the SID data as stored in the user token - return sid_t::duplicate_sid(((TOKEN_USER*) buffer.data())->User.Sid); + return sid_t::duplicate_sid(((TOKEN_USER *)buffer.data())->User.Sid); } }; struct eventlog { - static WORD get_event_type(details::log_msg const& msg) + static WORD get_event_type(details::log_msg const &msg) { switch (msg.level) { - case level::trace: - case level::debug: - return EVENTLOG_SUCCESS; + case level::trace: + case level::debug: + return EVENTLOG_SUCCESS; - case level::info: - return EVENTLOG_INFORMATION_TYPE; + case level::info: + return EVENTLOG_INFORMATION_TYPE; - case level::warn: - return EVENTLOG_WARNING_TYPE; + case level::warn: + return EVENTLOG_WARNING_TYPE; - case level::err: - case level::critical: - case level::off: - return EVENTLOG_ERROR_TYPE; + case level::err: + case level::critical: + case level::off: + return EVENTLOG_ERROR_TYPE; - default: - // should be unreachable - SPDLOG_THROW(std::logic_error(fmt::format("Unsupported log level {}", msg.level))); + default: + // should be unreachable + SPDLOG_THROW(std::logic_error(fmt::format("Unsupported log level {}", msg.level))); } } - static WORD get_event_category(details::log_msg const& msg) + static WORD get_event_category(details::log_msg const &msg) { - return (WORD) msg.level; + return (WORD)msg.level; } }; } // namespace internal - /* * Windows Event Log sink */ -template +template class win_eventlog_sink : public base_sink { private: - HANDLE hEventLog_ {NULL}; + HANDLE hEventLog_{NULL}; internal::sid_t current_user_sid_; - std::string source_; - WORD event_id_; + std::string source_; + WORD event_id_; HANDLE event_log_handle() { @@ -225,17 +222,9 @@ protected: formatter_->format(msg, formatted); formatted.push_back('\0'); LPCSTR lp_str = static_cast(formatted.data()); - - auto succeeded = ::ReportEvent( - event_log_handle(), - eventlog::get_event_type(msg), - eventlog::get_event_category(msg), - event_id_, - current_user_sid_.as_sid(), - 1, - 0, - &lp_str, - nullptr); + + auto succeeded = ::ReportEvent(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_, + current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr); if (!succeeded) { @@ -246,9 +235,9 @@ protected: void flush_() override {} public: - win_eventlog_sink(std::string const& source, WORD event_id = 1000 /* according to mscoree.dll */) + win_eventlog_sink(std::string const &source, WORD event_id = 1000 /* according to mscoree.dll */) : source_(source) - , event_id_ (event_id) + , event_id_(event_id) { try { @@ -256,8 +245,8 @@ public: } catch (...) { - // get_current_user_sid() is unlikely to fail and if it does, we can still proceed without - // current_user_sid but in the event log the record will have no user name + // get_current_user_sid() is unlikely to fail and if it does, we can still proceed without + // current_user_sid but in the event log the record will have no user name } } @@ -275,4 +264,3 @@ using win_eventlog_sink_st = win_eventlog::win_eventlog_sink - int format_float(char* buf, std::size_t size, const char* format, int precision, - T value) { +template +int format_float(char *buf, std::size_t size, const char *format, int precision, T value) +{ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - if (precision > 100000) - throw std::runtime_error( - "fuzz mode - avoid large allocation inside snprintf"); + if (precision > 100000) + throw std::runtime_error("fuzz mode - avoid large allocation inside snprintf"); #endif - // Suppress the warning about nonliteral format string. - auto snprintf_ptr = FMT_SNPRINTF; - return precision < 0 ? snprintf_ptr(buf, size, format, value) - : snprintf_ptr(buf, size, format, precision, value); - } - struct sprintf_specs { - int precision; - char type; - bool alt : 1; + // Suppress the warning about nonliteral format string. + auto snprintf_ptr = FMT_SNPRINTF; + return precision < 0 ? snprintf_ptr(buf, size, format, value) : snprintf_ptr(buf, size, format, precision, value); +} +struct sprintf_specs +{ + int precision; + char type; + bool alt : 1; - template - constexpr sprintf_specs(basic_format_specs specs) - : precision(specs.precision), type(specs.type), alt(specs.alt) {} + template + constexpr sprintf_specs(basic_format_specs specs) + : precision(specs.precision) + , type(specs.type) + , alt(specs.alt) + {} - constexpr bool has_precision() const { return precision >= 0; } - }; + constexpr bool has_precision() const + { + return precision >= 0; + } +}; // This is deprecated and is kept only to preserve ABI compatibility. - template - char* sprintf_format(Double value, internal::buffer& buf, - sprintf_specs specs) { - // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail. - FMT_ASSERT(buf.capacity() != 0, "empty buffer"); +template +char *sprintf_format(Double value, internal::buffer &buf, sprintf_specs specs) +{ + // Buffer capacity must be non-zero, otherwise MSVC's vsnprintf_s will fail. + FMT_ASSERT(buf.capacity() != 0, "empty buffer"); - // Build format string. - enum { max_format_size = 10 }; // longest format: %#-*.*Lg - char format[max_format_size]; - char* format_ptr = format; - *format_ptr++ = '%'; - if (specs.alt || !specs.type) *format_ptr++ = '#'; - if (specs.precision >= 0) { - *format_ptr++ = '.'; - *format_ptr++ = '*'; - } - if (std::is_same::value) *format_ptr++ = 'L'; + // Build format string. + enum + { + max_format_size = 10 + }; // longest format: %#-*.*Lg + char format[max_format_size]; + char *format_ptr = format; + *format_ptr++ = '%'; + if (specs.alt || !specs.type) + *format_ptr++ = '#'; + if (specs.precision >= 0) + { + *format_ptr++ = '.'; + *format_ptr++ = '*'; + } + if (std::is_same::value) + *format_ptr++ = 'L'; - char type = specs.type; + char type = specs.type; - if (type == '%') - type = 'f'; - else if (type == 0 || type == 'n') - type = 'g'; + if (type == '%') + type = 'f'; + else if (type == 0 || type == 'n') + type = 'g'; #if FMT_MSC_VER - if (type == 'F') { - // MSVC's printf doesn't support 'F'. - type = 'f'; - } + if (type == 'F') + { + // MSVC's printf doesn't support 'F'. + type = 'f'; + } #endif - *format_ptr++ = type; - *format_ptr = '\0'; + *format_ptr++ = type; + *format_ptr = '\0'; - // Format using snprintf. - char* start = nullptr; - char* decimal_point_pos = nullptr; - for (;;) { - std::size_t buffer_size = buf.capacity(); - start = &buf[0]; - int result = - format_float(start, buffer_size, format, specs.precision, value); - if (result >= 0) { - unsigned n = internal::to_unsigned(result); - if (n < buf.capacity()) { - // Find the decimal point. - auto p = buf.data(), end = p + n; - if (*p == '+' || *p == '-') ++p; - if (specs.type != 'a' && specs.type != 'A') { - while (p < end && *p >= '0' && *p <= '9') ++p; - if (p < end && *p != 'e' && *p != 'E') { - decimal_point_pos = p; - if (!specs.type) { - // Keep only one trailing zero after the decimal point. - ++p; - if (*p == '0') ++p; - while (p != end && *p >= '1' && *p <= '9') ++p; - char* where = p; - while (p != end && *p == '0') ++p; - if (p == end || *p < '0' || *p > '9') { - if (p != end) std::memmove(where, p, to_unsigned(end - p)); - n -= static_cast(p - where); - } - } - } + // Format using snprintf. + char *start = nullptr; + char *decimal_point_pos = nullptr; + for (;;) + { + std::size_t buffer_size = buf.capacity(); + start = &buf[0]; + int result = format_float(start, buffer_size, format, specs.precision, value); + if (result >= 0) + { + unsigned n = internal::to_unsigned(result); + if (n < buf.capacity()) + { + // Find the decimal point. + auto p = buf.data(), end = p + n; + if (*p == '+' || *p == '-') + ++p; + if (specs.type != 'a' && specs.type != 'A') + { + while (p < end && *p >= '0' && *p <= '9') + ++p; + if (p < end && *p != 'e' && *p != 'E') + { + decimal_point_pos = p; + if (!specs.type) + { + // Keep only one trailing zero after the decimal point. + ++p; + if (*p == '0') + ++p; + while (p != end && *p >= '1' && *p <= '9') + ++p; + char *where = p; + while (p != end && *p == '0') + ++p; + if (p == end || *p < '0' || *p > '9') + { + if (p != end) + std::memmove(where, p, to_unsigned(end - p)); + n -= static_cast(p - where); } - buf.resize(n); - break; // The buffer is large enough - continue with formatting. } - buf.reserve(n + 1); - } else { - // If result is negative we ask to increase the capacity by at least 1, - // but as std::vector, the buffer grows exponentially. - buf.reserve(buf.capacity() + 1); } } - return decimal_point_pos; + buf.resize(n); + break; // The buffer is large enough - continue with formatting. } - } // namespace internal + buf.reserve(n + 1); + } + else + { + // If result is negative we ask to increase the capacity by at least 1, + // but as std::vector, the buffer grows exponentially. + buf.reserve(buf.capacity() + 1); + } + } + return decimal_point_pos; +} +} // namespace internal - template FMT_API char* internal::sprintf_format(double, internal::buffer&, - sprintf_specs); - template FMT_API char* internal::sprintf_format(long double, - internal::buffer&, - sprintf_specs); +template FMT_API char *internal::sprintf_format(double, internal::buffer &, sprintf_specs); +template FMT_API char *internal::sprintf_format(long double, internal::buffer &, sprintf_specs); - template struct FMT_API internal::basic_data; +template struct FMT_API internal::basic_data; // Workaround a bug in MSVC2013 that prevents instantiation of format_float. - int (*instantiate_format_float)(double, int, internal::float_specs, - internal::buffer&) = - internal::format_float; +int (*instantiate_format_float)(double, int, internal::float_specs, internal::buffer &) = internal::format_float; #ifndef FMT_STATIC_THOUSANDS_SEPARATOR - template FMT_API internal::locale_ref::locale_ref(const std::locale& loc); - template FMT_API std::locale internal::locale_ref::get() const; +template FMT_API internal::locale_ref::locale_ref(const std::locale &loc); +template FMT_API std::locale internal::locale_ref::get() const; #endif // Explicit instantiations for char. - template FMT_API std::string internal::grouping_impl(locale_ref); - template FMT_API char internal::thousands_sep_impl(locale_ref); - template FMT_API char internal::decimal_point_impl(locale_ref); +template FMT_API std::string internal::grouping_impl(locale_ref); +template FMT_API char internal::thousands_sep_impl(locale_ref); +template FMT_API char internal::decimal_point_impl(locale_ref); - template FMT_API void internal::buffer::append(const char*, const char*); +template FMT_API void internal::buffer::append(const char *, const char *); - template FMT_API void internal::arg_map::init( - const basic_format_args& args); +template FMT_API void internal::arg_map::init(const basic_format_args &args); - template FMT_API std::string internal::vformat( - string_view, basic_format_args); +template FMT_API std::string internal::vformat(string_view, basic_format_args); - template FMT_API format_context::iterator internal::vformat_to( - internal::buffer&, string_view, basic_format_args); +template FMT_API format_context::iterator internal::vformat_to(internal::buffer &, string_view, basic_format_args); - template FMT_API int internal::snprintf_float(double, int, - internal::float_specs, - internal::buffer&); - template FMT_API int internal::snprintf_float(long double, int, - internal::float_specs, - internal::buffer&); - template FMT_API int internal::format_float(double, int, internal::float_specs, - internal::buffer&); - template FMT_API int internal::format_float(long double, int, - internal::float_specs, - internal::buffer&); +template FMT_API int internal::snprintf_float(double, int, internal::float_specs, internal::buffer &); +template FMT_API int internal::snprintf_float(long double, int, internal::float_specs, internal::buffer &); +template FMT_API int internal::format_float(double, int, internal::float_specs, internal::buffer &); +template FMT_API int internal::format_float(long double, int, internal::float_specs, internal::buffer &); // Explicit instantiations for wchar_t. - template FMT_API std::string internal::grouping_impl(locale_ref); - template FMT_API wchar_t internal::thousands_sep_impl(locale_ref); - template FMT_API wchar_t internal::decimal_point_impl(locale_ref); +template FMT_API std::string internal::grouping_impl(locale_ref); +template FMT_API wchar_t internal::thousands_sep_impl(locale_ref); +template FMT_API wchar_t internal::decimal_point_impl(locale_ref); - template FMT_API void internal::buffer::append(const wchar_t*, - const wchar_t*); +template FMT_API void internal::buffer::append(const wchar_t *, const wchar_t *); - template FMT_API std::wstring internal::vformat( - wstring_view, basic_format_args); +template FMT_API std::wstring internal::vformat(wstring_view, basic_format_args); FMT_END_NAMESPACE - #endif diff --git a/tests/test_eventlog.cpp b/tests/test_eventlog.cpp index 114f2cb7..6aca3a9e 100644 --- a/tests/test_eventlog.cpp +++ b/tests/test_eventlog.cpp @@ -7,7 +7,7 @@ static const LPCSTR TEST_SOURCE = "spdlog_test"; -static void test_single_print(std::function do_log, std::string const& expected_contents, WORD expected_ev_type) +static void test_single_print(std::function do_log, std::string const &expected_contents, WORD expected_ev_type) { using namespace std::chrono; do_log(expected_contents); @@ -24,26 +24,28 @@ static void test_single_print(std::function do_log, st REQUIRE(CloseEventLog(handle_)); } } - } event_log {::OpenEventLog(nullptr, TEST_SOURCE)}; + } event_log{::OpenEventLog(nullptr, TEST_SOURCE)}; REQUIRE(event_log.handle_); - DWORD read_bytes {}, size_needed{}; - auto ok = ::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed); - REQUIRE(!ok); + DWORD read_bytes{}, size_needed{}; + auto ok = + ::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, &read_bytes, 0, &read_bytes, &size_needed); + REQUIRE(!ok); REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER); std::vector record_buffer(size_needed); PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data(); - ok = ::ReadEventLog(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed); + ok = ::ReadEventLog( + event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0, record, size_needed, &read_bytes, &size_needed); REQUIRE(ok); REQUIRE(record->NumStrings == 1); REQUIRE(record->EventType == expected_ev_type); REQUIRE(record->TimeGenerated == expected_time_generated); - std::string message_in_log(((char*) record + record->StringOffset)); + std::string message_in_log(((char *)record + record->StringOffset)); REQUIRE(message_in_log == expected_contents + spdlog::details::os::default_eol); } @@ -58,12 +60,12 @@ TEST_CASE("eventlog", "[eventlog]") test_sink->set_pattern("%v"); - test_single_print([&test_logger] (std::string const& msg) { test_logger.trace(msg); }, "my trace message", EVENTLOG_SUCCESS); - test_single_print([&test_logger] (std::string const& msg) { test_logger.debug(msg); }, "my debug message", EVENTLOG_SUCCESS); - test_single_print([&test_logger] (std::string const& msg) { test_logger.info(msg); }, "my info message", EVENTLOG_INFORMATION_TYPE); - test_single_print([&test_logger] (std::string const& msg) { test_logger.warn(msg); }, "my warn message", EVENTLOG_WARNING_TYPE); - test_single_print([&test_logger] (std::string const& msg) { test_logger.error(msg); }, "my error message", EVENTLOG_ERROR_TYPE); - test_single_print([&test_logger] (std::string const& msg) { test_logger.critical(msg); }, "my critical message", EVENTLOG_ERROR_TYPE); + test_single_print([&test_logger](std::string const &msg) { test_logger.trace(msg); }, "my trace message", EVENTLOG_SUCCESS); + test_single_print([&test_logger](std::string const &msg) { test_logger.debug(msg); }, "my debug message", EVENTLOG_SUCCESS); + test_single_print([&test_logger](std::string const &msg) { test_logger.info(msg); }, "my info message", EVENTLOG_INFORMATION_TYPE); + test_single_print([&test_logger](std::string const &msg) { test_logger.warn(msg); }, "my warn message", EVENTLOG_WARNING_TYPE); + test_single_print([&test_logger](std::string const &msg) { test_logger.error(msg); }, "my error message", EVENTLOG_ERROR_TYPE); + test_single_print([&test_logger](std::string const &msg) { test_logger.critical(msg); }, "my critical message", EVENTLOG_ERROR_TYPE); } #endif //_WIN32 \ No newline at end of file diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index 0a1f3ffc..2c55ddba 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -17,8 +17,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]") logger->flush(); require_message_count(filename, 2); using spdlog::details::os::default_eol; - REQUIRE(file_contents(filename) == fmt::format("Test message 1{}Test message 2{}", - default_eol, default_eol)); + REQUIRE(file_contents(filename) == fmt::format("Test message 1{}Test message 2{}", default_eol, default_eol)); } TEST_CASE("flush_on", "[flush_on]]") @@ -38,8 +37,8 @@ TEST_CASE("flush_on", "[flush_on]]") require_message_count(filename, 3); using spdlog::details::os::default_eol; - REQUIRE(file_contents(filename) == fmt::format("Should not be flushed{}Test message 1{}Test message 2{}", - default_eol, default_eol, default_eol)); + REQUIRE(file_contents(filename) == + fmt::format("Should not be flushed{}Test message 1{}Test message 2{}", default_eol, default_eol, default_eol)); } TEST_CASE("rotating_file_logger1", "[rotating_logger]]") diff --git a/tests/utils.cpp b/tests/utils.cpp index 08c19c8a..7aeca2f8 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -46,9 +46,12 @@ std::size_t count_lines(const std::string &filename) void require_message_count(const std::string &filename, const std::size_t messages) { - if (strlen(spdlog::details::os::default_eol) == 0) { + if (strlen(spdlog::details::os::default_eol) == 0) + { REQUIRE(count_lines(filename) == 1); - } else { + } + else + { REQUIRE(count_lines(filename) == messages); } } From 7b19890debaca5e6c52d7570fab07bb533657713 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 17:18:18 +0200 Subject: [PATCH 45/81] Update tcp_sink.h --- include/spdlog/sinks/tcp_sink.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index c22980eb..99ea8413 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -5,11 +5,18 @@ #include #include +#include + #include #include #include #include +#include +#include + +#pragma once + namespace spdlog { namespace sinks { From 1b6d4fd277cae2b53dc85568449176e11c9c6334 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 17:19:22 +0200 Subject: [PATCH 46/81] Update tcp_sink.h --- include/spdlog/sinks/tcp_sink.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index 99ea8413..958d8875 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -24,6 +24,8 @@ template class tcp_sink : public spdlog::sinks::base_sink { public: + // connect to tcp host/port or throw if failed + // host can be hostname or ip address tcp_sink(std::string host, int port) { sock_ = connect_to(host, port); From ff59b079862a9dbbe4aeb712c0d8d39ac434bfe7 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 17:21:32 +0200 Subject: [PATCH 47/81] Update tcp_sink.h --- include/spdlog/sinks/tcp_sink.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index 958d8875..a44e6716 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -31,7 +31,7 @@ public: sock_ = connect_to(host, port); } - ~tcp_sink() + ~tcp_sink() override { if (sock_ != -1) { From 5da9818676977c0199a1091c300f5244cdd404f0 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 17:25:39 +0200 Subject: [PATCH 48/81] updated member name in win eventlog sink --- include/spdlog/sinks/win_eventlog_sink.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index b594e0c9..1adbddc3 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -194,23 +194,23 @@ template class win_eventlog_sink : public base_sink { private: - HANDLE hEventLog_{NULL}; + HANDLE h_eventlog_{NULL}; internal::sid_t current_user_sid_; std::string source_; WORD event_id_; HANDLE event_log_handle() { - if (!hEventLog_) + if (!h_eventlog_) { - hEventLog_ = ::RegisterEventSource(nullptr, source_.c_str()); - if (!hEventLog_ || hEventLog_ == (HANDLE)ERROR_ACCESS_DENIED) + h_eventlog_ = ::RegisterEventSource(nullptr, source_.c_str()); + if (!h_eventlog_ || h_eventlog_ == (HANDLE)ERROR_ACCESS_DENIED) { SPDLOG_THROW(internal::win32_error("RegisterEventSource")); } } - return hEventLog_; + return h_eventlog_; } protected: @@ -241,7 +241,7 @@ public: { try { - current_user_sid_ = internal::sid_t::get_current_user_sid(); + current_user_sid_ = internal::sid_t::sid_t(); } catch (...) { @@ -250,10 +250,11 @@ public: } } - ~win_eventlog_sink() + ~win_eventlog_sink() override { - if (hEventLog_) - DeregisterEventSource(hEventLog_); + if (h_eventlog_) { + DeregisterEventSource(h_eventlog_); + } } }; From 9f24f4bc69fd47fd0b11410f99284fee632959f2 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 10 Feb 2020 17:38:31 +0200 Subject: [PATCH 49/81] revert last commit --- include/spdlog/sinks/win_eventlog_sink.h | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index 1adbddc3..b594e0c9 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -194,23 +194,23 @@ template class win_eventlog_sink : public base_sink { private: - HANDLE h_eventlog_{NULL}; + HANDLE hEventLog_{NULL}; internal::sid_t current_user_sid_; std::string source_; WORD event_id_; HANDLE event_log_handle() { - if (!h_eventlog_) + if (!hEventLog_) { - h_eventlog_ = ::RegisterEventSource(nullptr, source_.c_str()); - if (!h_eventlog_ || h_eventlog_ == (HANDLE)ERROR_ACCESS_DENIED) + hEventLog_ = ::RegisterEventSource(nullptr, source_.c_str()); + if (!hEventLog_ || hEventLog_ == (HANDLE)ERROR_ACCESS_DENIED) { SPDLOG_THROW(internal::win32_error("RegisterEventSource")); } } - return h_eventlog_; + return hEventLog_; } protected: @@ -241,7 +241,7 @@ public: { try { - current_user_sid_ = internal::sid_t::sid_t(); + current_user_sid_ = internal::sid_t::get_current_user_sid(); } catch (...) { @@ -250,11 +250,10 @@ public: } } - ~win_eventlog_sink() override + ~win_eventlog_sink() { - if (h_eventlog_) { - DeregisterEventSource(h_eventlog_); - } + if (hEventLog_) + DeregisterEventSource(hEventLog_); } }; From eef981e05f78c55f7fe1e53ebb2389ab538e3d2e Mon Sep 17 00:00:00 2001 From: dominicpoeschko <45942148+dominicpoeschko@users.noreply.github.com> Date: Mon, 10 Feb 2020 17:52:54 +0100 Subject: [PATCH 50/81] Handling SPDLOG_PREVENT_CHILD_FD in tcp_sink Adding SOCK_CLOEXEC to socket Fixing bug in sink_it_ (bytes_sent not added to buffer) --- include/spdlog/sinks/tcp_sink.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index a44e6716..4b3a7457 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -47,7 +47,7 @@ protected: size_t bytes_sent = 0; while (bytes_sent < formatted.size()) { - auto write_result = ::write(sock_, formatted.data(), formatted.size() - bytes_sent); + auto write_result = ::write(sock_, formatted.data() + bytes_sent, formatted.size() - bytes_sent); if (write_result < 0) { SPDLOG_THROW(spdlog::spdlog_ex("write(2) failed", errno)); @@ -88,7 +88,12 @@ private: int last_errno = 0; for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) { - socket_rv = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + #ifdef SPDLOG_PREVENT_CHILD_FD + int const flags = SOCK_CLOEXEC; + #else + int const flags = 0; + #endif + socket_rv = ::socket(rp->ai_family, rp->ai_socktype | flags, rp->ai_protocol); if (socket_rv == -1) { last_errno = errno; From 574563d71158e30b25adb39620dbaba9ad0da129 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 10 Feb 2020 23:00:08 +0200 Subject: [PATCH 51/81] Update tcp_sink.h --- include/spdlog/sinks/tcp_sink.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index 4b3a7457..e380d3bb 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -106,9 +106,9 @@ private: } else { - socket_rv = -1; last_errno = errno; ::close(socket_rv); + socket_rv = -1; } } ::freeaddrinfo(addrinfo_result); From 0778211116cc302afa9db32e4986650b728e6124 Mon Sep 17 00:00:00 2001 From: tt4g Date: Wed, 12 Feb 2020 09:58:16 +0900 Subject: [PATCH 52/81] Add critical section to filename function of each file sink --- include/spdlog/sinks/daily_file_sink.h | 3 ++- include/spdlog/sinks/rotating_file_sink-inl.h | 3 ++- include/spdlog/sinks/rotating_file_sink.h | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index e122f10f..65ab24f8 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -70,8 +70,9 @@ public: } } - const filename_t &filename() const + const filename_t &filename() { + std::lock_guard lock(base_sink::mutex_); return file_helper_.filename(); } diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index bd2d175d..0d27f057 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -54,8 +54,9 @@ SPDLOG_INLINE filename_t rotating_file_sink::calc_filename(const filename } template -SPDLOG_INLINE const filename_t &rotating_file_sink::filename() const +SPDLOG_INLINE const filename_t &rotating_file_sink::filename() { + std::lock_guard lock(base_sink::mutex_); return file_helper_.filename(); } diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index 5be8583a..e306dd9b 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -24,7 +24,7 @@ class rotating_file_sink final : public base_sink public: rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false); static filename_t calc_filename(const filename_t &filename, std::size_t index); - const filename_t &filename() const; + const filename_t &filename(); protected: void sink_it_(const details::log_msg &msg) override; From 9e9da42c648f2e4f6196188e415eb2940e87fd65 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 12 Feb 2020 07:48:41 +0200 Subject: [PATCH 53/81] Update rotating_file_sink.h --- include/spdlog/sinks/rotating_file_sink.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index e306dd9b..b7b3a1dc 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -40,7 +40,7 @@ private: // delete the target if exists, and rename the src file to target // return true on success, false otherwise. - bool rename_file(const filename_t &src_filename, const filename_t &target_filename); + bool rename_file_(const filename_t &src_filename, const filename_t &target_filename); filename_t base_filename_; std::size_t max_size_; @@ -75,4 +75,4 @@ inline std::shared_ptr rotating_logger_st( #ifdef SPDLOG_HEADER_ONLY #include "rotating_file_sink-inl.h" -#endif \ No newline at end of file +#endif From 64dd4dc2193783a5ae6a649378876467e42cfca3 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 12 Feb 2020 07:49:29 +0200 Subject: [PATCH 54/81] Update rotating_file_sink-inl.h --- include/spdlog/sinks/rotating_file_sink-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 0d27f057..0419bb57 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -121,7 +121,7 @@ SPDLOG_INLINE void rotating_file_sink::rotate_() // delete the target if exists, and rename the src file to target // return true on success, false otherwise. template -SPDLOG_INLINE bool rotating_file_sink::rename_file(const filename_t &src_filename, const filename_t &target_filename) +SPDLOG_INLINE bool rotating_file_sink::rename_file_(const filename_t &src_filename, const filename_t &target_filename) { // try to delete the target file in case it already exists. (void)details::os::remove(target_filename); From 53a56b82af42e71c4232c80de8a42cab1709a854 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 12 Feb 2020 07:51:42 +0200 Subject: [PATCH 55/81] Update rotating_file_sink-inl.h --- include/spdlog/sinks/rotating_file_sink-inl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 0419bb57..fd8b363c 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -100,7 +100,7 @@ SPDLOG_INLINE void rotating_file_sink::rotate_() } filename_t target = calc_filename(base_filename_, i); - if (!rename_file(src, target)) + if (!rename_file_(src, target)) { // if failed try again after a small delay. // this is a workaround to a windows issue, where very high rotation @@ -121,7 +121,7 @@ SPDLOG_INLINE void rotating_file_sink::rotate_() // delete the target if exists, and rename the src file to target // return true on success, false otherwise. template -SPDLOG_INLINE bool rotating_file_sink::rename_file_(const filename_t &src_filename, const filename_t &target_filename) +SPDLOG_INLINE bool rotating_file_sink::rename_file_(yconst filename_t &src_filename, const filename_t &target_filename) { // try to delete the target file in case it already exists. (void)details::os::remove(target_filename); From a343328a211f5fe73c0f5a146c18a6489c8b5d4c Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 12 Feb 2020 08:56:24 +0200 Subject: [PATCH 56/81] Update rotating_file_sink-inl.h --- include/spdlog/sinks/rotating_file_sink-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index fd8b363c..e7a2533e 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -121,7 +121,7 @@ SPDLOG_INLINE void rotating_file_sink::rotate_() // delete the target if exists, and rename the src file to target // return true on success, false otherwise. template -SPDLOG_INLINE bool rotating_file_sink::rename_file_(yconst filename_t &src_filename, const filename_t &target_filename) +SPDLOG_INLINE bool rotating_file_sink::rename_file_(const filename_t &src_filename, const filename_t &target_filename) { // try to delete the target file in case it already exists. (void)details::os::remove(target_filename); From 0cf1af5bbf5397d8769fd5accfb8caf5924ed0fd Mon Sep 17 00:00:00 2001 From: tt4g Date: Wed, 12 Feb 2020 16:30:44 +0900 Subject: [PATCH 57/81] Avoid references to race data filename --- include/spdlog/sinks/daily_file_sink.h | 2 +- include/spdlog/sinks/rotating_file_sink-inl.h | 2 +- include/spdlog/sinks/rotating_file_sink.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 65ab24f8..8ed5a043 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -70,7 +70,7 @@ public: } } - const filename_t &filename() + filename_t filename() { std::lock_guard lock(base_sink::mutex_); return file_helper_.filename(); diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index e7a2533e..f6b970ce 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -54,7 +54,7 @@ SPDLOG_INLINE filename_t rotating_file_sink::calc_filename(const filename } template -SPDLOG_INLINE const filename_t &rotating_file_sink::filename() +SPDLOG_INLINE filename_t rotating_file_sink::filename() { std::lock_guard lock(base_sink::mutex_); return file_helper_.filename(); diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index b7b3a1dc..e1e85a7d 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -24,7 +24,7 @@ class rotating_file_sink final : public base_sink public: rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false); static filename_t calc_filename(const filename_t &filename, std::size_t index); - const filename_t &filename(); + filename_t filename(); protected: void sink_it_(const details::log_msg &msg) override; From 0f42744f5c34402541d47c2dfaa125155d33c8e3 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 12 Feb 2020 12:11:03 +0200 Subject: [PATCH 58/81] Update rotating_file_sink-inl.h --- include/spdlog/sinks/rotating_file_sink-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index f6b970ce..c30450ee 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -106,7 +106,7 @@ SPDLOG_INLINE void rotating_file_sink::rotate_() // this is a workaround to a windows issue, where very high rotation // rates can cause the rename to fail with permission denied (because of antivirus?). details::os::sleep_for_millis(100); - if (!rename_file(src, target)) + if (!rename_file_(src, target)) { file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit! current_size_ = 0; From 76d94e69ae08c0111bbd77bbf530c99476bc9c3c Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Sat, 15 Feb 2020 10:57:12 +0200 Subject: [PATCH 59/81] Fix #1439 --- include/spdlog/sinks/ansicolor_sink-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 8480b06d..6615fb45 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -111,7 +111,7 @@ SPDLOG_INLINE void ansicolor_sink::set_color_mode(color_mode mode) template SPDLOG_INLINE void ansicolor_sink::print_ccode_(const string_view_t &color_code) { - fwrite(color_code.data(), sizeof(string_view_t::char_type), color_code.size(), target_file_); + fwrite(color_code.data(), sizeof(string_view_t::value_type), color_code.size(), target_file_); } template From d4fd17f64f6aaba65fc47d7f04d7775bec485d0d Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 15 Feb 2020 11:29:04 +0200 Subject: [PATCH 60/81] Fixed #1439 --- include/spdlog/sinks/ansicolor_sink-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 6615fb45..58fa53fe 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -111,7 +111,7 @@ SPDLOG_INLINE void ansicolor_sink::set_color_mode(color_mode mode) template SPDLOG_INLINE void ansicolor_sink::print_ccode_(const string_view_t &color_code) { - fwrite(color_code.data(), sizeof(string_view_t::value_type), color_code.size(), target_file_); + fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); } template From 5c06306ccce44dbfd2a5d149100615a6f090dd7e Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 15 Feb 2020 12:05:07 +0200 Subject: [PATCH 61/81] Updated clang-tidy --- scripts/.clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/.clang-tidy b/scripts/.clang-tidy index c3802ca0..af1db756 100644 --- a/scripts/.clang-tidy +++ b/scripts/.clang-tidy @@ -13,7 +13,7 @@ readability-*,\ clang-analyzer-*' WarningsAsErrors: '' -HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h' +HeaderFilterRegex: 'include/spdlog/[^f].*' AnalyzeTemporaryDtors: false FormatStyle: none From 695912c7cf1b3ce59401f73bd7b18a666a77cda6 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 15 Feb 2020 12:06:01 +0200 Subject: [PATCH 62/81] Optimize ansicolor_sink to use array instead of map to find color codes --- include/spdlog/common.h | 1 + include/spdlog/sinks/ansicolor_sink.h | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 830220ef..e1108a0a 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -147,6 +147,7 @@ enum level_enum err = SPDLOG_LEVEL_ERROR, critical = SPDLOG_LEVEL_CRITICAL, off = SPDLOG_LEVEL_OFF, + n_levels }; #if !defined(SPDLOG_LEVEL_NAMES) diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 16433019..f81c40ac 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include namespace spdlog { namespace sinks { @@ -80,7 +80,7 @@ private: mutex_t &mutex_; bool should_do_colors_; std::unique_ptr formatter_; - std::unordered_map colors_; + std::array colors_; void print_ccode_(const string_view_t &color_code); void print_range_(const memory_buf_t &formatted, size_t start, size_t end); }; From 4b7c05903b99718accbb4eaa93b25a0686307479 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 15 Feb 2020 12:11:01 +0200 Subject: [PATCH 63/81] optimize wincolor_sink to use array instead of map to find color codes --- include/spdlog/sinks/wincolor_sink.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 743db5c6..a051a1df 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include namespace spdlog { @@ -52,7 +52,7 @@ protected: bool in_console_; bool should_do_colors_; std::unique_ptr formatter_; - std::unordered_map colors_; + std::array colors_; // set foreground color and return the orig console attributes (for resetting later) WORD set_foreground_color_(WORD attribs); From af75985ec6a003f0a16582756246d0bf0265971e Mon Sep 17 00:00:00 2001 From: Dmytro Milinevskyi Date: Sun, 16 Feb 2020 19:57:49 +0100 Subject: [PATCH 64/81] workaround for `Unknown extension ".c" for file` issue http://www.grokit.ca/cnt/CMakeProblemsSolutions/ --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c414f0e..17d992d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.2) +ENABLE_LANGUAGE(C) + #--------------------------------------------------------------------------------------- # Start spdlog project #--------------------------------------------------------------------------------------- From d38d53d9dd9a147d6e3d70e8218cdf85153f64f5 Mon Sep 17 00:00:00 2001 From: Crunkle Date: Mon, 17 Feb 2020 17:15:43 +0000 Subject: [PATCH 65/81] Fix Win32 event log sink --- include/spdlog/sinks/win_eventlog_sink.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index b594e0c9..83835743 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -219,7 +219,7 @@ protected: using namespace internal; memory_buf_t formatted; - formatter_->format(msg, formatted); + base_sink::formatter_->format(msg, formatted); formatted.push_back('\0'); LPCSTR lp_str = static_cast(formatted.data()); From 92467db591b3414dc58d3f05113b2ceeda82b602 Mon Sep 17 00:00:00 2001 From: Karl Liu Date: Fri, 21 Feb 2020 02:06:06 +0800 Subject: [PATCH 66/81] add example to FMT_STRING --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index d200a4d5..aab2b04c 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,15 @@ void android_example() android_logger->critical("Use \"adb shell logcat\" to view this message."); } ``` +--- +#### Compile-time format string syntax checking +```C++ +#include "spdlog/spdlog.h" +int main() +{ + spdlog::info(FMT_STRING("{:d} is an invalid format tag")); +} +``` ## Benchmarks From e47ecc1828973320a3f5a949666d74ab3e9c34b0 Mon Sep 17 00:00:00 2001 From: Craig Tyler Date: Wed, 11 Dec 2019 09:40:10 -0800 Subject: [PATCH 67/81] Updates supporting CPack RPM generation Enable command line overrides of default settings * CPACK_GENERATOR * CPACK_PACKAGE_RELOCATABLE Enables command line setting of generated RPM version attributes * CPACK_RPM_PACKAGE_RELEASE * CPACK_RPM_PACKAGE_ARCHITECTURE Adds dependency on pkgconfig RPM when not generating relocatable RPM --- cmake/spdlogCPack.cmake | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cmake/spdlogCPack.cmake b/cmake/spdlogCPack.cmake index 6ee2e51c..471a7ea3 100644 --- a/cmake/spdlogCPack.cmake +++ b/cmake/spdlogCPack.cmake @@ -1,7 +1,4 @@ -set(CPACK_GENERATOR - TGZ - ZIP - ) +set(CPACK_GENERATOR "TGZ;ZIP" CACHE STRING "Semicolon separated list of generators") set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY 0) set(CPACK_INSTALL_CMAKE_PROJECTS @@ -22,11 +19,32 @@ set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PR if (PROJECT_VERSION_TWEAK) set(CPACK_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION}.${PROJECT_VERSION_TWEAK}) endif () -set(CPACK_PACKAGE_RELOCATABLE ON) +set(CPACK_PACKAGE_RELOCATABLE ON CACHE BOOL "Build relocatable package") set(CPACK_RPM_PACKAGE_LICENSE "MIT") -set(CPACK_RPM_PACKAGE_GROUP "System Environment/Libraries") +set(CPACK_RPM_PACKAGE_GROUP "Development/Libraries") set(CPACK_RPM_PACKAGE_URL ${CPACK_PROJECT_URL}) set(CPACK_RPM_PACKAGE_DESCRIPTION "Very fast, header-only/compiled, C++ logging library.") +if (CPACK_PACKAGE_NAME) + set(CPACK_RPM_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}") +else() + set(CPACK_RPM_FILE_NAME "${PROJECT_NAME}-${CPACK_PACKAGE_VERSION}") +endif() + +if (CPACK_RPM_PACKAGE_RELEASE) + set(CPACK_RPM_FILE_NAME "${CPACK_RPM_FILE_NAME}-${CPACK_RPM_PACKAGE_RELEASE}") +endif () + +if (CPACK_RPM_PACKAGE_ARCHITECTURE) + set(CPACK_RPM_FILE_NAME "${CPACK_RPM_FILE_NAME}.${CPACK_RPM_PACKAGE_ARCHITECTURE}") +endif () +set(CPACK_RPM_FILE_NAME "${CPACK_RPM_FILE_NAME}.rpm") + +if (NOT CPACK_PACKAGE_RELOCATABLE) + # Depend on pkgconfig rpm to create the system pkgconfig folder + set(CPACK_RPM_PACKAGE_REQUIRES pkgconfig) + set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "${CPACK_PACKAGING_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig") +endif () + include(CPack) From 3848cbe24acc07e04c323f67ac2af2fe053cbc7c Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 24 Feb 2020 17:00:35 +0200 Subject: [PATCH 68/81] Fix #1452 --- include/spdlog/sinks/ansicolor_sink-inl.h | 2 +- include/spdlog/sinks/wincolor_sink-inl.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 58fa53fe..c75072af 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -43,7 +43,7 @@ SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg // Wrap the originally formatted message in color codes. // If color is not supported in the terminal, log as is instead. std::lock_guard lock(mutex_); - + msg.color_range_start = msg.color_range_end = 0; memory_buf_t formatted; formatter_->format(msg, formatted); if (should_do_colors_ && msg.color_range_end > msg.color_range_start) diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 6ce9cac3..52a82af8 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -52,6 +52,7 @@ template void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) { std::lock_guard lock(mutex_); + msg.color_range_start = msg.color_range_end = 0; memory_buf_t formatted; formatter_->format(msg, formatted); if (!in_console_) @@ -59,7 +60,6 @@ void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) write_to_file_(formatted); return; } - if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { // before color range From 64de8807e269cc5d562bdcf981ee8f5de7bd6168 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 24 Feb 2020 17:01:09 +0200 Subject: [PATCH 69/81] Fix #1452 --- include/spdlog/sinks/ansicolor_sink-inl.h | 3 ++- include/spdlog/sinks/wincolor_sink-inl.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index c75072af..a75dfa10 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -43,7 +43,8 @@ SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg // Wrap the originally formatted message in color codes. // If color is not supported in the terminal, log as is instead. std::lock_guard lock(mutex_); - msg.color_range_start = msg.color_range_end = 0; + msg.color_range_start = 0; + msg.color_range_end = 0; memory_buf_t formatted; formatter_->format(msg, formatted); if (should_do_colors_ && msg.color_range_end > msg.color_range_start) diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 52a82af8..9001c14e 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -52,7 +52,8 @@ template void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) { std::lock_guard lock(mutex_); - msg.color_range_start = msg.color_range_end = 0; + msg.color_range_start = 0; + msg.color_range_end = 0; memory_buf_t formatted; formatter_->format(msg, formatted); if (!in_console_) From 9f41903067dc05f480282faf5ed13159843ab51c Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 25 Feb 2020 15:00:42 +0200 Subject: [PATCH 70/81] Refactored tcp_client_sink --- include/spdlog/details/tcp_client.h | 145 ++++++++++++++++++++++++++++ include/spdlog/sinks/tcp_sink.h | 116 ++++++---------------- 2 files changed, 175 insertions(+), 86 deletions(-) create mode 100644 include/spdlog/details/tcp_client.h diff --git a/include/spdlog/details/tcp_client.h b/include/spdlog/details/tcp_client.h new file mode 100644 index 00000000..29f34839 --- /dev/null +++ b/include/spdlog/details/tcp_client.h @@ -0,0 +1,145 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// tcp client helper +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace spdlog { +namespace details { +class tcp_client +{ + int socket_ = -1; + +public: + bool is_connected() const + { + return socket_ != -1; + } + + void close() + { + if (is_connected()) + { + ::close(socket_); + socket_ = -1; + } + } + + int fd() const + { + return socket_; + } + + ~tcp_client() + { + close(); + } + + // try to connect or throw on failure + void connect(const std::string &host, int port) + { + close(); + spdlog::info("Connecting.."); + struct addrinfo hints{}; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_INET; // IPv4 + hints.ai_socktype = SOCK_STREAM; // TCP + hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value + hints.ai_protocol = 0; + + auto port_str = std::to_string(port); + struct addrinfo *addrinfo_result; + auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result); + if (rv != 0) + { + auto msg = fmt::format("::getaddrinfo failed: {}", gai_strerror(rv)); + SPDLOG_THROW(spdlog::spdlog_ex(msg)); + } + + // Try each address until we successfully connect(2). + int last_errno = 0; + for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) + { +#ifdef SPDLOG_PREVENT_CHILD_FD + int const flags = SOCK_CLOEXEC; +#else + int const flags = 0; +#endif + socket_ = ::socket(rp->ai_family, rp->ai_socktype | flags, rp->ai_protocol); + if (socket_ == -1) + { + last_errno = errno; + continue; + } + rv = ::connect(socket_, rp->ai_addr, rp->ai_addrlen); + if (rv == 0) + { + break; + } + else + { + last_errno = errno; + ::close(socket_); + socket_ = -1; + } + } + ::freeaddrinfo(addrinfo_result); + if (socket_ == -1) + { + SPDLOG_THROW(spdlog::spdlog_ex("::connect failed", last_errno)); + } + + // set TCP_NODELAY + int enable_flag = 1; + ::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, (char *)&enable_flag, sizeof(enable_flag)); + + // prevent sigpipe on systems where MSG_NOSIGNAL is not available +#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) + ::setsockopt(socket_, SOL_SOCKET, SO_NOSIGPIPE, (char *)&enable_flag, sizeof(enable_flag)); +#endif + +#if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) +#error "tcp_sink would raise SIGPIPE since niether SO_NOSIGPIPE nor MSG_NOSIGNAL are available" +#endif + } + + // Send exactly n_bytes of the given data. + // On error close the connection and throw. + void send(const char *data, size_t n_bytes) + { + size_t bytes_sent = 0; + while (bytes_sent < n_bytes) + { +#if defined(MSG_NOSIGNAL) + const int send_flags = MSG_NOSIGNAL; +#else + const int send_flags = 0; +#endif + auto write_result = ::send(socket_, data + bytes_sent, n_bytes - bytes_sent, send_flags); + if (write_result < 0) + { + close(); + SPDLOG_THROW(spdlog::spdlog_ex("write(2) failed", errno)); + } + + if (write_result == 0) // (probably should not happen but in any case..) + { + break; + } + bytes_sent += static_cast(write_result); + } + } +}; +} // namespace details +} // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index e380d3bb..30e34d95 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -6,121 +6,65 @@ #include #include #include - -#include -#include -#include -#include - +#include #include #include +#include +#include #pragma once +// tcp client sink +// connect to remote address and send the formatted log. +// will attempt to reconnect if connection drops. + namespace spdlog { namespace sinks { +struct tcp_sink_config +{ + std::string server_host; + int server_port; + bool lazy_connect = false; // connect on first log call instead of in construction + + tcp_sink_config(std::string host, int port) + : server_host{std::move(host)} + , server_port{port} + {} +}; + template class tcp_sink : public spdlog::sinks::base_sink { public: // connect to tcp host/port or throw if failed // host can be hostname or ip address - tcp_sink(std::string host, int port) + tcp_sink(tcp_sink_config sink_config) + : config_{std::move(sink_config)} { - sock_ = connect_to(host, port); - } - - ~tcp_sink() override - { - if (sock_ != -1) + if (!config_.lazy_connect) { - ::close(sock_); + this->client_.connect(config_.server_host, config_.server_port); } } + ~tcp_sink() override {} + protected: void sink_it_(const spdlog::details::log_msg &msg) override { spdlog::memory_buf_t formatted; spdlog::sinks::base_sink::formatter_->format(msg, formatted); - size_t bytes_sent = 0; - while (bytes_sent < formatted.size()) + if (!client_.is_connected()) { - auto write_result = ::write(sock_, formatted.data() + bytes_sent, formatted.size() - bytes_sent); - if (write_result < 0) - { - SPDLOG_THROW(spdlog::spdlog_ex("write(2) failed", errno)); - } - - if (write_result == 0) // (probably should not happen but in any case..) - { - break; - } - bytes_sent += static_cast(write_result); + client_.connect(config_.server_host, config_.server_port); } + client_.send(formatted.data(), formatted.size()); } void flush_() override {} - -private: - // try to connect and return socket fd or throw on failure - int connect_to(const std::string &host, int port) - { - struct addrinfo hints; - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_INET; // IPv4 - hints.ai_socktype = SOCK_STREAM; // TCP - hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value - hints.ai_protocol = 0; - - auto port_str = std::to_string(port); - struct addrinfo *addrinfo_result; - auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result); - if (rv != 0) - { - auto msg = fmt::format("::getaddrinfo failed: {}", gai_strerror(rv)); - SPDLOG_THROW(spdlog::spdlog_ex(msg)); - } - - // Try each address until we successfully connect(2). - int socket_rv = -1; - int last_errno = 0; - for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) - { - #ifdef SPDLOG_PREVENT_CHILD_FD - int const flags = SOCK_CLOEXEC; - #else - int const flags = 0; - #endif - socket_rv = ::socket(rp->ai_family, rp->ai_socktype | flags, rp->ai_protocol); - if (socket_rv == -1) - { - last_errno = errno; - continue; - } - rv = ::connect(socket_rv, rp->ai_addr, rp->ai_addrlen); - if (rv == 0) - { - break; - } - else - { - last_errno = errno; - ::close(socket_rv); - socket_rv = -1; - } - } - ::freeaddrinfo(addrinfo_result); - if (socket_rv == -1) - { - SPDLOG_THROW(spdlog::spdlog_ex("::connect failed", last_errno)); - } - return socket_rv; - } - -private: - int sock_ = -1; + tcp_sink_config config_; + details::tcp_client client_; }; using tcp_sink_mt = tcp_sink; From 0b91d55269c9e6c9e3eda20cbd193977e50b84f5 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 25 Feb 2020 15:09:03 +0200 Subject: [PATCH 71/81] Refactored tcp_client_sink --- include/spdlog/details/tcp_client.h | 4 ++++ include/spdlog/sinks/tcp_sink.h | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/tcp_client.h b/include/spdlog/details/tcp_client.h index 29f34839..5b2f0af1 100644 --- a/include/spdlog/details/tcp_client.h +++ b/include/spdlog/details/tcp_client.h @@ -3,6 +3,10 @@ #pragma once +#ifdef _WIN32 +#error tcp_client not supported under windows yet +#endif + // tcp client helper #include #include diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h index 30e34d95..662c0d96 100644 --- a/include/spdlog/sinks/tcp_sink.h +++ b/include/spdlog/sinks/tcp_sink.h @@ -14,9 +14,10 @@ #pragma once -// tcp client sink -// connect to remote address and send the formatted log. -// will attempt to reconnect if connection drops. +// Simple tcp client sink +// Connects to remote address and send the formatted log. +// Will attempt to reconnect if connection drops. +// If more complicated behaviour is needed (i.e get responses), you can inherit it and override the sink_it_ method. namespace spdlog { namespace sinks { @@ -39,7 +40,7 @@ class tcp_sink : public spdlog::sinks::base_sink public: // connect to tcp host/port or throw if failed // host can be hostname or ip address - tcp_sink(tcp_sink_config sink_config) + explicit tcp_sink(tcp_sink_config sink_config) : config_{std::move(sink_config)} { if (!config_.lazy_connect) @@ -48,7 +49,7 @@ public: } } - ~tcp_sink() override {} + ~tcp_sink() override = default; protected: void sink_it_(const spdlog::details::log_msg &msg) override From c71b433a358e7e172e1fc4d4c106e4709c3f4b03 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 25 Feb 2020 15:10:44 +0200 Subject: [PATCH 72/81] clang-format --- include/spdlog/sinks/ansicolor_sink.h | 2 +- include/spdlog/sinks/wincolor_sink.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index f81c40ac..d3c93284 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -80,7 +80,7 @@ private: mutex_t &mutex_; bool should_do_colors_; std::unique_ptr formatter_; - std::array colors_; + std::array colors_; void print_ccode_(const string_view_t &color_code); void print_range_(const memory_buf_t &formatted, size_t start, size_t end); }; diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index a051a1df..f1b8f4ac 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -52,7 +52,7 @@ protected: bool in_console_; bool should_do_colors_; std::unique_ptr formatter_; - std::array colors_; + std::array colors_; // set foreground color and return the orig console attributes (for resetting later) WORD set_foreground_color_(WORD attribs); From 6e763d277639fd967aea7756071761920df2c568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bodor?= Date: Tue, 25 Feb 2020 13:51:43 +0100 Subject: [PATCH 73/81] only include windows.h when it's unavoidable --- include/spdlog/common.h | 12 ------------ include/spdlog/sinks/msvc_sink.h | 9 +++++++++ include/spdlog/sinks/win_eventlog_sink.h | 9 +++++++++ include/spdlog/sinks/wincolor_sink.h | 10 ++++++++++ 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index e1108a0a..cb6355cf 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -15,18 +15,6 @@ #include #include -#ifdef _WIN32 -#ifndef NOMINMAX -#define NOMINMAX // prevent windows redefining min/max -#endif - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - -#include -#endif //_WIN32 - #ifdef SPDLOG_COMPILED_LIB #undef SPDLOG_HEADER_ONLY #define SPDLOG_INLINE diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index 6db10bc9..b3887ac7 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -8,6 +8,15 @@ #include #include +#ifndef NOMINMAX +#define NOMINMAX // prevent windows redefining min/max +#endif + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#include #include #include diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index 83835743..e3d4e3a6 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -30,6 +30,15 @@ Windows Registry Editor Version 5.00 #pragma once +#ifndef NOMINMAX +#define NOMINMAX // prevent windows redefining min/max +#endif + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#include #include #include diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index a051a1df..9feab90d 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -12,6 +12,16 @@ #include #include #include + +#ifndef NOMINMAX +#define NOMINMAX // prevent windows redefining min/max +#endif + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#include #include namespace spdlog { From ffe272c16582b09005e0361be60278729d8cd8dd Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 25 Feb 2020 18:49:28 +0200 Subject: [PATCH 74/81] Added SDPLOG_TIDY option to CMakeLists.txt instead of clang_tidy.sh --- scripts/.clang-tidy => .clang-tidy | 2 +- CMakeLists.txt | 12 +++++++++++- scripts/clang_tidy.sh | 5 ----- 3 files changed, 12 insertions(+), 7 deletions(-) rename scripts/.clang-tidy => .clang-tidy (96%) delete mode 100755 scripts/clang_tidy.sh diff --git a/scripts/.clang-tidy b/.clang-tidy similarity index 96% rename from scripts/.clang-tidy rename to .clang-tidy index af1db756..651fa8e9 100644 --- a/scripts/.clang-tidy +++ b/.clang-tidy @@ -13,7 +13,7 @@ readability-*,\ clang-analyzer-*' WarningsAsErrors: '' -HeaderFilterRegex: 'include/spdlog/[^f].*' +HeaderFilterRegex: '*spdlog/[^f].*' AnalyzeTemporaryDtors: false FormatStyle: none diff --git a/CMakeLists.txt b/CMakeLists.txt index 17d992d4..d617790d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,17 @@ option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each l option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" OFF) option(SPDLOG_NO_ATOMIC_LEVELS "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently" OFF) +# clang-tidy +if(${CMAKE_VERSION} VERSION_GREATER "3.5") + option(SPDLOG_TIDY "run clang-tidy" OFF) +endif() + +if(SPDLOG_TIDY) + set(CMAKE_CXX_CLANG_TIDY "clang-tidy") + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + message(STATUS "Enabled clang-tidy") +endif() + find_package(Threads REQUIRED) message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) #--------------------------------------------------------------------------------------- @@ -289,6 +300,5 @@ if (SPDLOG_INSTALL) # Support creation of installable packages #--------------------------------------------------------------------------------------- include(cmake/spdlogCPack.cmake) - endif () diff --git a/scripts/clang_tidy.sh b/scripts/clang_tidy.sh deleted file mode 100755 index b62bfaf1..00000000 --- a/scripts/clang_tidy.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -cd "$(dirname "$0")" - -clang-tidy ../example/example.cpp -- -I ../include From daaa0253567c261d8e8c94f8958a1d3739e1f15d Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 25 Feb 2020 18:52:35 +0200 Subject: [PATCH 75/81] moved .clang-format to top level --- scripts/.clang-format => .clang-format | 0 scripts/format.sh | 8 ++++---- 2 files changed, 4 insertions(+), 4 deletions(-) rename scripts/.clang-format => .clang-format (100%) diff --git a/scripts/.clang-format b/.clang-format similarity index 100% rename from scripts/.clang-format rename to .clang-format diff --git a/scripts/format.sh b/scripts/format.sh index ef52a5f1..d1c36007 100755 --- a/scripts/format.sh +++ b/scripts/format.sh @@ -1,12 +1,12 @@ #!/bin/bash -cd "$(dirname "$0")" - +cd "$(dirname "$0")"/.. +pwd echo -n "Running dos2unix " -find .. -name "*\.h" -o -name "*\.cpp"|grep -v bundled|xargs -I {} sh -c "dos2unix '{}' 2>/dev/null; echo -n '.'" +find . -name "*\.h" -o -name "*\.cpp"|grep -v bundled|xargs -I {} sh -c "dos2unix '{}' 2>/dev/null; echo -n '.'" echo echo -n "Running clang-format " -find .. -name "*\.h" -o -name "*\.cpp"|grep -v bundled|xargs -I {} sh -c "clang-format -i {}; echo -n '.'" +find . -name "*\.h" -o -name "*\.cpp"|grep -v bundled|xargs -I {} sh -c "clang-format -i {}; echo -n '.'" echo From fab33dd2306b929cdbe549ae3a80770add66580b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bodor?= Date: Wed, 26 Feb 2020 08:54:56 +0100 Subject: [PATCH 76/81] refactor: extract windows.h include to details/windows_include.h --- include/spdlog/details/windows_include.h | 11 +++++++++++ include/spdlog/sinks/msvc_sink.h | 10 +--------- include/spdlog/sinks/win_eventlog_sink.h | 14 +++----------- include/spdlog/sinks/wincolor_sink.h | 10 +--------- 4 files changed, 16 insertions(+), 29 deletions(-) create mode 100644 include/spdlog/details/windows_include.h diff --git a/include/spdlog/details/windows_include.h b/include/spdlog/details/windows_include.h new file mode 100644 index 00000000..6a2f14f9 --- /dev/null +++ b/include/spdlog/details/windows_include.h @@ -0,0 +1,11 @@ +#pragma once + +#ifndef NOMINMAX +#define NOMINMAX // prevent windows redefining min/max +#endif + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif + +#include diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index b3887ac7..f6c25acb 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -8,15 +8,7 @@ #include #include -#ifndef NOMINMAX -#define NOMINMAX // prevent windows redefining min/max -#endif - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - -#include +#include #include #include diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h index e3d4e3a6..d9839110 100644 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -30,20 +30,12 @@ Windows Registry Editor Version 5.00 #pragma once -#ifndef NOMINMAX -#define NOMINMAX // prevent windows redefining min/max -#endif - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - -#include -#include - #include #include +#include +#include + #include #include #include diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 9feab90d..8ac63d3d 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -13,15 +13,7 @@ #include #include -#ifndef NOMINMAX -#define NOMINMAX // prevent windows redefining min/max -#endif - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - -#include +#include #include namespace spdlog { From db26a103d6253ecc7d61bcfb3970a27dfc019994 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 26 Feb 2020 12:50:51 +0200 Subject: [PATCH 77/81] Update spdlog.h --- include/spdlog/spdlog.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index facb0b3f..ee723a62 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -40,7 +40,7 @@ inline std::shared_ptr create(std::string logger_name, SinkArgs // formatter and flush level will be set according the global settings. // // NOTE: -// Use this function when creating loggers manually. +// Useful when creating loggers manually and initializing them according to the global settings. // // Example: // auto console_sink = std::make_shared(); From ab2f3307eb3233bfcf2d2e3d2b3d043aad9226c6 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 26 Feb 2020 12:51:16 +0200 Subject: [PATCH 78/81] Update spdlog.h --- include/spdlog/spdlog.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index ee723a62..541986dd 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -39,7 +39,6 @@ inline std::shared_ptr create(std::string logger_name, SinkArgs // Initialize and register a logger, // formatter and flush level will be set according the global settings. // -// NOTE: // Useful when creating loggers manually and initializing them according to the global settings. // // Example: From fccb25586f11bee1052bc088f0a5588f9cdcafb7 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 26 Feb 2020 12:52:46 +0200 Subject: [PATCH 79/81] Update spdlog.h --- include/spdlog/spdlog.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 541986dd..7a05e6fe 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -39,7 +39,7 @@ inline std::shared_ptr create(std::string logger_name, SinkArgs // Initialize and register a logger, // formatter and flush level will be set according the global settings. // -// Useful when creating loggers manually and initializing them according to the global settings. +// Useful for initializing manually created loggers with the global settings. // // Example: // auto console_sink = std::make_shared(); From 4cdb159ccb72002243220f3ec1160206bb6faf39 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Wed, 26 Feb 2020 12:54:31 +0200 Subject: [PATCH 80/81] Update spdlog.h --- include/spdlog/spdlog.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 7a05e6fe..9ac04be0 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -42,9 +42,8 @@ inline std::shared_ptr create(std::string logger_name, SinkArgs // Useful for initializing manually created loggers with the global settings. // // Example: -// auto console_sink = std::make_shared(); -// auto console_logger = std::make_shared("console_logger", console_sink); -// spdlog::initialize_logger(console_logger); +// auto mylogger = std::make_shared("mylogger", ...); +// spdlog::initialize_logger(mylogger); void initialize_logger(std::shared_ptr logger); // Return an existing logger or nullptr if a logger with such name doesn't From 05ecad4263bf49bbc0b396a7168115050d2e1390 Mon Sep 17 00:00:00 2001 From: gabime Date: Wed, 26 Feb 2020 18:10:47 +0200 Subject: [PATCH 81/81] Use windows_include in os-inl.h --- include/spdlog/details/os-inl.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index e83c2ce4..d77aab11 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -23,16 +23,9 @@ #ifdef _WIN32 -#ifndef NOMINMAX -#define NOMINMAX // prevent windows redefining min/max -#endif - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif #include // _get_osfhandle and _isatty support #include // _get_pid support -#include +#include #ifdef __MINGW32__ #include