diff options
author | gabime <gmelman1@gmail.com> | 2019-06-14 00:23:25 +0300 |
---|---|---|
committer | gabime <gmelman1@gmail.com> | 2019-06-14 00:23:25 +0300 |
commit | c09dee7717e1ae7289fe18977405e9c33fb48ab0 (patch) | |
tree | 310a7ef1e2cb2e288b9941d3e9b43b8d045914ca /include/spdlog | |
parent | 352281313fe1c4313bc222cb9de222afd50c822f (diff) |
Moved source filename shortening to pattern formatter instead of macro
Diffstat (limited to 'include/spdlog')
-rw-r--r-- | include/spdlog/common.h | 13 | ||||
-rw-r--r-- | include/spdlog/details/pattern_formatter-inl.h | 35 | ||||
-rw-r--r-- | include/spdlog/spdlog.h | 2 |
3 files changed, 34 insertions, 16 deletions
diff --git a/include/spdlog/common.h b/include/spdlog/common.h index fdd887b7..82f1629b 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -53,19 +53,6 @@ #endif #endif -// Get the basename of __FILE__ (at compile time if possible) -#if FMT_HAS_FEATURE(__builtin_strrchr) -#define SPDLOG_STRRCHR(str, sep) __builtin_strrchr(str, sep) -#else -#define SPDLOG_STRRCHR(str, sep) strrchr(str, sep) -#endif //__builtin_strrchr not defined - -#ifdef _WIN32 -#define SPDLOG_FILE_BASENAME(file) SPDLOG_STRRCHR("\\" file, '\\') + 1 -#else -#define SPDLOG_FILE_BASENAME(file) SPDLOG_STRRCHR("/" file, '/') + 1 -#endif - #ifndef SPDLOG_FUNCTION #define SPDLOG_FUNCTION __FUNCTION__ #endif diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/details/pattern_formatter-inl.h index 99964030..6124c946 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/details/pattern_formatter-inl.h @@ -17,6 +17,7 @@ #include <chrono> #include <ctime> #include <cctype> +#include <cstring> #include <memory> #include <mutex> #include <string> @@ -820,6 +821,31 @@ public: } }; +class short_filename_formatter final : public flag_formatter +{ +public: + explicit short_filename_formatter(padding_info padinfo) + : flag_formatter(padinfo) + {} + + static const char* basename(const char* filename) + { + const char *rv = std::strrchr(filename, os::folder_sep); + return rv != nullptr ? rv + 1: filename; + } + + void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override + { + if (msg.source.empty()) + { + return; + } + auto filename = basename(msg.source.filename); + scoped_pad p(filename, padinfo_, dest); + fmt_helper::append_string_view(filename, dest); + } +}; + class source_linenum_formatter final : public flag_formatter { public: @@ -944,7 +970,8 @@ public: if (!msg.source.empty()) { dest.push_back('['); - fmt_helper::append_string_view(msg.source.filename, dest); + const char *filename = details::short_filename_formatter::basename(msg.source.filename); + fmt_helper::append_string_view(filename, dest); dest.push_back(':'); fmt_helper::append_int(msg.source.line, dest); dest.push_back(']'); @@ -1154,7 +1181,11 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i formatters_.push_back(details::make_unique<details::source_location_formatter>(padding)); break; - case ('s'): // source filename + case ('s'): // short source filename - without directory name + formatters_.push_back(details::make_unique<details::short_filename_formatter>(padding)); + break; + + case ('g'): // full source filename formatters_.push_back(details::make_unique<details::source_filename_formatter>(padding)); break; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index fa4c9649..d3dd2af1 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -267,7 +267,7 @@ inline void critical(const wchar_t *fmt, const Args &... args) do \ { \ if (logger->should_log(level)) \ - logger->log(spdlog::source_loc{SPDLOG_FILE_BASENAME(__FILE__), __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ + logger->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__); \ } while (0) #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE |