Fixed some tidy warnings

This commit is contained in:
gabime 2024-11-30 11:36:43 +02:00
parent 3566e0f027
commit 82bb5114e2
11 changed files with 45 additions and 44 deletions

View File

@ -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
'

View File

@ -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

View File

@ -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;

View File

@ -184,11 +184,11 @@ protected:
// common implementation for after templated public api has been resolved to format string and
// args
template <typename... Args>
void log_with_format_(source_loc loc, level lvl, const format_string_t<Args...> &fmt, Args &&...args) {
void log_with_format_(source_loc loc, const level lvl, const format_string_t<Args...> &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)

View File

@ -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<level>(std::distance(std::begin(level_string_views), it));
// check also for "warn" and "err" before giving up

View File

@ -5,6 +5,7 @@
#include <cerrno>
#include <cstdio>
#include <utility>
#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(); }

View File

@ -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<size_t>(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<mode_t>(0755)) == 0; }
// create the given directory - and all directories leading to it
// return true on success or if the directory already exists

View File

@ -80,10 +80,9 @@ std::shared_ptr<logger> 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<logger> 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<logger> registry::get(const char *logger_name) { return get(std::string_view(logger_name)); }

View File

@ -40,9 +40,8 @@ void logger::set_formatter(std::unique_ptr<formatter> 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());
}
}

View File

@ -46,6 +46,9 @@ public:
}
}
scoped_padder(const scoped_padder &) = delete;
scoped_padder &operator=(const scoped_padder &) = delete;
template <typename T>
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<const char *, 7> days{{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}};
static constexpr std::array<const char *, 7> days{{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}};
template <typename ScopedPadder>
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<size_t>(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<size_t>(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<const char *, 7> full_days{{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}};
static constexpr std::array<const char *, 7> full_days{{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}};
template <typename ScopedPadder>
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<size_t>(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<size_t>(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<size_t>(tm_time.tm_mon)]};
string_view_t field_value{months.at(static_cast<size_t>(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<size_t>(tm_time.tm_mon)]};
string_view_t field_value{full_months.at(static_cast<size_t>(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<size_t>(tm_time.tm_wday)], dest);
fmt_helper::append_string_view(days.at(static_cast<size_t>(tm_time.tm_wday)), dest);
dest.push_back(' ');
fmt_helper::append_string_view(months[static_cast<size_t>(tm_time.tm_mon)], dest);
fmt_helper::append_string_view(months.at(static_cast<size_t>(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<char>::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<formatter> 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_formatter>(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<unsigned char>(*it))) {
if (it == end || (std::isdigit(static_cast<unsigned char>(*it)) == 0)) {
return padding_info{}; // no padding if no digit found here
}
auto width = static_cast<size_t>(*it) - '0';
for (++it; it != end && std::isdigit(static_cast<unsigned char>(*it)); ++it) {
for (++it; it != end && (std::isdigit(static_cast<unsigned char>(*it)) != 0); ++it) {
auto digit = static_cast<size_t>(*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<size_t>(width, max_width), side, truncate};
return padding_info{std::min<size_t>(width, max_width), side, truncate};
}
void pattern_formatter::compile_pattern_(const std::string &pattern) {

View File

@ -45,11 +45,12 @@ rotating_file_sink<Mutex>::rotating_file_sink(filename_t base_filename,
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
template <typename Mutex>
filename_t rotating_file_sink<Mutex>::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);
}