From 31f0c40893c05e01e829769eb29715809cffaa8a Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 7 Jul 2023 10:47:03 +0300 Subject: [PATCH] migrate to fmt-based logging in some more places and improve logging. --- lib/message/mu-document.cc | 4 ---- lib/mu-config.hh | 7 +++---- lib/mu-contacts-cache.cc | 25 +++++++----------------- lib/mu-query-threads.cc | 9 ++++----- lib/mu-script.cc | 6 ++---- lib/utils/mu-utils.hh | 40 +++++++++++++++++++++++++++----------- mu/mu-cmd-add.cc | 3 +-- mu/mu-cmd-info.cc | 9 ++++----- mu/mu-cmd-remove.cc | 2 +- 9 files changed, 51 insertions(+), 54 deletions(-) diff --git a/lib/message/mu-document.cc b/lib/message/mu-document.cc index 0d93bb45..3108ddf6 100644 --- a/lib/message/mu-document.cc +++ b/lib/message/mu-document.cc @@ -34,10 +34,6 @@ using namespace Mu; -constexpr uint8_t SepaChar1 = 0xfe; -constexpr uint8_t SepaChar2 = 0xff; - - const Xapian::Document& Document::xapian_document() const { diff --git a/lib/mu-config.hh b/lib/mu-config.hh index df61106d..f3883fa6 100644 --- a/lib/mu-config.hh +++ b/lib/mu-config.hh @@ -256,13 +256,13 @@ public: const auto strval = std::invoke([&]{ if constexpr (prop.type == Type::Number || prop.type == Type::Timestamp) - return format("%" PRIi64, static_cast(val)); + return mu_format("{}", static_cast(val)); else if constexpr (prop.type == Type::Path || prop.type == Type::String) return std::string{val}; else if constexpr (prop.type == Type::StringList) return join(val, SepaChar1); - - throw std::logic_error("invalid prop " + std::string{prop.name}); + else + throw std::logic_error("invalid prop " + std::string{prop.name}); }); cstore_.set_metadata(std::string{prop.name}, strval); @@ -293,7 +293,6 @@ public: } private: - static constexpr uint8_t SepaChar1 = 0xfe; MetadataIface& cstore_; }; diff --git a/lib/mu-contacts-cache.cc b/lib/mu-contacts-cache.cc index 3f48778d..035949e0 100644 --- a/lib/mu-contacts-cache.cc +++ b/lib/mu-contacts-cache.cc @@ -122,9 +122,6 @@ private: } }; -constexpr auto Separator = "\xff"; // Invalid in UTF-8 - - ContactUMap ContactsCache::Private::deserialize(const std::string& serialized) const { @@ -133,7 +130,7 @@ ContactsCache::Private::deserialize(const std::string& serialized) const std::string line; while (getline(ss, line)) { - const auto parts = Mu::split(line, Separator); + const auto parts = Mu::split(line, SepaChar2); if (G_UNLIKELY(parts.size() != 5)) { mu_warning("error: '{}'", line); continue; @@ -168,20 +165,12 @@ ContactsCache::Private::serialize() const for (auto& item : contacts_) { const auto& ci{item.second}; - s += Mu::format("%s%s" - "%s%s" - "%d%s" - "%" G_GINT64_FORMAT "%s" - "%" G_GINT64_FORMAT "\n", - ci.email.c_str(), - Separator, - ci.name.c_str(), - Separator, - ci.personal ? 1 : 0, - Separator, - (gint64)ci.message_date, - Separator, - (gint64)ci.frequency); + s += mu_format("{}{}{}{}{}{}{}{}{}\n", + ci.email, SepaChar2, + ci.name, SepaChar2, + ci.personal ? 1 : 0, SepaChar2, + ci.message_date, SepaChar2, + ci.frequency); } config_db_.set(s); dirty_ = 0; diff --git a/lib/mu-query-threads.cc b/lib/mu-query-threads.cc index be2f68f7..3f92d520 100644 --- a/lib/mu-query-threads.cc +++ b/lib/mu-query-threads.cc @@ -379,7 +379,6 @@ subject_matches(const std::string& sub1, const std::string& sub2) } }; - // g_debug ("'%s' '%s'", search_str(sub1), search_str(sub2)); return g_strcmp0(search_str(sub1), search_str(sub2)) == 0; } @@ -661,10 +660,10 @@ assert_thread_paths(const MockQueryResults& qrs, const Expected& expected) qr.path().value_or("") == exp.first; }); g_assert_true(it != qrs.end()); - g_debug("thread-path (%s@%s): expected: '%s'; got '%s'", - it->message_id().value_or("").c_str(), - it->path().value_or("").c_str(), - exp.second.c_str(), it->query_match().thread_path.c_str()); + mu_debug("thread-path ({}@{}): expected: '{}'; got '{}'", + it->message_id().value_or(""), + it->path().value_or(""), + exp.second, it->query_match().thread_path); g_assert_cmpstr(exp.second.c_str(), ==, it->query_match().thread_path.c_str()); } } diff --git a/lib/mu-script.cc b/lib/mu-script.cc index b5393e64..2027977e 100644 --- a/lib/mu-script.cc +++ b/lib/mu-script.cc @@ -143,10 +143,8 @@ Mu::run_script(const std::string& path, #else std::string mainargs; for (auto&& arg: args) - mainargs += format("%s\"%s\"", - mainargs.empty() ? "" : " ", arg.c_str()); - auto expr = format("(main '(\"%s\" %s))", - get_name(path).c_str(), mainargs.c_str()); + mainargs += mu_format("{}\"{}\"", mainargs.empty() ? "" : " ", arg); + auto expr = mu_format("(main '(\"{}\" {}))", get_name(path), mainargs); std::vector argv = { GUILE_BINARY, diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index ca8461fe..bbd85452 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -46,6 +46,15 @@ namespace Mu { + +/* + * Separator characters used in various places; importantly, + * they are not used in UTF-8 + */ +constexpr const auto SepaChar1 = '\xfe'; +constexpr const auto SepaChar2 = '\xff'; + + /* * Logging/printing/formatting functions connect libfmt with the Glib logging * system. We wrap so perhaps at some point (C++23?) we can use std:: instead. @@ -53,35 +62,38 @@ namespace Mu { /* * Debug/error/warning logging + * + * The 'noexcept' means that they _wilL_ terminate the program + * when the formatting fails (ie. a bug) */ template -inline void mu_debug(fmt::format_string frm, T&&... args) noexcept { +void mu_debug(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_DEBUG, "%s", fmt::format(frm, std::forward(args)...).c_str()); } template -inline void mu_info(fmt::format_string frm, T&&... args) noexcept { +void mu_info(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_INFO, "%s", fmt::format(frm, std::forward(args)...).c_str()); } template -inline void mu_message(fmt::format_string frm, T&&... args) noexcept { +void mu_message(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_MESSAGE, "%s", fmt::format(frm, std::forward(args)...).c_str()); } template -inline void mu_warning(fmt::format_string frm, T&&... args) noexcept { +void mu_warning(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_WARNING, "%s", fmt::format(frm, std::forward(args)...).c_str()); } template -inline void mu_critical(fmt::format_string frm, T&&... args) noexcept { +void mu_critical(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_CRITICAL, "%s", fmt::format(frm, std::forward(args)...).c_str()); } template -inline void mu_error(fmt::format_string frm, T&&... args) noexcept { +void mu_error(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_ERROR, "%s", fmt::format(frm, std::forward(args)...).c_str()); } @@ -91,19 +103,19 @@ inline void mu_error(fmt::format_string frm, T&&... args) noexcept { */ template -inline void mu_print(fmt::format_string frm, T&&... args) noexcept { +void mu_print(fmt::format_string frm, T&&... args) noexcept { fmt::print(frm, std::forward(args)...); } template -inline void mu_println(fmt::format_string frm, T&&... args) noexcept { +void mu_println(fmt::format_string frm, T&&... args) noexcept { fmt::println(frm, std::forward(args)...); } template -inline void mu_printerr(fmt::format_string frm, T&&... args) noexcept { +void mu_printerr(fmt::format_string frm, T&&... args) noexcept { fmt::print(stderr, frm, std::forward(args)...); } template -inline void mu_printerrln(fmt::format_string frm, T&&... args) noexcept { +void mu_printerrln(fmt::format_string frm, T&&... args) noexcept { fmt::println(stderr, frm, std::forward(args)...); } @@ -112,10 +124,16 @@ inline void mu_printerrln(fmt::format_string frm, T&&... args) noexcept { * Fprmatting */ template -inline std::string mu_format(fmt::format_string frm, T&&... args) noexcept { +std::string mu_format(fmt::format_string frm, T&&... args) noexcept { return fmt::format(frm, std::forward(args)...); } +template +auto mu_join(Range&& range, std::string_view sepa) { + return fmt::join(std::forward(range), sepa); +} + + using StringVec = std::vector; /** diff --git a/mu/mu-cmd-add.cc b/mu/mu-cmd-add.cc index f30d6105..af1f1426 100644 --- a/mu/mu-cmd-add.cc +++ b/mu/mu-cmd-add.cc @@ -30,8 +30,7 @@ Mu::mu_cmd_add(Mu::Store& store, const Options& opts) if (!docid) return Err(docid.error()); else - g_debug("added message @ %s, docid=%u", - file.c_str(), docid.value()); + mu_debug("added message @ {}, docid={}", file, *docid); } return Ok(); diff --git a/mu/mu-cmd-info.cc b/mu/mu-cmd-info.cc index 5fd46d80..9d2093bd 100644 --- a/mu/mu-cmd-info.cc +++ b/mu/mu-cmd-info.cc @@ -129,7 +129,8 @@ topic_fields(const Options& opts) }); colorify(fields, opts); - std::cout << fields << '\n'; + + std::cout << "# Message fields\n" << fields << '\n'; return Ok(); } @@ -169,8 +170,7 @@ topic_flags(const Options& opts) colorify(flags, opts); - std::cout << flags << '\n'; - + std::cout << "# Message flags\n" << flags << '\n'; return Ok(); } @@ -251,8 +251,6 @@ topic_mu(const Options& opts) std::cout << info << '\n'; - // mu_println("{}", info); - return Ok(); } @@ -268,6 +266,7 @@ Mu::mu_cmd_info(const Mu::Store& store, const Options& opts) return topic_store(store, opts); else if (topic == "fields") { topic_fields(opts); + std::cout << std::endl; return topic_flags(opts); } else if (topic == "mu") { return topic_mu(opts); diff --git a/mu/mu-cmd-remove.cc b/mu/mu-cmd-remove.cc index b7849989..093813d4 100644 --- a/mu/mu-cmd-remove.cc +++ b/mu/mu-cmd-remove.cc @@ -30,7 +30,7 @@ Mu::mu_cmd_remove(Mu::Store& store, const Options& opts) if (!res) return Err(Error::Code::File, "failed to remove {}", file.c_str()); else - g_debug("removed message @ %s", file.c_str()); + mu_debug("removed message @ {}", file); } return Ok();