diff options
author | BobTheBob <32057864+BobTheBob9@users.noreply.github.com> | 2022-08-24 00:32:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-24 00:32:31 +0100 |
commit | f9bc3c9d1834cb8bd5f872b749b057c33e8b0102 (patch) | |
tree | e96e2da0d95798dd42eddf644a82a74555db858f /NorthstarDLL/include/spdlog/sinks | |
parent | 812893d8219daa60f5b5b7fd22cbd6b175603399 (diff) | |
download | NorthstarLauncher-f9bc3c9d1834cb8bd5f872b749b057c33e8b0102.tar.gz NorthstarLauncher-f9bc3c9d1834cb8bd5f872b749b057c33e8b0102.zip |
Adjust folder structure (#242)
* Adjust folder structure
* change launcher directory name
Diffstat (limited to 'NorthstarDLL/include/spdlog/sinks')
29 files changed, 2837 insertions, 0 deletions
diff --git a/NorthstarDLL/include/spdlog/sinks/android_sink.h b/NorthstarDLL/include/spdlog/sinks/android_sink.h new file mode 100644 index 00000000..9686adff --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/android_sink.h @@ -0,0 +1,119 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifdef __ANDROID__ + +#include <spdlog/details/fmt_helper.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/details/os.h> +#include <spdlog/sinks/base_sink.h> +#include <spdlog/details/synchronous_factory.h> + +#include <android/log.h> +#include <chrono> +#include <mutex> +#include <string> +#include <thread> + +#if !defined(SPDLOG_ANDROID_RETRIES) +#define SPDLOG_ANDROID_RETRIES 2 +#endif + +namespace spdlog { +namespace sinks { + +/* + * Android sink (logging using __android_log_write) + */ +template<typename Mutex> +class android_sink final : public base_sink<Mutex> +{ +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<Mutex>::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<std::mutex>; +using android_sink_st = android_sink<details::null_mutex>; +} // namespace sinks + +// Create and register android syslog logger + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog") +{ + return Factory::template create<sinks::android_sink_mt>(logger_name, tag); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog") +{ + return Factory::template create<sinks::android_sink_st>(logger_name, tag); +} + +} // namespace spdlog + +#endif // __ANDROID__
\ No newline at end of file diff --git a/NorthstarDLL/include/spdlog/sinks/ansicolor_sink-inl.h b/NorthstarDLL/include/spdlog/sinks/ansicolor_sink-inl.h new file mode 100644 index 00000000..61c0a719 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/ansicolor_sink-inl.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 + +#ifndef SPDLOG_HEADER_ONLY +#include <spdlog/sinks/ansicolor_sink.h> +#endif + +#include <spdlog/pattern_formatter.h> +#include <spdlog/details/os.h> + +namespace spdlog { +namespace sinks { + +template<typename ConsoleMutex> +SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, color_mode mode) + : target_file_(target_file) + , mutex_(ConsoleMutex::mutex()) + , formatter_(details::make_unique<spdlog::pattern_formatter>()) + +{ + 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<typename ConsoleMutex> +SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, string_view_t color) +{ + std::lock_guard<mutex_t> lock(mutex_); + colors_[color_level] = to_string_(color); +} + +template<typename ConsoleMutex> +SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::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<mutex_t> 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<typename ConsoleMutex> +SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::flush() +{ + std::lock_guard<mutex_t> lock(mutex_); + fflush(target_file_); +} + +template<typename ConsoleMutex> +SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) +{ + std::lock_guard<mutex_t> lock(mutex_); + formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern)); +} + +template<typename ConsoleMutex> +SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) +{ + std::lock_guard<mutex_t> lock(mutex_); + formatter_ = std::move(sink_formatter); +} + +template<typename ConsoleMutex> +SPDLOG_INLINE bool ansicolor_sink<ConsoleMutex>::should_color() +{ + return should_do_colors_; +} + +template<typename ConsoleMutex> +SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::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<typename ConsoleMutex> +SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code) +{ + fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); +} + +template<typename ConsoleMutex> +SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) +{ + fwrite(formatted.data() + start, sizeof(char), end - start, target_file_); +} + +template<typename ConsoleMutex> +SPDLOG_INLINE std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv) +{ + return std::string(sv.data(), sv.size()); +} + +// ansicolor_stdout_sink +template<typename ConsoleMutex> +SPDLOG_INLINE ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode) + : ansicolor_sink<ConsoleMutex>(stdout, mode) +{} + +// ansicolor_stderr_sink +template<typename ConsoleMutex> +SPDLOG_INLINE ansicolor_stderr_sink<ConsoleMutex>::ansicolor_stderr_sink(color_mode mode) + : ansicolor_sink<ConsoleMutex>(stderr, mode) +{} + +} // namespace sinks +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/ansicolor_sink.h b/NorthstarDLL/include/spdlog/sinks/ansicolor_sink.h new file mode 100644 index 00000000..4438f706 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/ansicolor_sink.h @@ -0,0 +1,118 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/details/console_globals.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/sinks/sink.h> +#include <memory> +#include <mutex> +#include <string> +#include <array> + +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<typename ConsoleMutex> +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<spdlog::formatter> 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<spdlog::formatter> formatter_; + std::array<std::string, level::n_levels> 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<typename ConsoleMutex> +class ansicolor_stdout_sink : public ansicolor_sink<ConsoleMutex> +{ +public: + explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic); +}; + +template<typename ConsoleMutex> +class ansicolor_stderr_sink : public ansicolor_sink<ConsoleMutex> +{ +public: + explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic); +}; + +using ansicolor_stdout_sink_mt = ansicolor_stdout_sink<details::console_mutex>; +using ansicolor_stdout_sink_st = ansicolor_stdout_sink<details::console_nullmutex>; + +using ansicolor_stderr_sink_mt = ansicolor_stderr_sink<details::console_mutex>; +using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::console_nullmutex>; + +} // namespace sinks +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY +#include "ansicolor_sink-inl.h" +#endif diff --git a/NorthstarDLL/include/spdlog/sinks/base_sink-inl.h b/NorthstarDLL/include/spdlog/sinks/base_sink-inl.h new file mode 100644 index 00000000..b15fb0e6 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/base_sink-inl.h @@ -0,0 +1,63 @@ +// 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 <spdlog/sinks/base_sink.h> +#endif + +#include <spdlog/common.h> +#include <spdlog/pattern_formatter.h> + +#include <memory> + +template<typename Mutex> +SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::base_sink() + : formatter_{details::make_unique<spdlog::pattern_formatter>()} +{} + +template<typename Mutex> +SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::base_sink(std::unique_ptr<spdlog::formatter> formatter) + : formatter_{std::move(formatter)} +{} + +template<typename Mutex> +void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::log(const details::log_msg &msg) +{ + std::lock_guard<Mutex> lock(mutex_); + sink_it_(msg); +} + +template<typename Mutex> +void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::flush() +{ + std::lock_guard<Mutex> lock(mutex_); + flush_(); +} + +template<typename Mutex> +void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern(const std::string &pattern) +{ + std::lock_guard<Mutex> lock(mutex_); + set_pattern_(pattern); +} + +template<typename Mutex> +void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) +{ + std::lock_guard<Mutex> lock(mutex_); + set_formatter_(std::move(sink_formatter)); +} + +template<typename Mutex> +void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern_(const std::string &pattern) +{ + set_formatter_(details::make_unique<spdlog::pattern_formatter>(pattern)); +} + +template<typename Mutex> +void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) +{ + formatter_ = std::move(sink_formatter); +} diff --git a/NorthstarDLL/include/spdlog/sinks/base_sink.h b/NorthstarDLL/include/spdlog/sinks/base_sink.h new file mode 100644 index 00000000..bf5072f8 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/base_sink.h @@ -0,0 +1,52 @@ +// 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 <spdlog/common.h> +#include <spdlog/details/log_msg.h> +#include <spdlog/sinks/sink.h> + +namespace spdlog { +namespace sinks { +template<typename Mutex> +class base_sink : public sink +{ +public: + base_sink(); + explicit base_sink(std::unique_ptr<spdlog::formatter> 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<spdlog::formatter> sink_formatter) final; + +protected: + // sink formatter + std::unique_ptr<spdlog::formatter> 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<spdlog::formatter> sink_formatter); +}; +} // namespace sinks +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY +#include "base_sink-inl.h" +#endif diff --git a/NorthstarDLL/include/spdlog/sinks/basic_file_sink-inl.h b/NorthstarDLL/include/spdlog/sinks/basic_file_sink-inl.h new file mode 100644 index 00000000..1260d15c --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/basic_file_sink-inl.h @@ -0,0 +1,43 @@ +// 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 <spdlog/sinks/basic_file_sink.h> +#endif + +#include <spdlog/common.h> +#include <spdlog/details/os.h> + +namespace spdlog { +namespace sinks { + +template<typename Mutex> +SPDLOG_INLINE basic_file_sink<Mutex>::basic_file_sink(const filename_t &filename, bool truncate) +{ + file_helper_.open(filename, truncate); +} + +template<typename Mutex> +SPDLOG_INLINE const filename_t &basic_file_sink<Mutex>::filename() const +{ + return file_helper_.filename(); +} + +template<typename Mutex> +SPDLOG_INLINE void basic_file_sink<Mutex>::sink_it_(const details::log_msg &msg) +{ + memory_buf_t formatted; + base_sink<Mutex>::formatter_->format(msg, formatted); + file_helper_.write(formatted); +} + +template<typename Mutex> +SPDLOG_INLINE void basic_file_sink<Mutex>::flush_() +{ + file_helper_.flush(); +} + +} // namespace sinks +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/basic_file_sink.h b/NorthstarDLL/include/spdlog/sinks/basic_file_sink.h new file mode 100644 index 00000000..0ab9a4a1 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/basic_file_sink.h @@ -0,0 +1,58 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/details/file_helper.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/sinks/base_sink.h> +#include <spdlog/details/synchronous_factory.h> + +#include <mutex> +#include <string> + +namespace spdlog { +namespace sinks { +/* + * Trivial file sink with single file as target + */ +template<typename Mutex> +class basic_file_sink final : public base_sink<Mutex> +{ +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<std::mutex>; +using basic_file_sink_st = basic_file_sink<details::null_mutex>; + +} // namespace sinks + +// +// factory functions +// +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false) +{ + return Factory::template create<sinks::basic_file_sink_mt>(logger_name, filename, truncate); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false) +{ + return Factory::template create<sinks::basic_file_sink_st>(logger_name, filename, truncate); +} + +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY +#include "basic_file_sink-inl.h" +#endif
\ No newline at end of file diff --git a/NorthstarDLL/include/spdlog/sinks/daily_file_sink.h b/NorthstarDLL/include/spdlog/sinks/daily_file_sink.h new file mode 100644 index 00000000..16be821a --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/daily_file_sink.h @@ -0,0 +1,238 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/common.h> +#include <spdlog/details/file_helper.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/fmt/fmt.h> +#include <spdlog/fmt/chrono.h> +#include <spdlog/sinks/base_sink.h> +#include <spdlog/details/os.h> +#include <spdlog/details/circular_q.h> +#include <spdlog/details/synchronous_factory.h> + +#include <chrono> +#include <cstdio> +#include <ctime> +#include <mutex> +#include <string> + +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<spdlog::sinks::daily_file_format_sink_mt>("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<typename Mutex, typename FileNameCalc = daily_filename_calculator> +class daily_file_sink final : public base_sink<Mutex> +{ +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<Mutex> lock(base_sink<Mutex>::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<Mutex>::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<filename_t>(static_cast<size_t>(max_files_)); + std::vector<filename_t> 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<filename_t> filenames_q_; +}; + +using daily_file_sink_mt = daily_file_sink<std::mutex>; +using daily_file_sink_st = daily_file_sink<details::null_mutex>; +using daily_file_format_sink_mt = daily_file_sink<std::mutex, daily_filename_format_calculator>; +using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_filename_format_calculator>; + +} // namespace sinks + +// +// factory functions +// +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> 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<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> 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<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate, max_files); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> 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<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> 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<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate, max_files); +} +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/dist_sink.h b/NorthstarDLL/include/spdlog/sinks/dist_sink.h new file mode 100644 index 00000000..8fccb4ee --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/dist_sink.h @@ -0,0 +1,97 @@ +// 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 <spdlog/details/log_msg.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/pattern_formatter.h> + +#include <algorithm> +#include <memory> +#include <mutex> +#include <vector> + +// Distribution sink (mux). Stores a vector of sinks which get called when log +// is called + +namespace spdlog { +namespace sinks { + +template<typename Mutex> +class dist_sink : public base_sink<Mutex> +{ +public: + dist_sink() = default; + explicit dist_sink(std::vector<std::shared_ptr<sink>> sinks) + : sinks_(sinks) + {} + + dist_sink(const dist_sink &) = delete; + dist_sink &operator=(const dist_sink &) = delete; + + void add_sink(std::shared_ptr<sink> sink) + { + std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); + sinks_.push_back(sink); + } + + void remove_sink(std::shared_ptr<sink> sink) + { + std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); + sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); + } + + void set_sinks(std::vector<std::shared_ptr<sink>> sinks) + { + std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); + sinks_ = std::move(sinks); + } + + std::vector<std::shared_ptr<sink>> &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<spdlog::pattern_formatter>(pattern)); + } + + void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override + { + base_sink<Mutex>::formatter_ = std::move(sink_formatter); + for (auto &sink : sinks_) + { + sink->set_formatter(base_sink<Mutex>::formatter_->clone()); + } + } + std::vector<std::shared_ptr<sink>> sinks_; +}; + +using dist_sink_mt = dist_sink<std::mutex>; +using dist_sink_st = dist_sink<details::null_mutex>; + +} // namespace sinks +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/dup_filter_sink.h b/NorthstarDLL/include/spdlog/sinks/dup_filter_sink.h new file mode 100644 index 00000000..c9a08d68 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/dup_filter_sink.h @@ -0,0 +1,90 @@ +// 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 <spdlog/details/null_mutex.h> +#include <spdlog/details/log_msg.h> + +#include <mutex> +#include <string> +#include <chrono> + +// Duplicate message removal sink. +// Skip the message if previous one is identical and less than "max_skip_duration" have passed +// +// Example: +// +// #include <spdlog/sinks/dup_filter_sink.h> +// +// int main() { +// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5)); +// dup_filter->add_sink(std::make_shared<stdout_color_sink_mt>()); +// 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<typename Mutex> +class dup_filter_sink : public dist_sink<Mutex> +{ +public: + template<class Rep, class Period> + explicit dup_filter_sink(std::chrono::duration<Rep, Period> 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<Mutex>::sink_it_(skipped_msg); + } + + // log current message + dist_sink<Mutex>::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<std::mutex>; +using dup_filter_sink_st = dup_filter_sink<details::null_mutex>; + +} // namespace sinks +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/hourly_file_sink.h b/NorthstarDLL/include/spdlog/sinks/hourly_file_sink.h new file mode 100644 index 00000000..f5e34366 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/hourly_file_sink.h @@ -0,0 +1,194 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/common.h> +#include <spdlog/details/file_helper.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/fmt/fmt.h> +#include <spdlog/sinks/base_sink.h> +#include <spdlog/details/os.h> +#include <spdlog/details/circular_q.h> +#include <spdlog/details/synchronous_factory.h> + +#include <chrono> +#include <cstdio> +#include <ctime> +#include <mutex> +#include <string> + +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<typename Mutex, typename FileNameCalc = hourly_filename_calculator> +class hourly_file_sink final : public base_sink<Mutex> +{ +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<Mutex> lock(base_sink<Mutex>::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<Mutex>::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<filename_t>(static_cast<size_t>(max_files_)); + std::vector<filename_t> 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<filename_t> filenames_q_; +}; + +using hourly_file_sink_mt = hourly_file_sink<std::mutex>; +using hourly_file_sink_st = hourly_file_sink<details::null_mutex>; + +} // namespace sinks + +// +// factory functions +// +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> hourly_logger_mt( + const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0) +{ + return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate, max_files); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> hourly_logger_st( + const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0) +{ + return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate, max_files); +} +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/msvc_sink.h b/NorthstarDLL/include/spdlog/sinks/msvc_sink.h new file mode 100644 index 00000000..8fec428d --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/msvc_sink.h @@ -0,0 +1,49 @@ +// Copyright(c) 2016 Alexander Dalshov. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#if defined(_WIN32) + +#include <spdlog/details/null_mutex.h> +#include <spdlog/sinks/base_sink.h> + +#include <mutex> +#include <string> + + +// 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<typename Mutex> +class msvc_sink : public base_sink<Mutex> +{ +public: + msvc_sink() = default; + +protected: + void sink_it_(const details::log_msg &msg) override + { + memory_buf_t formatted; + base_sink<Mutex>::formatter_->format(msg, formatted); + OutputDebugStringA(fmt::to_string(formatted).c_str()); + } + + void flush_() override {} +}; + +using msvc_sink_mt = msvc_sink<std::mutex>; +using msvc_sink_st = msvc_sink<details::null_mutex>; + +using windebug_sink_mt = msvc_sink_mt; +using windebug_sink_st = msvc_sink_st; + +} // namespace sinks +} // namespace spdlog + +#endif diff --git a/NorthstarDLL/include/spdlog/sinks/null_sink.h b/NorthstarDLL/include/spdlog/sinks/null_sink.h new file mode 100644 index 00000000..eb832801 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/null_sink.h @@ -0,0 +1,44 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/details/null_mutex.h> +#include <spdlog/sinks/base_sink.h> +#include <spdlog/details/synchronous_factory.h> + +#include <mutex> + +namespace spdlog { +namespace sinks { + +template<typename Mutex> +class null_sink : public base_sink<Mutex> +{ +protected: + void sink_it_(const details::log_msg &) override {} + void flush_() override {} +}; + +using null_sink_mt = null_sink<details::null_mutex>; +using null_sink_st = null_sink<details::null_mutex>; + +} // namespace sinks + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> null_logger_mt(const std::string &logger_name) +{ + auto null_logger = Factory::template create<sinks::null_sink_mt>(logger_name); + null_logger->set_level(level::off); + return null_logger; +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> null_logger_st(const std::string &logger_name) +{ + auto null_logger = Factory::template create<sinks::null_sink_st>(logger_name); + null_logger->set_level(level::off); + return null_logger; +} + +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/ostream_sink.h b/NorthstarDLL/include/spdlog/sinks/ostream_sink.h new file mode 100644 index 00000000..95c1e962 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/ostream_sink.h @@ -0,0 +1,50 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/details/null_mutex.h> +#include <spdlog/sinks/base_sink.h> + +#include <mutex> +#include <ostream> + +namespace spdlog { +namespace sinks { +template<typename Mutex> +class ostream_sink final : public base_sink<Mutex> +{ +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<Mutex>::formatter_->format(msg, formatted); + ostream_.write(formatted.data(), static_cast<std::streamsize>(formatted.size())); + if (force_flush_) + { + ostream_.flush(); + } + } + + void flush_() override + { + ostream_.flush(); + } + + std::ostream &ostream_; + bool force_flush_; +}; + +using ostream_sink_mt = ostream_sink<std::mutex>; +using ostream_sink_st = ostream_sink<details::null_mutex>; + +} // namespace sinks +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/ringbuffer_sink.h b/NorthstarDLL/include/spdlog/sinks/ringbuffer_sink.h new file mode 100644 index 00000000..1ee3f691 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/ringbuffer_sink.h @@ -0,0 +1,74 @@ +// 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 <mutex> +#include <string> +#include <vector> + +namespace spdlog { +namespace sinks { +/* + * Ring buffer sink + */ +template<typename Mutex> +class ringbuffer_sink final : public base_sink<Mutex> +{ +public: + explicit ringbuffer_sink(size_t n_items) + : q_{n_items} + {} + + std::vector<details::log_msg_buffer> last_raw(size_t lim = 0) + { + std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); + auto items_available = q_.size(); + auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; + std::vector<details::log_msg_buffer> 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<std::string> last_formatted(size_t lim = 0) + { + std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); + auto items_available = q_.size(); + auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; + std::vector<std::string> ret; + ret.reserve(n_items); + for (size_t i = (items_available - n_items); i < items_available; i++) + { + memory_buf_t formatted; + base_sink<Mutex>::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<details::log_msg_buffer> q_; +}; + +using ringbuffer_sink_mt = ringbuffer_sink<std::mutex>; +using ringbuffer_sink_st = ringbuffer_sink<details::null_mutex>; + +} // namespace sinks + +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/rotating_file_sink-inl.h b/NorthstarDLL/include/spdlog/sinks/rotating_file_sink-inl.h new file mode 100644 index 00000000..d715ebf3 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/rotating_file_sink-inl.h @@ -0,0 +1,131 @@ +// 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 <spdlog/sinks/rotating_file_sink.h> +#endif + +#include <spdlog/common.h> + +#include <spdlog/details/file_helper.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/fmt/fmt.h> + +#include <cerrno> +#include <chrono> +#include <ctime> +#include <mutex> +#include <string> +#include <tuple> + +namespace spdlog { +namespace sinks { + +template<typename Mutex> +SPDLOG_INLINE rotating_file_sink<Mutex>::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<typename Mutex> +SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::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<typename Mutex> +SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::filename() +{ + std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); + return file_helper_.filename(); +} + +template<typename Mutex> +SPDLOG_INLINE void rotating_file_sink<Mutex>::sink_it_(const details::log_msg &msg) +{ + memory_buf_t formatted; + base_sink<Mutex>::formatter_->format(msg, formatted); + current_size_ += formatted.size(); + if (current_size_ > max_size_) + { + rotate_(); + current_size_ = formatted.size(); + } + file_helper_.write(formatted); +} + +template<typename Mutex> +SPDLOG_INLINE void rotating_file_sink<Mutex>::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<typename Mutex> +SPDLOG_INLINE void rotating_file_sink<Mutex>::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<typename Mutex> +SPDLOG_INLINE bool rotating_file_sink<Mutex>::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/NorthstarDLL/include/spdlog/sinks/rotating_file_sink.h b/NorthstarDLL/include/spdlog/sinks/rotating_file_sink.h new file mode 100644 index 00000000..e1e85a7d --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/rotating_file_sink.h @@ -0,0 +1,78 @@ +// 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/file_helper.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/details/synchronous_factory.h> + +#include <chrono> +#include <mutex> +#include <string> + +namespace spdlog { +namespace sinks { + +// +// Rotating file sink based on size +// +template<typename Mutex> +class rotating_file_sink final : public base_sink<Mutex> +{ +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<std::mutex>; +using rotating_file_sink_st = rotating_file_sink<details::null_mutex>; + +} // namespace sinks + +// +// factory functions +// + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> 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<sinks::rotating_file_sink_mt>(logger_name, filename, max_file_size, max_files, rotate_on_open); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> 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<sinks::rotating_file_sink_st>(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/NorthstarDLL/include/spdlog/sinks/sink-inl.h b/NorthstarDLL/include/spdlog/sinks/sink-inl.h new file mode 100644 index 00000000..a8dd6a6c --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/sink-inl.h @@ -0,0 +1,25 @@ +// 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 <spdlog/sinks/sink.h> +#endif + +#include <spdlog/common.h> + +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<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed)); +} diff --git a/NorthstarDLL/include/spdlog/sinks/sink.h b/NorthstarDLL/include/spdlog/sinks/sink.h new file mode 100644 index 00000000..be99744d --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/sink.h @@ -0,0 +1,35 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/details/log_msg.h> +#include <spdlog/formatter.h> + +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<spdlog::formatter> 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/NorthstarDLL/include/spdlog/sinks/stdout_color_sinks-inl.h b/NorthstarDLL/include/spdlog/sinks/stdout_color_sinks-inl.h new file mode 100644 index 00000000..653aca80 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/stdout_color_sinks-inl.h @@ -0,0 +1,38 @@ +// 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 <spdlog/sinks/stdout_color_sinks.h> +#endif + +#include <spdlog/logger.h> +#include <spdlog/common.h> + +namespace spdlog { + +template<typename Factory> +SPDLOG_INLINE std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode) +{ + return Factory::template create<sinks::stdout_color_sink_mt>(logger_name, mode); +} + +template<typename Factory> +SPDLOG_INLINE std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode) +{ + return Factory::template create<sinks::stdout_color_sink_st>(logger_name, mode); +} + +template<typename Factory> +SPDLOG_INLINE std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode) +{ + return Factory::template create<sinks::stderr_color_sink_mt>(logger_name, mode); +} + +template<typename Factory> +SPDLOG_INLINE std::shared_ptr<logger> stderr_color_st(const std::string &logger_name, color_mode mode) +{ + return Factory::template create<sinks::stderr_color_sink_st>(logger_name, mode); +} +} // namespace spdlog
\ No newline at end of file diff --git a/NorthstarDLL/include/spdlog/sinks/stdout_color_sinks.h b/NorthstarDLL/include/spdlog/sinks/stdout_color_sinks.h new file mode 100644 index 00000000..e67aa91b --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/stdout_color_sinks.h @@ -0,0 +1,45 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifdef _WIN32 +#include <spdlog/sinks/wincolor_sink.h> +#else +#include <spdlog/sinks/ansicolor_sink.h> +#endif + +#include <spdlog/details/synchronous_factory.h> + +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<typename Factory = spdlog::synchronous_factory> +std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); + +template<typename Factory = spdlog::synchronous_factory> +std::shared_ptr<logger> stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); + +template<typename Factory = spdlog::synchronous_factory> +std::shared_ptr<logger> stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); + +template<typename Factory = spdlog::synchronous_factory> +std::shared_ptr<logger> 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/NorthstarDLL/include/spdlog/sinks/stdout_sinks-inl.h b/NorthstarDLL/include/spdlog/sinks/stdout_sinks-inl.h new file mode 100644 index 00000000..25192607 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/stdout_sinks-inl.h @@ -0,0 +1,135 @@ +// 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 <spdlog/sinks/stdout_sinks.h> +#endif + +#include <spdlog/details/console_globals.h> +#include <spdlog/pattern_formatter.h> +#include <memory> + +#ifdef _WIN32 +// under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675) +// so instead we use ::FileWrite +#include <spdlog/details/windows_include.h> +#include <fileapi.h> // WriteFile (..) +#include <io.h> // _get_osfhandle(..) +#include <stdio.h> // _fileno(..) +#endif // WIN32 + +namespace spdlog { + +namespace sinks { + +template<typename ConsoleMutex> +SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file) + : mutex_(ConsoleMutex::mutex()) + , file_(file) + , formatter_(details::make_unique<spdlog::pattern_formatter>()) +{ +#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<typename ConsoleMutex> +SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg) +{ +#ifdef _WIN32 + if (handle_ == INVALID_HANDLE_VALUE) + { + return; + } + std::lock_guard<mutex_t> 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<DWORD>(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<mutex_t> 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<typename ConsoleMutex> +SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::flush() +{ + std::lock_guard<mutex_t> lock(mutex_); + fflush(file_); +} + +template<typename ConsoleMutex> +SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern) +{ + std::lock_guard<mutex_t> lock(mutex_); + formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern)); +} + +template<typename ConsoleMutex> +SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) +{ + std::lock_guard<mutex_t> lock(mutex_); + formatter_ = std::move(sink_formatter); +} + +// stdout sink +template<typename ConsoleMutex> +SPDLOG_INLINE stdout_sink<ConsoleMutex>::stdout_sink() + : stdout_sink_base<ConsoleMutex>(stdout) +{} + +// stderr sink +template<typename ConsoleMutex> +SPDLOG_INLINE stderr_sink<ConsoleMutex>::stderr_sink() + : stdout_sink_base<ConsoleMutex>(stderr) +{} + +} // namespace sinks + +// factory methods +template<typename Factory> +SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name) +{ + return Factory::template create<sinks::stdout_sink_mt>(logger_name); +} + +template<typename Factory> +SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name) +{ + return Factory::template create<sinks::stdout_sink_st>(logger_name); +} + +template<typename Factory> +SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name) +{ + return Factory::template create<sinks::stderr_sink_mt>(logger_name); +} + +template<typename Factory> +SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) +{ + return Factory::template create<sinks::stderr_sink_st>(logger_name); +} +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/stdout_sinks.h b/NorthstarDLL/include/spdlog/sinks/stdout_sinks.h new file mode 100644 index 00000000..40688a46 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/stdout_sinks.h @@ -0,0 +1,87 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/details/console_globals.h> +#include <spdlog/details/synchronous_factory.h> +#include <spdlog/sinks/sink.h> +#include <cstdio> + +#ifdef _WIN32 +#include <spdlog/details/windows_include.h> +#endif + +namespace spdlog { + +namespace sinks { + +template<typename ConsoleMutex> +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<spdlog::formatter> sink_formatter) override; + +protected: + mutex_t &mutex_; + FILE *file_; + std::unique_ptr<spdlog::formatter> formatter_; +#ifdef _WIN32 + HANDLE handle_; +#endif // WIN32 +}; + +template<typename ConsoleMutex> +class stdout_sink : public stdout_sink_base<ConsoleMutex> +{ +public: + stdout_sink(); +}; + +template<typename ConsoleMutex> +class stderr_sink : public stdout_sink_base<ConsoleMutex> +{ +public: + stderr_sink(); +}; + +using stdout_sink_mt = stdout_sink<details::console_mutex>; +using stdout_sink_st = stdout_sink<details::console_nullmutex>; + +using stderr_sink_mt = stderr_sink<details::console_mutex>; +using stderr_sink_st = stderr_sink<details::console_nullmutex>; + +} // namespace sinks + +// factory methods +template<typename Factory = spdlog::synchronous_factory> +std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name); + +template<typename Factory = spdlog::synchronous_factory> +std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name); + +template<typename Factory = spdlog::synchronous_factory> +std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name); + +template<typename Factory = spdlog::synchronous_factory> +std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name); + +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY +#include "stdout_sinks-inl.h" +#endif diff --git a/NorthstarDLL/include/spdlog/sinks/syslog_sink.h b/NorthstarDLL/include/spdlog/sinks/syslog_sink.h new file mode 100644 index 00000000..7c38fcb5 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/syslog_sink.h @@ -0,0 +1,109 @@ +// 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/null_mutex.h> +#include <spdlog/details/synchronous_factory.h> + +#include <array> +#include <string> +#include <syslog.h> + +namespace spdlog { +namespace sinks { +/** + * Sink that write to syslog using the `syscall()` library call. + */ +template<typename Mutex> +class syslog_sink : public base_sink<Mutex> +{ + +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<Mutex>::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<size_t>(std::numeric_limits<int>::max())) + { + length = static_cast<size_t>(std::numeric_limits<int>::max()); + } + + ::syslog(syslog_prio_from_level(msg), "%.*s", static_cast<int>(length), payload.data()); + } + + void flush_() override {} + bool enable_formatting_ = false; + +private: + using levels_array = std::array<int, 7>; + 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<levels_array::size_type>(msg.level)); + } +}; + +using syslog_sink_mt = syslog_sink<std::mutex>; +using syslog_sink_st = syslog_sink<details::null_mutex>; +} // namespace sinks + +// Create and register a syslog logger +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> 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<sinks::syslog_sink_mt>(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> 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<sinks::syslog_sink_st>(logger_name, syslog_ident, syslog_option, syslog_facility, enable_formatting); +} +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/systemd_sink.h b/NorthstarDLL/include/spdlog/sinks/systemd_sink.h new file mode 100644 index 00000000..d23824b8 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/systemd_sink.h @@ -0,0 +1,103 @@ +// Copyright(c) 2019 ZVYAGIN.Alexander@gmail.com +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/sinks/base_sink.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/details/synchronous_factory.h> + +#include <array> +#ifndef SD_JOURNAL_SUPPRESS_LOCATION +#define SD_JOURNAL_SUPPRESS_LOCATION +#endif +#include <systemd/sd-journal.h> + +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<typename Mutex> +class systemd_sink : public base_sink<Mutex> +{ +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<int, 7>; + 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<size_t>(std::numeric_limits<int>::max())) + { + length = static_cast<size_t>(std::numeric_limits<int>::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<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), + "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(msg.logger_name.size()), msg.logger_name.data(), nullptr); + } + else + { + err = (sd_journal_send)("MESSAGE=%.*s", static_cast<int>(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), + "SYSLOG_IDENTIFIER=%.*s", static_cast<int>(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<levels_array::size_type>(l)); + } + + void flush_() override {} +}; + +using systemd_sink_mt = systemd_sink<std::mutex>; +using systemd_sink_st = systemd_sink<details::null_mutex>; +} // namespace sinks + +// Create and register a syslog logger +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> systemd_logger_mt(const std::string &logger_name) +{ + return Factory::template create<sinks::systemd_sink_mt>(logger_name); +} + +template<typename Factory = spdlog::synchronous_factory> +inline std::shared_ptr<logger> systemd_logger_st(const std::string &logger_name) +{ + return Factory::template create<sinks::systemd_sink_st>(logger_name); +} +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/tcp_sink.h b/NorthstarDLL/include/spdlog/sinks/tcp_sink.h new file mode 100644 index 00000000..9dd9e51d --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/tcp_sink.h @@ -0,0 +1,81 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/common.h> +#include <spdlog/sinks/base_sink.h> +#include <spdlog/details/null_mutex.h> +#ifdef _WIN32 +#include <spdlog/details/tcp_client-windows.h> +#else +#include <spdlog/details/tcp_client.h> +#endif + +#include <mutex> +#include <string> +#include <chrono> +#include <functional> + +#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<typename Mutex> +class tcp_sink : public spdlog::sinks::base_sink<Mutex> +{ +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<Mutex>::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<std::mutex>; +using tcp_sink_st = tcp_sink<spdlog::details::null_mutex>; + +} // namespace sinks +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/win_eventlog_sink.h b/NorthstarDLL/include/spdlog/sinks/win_eventlog_sink.h new file mode 100644 index 00000000..68170dd9 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/win_eventlog_sink.h @@ -0,0 +1,276 @@ +// 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. +// 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\<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 <spdlog/details/null_mutex.h> +#include <spdlog/sinks/base_sink.h> + +#include <spdlog/details/windows_include.h> +#include <winbase.h> + +#include <mutex> +#include <string> +#include <vector> + +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<char> 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<unsigned char> buffer(static_cast<size_t>(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<typename Mutex> +class win_eventlog_sink : public base_sink<Mutex> +{ +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<Mutex>::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<std::mutex>; +using win_eventlog_sink_st = win_eventlog::win_eventlog_sink<details::null_mutex>; + +} // namespace sinks +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/wincolor_sink-inl.h b/NorthstarDLL/include/spdlog/sinks/wincolor_sink-inl.h new file mode 100644 index 00000000..e2676d3c --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/wincolor_sink-inl.h @@ -0,0 +1,175 @@ +// 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 <spdlog/sinks/wincolor_sink.h> +#endif + +#include <spdlog/details/windows_include.h> +#include <wincon.h> + +#include <spdlog/common.h> +#include <spdlog/pattern_formatter.h> + +namespace spdlog { +namespace sinks { +template<typename ConsoleMutex> +SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color_mode mode) + : out_handle_(out_handle) + , mutex_(ConsoleMutex::mutex()) + , formatter_(details::make_unique<spdlog::pattern_formatter>()) +{ + + 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<typename ConsoleMutex> +SPDLOG_INLINE wincolor_sink<ConsoleMutex>::~wincolor_sink() +{ + this->flush(); +} + +// change the color for the given level +template<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color(level::level_enum level, std::uint16_t color) +{ + std::lock_guard<mutex_t> lock(mutex_); + colors_[level] = color; +} + +template<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg) +{ + if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) + { + return; + } + + std::lock_guard<mutex_t> 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<WORD>(set_foreground_color_(colors_[msg.level])); + print_range_(formatted, msg.color_range_start, msg.color_range_end); + // reset to orig colors + ::SetConsoleTextAttribute(static_cast<HANDLE>(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<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::flush() +{ + // windows console always flushed? +} + +template<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_pattern(const std::string &pattern) +{ + std::lock_guard<mutex_t> lock(mutex_); + formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern)); +} + +template<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) +{ + std::lock_guard<mutex_t> lock(mutex_); + formatter_ = std::move(sink_formatter); +} + +template<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) +{ + std::lock_guard<mutex_t> lock(mutex_); + set_color_mode_impl(mode); +} + +template<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::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<HANDLE>(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<typename ConsoleMutex> +std::uint16_t SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_foreground_color_(std::uint16_t attribs) +{ + CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; + if (!::GetConsoleScreenBufferInfo(static_cast<HANDLE>(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<WORD>(attribs) | (orig_buffer_info.wAttributes & 0xfff0); + auto ignored = ::SetConsoleTextAttribute(static_cast<HANDLE>(out_handle_), static_cast<WORD>(new_attribs)); + (void)(ignored); + return static_cast<std::uint16_t>(orig_buffer_info.wAttributes); // return orig attribs +} + +// print a range of formatted message to console +template<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, size_t start, size_t end) +{ + if (end > start) + { + auto size = static_cast<DWORD>(end - start); + auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start, size, nullptr, nullptr); + (void)(ignored); + } +} + +template<typename ConsoleMutex> +void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::write_to_file_(const memory_buf_t &formatted) +{ + auto size = static_cast<DWORD>(formatted.size()); + DWORD bytes_written = 0; + auto ignored = ::WriteFile(static_cast<HANDLE>(out_handle_), formatted.data(), size, &bytes_written, nullptr); + (void)(ignored); +} + +// wincolor_stdout_sink +template<typename ConsoleMutex> +SPDLOG_INLINE wincolor_stdout_sink<ConsoleMutex>::wincolor_stdout_sink(color_mode mode) + : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_OUTPUT_HANDLE), mode) +{} + +// wincolor_stderr_sink +template<typename ConsoleMutex> +SPDLOG_INLINE wincolor_stderr_sink<ConsoleMutex>::wincolor_stderr_sink(color_mode mode) + : wincolor_sink<ConsoleMutex>(::GetStdHandle(STD_ERROR_HANDLE), mode) +{} +} // namespace sinks +} // namespace spdlog diff --git a/NorthstarDLL/include/spdlog/sinks/wincolor_sink.h b/NorthstarDLL/include/spdlog/sinks/wincolor_sink.h new file mode 100644 index 00000000..a82f1b24 --- /dev/null +++ b/NorthstarDLL/include/spdlog/sinks/wincolor_sink.h @@ -0,0 +1,85 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include <spdlog/common.h> +#include <spdlog/details/console_globals.h> +#include <spdlog/details/null_mutex.h> +#include <spdlog/sinks/sink.h> + +#include <memory> +#include <mutex> +#include <string> +#include <array> +#include <cstdint> + +namespace spdlog { +namespace sinks { +/* + * Windows color console sink. Uses WriteConsoleA to write to the console with + * colors + */ +template<typename ConsoleMutex> +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<spdlog::formatter> 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<spdlog::formatter> formatter_; + std::array<std::uint16_t, level::n_levels> 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<typename ConsoleMutex> +class wincolor_stdout_sink : public wincolor_sink<ConsoleMutex> +{ +public: + explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic); +}; + +template<typename ConsoleMutex> +class wincolor_stderr_sink : public wincolor_sink<ConsoleMutex> +{ +public: + explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic); +}; + +using wincolor_stdout_sink_mt = wincolor_stdout_sink<details::console_mutex>; +using wincolor_stdout_sink_st = wincolor_stdout_sink<details::console_nullmutex>; + +using wincolor_stderr_sink_mt = wincolor_stderr_sink<details::console_mutex>; +using wincolor_stderr_sink_st = wincolor_stderr_sink<details::console_nullmutex>; +} // namespace sinks +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY +#include "wincolor_sink-inl.h" +#endif |