From 71f0ee98ccc85d41ba7587d122c83011ab1e25c3 Mon Sep 17 00:00:00 2001 From: F1F7Y <64418963+F1F7Y@users.noreply.github.com> Date: Fri, 30 Jun 2023 03:10:24 +0200 Subject: Reorganize third-party dependencies into `thirdparty` directory (#491) * rename `include` to `thirdparty` * remove duplicate minhook in wsock32 * move minhook into its own directory * move openssl lib into separate directories --- include/spdlog/sinks/android_sink.h | 119 ----------- include/spdlog/sinks/ansicolor_sink-inl.h | 145 -------------- include/spdlog/sinks/ansicolor_sink.h | 118 ----------- include/spdlog/sinks/base_sink-inl.h | 63 ------ include/spdlog/sinks/base_sink.h | 52 ----- include/spdlog/sinks/basic_file_sink-inl.h | 43 ---- include/spdlog/sinks/basic_file_sink.h | 58 ------ include/spdlog/sinks/daily_file_sink.h | 238 ---------------------- include/spdlog/sinks/dist_sink.h | 97 --------- include/spdlog/sinks/dup_filter_sink.h | 90 --------- include/spdlog/sinks/hourly_file_sink.h | 194 ------------------ include/spdlog/sinks/msvc_sink.h | 49 ----- include/spdlog/sinks/null_sink.h | 44 ---- include/spdlog/sinks/ostream_sink.h | 50 ----- include/spdlog/sinks/ringbuffer_sink.h | 74 ------- include/spdlog/sinks/rotating_file_sink-inl.h | 131 ------------ include/spdlog/sinks/rotating_file_sink.h | 78 -------- include/spdlog/sinks/sink-inl.h | 25 --- include/spdlog/sinks/sink.h | 35 ---- include/spdlog/sinks/stdout_color_sinks-inl.h | 38 ---- include/spdlog/sinks/stdout_color_sinks.h | 45 ----- include/spdlog/sinks/stdout_sinks-inl.h | 135 ------------- include/spdlog/sinks/stdout_sinks.h | 87 -------- include/spdlog/sinks/syslog_sink.h | 109 ---------- include/spdlog/sinks/systemd_sink.h | 103 ---------- include/spdlog/sinks/tcp_sink.h | 81 -------- include/spdlog/sinks/win_eventlog_sink.h | 276 -------------------------- include/spdlog/sinks/wincolor_sink-inl.h | 175 ---------------- include/spdlog/sinks/wincolor_sink.h | 85 -------- 29 files changed, 2837 deletions(-) delete mode 100644 include/spdlog/sinks/android_sink.h delete mode 100644 include/spdlog/sinks/ansicolor_sink-inl.h delete mode 100644 include/spdlog/sinks/ansicolor_sink.h delete mode 100644 include/spdlog/sinks/base_sink-inl.h delete mode 100644 include/spdlog/sinks/base_sink.h delete mode 100644 include/spdlog/sinks/basic_file_sink-inl.h delete mode 100644 include/spdlog/sinks/basic_file_sink.h delete mode 100644 include/spdlog/sinks/daily_file_sink.h delete mode 100644 include/spdlog/sinks/dist_sink.h delete mode 100644 include/spdlog/sinks/dup_filter_sink.h delete mode 100644 include/spdlog/sinks/hourly_file_sink.h delete mode 100644 include/spdlog/sinks/msvc_sink.h delete mode 100644 include/spdlog/sinks/null_sink.h delete mode 100644 include/spdlog/sinks/ostream_sink.h delete mode 100644 include/spdlog/sinks/ringbuffer_sink.h delete mode 100644 include/spdlog/sinks/rotating_file_sink-inl.h delete mode 100644 include/spdlog/sinks/rotating_file_sink.h delete mode 100644 include/spdlog/sinks/sink-inl.h delete mode 100644 include/spdlog/sinks/sink.h delete mode 100644 include/spdlog/sinks/stdout_color_sinks-inl.h delete mode 100644 include/spdlog/sinks/stdout_color_sinks.h delete mode 100644 include/spdlog/sinks/stdout_sinks-inl.h delete mode 100644 include/spdlog/sinks/stdout_sinks.h delete mode 100644 include/spdlog/sinks/syslog_sink.h delete mode 100644 include/spdlog/sinks/systemd_sink.h delete mode 100644 include/spdlog/sinks/tcp_sink.h delete mode 100644 include/spdlog/sinks/win_eventlog_sink.h delete mode 100644 include/spdlog/sinks/wincolor_sink-inl.h delete mode 100644 include/spdlog/sinks/wincolor_sink.h (limited to 'include/spdlog/sinks') diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h deleted file mode 100644 index 659beabb..00000000 --- a/include/spdlog/sinks/android_sink.h +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifdef __ANDROID__ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#if !defined(SPDLOG_ANDROID_RETRIES) -#define SPDLOG_ANDROID_RETRIES 2 -#endif - -namespace spdlog { -namespace sinks { - -/* - * Android sink (logging using __android_log_write) - */ -template -class android_sink final : public base_sink -{ -public: - explicit android_sink(std::string tag = "spdlog", bool use_raw_msg = false) - : tag_(std::move(tag)) - , use_raw_msg_(use_raw_msg) - {} - -protected: - void sink_it_(const details::log_msg &msg) override - { - const android_LogPriority priority = convert_to_android_(msg.level); - memory_buf_t formatted; - if (use_raw_msg_) - { - details::fmt_helper::append_string_view(msg.payload, formatted); - } - else - { - base_sink::formatter_->format(msg, formatted); - } - formatted.push_back('\0'); - const char *msg_output = formatted.data(); - - // See system/core/liblog/logger_write.c for explanation of return value - int ret = __android_log_write(priority, tag_.c_str(), msg_output); - int retry_count = 0; - while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) - { - details::os::sleep_for_millis(5); - ret = __android_log_write(priority, tag_.c_str(), msg_output); - retry_count++; - } - - if (ret < 0) - { - throw_spdlog_ex("__android_log_write() failed", ret); - } - } - - void flush_() override {} - -private: - static android_LogPriority convert_to_android_(spdlog::level::level_enum level) - { - switch (level) - { - case spdlog::level::trace: - return ANDROID_LOG_VERBOSE; - case spdlog::level::debug: - return ANDROID_LOG_DEBUG; - case spdlog::level::info: - return ANDROID_LOG_INFO; - case spdlog::level::warn: - return ANDROID_LOG_WARN; - case spdlog::level::err: - return ANDROID_LOG_ERROR; - case spdlog::level::critical: - return ANDROID_LOG_FATAL; - default: - return ANDROID_LOG_DEFAULT; - } - } - - std::string tag_; - bool use_raw_msg_; -}; - -using android_sink_mt = android_sink; -using android_sink_st = android_sink; -} // namespace sinks - -// Create and register android syslog logger - -template -inline std::shared_ptr android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog") -{ - return Factory::template create(logger_name, tag); -} - -template -inline std::shared_ptr android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog") -{ - return Factory::template create(logger_name, tag); -} - -} // namespace spdlog - -#endif // __ANDROID__ diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h deleted file mode 100644 index 61c0a719..00000000 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ /dev/null @@ -1,145 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY -#include -#endif - -#include -#include - -namespace spdlog { -namespace sinks { - -template -SPDLOG_INLINE ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) - : target_file_(target_file) - , mutex_(ConsoleMutex::mutex()) - , formatter_(details::make_unique()) - -{ - set_color_mode(mode); - colors_[level::trace] = to_string_(white); - colors_[level::debug] = to_string_(cyan); - colors_[level::info] = to_string_(green); - colors_[level::warn] = to_string_(yellow_bold); - colors_[level::err] = to_string_(red_bold); - colors_[level::critical] = to_string_(bold_on_red); - colors_[level::off] = to_string_(reset); -} - -template -SPDLOG_INLINE void ansicolor_sink::set_color(level::level_enum color_level, string_view_t color) -{ - std::lock_guard lock(mutex_); - colors_[color_level] = to_string_(color); -} - -template -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 = 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) - { - // before color range - print_range_(formatted, 0, msg.color_range_start); - // in color range - print_ccode_(colors_[msg.level]); - print_range_(formatted, msg.color_range_start, msg.color_range_end); - print_ccode_(reset); - // after color range - print_range_(formatted, msg.color_range_end, formatted.size()); - } - else // no color - { - print_range_(formatted, 0, formatted.size()); - } - fflush(target_file_); -} - -template -SPDLOG_INLINE void ansicolor_sink::flush() -{ - std::lock_guard lock(mutex_); - fflush(target_file_); -} - -template -SPDLOG_INLINE void ansicolor_sink::set_pattern(const std::string &pattern) -{ - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} - -template -SPDLOG_INLINE void ansicolor_sink::set_formatter(std::unique_ptr sink_formatter) -{ - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); -} - -template -SPDLOG_INLINE bool ansicolor_sink::should_color() -{ - return should_do_colors_; -} - -template -SPDLOG_INLINE void ansicolor_sink::set_color_mode(color_mode mode) -{ - switch (mode) - { - case color_mode::always: - should_do_colors_ = true; - return; - case color_mode::automatic: - should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal(); - return; - case color_mode::never: - should_do_colors_ = false; - return; - default: - should_do_colors_ = false; - } -} - -template -SPDLOG_INLINE void ansicolor_sink::print_ccode_(const string_view_t &color_code) -{ - fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); -} - -template -SPDLOG_INLINE void ansicolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) -{ - fwrite(formatted.data() + start, sizeof(char), end - start, target_file_); -} - -template -SPDLOG_INLINE std::string ansicolor_sink::to_string_(const string_view_t &sv) -{ - return std::string(sv.data(), sv.size()); -} - -// ansicolor_stdout_sink -template -SPDLOG_INLINE ansicolor_stdout_sink::ansicolor_stdout_sink(color_mode mode) - : ansicolor_sink(stdout, mode) -{} - -// ansicolor_stderr_sink -template -SPDLOG_INLINE ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) - : ansicolor_sink(stderr, mode) -{} - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h deleted file mode 100644 index 4438f706..00000000 --- a/include/spdlog/sinks/ansicolor_sink.h +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { - -/** - * This sink prefixes the output with an ANSI escape sequence color code - * depending on the severity - * of the message. - * If no color terminal detected, omit the escape codes. - */ - -template -class ansicolor_sink : public sink -{ -public: - using mutex_t = typename ConsoleMutex::mutex_t; - ansicolor_sink(FILE *target_file, color_mode mode); - ~ansicolor_sink() override = default; - - ansicolor_sink(const ansicolor_sink &other) = delete; - ansicolor_sink(ansicolor_sink &&other) = delete; - - ansicolor_sink &operator=(const ansicolor_sink &other) = delete; - ansicolor_sink &operator=(ansicolor_sink &&other) = delete; - - void set_color(level::level_enum color_level, string_view_t color); - void set_color_mode(color_mode mode); - bool should_color(); - - void log(const details::log_msg &msg) override; - void flush() override; - void set_pattern(const std::string &pattern) final; - void set_formatter(std::unique_ptr sink_formatter) override; - - // Formatting codes - const string_view_t reset = "\033[m"; - const string_view_t bold = "\033[1m"; - const string_view_t dark = "\033[2m"; - const string_view_t underline = "\033[4m"; - const string_view_t blink = "\033[5m"; - const string_view_t reverse = "\033[7m"; - const string_view_t concealed = "\033[8m"; - const string_view_t clear_line = "\033[K"; - - // Foreground colors - const string_view_t black = "\033[30m"; - const string_view_t red = "\033[31m"; - const string_view_t green = "\033[32m"; - const string_view_t yellow = "\033[33m"; - const string_view_t blue = "\033[34m"; - const string_view_t magenta = "\033[35m"; - const string_view_t cyan = "\033[36m"; - const string_view_t white = "\033[37m"; - - /// Background colors - const string_view_t on_black = "\033[40m"; - const string_view_t on_red = "\033[41m"; - const string_view_t on_green = "\033[42m"; - const string_view_t on_yellow = "\033[43m"; - const string_view_t on_blue = "\033[44m"; - const string_view_t on_magenta = "\033[45m"; - const string_view_t on_cyan = "\033[46m"; - const string_view_t on_white = "\033[47m"; - - /// Bold colors - const string_view_t yellow_bold = "\033[33m\033[1m"; - const string_view_t red_bold = "\033[31m\033[1m"; - const string_view_t bold_on_red = "\033[1m\033[41m"; - -private: - FILE *target_file_; - mutex_t &mutex_; - bool should_do_colors_; - std::unique_ptr formatter_; - 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); - static std::string to_string_(const string_view_t &sv); -}; - -template -class ansicolor_stdout_sink : public ansicolor_sink -{ -public: - explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic); -}; - -template -class ansicolor_stderr_sink : public ansicolor_sink -{ -public: - explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic); -}; - -using ansicolor_stdout_sink_mt = ansicolor_stdout_sink; -using ansicolor_stdout_sink_st = ansicolor_stdout_sink; - -using ansicolor_stderr_sink_mt = ansicolor_stderr_sink; -using ansicolor_stderr_sink_st = ansicolor_stderr_sink; - -} // namespace sinks -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "ansicolor_sink-inl.h" -#endif diff --git a/include/spdlog/sinks/base_sink-inl.h b/include/spdlog/sinks/base_sink-inl.h deleted file mode 100644 index b15fb0e6..00000000 --- a/include/spdlog/sinks/base_sink-inl.h +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY -#include -#endif - -#include -#include - -#include - -template -SPDLOG_INLINE spdlog::sinks::base_sink::base_sink() - : formatter_{details::make_unique()} -{} - -template -SPDLOG_INLINE spdlog::sinks::base_sink::base_sink(std::unique_ptr formatter) - : formatter_{std::move(formatter)} -{} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::log(const details::log_msg &msg) -{ - std::lock_guard lock(mutex_); - sink_it_(msg); -} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::flush() -{ - std::lock_guard lock(mutex_); - flush_(); -} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern(const std::string &pattern) -{ - std::lock_guard lock(mutex_); - set_pattern_(pattern); -} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::set_formatter(std::unique_ptr sink_formatter) -{ - std::lock_guard lock(mutex_); - set_formatter_(std::move(sink_formatter)); -} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern_(const std::string &pattern) -{ - set_formatter_(details::make_unique(pattern)); -} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::set_formatter_(std::unique_ptr sink_formatter) -{ - formatter_ = std::move(sink_formatter); -} diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h deleted file mode 100644 index bf5072f8..00000000 --- a/include/spdlog/sinks/base_sink.h +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once -// -// base sink templated over a mutex (either dummy or real) -// concrete implementation should override the sink_it_() and flush_() methods. -// locking is taken care of in this class - no locking needed by the -// implementers.. -// - -#include -#include -#include - -namespace spdlog { -namespace sinks { -template -class base_sink : public sink -{ -public: - base_sink(); - explicit base_sink(std::unique_ptr formatter); - ~base_sink() override = default; - - base_sink(const base_sink &) = delete; - base_sink(base_sink &&) = delete; - - base_sink &operator=(const base_sink &) = delete; - base_sink &operator=(base_sink &&) = delete; - - void log(const details::log_msg &msg) final; - void flush() final; - void set_pattern(const std::string &pattern) final; - void set_formatter(std::unique_ptr sink_formatter) final; - -protected: - // sink formatter - std::unique_ptr formatter_; - Mutex mutex_; - - virtual void sink_it_(const details::log_msg &msg) = 0; - virtual void flush_() = 0; - virtual void set_pattern_(const std::string &pattern); - virtual void set_formatter_(std::unique_ptr sink_formatter); -}; -} // namespace sinks -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "base_sink-inl.h" -#endif diff --git a/include/spdlog/sinks/basic_file_sink-inl.h b/include/spdlog/sinks/basic_file_sink-inl.h deleted file mode 100644 index 1260d15c..00000000 --- a/include/spdlog/sinks/basic_file_sink-inl.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY -#include -#endif - -#include -#include - -namespace spdlog { -namespace sinks { - -template -SPDLOG_INLINE basic_file_sink::basic_file_sink(const filename_t &filename, bool truncate) -{ - file_helper_.open(filename, truncate); -} - -template -SPDLOG_INLINE const filename_t &basic_file_sink::filename() const -{ - return file_helper_.filename(); -} - -template -SPDLOG_INLINE void basic_file_sink::sink_it_(const details::log_msg &msg) -{ - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - file_helper_.write(formatted); -} - -template -SPDLOG_INLINE void basic_file_sink::flush_() -{ - file_helper_.flush(); -} - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h deleted file mode 100644 index 4a742624..00000000 --- a/include/spdlog/sinks/basic_file_sink.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#include -#include - -namespace spdlog { -namespace sinks { -/* - * Trivial file sink with single file as target - */ -template -class basic_file_sink final : public base_sink -{ -public: - explicit basic_file_sink(const filename_t &filename, bool truncate = false); - const filename_t &filename() const; - -protected: - void sink_it_(const details::log_msg &msg) override; - void flush_() override; - -private: - details::file_helper file_helper_; -}; - -using basic_file_sink_mt = basic_file_sink; -using basic_file_sink_st = basic_file_sink; - -} // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false) -{ - return Factory::template create(logger_name, filename, truncate); -} - -template -inline std::shared_ptr basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false) -{ - return Factory::template create(logger_name, filename, truncate); -} - -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "basic_file_sink-inl.h" -#endif diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h deleted file mode 100644 index 16be821a..00000000 --- a/include/spdlog/sinks/daily_file_sink.h +++ /dev/null @@ -1,238 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { - -/* - * Generator of daily log file names in format basename.YYYY-MM-DD.ext - */ -struct daily_filename_calculator -{ - // Create filename for the form basename.YYYY-MM-DD - static filename_t calc_filename(const filename_t &filename, const tm &now_tm) - { - filename_t basename, ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); - return fmt::format( - SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext); - } -}; - -/* - * Generator of daily log file names with strftime format. - * Usages: - * auto sink = std::make_shared("myapp-%Y-%m-%d:%H:%M:%S.log", hour, minute);" - * auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", hour, minute)" - * - */ -struct daily_filename_format_calculator -{ - static filename_t calc_filename (const filename_t &filename, const tm &now_tm) - { - // generate fmt datetime format string, e.g. {:%Y-%m-%d}. - filename_t fmt_filename = fmt::format(SPDLOG_FILENAME_T ("{{:{}}}"), filename); - return fmt::format(fmt_filename, now_tm); - } -}; - -/* - * Rotating file sink based on date. - * If truncate != false , the created file will be truncated. - * If max_files > 0, retain only the last max_files and delete previous. - */ -template -class daily_file_sink final : public base_sink -{ -public: - // create daily file sink which rotates on given time - daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0) - : base_filename_(std::move(base_filename)) - , rotation_h_(rotation_hour) - , rotation_m_(rotation_minute) - , truncate_(truncate) - , max_files_(max_files) - , filenames_q_() - { - if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) - { - throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); - } - - auto now = log_clock::now(); - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); - file_helper_.open(filename, truncate_); - rotation_tp_ = next_rotation_tp_(); - - if (max_files_ > 0) - { - init_filenames_q_(); - } - } - - filename_t filename() - { - std::lock_guard lock(base_sink::mutex_); - return file_helper_.filename(); - } - -protected: - void sink_it_(const details::log_msg &msg) override - { - auto time = msg.time; - bool should_rotate = time >= rotation_tp_; - if (should_rotate) - { - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); - file_helper_.open(filename, truncate_); - rotation_tp_ = next_rotation_tp_(); - } - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - file_helper_.write(formatted); - - // Do the cleaning only at the end because it might throw on failure. - if (should_rotate && max_files_ > 0) - { - delete_old_(); - } - } - - void flush_() override - { - file_helper_.flush(); - } - -private: - void init_filenames_q_() - { - 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 (!path_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); - return spdlog::details::os::localtime(tnow); - } - - log_clock::time_point next_rotation_tp_() - { - auto now = log_clock::now(); - tm date = now_tm(now); - date.tm_hour = rotation_h_; - date.tm_min = rotation_m_; - date.tm_sec = 0; - auto rotation_time = log_clock::from_time_t(std::mktime(&date)); - if (rotation_time > now) - { - return rotation_time; - } - return {rotation_time + std::chrono::hours(24)}; - } - - // Delete the file N rotations ago. - // Throw spdlog_ex on failure to delete the old file. - void delete_old_() - { - using details::os::filename_to_str; - using details::os::remove_if_exists; - - filename_t current_file = file_helper_.filename(); - if (filenames_q_.full()) - { - auto old_filename = std::move(filenames_q_.front()); - filenames_q_.pop_front(); - bool ok = remove_if_exists(old_filename) == 0; - if (!ok) - { - filenames_q_.push_back(std::move(current_file)); - throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), errno); - } - } - filenames_q_.push_back(std::move(current_file)); - } - - filename_t base_filename_; - int rotation_h_; - int rotation_m_; - log_clock::time_point rotation_tp_; - details::file_helper file_helper_; - bool truncate_; - uint16_t max_files_; - details::circular_q filenames_q_; -}; - -using daily_file_sink_mt = daily_file_sink; -using daily_file_sink_st = daily_file_sink; -using daily_file_format_sink_mt = daily_file_sink; -using daily_file_format_sink_st = daily_file_sink; - -} // namespace sinks - -// -// factory functions -// -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, uint16_t max_files = 0) -{ - return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); -} - -template -inline std::shared_ptr daily_logger_format_mt( - 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, 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, uint16_t max_files = 0) -{ - return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); -} - -template -inline std::shared_ptr daily_logger_format_st( - 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, max_files); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h deleted file mode 100644 index 8fccb4ee..00000000 --- a/include/spdlog/sinks/dist_sink.h +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include "base_sink.h" -#include -#include -#include - -#include -#include -#include -#include - -// Distribution sink (mux). Stores a vector of sinks which get called when log -// is called - -namespace spdlog { -namespace sinks { - -template -class dist_sink : public base_sink -{ -public: - dist_sink() = default; - explicit dist_sink(std::vector> sinks) - : sinks_(sinks) - {} - - dist_sink(const dist_sink &) = delete; - dist_sink &operator=(const dist_sink &) = delete; - - void add_sink(std::shared_ptr sink) - { - std::lock_guard lock(base_sink::mutex_); - sinks_.push_back(sink); - } - - void remove_sink(std::shared_ptr sink) - { - std::lock_guard lock(base_sink::mutex_); - sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); - } - - void set_sinks(std::vector> sinks) - { - std::lock_guard lock(base_sink::mutex_); - sinks_ = std::move(sinks); - } - - std::vector> &sinks() - { - return sinks_; - } - -protected: - void sink_it_(const details::log_msg &msg) override - { - for (auto &sink : sinks_) - { - if (sink->should_log(msg.level)) - { - sink->log(msg); - } - } - } - - void flush_() override - { - for (auto &sink : sinks_) - { - sink->flush(); - } - } - - void set_pattern_(const std::string &pattern) override - { - set_formatter_(details::make_unique(pattern)); - } - - void set_formatter_(std::unique_ptr sink_formatter) override - { - base_sink::formatter_ = std::move(sink_formatter); - for (auto &sink : sinks_) - { - sink->set_formatter(base_sink::formatter_->clone()); - } - } - std::vector> sinks_; -}; - -using dist_sink_mt = dist_sink; -using dist_sink_st = dist_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/dup_filter_sink.h b/include/spdlog/sinks/dup_filter_sink.h deleted file mode 100644 index c9a08d68..00000000 --- a/include/spdlog/sinks/dup_filter_sink.h +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include "dist_sink.h" -#include -#include - -#include -#include -#include - -// Duplicate message removal sink. -// Skip the message if previous one is identical and less than "max_skip_duration" have passed -// -// Example: -// -// #include -// -// int main() { -// auto dup_filter = std::make_shared(std::chrono::seconds(5)); -// dup_filter->add_sink(std::make_shared()); -// spdlog::logger l("logger", dup_filter); -// l.info("Hello"); -// l.info("Hello"); -// l.info("Hello"); -// l.info("Different Hello"); -// } -// -// Will produce: -// [2019-06-25 17:50:56.511] [logger] [info] Hello -// [2019-06-25 17:50:56.512] [logger] [info] Skipped 3 duplicate messages.. -// [2019-06-25 17:50:56.512] [logger] [info] Different Hello - -namespace spdlog { -namespace sinks { -template -class dup_filter_sink : public dist_sink -{ -public: - template - explicit dup_filter_sink(std::chrono::duration max_skip_duration) - : max_skip_duration_{max_skip_duration} - {} - -protected: - std::chrono::microseconds max_skip_duration_; - log_clock::time_point last_msg_time_; - std::string last_msg_payload_; - size_t skip_counter_ = 0; - - void sink_it_(const details::log_msg &msg) override - { - bool filtered = filter_(msg); - if (!filtered) - { - skip_counter_ += 1; - return; - } - - // log the "skipped.." message - if (skip_counter_ > 0) - { - memory_buf_t buf; - fmt::format_to(buf, "Skipped {} duplicate messages..", skip_counter_); - details::log_msg skipped_msg{msg.logger_name, level::info, string_view_t{buf.data(), buf.size()}}; - dist_sink::sink_it_(skipped_msg); - } - - // log current message - dist_sink::sink_it_(msg); - last_msg_time_ = msg.time; - skip_counter_ = 0; - last_msg_payload_.assign(msg.payload.data(), msg.payload.data() + msg.payload.size()); - } - - // return whether the log msg should be displayed (true) or skipped (false) - bool filter_(const details::log_msg &msg) - { - auto filter_duration = msg.time - last_msg_time_; - return (filter_duration > max_skip_duration_) || (msg.payload != last_msg_payload_); - } -}; - -using dup_filter_sink_mt = dup_filter_sink; -using dup_filter_sink_st = dup_filter_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h deleted file mode 100644 index f5e34366..00000000 --- a/include/spdlog/sinks/hourly_file_sink.h +++ /dev/null @@ -1,194 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { - -/* - * Generator of Hourly log file names in format basename.YYYY-MM-DD-HH.ext - */ -struct hourly_filename_calculator -{ - // Create filename for the form basename.YYYY-MM-DD-H - static filename_t calc_filename(const filename_t &filename, const tm &now_tm) - { - filename_t basename, ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); - return fmt::format( - SPDLOG_FILENAME_T("{}_{:04d}{:02d}{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, now_tm.tm_hour, ext); - } -}; - -/* - * Rotating file sink based on time. - * If truncate != false , the created file will be truncated. - * If max_files > 0, retain only the last max_files and delete previous. - */ -template -class hourly_file_sink final : public base_sink -{ -public: - // create hourly file sink which rotates on given time - hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0) - : base_filename_(std::move(base_filename)) - , truncate_(truncate) - , max_files_(max_files) - , filenames_q_() - { - auto now = log_clock::now(); - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); - file_helper_.open(filename, truncate_); - rotation_tp_ = next_rotation_tp_(); - - if (max_files_ > 0) - { - init_filenames_q_(); - } - } - - filename_t filename() - { - std::lock_guard lock(base_sink::mutex_); - return file_helper_.filename(); - } - -protected: - void sink_it_(const details::log_msg &msg) override - { - auto time = msg.time; - bool should_rotate = time >= rotation_tp_; - if (should_rotate) - { - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); - file_helper_.open(filename, truncate_); - rotation_tp_ = next_rotation_tp_(); - } - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - file_helper_.write(formatted); - - // Do the cleaning only at the end because it might throw on failure. - if (should_rotate && max_files_ > 0) - { - delete_old_(); - } - } - - void flush_() override - { - file_helper_.flush(); - } - -private: - void init_filenames_q_() - { - 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 (!path_exists(filename)) - { - break; - } - filenames.emplace_back(filename); - now -= std::chrono::hours(1); - } - 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); - return spdlog::details::os::localtime(tnow); - } - - log_clock::time_point next_rotation_tp_() - { - auto now = log_clock::now(); - tm date = now_tm(now); - date.tm_min = 0; - date.tm_sec = 0; - auto rotation_time = log_clock::from_time_t(std::mktime(&date)); - if (rotation_time > now) - { - return rotation_time; - } - return {rotation_time + std::chrono::hours(1)}; - } - - // Delete the file N rotations ago. - // Throw spdlog_ex on failure to delete the old file. - void delete_old_() - { - using details::os::filename_to_str; - using details::os::remove_if_exists; - - filename_t current_file = file_helper_.filename(); - if (filenames_q_.full()) - { - auto old_filename = std::move(filenames_q_.front()); - filenames_q_.pop_front(); - bool ok = remove_if_exists(old_filename) == 0; - if (!ok) - { - filenames_q_.push_back(std::move(current_file)); - SPDLOG_THROW(spdlog_ex("Failed removing hourly file " + filename_to_str(old_filename), errno)); - } - } - filenames_q_.push_back(std::move(current_file)); - } - - filename_t base_filename_; - log_clock::time_point rotation_tp_; - details::file_helper file_helper_; - bool truncate_; - uint16_t max_files_; - details::circular_q filenames_q_; -}; - -using hourly_file_sink_mt = hourly_file_sink; -using hourly_file_sink_st = hourly_file_sink; - -} // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr hourly_logger_mt( - const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0) -{ - return Factory::template create(logger_name, filename, truncate, max_files); -} - -template -inline std::shared_ptr hourly_logger_st( - const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0) -{ - return Factory::template create(logger_name, filename, truncate, max_files); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h deleted file mode 100644 index 8fec428d..00000000 --- a/include/spdlog/sinks/msvc_sink.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright(c) 2016 Alexander Dalshov. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#if defined(_WIN32) - -#include -#include - -#include -#include - - -// Avoid including windows.h (https://stackoverflow.com/a/30741042) -extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString); - -namespace spdlog { -namespace sinks { -/* - * MSVC sink (logging using OutputDebugStringA) - */ -template -class msvc_sink : public base_sink -{ -public: - msvc_sink() = default; - -protected: - void sink_it_(const details::log_msg &msg) override - { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - OutputDebugStringA(fmt::to_string(formatted).c_str()); - } - - void flush_() override {} -}; - -using msvc_sink_mt = msvc_sink; -using msvc_sink_st = msvc_sink; - -using windebug_sink_mt = msvc_sink_mt; -using windebug_sink_st = msvc_sink_st; - -} // namespace sinks -} // namespace spdlog - -#endif diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h deleted file mode 100644 index eb832801..00000000 --- a/include/spdlog/sinks/null_sink.h +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include - -#include - -namespace spdlog { -namespace sinks { - -template -class null_sink : public base_sink -{ -protected: - void sink_it_(const details::log_msg &) override {} - void flush_() override {} -}; - -using null_sink_mt = null_sink; -using null_sink_st = null_sink; - -} // namespace sinks - -template -inline std::shared_ptr null_logger_mt(const std::string &logger_name) -{ - auto null_logger = Factory::template create(logger_name); - null_logger->set_level(level::off); - return null_logger; -} - -template -inline std::shared_ptr null_logger_st(const std::string &logger_name) -{ - auto null_logger = Factory::template create(logger_name); - null_logger->set_level(level::off); - return null_logger; -} - -} // namespace spdlog diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h deleted file mode 100644 index 95c1e962..00000000 --- a/include/spdlog/sinks/ostream_sink.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -#include -#include - -namespace spdlog { -namespace sinks { -template -class ostream_sink final : public base_sink -{ -public: - explicit ostream_sink(std::ostream &os, bool force_flush = false) - : ostream_(os) - , force_flush_(force_flush) - {} - ostream_sink(const ostream_sink &) = delete; - ostream_sink &operator=(const ostream_sink &) = delete; - -protected: - void sink_it_(const details::log_msg &msg) override - { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - ostream_.write(formatted.data(), static_cast(formatted.size())); - if (force_flush_) - { - ostream_.flush(); - } - } - - void flush_() override - { - ostream_.flush(); - } - - std::ostream &ostream_; - bool force_flush_; -}; - -using ostream_sink_mt = ostream_sink; -using ostream_sink_st = ostream_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h deleted file mode 100644 index 1ee3f691..00000000 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include "spdlog/sinks/base_sink.h" -#include "spdlog/details/circular_q.h" -#include "spdlog/details/log_msg_buffer.h" -#include "spdlog/details/null_mutex.h" - -#include -#include -#include - -namespace spdlog { -namespace sinks { -/* - * Ring buffer sink - */ -template -class ringbuffer_sink final : public base_sink -{ -public: - explicit ringbuffer_sink(size_t n_items) - : q_{n_items} - {} - - std::vector last_raw(size_t lim = 0) - { - std::lock_guard lock(base_sink::mutex_); - auto items_available = q_.size(); - auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; - std::vector ret; - ret.reserve(n_items); - for (size_t i = (items_available - n_items); i < items_available; i++) - { - ret.push_back(q_.at(i)); - } - return ret; - } - - std::vector last_formatted(size_t lim = 0) - { - std::lock_guard lock(base_sink::mutex_); - auto items_available = q_.size(); - auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; - std::vector ret; - ret.reserve(n_items); - for (size_t i = (items_available - n_items); i < items_available; i++) - { - memory_buf_t formatted; - base_sink::formatter_->format(q_.at(i), formatted); - ret.push_back(fmt::to_string(formatted)); - } - return ret; - } - -protected: - void sink_it_(const details::log_msg &msg) override - { - q_.push_back(details::log_msg_buffer{msg}); - } - void flush_() override {} - -private: - details::circular_q q_; -}; - -using ringbuffer_sink_mt = ringbuffer_sink; -using ringbuffer_sink_st = ringbuffer_sink; - -} // namespace sinks - -} // namespace spdlog diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h deleted file mode 100644 index d715ebf3..00000000 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY -#include -#endif - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { - -template -SPDLOG_INLINE rotating_file_sink::rotating_file_sink( - filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open) - : base_filename_(std::move(base_filename)) - , max_size_(max_size) - , max_files_(max_files) -{ - file_helper_.open(calc_filename(base_filename_, 0)); - current_size_ = file_helper_.size(); // expensive. called only once - if (rotate_on_open && current_size_ > 0) - { - rotate_(); - } -} - -// calc filename according to index and file extension if exists. -// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt". -template -SPDLOG_INLINE filename_t rotating_file_sink::calc_filename(const filename_t &filename, std::size_t index) -{ - if (index == 0u) - { - return filename; - } - - filename_t basename, ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); - return fmt::format(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext); -} - -template -SPDLOG_INLINE filename_t rotating_file_sink::filename() -{ - std::lock_guard lock(base_sink::mutex_); - return file_helper_.filename(); -} - -template -SPDLOG_INLINE void rotating_file_sink::sink_it_(const details::log_msg &msg) -{ - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - current_size_ += formatted.size(); - if (current_size_ > max_size_) - { - rotate_(); - current_size_ = formatted.size(); - } - file_helper_.write(formatted); -} - -template -SPDLOG_INLINE void rotating_file_sink::flush_() -{ - file_helper_.flush(); -} - -// Rotate files: -// log.txt -> log.1.txt -// log.1.txt -> log.2.txt -// log.2.txt -> log.3.txt -// log.3.txt -> delete -template -SPDLOG_INLINE void rotating_file_sink::rotate_() -{ - using details::os::filename_to_str; - using details::os::path_exists; - file_helper_.close(); - for (auto i = max_files_; i > 0; --i) - { - filename_t src = calc_filename(base_filename_, i - 1); - if (!path_exists(src)) - { - continue; - } - filename_t target = calc_filename(base_filename_, i); - - 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 - // rates can cause the rename to fail with permission denied (because of antivirus?). - details::os::sleep_for_millis(100); - 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; - throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno); - } - } - } - file_helper_.reopen(true); -} - -// 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) -{ - // try to delete the target file in case it already exists. - (void)details::os::remove(target_filename); - return details::os::rename(src_filename, target_filename) == 0; -} - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h deleted file mode 100644 index e1e85a7d..00000000 --- a/include/spdlog/sinks/rotating_file_sink.h +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#include -#include -#include - -namespace spdlog { -namespace sinks { - -// -// Rotating file sink based on size -// -template -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); - filename_t filename(); - -protected: - void sink_it_(const details::log_msg &msg) override; - void flush_() override; - -private: - // Rotate files: - // log.txt -> log.1.txt - // log.1.txt -> log.2.txt - // log.2.txt -> log.3.txt - // log.3.txt -> delete - void rotate_(); - - // 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); - - filename_t base_filename_; - std::size_t max_size_; - std::size_t max_files_; - std::size_t current_size_; - details::file_helper file_helper_; -}; - -using rotating_file_sink_mt = rotating_file_sink; -using rotating_file_sink_st = rotating_file_sink; - -} // namespace sinks - -// -// factory functions -// - -template -inline std::shared_ptr rotating_logger_mt( - const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false) -{ - return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open); -} - -template -inline std::shared_ptr rotating_logger_st( - const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false) -{ - return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open); -} -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "rotating_file_sink-inl.h" -#endif diff --git a/include/spdlog/sinks/sink-inl.h b/include/spdlog/sinks/sink-inl.h deleted file mode 100644 index a8dd6a6c..00000000 --- a/include/spdlog/sinks/sink-inl.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY -#include -#endif - -#include - -SPDLOG_INLINE bool spdlog::sinks::sink::should_log(spdlog::level::level_enum msg_level) const -{ - return msg_level >= level_.load(std::memory_order_relaxed); -} - -SPDLOG_INLINE void spdlog::sinks::sink::set_level(level::level_enum log_level) -{ - level_.store(log_level, std::memory_order_relaxed); -} - -SPDLOG_INLINE spdlog::level::level_enum spdlog::sinks::sink::level() const -{ - return static_cast(level_.load(std::memory_order_relaxed)); -} diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h deleted file mode 100644 index be99744d..00000000 --- a/include/spdlog/sinks/sink.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -namespace spdlog { - -namespace sinks { -class SPDLOG_API sink -{ -public: - virtual ~sink() = default; - virtual void log(const details::log_msg &msg) = 0; - virtual void flush() = 0; - virtual void set_pattern(const std::string &pattern) = 0; - virtual void set_formatter(std::unique_ptr sink_formatter) = 0; - - void set_level(level::level_enum log_level); - level::level_enum level() const; - bool should_log(level::level_enum msg_level) const; - -protected: - // sink log level - default is all - level_t level_{level::trace}; -}; - -} // namespace sinks -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "sink-inl.h" -#endif diff --git a/include/spdlog/sinks/stdout_color_sinks-inl.h b/include/spdlog/sinks/stdout_color_sinks-inl.h deleted file mode 100644 index 935f1ccc..00000000 --- a/include/spdlog/sinks/stdout_color_sinks-inl.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY -#include -#endif - -#include -#include - -namespace spdlog { - -template -SPDLOG_INLINE std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode) -{ - return Factory::template create(logger_name, mode); -} - -template -SPDLOG_INLINE std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode) -{ - return Factory::template create(logger_name, mode); -} - -template -SPDLOG_INLINE std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode) -{ - return Factory::template create(logger_name, mode); -} - -template -SPDLOG_INLINE std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode) -{ - return Factory::template create(logger_name, mode); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h deleted file mode 100644 index e67aa91b..00000000 --- a/include/spdlog/sinks/stdout_color_sinks.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifdef _WIN32 -#include -#else -#include -#endif - -#include - -namespace spdlog { -namespace sinks { -#ifdef _WIN32 -using stdout_color_sink_mt = wincolor_stdout_sink_mt; -using stdout_color_sink_st = wincolor_stdout_sink_st; -using stderr_color_sink_mt = wincolor_stderr_sink_mt; -using stderr_color_sink_st = wincolor_stderr_sink_st; -#else -using stdout_color_sink_mt = ansicolor_stdout_sink_mt; -using stdout_color_sink_st = ansicolor_stdout_sink_st; -using stderr_color_sink_mt = ansicolor_stderr_sink_mt; -using stderr_color_sink_st = ansicolor_stderr_sink_st; -#endif -} // namespace sinks - -template -std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); - -template -std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); - -template -std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); - -template -std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); - -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "stdout_color_sinks-inl.h" -#endif diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h deleted file mode 100644 index 25192607..00000000 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ /dev/null @@ -1,135 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY -#include -#endif - -#include -#include -#include - -#ifdef _WIN32 -// under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675) -// so instead we use ::FileWrite -#include -#include // WriteFile (..) -#include // _get_osfhandle(..) -#include // _fileno(..) -#endif // WIN32 - -namespace spdlog { - -namespace sinks { - -template -SPDLOG_INLINE stdout_sink_base::stdout_sink_base(FILE *file) - : mutex_(ConsoleMutex::mutex()) - , file_(file) - , formatter_(details::make_unique()) -{ -#ifdef _WIN32 - // get windows handle from the FILE* object - - handle_ = (HANDLE)::_get_osfhandle(::_fileno(file_)); - - // don't throw to support cases where no console is attached, - // and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE). - // throw only if non stdout/stderr target is requested (probably regular file and not console). - if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr) - { - throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno); - } -#endif // WIN32 -} - -template -SPDLOG_INLINE void stdout_sink_base::log(const details::log_msg &msg) -{ -#ifdef _WIN32 - if (handle_ == INVALID_HANDLE_VALUE) - { - return; - } - std::lock_guard lock(mutex_); - memory_buf_t formatted; - formatter_->format(msg, formatted); - ::fflush(file_); // flush in case there is somthing in this file_ already - auto size = static_cast(formatted.size()); - DWORD bytes_written = 0; - bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0; - if (!ok) - { - throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError())); - } -#else - std::lock_guard lock(mutex_); - memory_buf_t formatted; - formatter_->format(msg, formatted); - ::fwrite(formatted.data(), sizeof(char), formatted.size(), file_); - ::fflush(file_); // flush every line to terminal -#endif // WIN32 -} - -template -SPDLOG_INLINE void stdout_sink_base::flush() -{ - std::lock_guard lock(mutex_); - fflush(file_); -} - -template -SPDLOG_INLINE void stdout_sink_base::set_pattern(const std::string &pattern) -{ - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} - -template -SPDLOG_INLINE void stdout_sink_base::set_formatter(std::unique_ptr sink_formatter) -{ - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); -} - -// stdout sink -template -SPDLOG_INLINE stdout_sink::stdout_sink() - : stdout_sink_base(stdout) -{} - -// stderr sink -template -SPDLOG_INLINE stderr_sink::stderr_sink() - : stdout_sink_base(stderr) -{} - -} // namespace sinks - -// factory methods -template -SPDLOG_INLINE std::shared_ptr stdout_logger_mt(const std::string &logger_name) -{ - return Factory::template create(logger_name); -} - -template -SPDLOG_INLINE std::shared_ptr stdout_logger_st(const std::string &logger_name) -{ - return Factory::template create(logger_name); -} - -template -SPDLOG_INLINE std::shared_ptr stderr_logger_mt(const std::string &logger_name) -{ - return Factory::template create(logger_name); -} - -template -SPDLOG_INLINE std::shared_ptr stderr_logger_st(const std::string &logger_name) -{ - return Factory::template create(logger_name); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h deleted file mode 100644 index 40688a46..00000000 --- a/include/spdlog/sinks/stdout_sinks.h +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#ifdef _WIN32 -#include -#endif - -namespace spdlog { - -namespace sinks { - -template -class stdout_sink_base : public sink -{ -public: - using mutex_t = typename ConsoleMutex::mutex_t; - explicit stdout_sink_base(FILE *file); - ~stdout_sink_base() override = default; - - stdout_sink_base(const stdout_sink_base &other) = delete; - stdout_sink_base(stdout_sink_base &&other) = delete; - - stdout_sink_base &operator=(const stdout_sink_base &other) = delete; - stdout_sink_base &operator=(stdout_sink_base &&other) = delete; - - void log(const details::log_msg &msg) override; - void flush() override; - void set_pattern(const std::string &pattern) override; - - void set_formatter(std::unique_ptr sink_formatter) override; - -protected: - mutex_t &mutex_; - FILE *file_; - std::unique_ptr formatter_; -#ifdef _WIN32 - HANDLE handle_; -#endif // WIN32 -}; - -template -class stdout_sink : public stdout_sink_base -{ -public: - stdout_sink(); -}; - -template -class stderr_sink : public stdout_sink_base -{ -public: - stderr_sink(); -}; - -using stdout_sink_mt = stdout_sink; -using stdout_sink_st = stdout_sink; - -using stderr_sink_mt = stderr_sink; -using stderr_sink_st = stderr_sink; - -} // namespace sinks - -// factory methods -template -std::shared_ptr stdout_logger_mt(const std::string &logger_name); - -template -std::shared_ptr stdout_logger_st(const std::string &logger_name); - -template -std::shared_ptr stderr_logger_mt(const std::string &logger_name); - -template -std::shared_ptr stderr_logger_st(const std::string &logger_name); - -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "stdout_sinks-inl.h" -#endif diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h deleted file mode 100644 index 7c38fcb5..00000000 --- a/include/spdlog/sinks/syslog_sink.h +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include - -#include -#include -#include - -namespace spdlog { -namespace sinks { -/** - * Sink that write to syslog using the `syscall()` library call. - */ -template -class syslog_sink : public base_sink -{ - -public: - syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting) - : enable_formatting_{enable_formatting} - , syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, - /* spdlog::level::debug */ LOG_DEBUG, - /* spdlog::level::info */ LOG_INFO, - /* spdlog::level::warn */ LOG_WARNING, - /* spdlog::level::err */ LOG_ERR, - /* spdlog::level::critical */ LOG_CRIT, - /* spdlog::level::off */ LOG_INFO}} - , ident_{std::move(ident)} - { - // set ident to be program name if empty - ::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility); - } - - ~syslog_sink() override - { - ::closelog(); - } - - syslog_sink(const syslog_sink &) = delete; - syslog_sink &operator=(const syslog_sink &) = delete; - -protected: - void sink_it_(const details::log_msg &msg) override - { - string_view_t payload; - memory_buf_t formatted; - if (enable_formatting_) - { - base_sink::formatter_->format(msg, formatted); - payload = string_view_t(formatted.data(), formatted.size()); - } - else - { - payload = msg.payload; - } - - size_t length = payload.size(); - // limit to max int - if (length > static_cast(std::numeric_limits::max())) - { - length = static_cast(std::numeric_limits::max()); - } - - ::syslog(syslog_prio_from_level(msg), "%.*s", static_cast(length), payload.data()); - } - - void flush_() override {} - bool enable_formatting_ = false; - -private: - using levels_array = std::array; - levels_array syslog_levels_; - // must store the ident because the man says openlog might use the pointer as - // is and not a string copy - const std::string ident_; - - // - // Simply maps spdlog's log level to syslog priority level. - // - int syslog_prio_from_level(const details::log_msg &msg) const - { - return syslog_levels_.at(static_cast(msg.level)); - } -}; - -using syslog_sink_mt = syslog_sink; -using syslog_sink_st = syslog_sink; -} // namespace sinks - -// Create and register a syslog logger -template -inline std::shared_ptr syslog_logger_mt(const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, - int syslog_facility = LOG_USER, bool enable_formatting = false) -{ - return Factory::template create(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting); -} - -template -inline std::shared_ptr syslog_logger_st(const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, - int syslog_facility = LOG_USER, bool enable_formatting = false) -{ - return Factory::template create(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h deleted file mode 100644 index d23824b8..00000000 --- a/include/spdlog/sinks/systemd_sink.h +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright(c) 2019 ZVYAGIN.Alexander@gmail.com -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include - -#include -#ifndef SD_JOURNAL_SUPPRESS_LOCATION -#define SD_JOURNAL_SUPPRESS_LOCATION -#endif -#include - -namespace spdlog { -namespace sinks { - -/** - * Sink that write to systemd journal using the `sd_journal_send()` library call. - * - * Locking is not needed, as `sd_journal_send()` itself is thread-safe. - */ -template -class systemd_sink : public base_sink -{ -public: - // - systemd_sink() - : syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, - /* spdlog::level::debug */ LOG_DEBUG, - /* spdlog::level::info */ LOG_INFO, - /* spdlog::level::warn */ LOG_WARNING, - /* spdlog::level::err */ LOG_ERR, - /* spdlog::level::critical */ LOG_CRIT, - /* spdlog::level::off */ LOG_INFO}} - {} - - ~systemd_sink() override {} - - systemd_sink(const systemd_sink &) = delete; - systemd_sink &operator=(const systemd_sink &) = delete; - -protected: - using levels_array = std::array; - levels_array syslog_levels_; - - void sink_it_(const details::log_msg &msg) override - { - int err; - - size_t length = msg.payload.size(); - // limit to max int - if (length > static_cast(std::numeric_limits::max())) - { - length = static_cast(std::numeric_limits::max()); - } - - // Do not send source location if not available - if (msg.source.empty()) - { - // Note: function call inside '()' to avoid macro expansion - err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), - "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), nullptr); - } - else - { - err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), - "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), "CODE_FILE=%s", - msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); - } - - if (err) - { - throw_spdlog_ex("Failed writing to systemd", errno); - } - } - - int syslog_level(level::level_enum l) - { - return syslog_levels_.at(static_cast(l)); - } - - void flush_() override {} -}; - -using systemd_sink_mt = systemd_sink; -using systemd_sink_st = systemd_sink; -} // namespace sinks - -// Create and register a syslog logger -template -inline std::shared_ptr systemd_logger_mt(const std::string &logger_name) -{ - return Factory::template create(logger_name); -} - -template -inline std::shared_ptr systemd_logger_st(const std::string &logger_name) -{ - return Factory::template create(logger_name); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h deleted file mode 100644 index 9dd9e51d..00000000 --- a/include/spdlog/sinks/tcp_sink.h +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#ifdef _WIN32 -#include -#else -#include -#endif - -#include -#include -#include -#include - -#pragma once - -// 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 { - -struct tcp_sink_config -{ - std::string server_host; - int server_port; - bool lazy_connect = false; // if true connect on first log call instead of on 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 - - explicit tcp_sink(tcp_sink_config sink_config) - : config_{std::move(sink_config)} - { - if (!config_.lazy_connect) - { - this->client_.connect(config_.server_host, config_.server_port); - } - } - - ~tcp_sink() override = default; - -protected: - void sink_it_(const spdlog::details::log_msg &msg) override - { - spdlog::memory_buf_t formatted; - spdlog::sinks::base_sink::formatter_->format(msg, formatted); - if (!client_.is_connected()) - { - client_.connect(config_.server_host, config_.server_port); - } - client_.send(formatted.data(), formatted.size()); - } - - void flush_() override {} - tcp_sink_config config_; - details::tcp_client client_; -}; - -using tcp_sink_mt = tcp_sink; -using tcp_sink_st = tcp_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h deleted file mode 100644 index 68170dd9..00000000 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ /dev/null @@ -1,276 +0,0 @@ -// 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. 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 -// 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. - -/*--------------------------------------------------------------------------------------- - -Windows Registry Editor Version 5.00 - -[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\] - -[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,\ - 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 -#include -#include - -namespace spdlog { -namespace sinks { - -namespace win_eventlog { - -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()) - { - std::string system_message; - - LPSTR format_message_result{}; - auto format_message_succeeded = - ::FormatMessageA(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); - } - - explicit 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)) - { - 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 token_handle_ = INVALID_HANDLE_VALUE; - explicit process_token_t(HANDLE process) - { - if (!::OpenProcessToken(process, TOKEN_QUERY, &token_handle_)) - { - SPDLOG_THROW(win32_error("OpenProcessToken")); - } - } - - ~process_token_t() - { - ::CloseHandle(token_handle_); - } - - } 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; - if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize)) - { - SPDLOG_THROW(win32_error("GetTokenInformation should fail")); - } - - // get user token - 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); - } -}; - -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: - return EVENTLOG_INFORMATION_TYPE; - } - } - - 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_; - - HANDLE event_log_handle() - { - if (!hEventLog_) - { - hEventLog_ = ::RegisterEventSourceA(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; - - bool succeeded; - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - formatted.push_back('\0'); - -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - wmemory_buf_t buf; - details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf); - - LPCWSTR lp_wstr = buf.data(); - succeeded = ::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_, - current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr); -#else - LPCSTR lp_str = formatted.data(); - succeeded = ::ReportEventA(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); -#endif - - if (!succeeded) - { - SPDLOG_THROW(win32_error("ReportEvent")); - } - } - - void flush_() override {} - -public: - win_eventlog_sink(std::string const &source, WORD event_id = 1000 /* according to mscoree.dll */) - : source_(source) - , event_id_(event_id) - { - try - { - current_user_sid_ = internal::sid_t::get_current_user_sid(); - } - 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 - } - } - - ~win_eventlog_sink() - { - if (hEventLog_) - DeregisterEventSource(hEventLog_); - } -}; - -} // 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/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h deleted file mode 100644 index e2676d3c..00000000 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ /dev/null @@ -1,175 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY -#include -#endif - -#include -#include - -#include -#include - -namespace spdlog { -namespace sinks { -template -SPDLOG_INLINE wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) - : out_handle_(out_handle) - , mutex_(ConsoleMutex::mutex()) - , formatter_(details::make_unique()) -{ - - set_color_mode_impl(mode); - // set level colors - colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white - colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan - colors_[level::info] = FOREGROUND_GREEN; // green - colors_[level::warn] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow - colors_[level::err] = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red - colors_[level::critical] = - BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; // intense white on red background - colors_[level::off] = 0; -} - -template -SPDLOG_INLINE wincolor_sink::~wincolor_sink() -{ - this->flush(); -} - -// change the color for the given level -template -void SPDLOG_INLINE wincolor_sink::set_color(level::level_enum level, std::uint16_t color) -{ - std::lock_guard lock(mutex_); - colors_[level] = color; -} - -template -void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) -{ - if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) - { - return; - } - - std::lock_guard lock(mutex_); - 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) - { - // before color range - print_range_(formatted, 0, msg.color_range_start); - // in color range - auto orig_attribs = static_cast(set_foreground_color_(colors_[msg.level])); - print_range_(formatted, msg.color_range_start, msg.color_range_end); - // reset to orig colors - ::SetConsoleTextAttribute(static_cast(out_handle_), orig_attribs); - print_range_(formatted, msg.color_range_end, formatted.size()); - } - else // print without colors if color range is invalid (or color is disabled) - { - write_to_file_(formatted); - } -} - -template -void SPDLOG_INLINE wincolor_sink::flush() -{ - // windows console always flushed? -} - -template -void SPDLOG_INLINE wincolor_sink::set_pattern(const std::string &pattern) -{ - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} - -template -void SPDLOG_INLINE wincolor_sink::set_formatter(std::unique_ptr sink_formatter) -{ - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); -} - -template -void SPDLOG_INLINE wincolor_sink::set_color_mode(color_mode mode) -{ - std::lock_guard lock(mutex_); - set_color_mode_impl(mode); -} - -template -void SPDLOG_INLINE wincolor_sink::set_color_mode_impl(color_mode mode) -{ - if (mode == color_mode::automatic) - { - // should do colors only if out_handle_ points to actual console. - DWORD console_mode; - bool in_console = ::GetConsoleMode(static_cast(out_handle_), &console_mode) != 0; - should_do_colors_ = in_console; - } - else - { - should_do_colors_ = mode == color_mode::always ? true : false; - } -} - -// set foreground color and return the orig console attributes (for resetting later) -template -std::uint16_t SPDLOG_INLINE wincolor_sink::set_foreground_color_(std::uint16_t attribs) -{ - CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; - if (!::GetConsoleScreenBufferInfo(static_cast(out_handle_), &orig_buffer_info)) - { - // just return white if failed getting console info - return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; - } - - // change only the foreground bits (lowest 4 bits) - auto new_attribs = static_cast(attribs) | (orig_buffer_info.wAttributes & 0xfff0); - auto ignored = ::SetConsoleTextAttribute(static_cast(out_handle_), static_cast(new_attribs)); - (void)(ignored); - return static_cast(orig_buffer_info.wAttributes); // return orig attribs -} - -// print a range of formatted message to console -template -void SPDLOG_INLINE wincolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) -{ - if (end > start) - { - auto size = static_cast(end - start); - auto ignored = ::WriteConsoleA(static_cast(out_handle_), formatted.data() + start, size, nullptr, nullptr); - (void)(ignored); - } -} - -template -void SPDLOG_INLINE wincolor_sink::write_to_file_(const memory_buf_t &formatted) -{ - auto size = static_cast(formatted.size()); - DWORD bytes_written = 0; - auto ignored = ::WriteFile(static_cast(out_handle_), formatted.data(), size, &bytes_written, nullptr); - (void)(ignored); -} - -// wincolor_stdout_sink -template -SPDLOG_INLINE wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) -{} - -// wincolor_stderr_sink -template -SPDLOG_INLINE wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) -{} -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h deleted file mode 100644 index a82f1b24..00000000 --- a/include/spdlog/sinks/wincolor_sink.h +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { -/* - * Windows color console sink. Uses WriteConsoleA to write to the console with - * colors - */ -template -class wincolor_sink : public sink -{ -public: - wincolor_sink(void *out_handle, color_mode mode); - ~wincolor_sink() override; - - wincolor_sink(const wincolor_sink &other) = delete; - wincolor_sink &operator=(const wincolor_sink &other) = delete; - - // change the color for the given level - void set_color(level::level_enum level, std::uint16_t color); - void log(const details::log_msg &msg) final override; - void flush() final override; - void set_pattern(const std::string &pattern) override final; - void set_formatter(std::unique_ptr sink_formatter) override final; - void set_color_mode(color_mode mode); - -protected: - using mutex_t = typename ConsoleMutex::mutex_t; - void *out_handle_; - mutex_t &mutex_; - bool should_do_colors_; - std::unique_ptr formatter_; - std::array colors_; - - // set foreground color and return the orig console attributes (for resetting later) - std::uint16_t set_foreground_color_(std::uint16_t attribs); - - // print a range of formatted message to console - void print_range_(const memory_buf_t &formatted, size_t start, size_t end); - - // in case we are redirected to file (not in console mode) - void write_to_file_(const memory_buf_t &formatted); - - void set_color_mode_impl(color_mode mode); -}; - -template -class wincolor_stdout_sink : public wincolor_sink -{ -public: - explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic); -}; - -template -class wincolor_stderr_sink : public wincolor_sink -{ -public: - explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic); -}; - -using wincolor_stdout_sink_mt = wincolor_stdout_sink; -using wincolor_stdout_sink_st = wincolor_stdout_sink; - -using wincolor_stderr_sink_mt = wincolor_stderr_sink; -using wincolor_stderr_sink_st = wincolor_stderr_sink; -} // namespace sinks -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY -#include "wincolor_sink-inl.h" -#endif -- cgit v1.2.3