diff --git a/.clang-tidy b/.clang-tidy index cb2c5202..c4c73b3d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -23,7 +23,8 @@ clang-analyzer-*, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, -readability-named-parameter, -cert-env33-c, --modernize-concat-nested-namespaces +-modernize-concat-nested-namespaces, +-cppcoreguidelines-owning-memory ' diff --git a/include/spdlog/cfg/helpers.h b/include/spdlog/cfg/helpers.h index f928d91d..c9acf7dd 100644 --- a/include/spdlog/cfg/helpers.h +++ b/include/spdlog/cfg/helpers.h @@ -19,7 +19,7 @@ namespace helpers { // turn off all logging except for logger1: "off,logger1=debug" // turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info" // -SPDLOG_API void load_levels(const std::string &txt); +SPDLOG_API void load_levels(const std::string &input); } // namespace helpers } // namespace cfg } // namespace spdlog diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index c267129c..1e5ca4c6 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -17,7 +17,7 @@ namespace details { class SPDLOG_API file_helper { public: file_helper() = default; - explicit file_helper(const file_event_handlers &event_handlers); + explicit file_helper(file_event_handlers event_handlers); file_helper(const file_helper &) = delete; file_helper &operator=(const file_helper &) = delete; diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index b9ed7e1f..36f8df9a 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -184,11 +184,11 @@ protected: // common implementation for after templated public api has been resolved to format string and // args template - void log_with_format_(source_loc loc, level lvl, const format_string_t &fmt, Args &&...args) { + void log_with_format_(source_loc loc, const level lvl, const format_string_t &format_string, Args &&...args) { assert(should_log(lvl)); SPDLOG_TRY { memory_buf_t buf; - fmt::vformat_to(std::back_inserter(buf), fmt, fmt::make_format_args(args...)); + fmt::vformat_to(std::back_inserter(buf), format_string, fmt::make_format_args(args...)); sink_it_(details::log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()))); } SPDLOG_LOGGER_CATCH(loc) diff --git a/src/common.cpp b/src/common.cpp index c847eafd..fd046237 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -9,7 +9,7 @@ namespace spdlog { spdlog::level level_from_str(const std::string &name) noexcept { - auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name); + const auto *it = std::find(std::begin(level_string_views), std::end(level_string_views), name); if (it != std::end(level_string_views)) return static_cast(std::distance(std::begin(level_string_views), it)); // check also for "warn" and "err" before giving up diff --git a/src/details/file_helper.cpp b/src/details/file_helper.cpp index 96df3cbe..54aa2d2a 100644 --- a/src/details/file_helper.cpp +++ b/src/details/file_helper.cpp @@ -5,6 +5,7 @@ #include #include +#include #include "spdlog/common.h" #include "spdlog/details/os.h" @@ -12,8 +13,8 @@ namespace spdlog { namespace details { -file_helper::file_helper(const file_event_handlers &event_handlers) - : event_handlers_(event_handlers) {} +file_helper::file_helper(file_event_handlers event_handlers) + : event_handlers_(std::move(event_handlers)) {} file_helper::~file_helper() { close(); } diff --git a/src/details/os_unix.cpp b/src/details/os_unix.cpp index c1026b6d..f91fc69b 100644 --- a/src/details/os_unix.cpp +++ b/src/details/os_unix.cpp @@ -109,7 +109,7 @@ int rename(const filename_t &filename1, const filename_t &filename2) noexcept { // Return true if path exists (file or directory) bool path_exists(const filename_t &filename) noexcept { - struct stat buffer; + struct stat buffer{}; return (::stat(filename.c_str(), &buffer) == 0); } @@ -127,7 +127,7 @@ size_t filesize(FILE *f) { #endif // 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated) #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64)) - struct stat64 st; + struct stat64 st{}; if (::fstat64(fd, &st) == 0) { return static_cast(st.st_size); } @@ -275,7 +275,7 @@ bool is_color_terminal() noexcept { bool in_terminal(FILE *file) noexcept { return ::isatty(fileno(file)) != 0; } // return true on success -static bool mkdir_(const filename_t &path) { return ::mkdir(path.c_str(), mode_t(0755)) == 0; } +static bool mkdir_(const filename_t &path) { return ::mkdir(path.c_str(), static_cast(0755)) == 0; } // create the given directory - and all directories leading to it // return true on success or if the directory already exists diff --git a/src/details/registry.cpp b/src/details/registry.cpp index 1c53a16a..e60c7fd1 100644 --- a/src/details/registry.cpp +++ b/src/details/registry.cpp @@ -80,10 +80,9 @@ std::shared_ptr registry::get(const std::string &logger_name) { } } return nullptr; - } else { - auto found = loggers_.find(logger_name); - return found == loggers_.end() ? nullptr : found->second; } + auto found = loggers_.find(logger_name); + return found == loggers_.end() ? nullptr : found->second; } // if the map is small do a sequential search and avoid creating string for find(logger_name) @@ -99,10 +98,8 @@ std::shared_ptr registry::get(std::string_view logger_name) { return nullptr; } // otherwise use the normal map lookup - else { - auto found = loggers_.find(std::string(logger_name)); - return found == loggers_.end() ? nullptr : found->second; - } + const auto found = loggers_.find(std::string(logger_name)); + return found == loggers_.end() ? nullptr : found->second; } std::shared_ptr registry::get(const char *logger_name) { return get(std::string_view(logger_name)); } diff --git a/src/logger.cpp b/src/logger.cpp index 4fc320e4..96dbcad6 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -40,9 +40,8 @@ void logger::set_formatter(std::unique_ptr f) { // last element - we can move it. (*it)->set_formatter(std::move(f)); break; // to prevent clang-tidy warning - } else { - (*it)->set_formatter(f->clone()); } + (*it)->set_formatter(f->clone()); } } diff --git a/src/pattern_formatter.cpp b/src/pattern_formatter.cpp index 1dbbf318..a4c8620e 100644 --- a/src/pattern_formatter.cpp +++ b/src/pattern_formatter.cpp @@ -46,6 +46,9 @@ public: } } + scoped_padder(const scoped_padder &) = delete; + scoped_padder &operator=(const scoped_padder &) = delete; + template static unsigned int count_digits(T n) { return fmt_helper::count_digits(n); @@ -127,7 +130,7 @@ static const char *ampm(const tm &t) { return t.tm_hour >= 12 ? "PM" : "AM"; } static int to12h(const tm &t) { return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour; } // Abbreviated weekday name -static std::array days{{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}}; +static constexpr std::array days{{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}}; template class a_formatter final : public flag_formatter { @@ -135,15 +138,15 @@ public: explicit a_formatter(padding_info padinfo) : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - string_view_t field_value{days[static_cast(tm_time.tm_wday)]}; + void format(const log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + string_view_t field_value{days.at(static_cast(tm_time.tm_wday))}; ScopedPadder p(field_value.size(), padinfo_, dest); fmt_helper::append_string_view(field_value, dest); } }; // Full weekday name -static std::array full_days{{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}}; +static constexpr std::array full_days{{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}}; template class A_formatter final : public flag_formatter { @@ -151,8 +154,8 @@ public: explicit A_formatter(padding_info padinfo) : flag_formatter(padinfo) {} - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - string_view_t field_value{full_days[static_cast(tm_time.tm_wday)]}; + void format(const log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + string_view_t field_value{full_days.at(static_cast(tm_time.tm_wday))}; ScopedPadder p(field_value.size(), padinfo_, dest); fmt_helper::append_string_view(field_value, dest); } @@ -169,7 +172,7 @@ public: : flag_formatter(padinfo) {} void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - string_view_t field_value{months[static_cast(tm_time.tm_mon)]}; + string_view_t field_value{months.at(static_cast(tm_time.tm_mon))}; ScopedPadder p(field_value.size(), padinfo_, dest); fmt_helper::append_string_view(field_value, dest); } @@ -186,7 +189,7 @@ public: : flag_formatter(padinfo) {} void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - string_view_t field_value{full_months[static_cast(tm_time.tm_mon)]}; + string_view_t field_value{full_months.at(static_cast(tm_time.tm_mon))}; ScopedPadder p(field_value.size(), padinfo_, dest); fmt_helper::append_string_view(field_value, dest); } @@ -203,9 +206,9 @@ public: const size_t field_size = 24; ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::append_string_view(days[static_cast(tm_time.tm_wday)], dest); + fmt_helper::append_string_view(days.at(static_cast(tm_time.tm_wday)), dest); dest.push_back(' '); - fmt_helper::append_string_view(months[static_cast(tm_time.tm_mon)], dest); + fmt_helper::append_string_view(months.at(static_cast(tm_time.tm_mon)), dest); dest.push_back(' '); fmt_helper::append_int(tm_time.tm_mday, dest); dest.push_back(' '); @@ -493,8 +496,11 @@ public: : flag_formatter(padinfo) {} z_formatter() = default; + ~z_formatter() override = default; z_formatter(const z_formatter &) = delete; z_formatter &operator=(const z_formatter &) = delete; + z_formatter(z_formatter &&) = delete; + z_formatter &operator=(z_formatter &&) = delete; void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override { const size_t field_size = 6; @@ -626,12 +632,10 @@ public: return; } - size_t text_size; + size_t text_size = 0; if (padinfo_.enabled()) { // calc text size for padding based on "filename:line" text_size = std::char_traits::length(msg.source.filename) + ScopedPadder::count_digits(msg.source.line) + 1; - } else { - text_size = 0; } ScopedPadder p(text_size, padinfo_, dest); @@ -784,7 +788,7 @@ public: dest.push_back(' '); // append logger name if exists - if (msg.logger_name.size() > 0) { + if (!msg.logger_name.empty()) { dest.push_back('['); fmt_helper::append_string_view(msg.logger_name, dest); dest.push_back(']'); @@ -846,7 +850,7 @@ pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eo std::unique_ptr pattern_formatter::clone() const { custom_flags cloned_custom_formatters; - for (auto &it : custom_handlers_) { + for (const auto &it : custom_handlers_) { cloned_custom_formatters[it.first] = it.second->clone(); } auto cloned = std::make_unique(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters)); @@ -1137,29 +1141,27 @@ details::padding_info pattern_formatter::handle_padspec_(std::string::const_iter ++it; break; default: - side = details::padding_info::pad_side::left; + side = padding_info::pad_side::left; break; } - if (it == end || !std::isdigit(static_cast(*it))) { + if (it == end || (std::isdigit(static_cast(*it)) == 0)) { return padding_info{}; // no padding if no digit found here } auto width = static_cast(*it) - '0'; - for (++it; it != end && std::isdigit(static_cast(*it)); ++it) { + for (++it; it != end && (std::isdigit(static_cast(*it)) != 0); ++it) { auto digit = static_cast(*it) - '0'; width = width * 10 + digit; } // search for the optional truncate marker '!' - bool truncate; + bool truncate = false; if (it != end && *it == '!') { truncate = true; ++it; - } else { - truncate = false; } - return details::padding_info{std::min(width, max_width), side, truncate}; + return padding_info{std::min(width, max_width), side, truncate}; } void pattern_formatter::compile_pattern_(const std::string &pattern) { diff --git a/src/sinks/rotating_file_sink.cpp b/src/sinks/rotating_file_sink.cpp index b6a46ff5..5b2fad84 100644 --- a/src/sinks/rotating_file_sink.cpp +++ b/src/sinks/rotating_file_sink.cpp @@ -45,11 +45,12 @@ rotating_file_sink::rotating_file_sink(filename_t base_filename, // e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt". template filename_t rotating_file_sink::calc_filename(const filename_t &filename, std::size_t index) { - if (index == 0u) { + if (index == 0U) { return filename; } - filename_t basename, ext; + filename_t basename; + filename_t ext; std::tie(basename, ext) = details::file_helper::split_by_extension(filename); return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}.{}{}")), basename, index, ext); }