From cf73f02eafbb9a722cf0b82d8339514c9ba65a0d Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 2 Dec 2016 17:09:00 +0200 Subject: [PATCH] pid support (added the %P option to formatter) --- include/spdlog/details/os.h | 11 +++++++++++ include/spdlog/details/pattern_formatter_impl.h | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 1fc090be..e97d4b71 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -356,6 +356,17 @@ inline std::string errno_str(int err_num) #endif } +inline int pid() +{ + +#ifdef _WIN32 + return ::_getpid(); +#else + return static_cast(::getpid()); +#endif + +} + } //os } //details } //spdlog diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 73c0db3b..9a6e10ea 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -356,7 +356,7 @@ private: -//Thread id +// Thread id class t_formatter:public flag_formatter { void format(details::log_msg& msg, const std::tm&) override @@ -365,6 +365,15 @@ class t_formatter:public flag_formatter } }; +// Current pid +class pid_formatter:public flag_formatter +{ + void format(details::log_msg& msg, const std::tm&) override + { + msg.formatted << details::os::pid(); + } +}; + class v_formatter:public flag_formatter { @@ -453,6 +462,8 @@ class full_formatter:public flag_formatter } }; + + } } /////////////////////////////////////////////////////////////////////////////// @@ -611,6 +622,10 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) _formatters.push_back(std::unique_ptr(new details::full_formatter())); break; + case ('P'): + _formatters.push_back(std::unique_ptr(new details::pid_formatter())); + break; + default: //Unkown flag appears as is _formatters.push_back(std::unique_ptr(new details::ch_formatter('%'))); _formatters.push_back(std::unique_ptr(new details::ch_formatter(flag)));