Skip to content

Commit

Permalink
migrate to fmt-based logging in some more places
Browse files Browse the repository at this point in the history
and improve logging.
  • Loading branch information
djcb committed Jul 8, 2023
1 parent 4171fe1 commit 31f0c40
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 54 deletions.
4 changes: 0 additions & 4 deletions lib/message/mu-document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@

using namespace Mu;

constexpr uint8_t SepaChar1 = 0xfe;
constexpr uint8_t SepaChar2 = 0xff;


const Xapian::Document&
Document::xapian_document() const
{
Expand Down
7 changes: 3 additions & 4 deletions lib/mu-config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(val));
return mu_format("{}", static_cast<int64_t>(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);
Expand Down Expand Up @@ -293,7 +293,6 @@ public:
}

private:
static constexpr uint8_t SepaChar1 = 0xfe;
MetadataIface& cstore_;
};

Expand Down
25 changes: 7 additions & 18 deletions lib/mu-contacts-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ struct ContactsCache::Private {
}
};

constexpr auto Separator = "\xff"; // Invalid in UTF-8


ContactUMap
ContactsCache::Private::deserialize(const std::string& serialized) const
{
Expand All @@ -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;
Expand Down Expand Up @@ -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<Config::Id::Contacts>(s);
dirty_ = 0;
Expand Down
9 changes: 4 additions & 5 deletions lib/mu-query-threads.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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("<none>").c_str(),
it->path().value_or("<none>").c_str(),
exp.second.c_str(), it->query_match().thread_path.c_str());
mu_debug("thread-path ({}@{}): expected: '{}'; got '{}'",
it->message_id().value_or("<none>"),
it->path().value_or("<none>"),
exp.second, it->query_match().thread_path);
g_assert_cmpstr(exp.second.c_str(), ==, it->query_match().thread_path.c_str());
}
}
Expand Down
6 changes: 2 additions & 4 deletions lib/mu-script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*> argv = {
GUILE_BINARY,
Expand Down
40 changes: 29 additions & 11 deletions lib/utils/mu-utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,54 @@

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.
*/

/*
* Debug/error/warning logging
*
* The 'noexcept' means that they _wilL_ terminate the program
* when the formatting fails (ie. a bug)
*/

template<typename...T>
inline void mu_debug(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_debug(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_DEBUG, "%s",
fmt::format(frm, std::forward<T>(args)...).c_str());
}
template<typename...T>
inline void mu_info(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_info(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_INFO, "%s",
fmt::format(frm, std::forward<T>(args)...).c_str());
}
template<typename...T>
inline void mu_message(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_message(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_MESSAGE, "%s",
fmt::format(frm, std::forward<T>(args)...).c_str());
}
template<typename...T>
inline void mu_warning(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_warning(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_WARNING, "%s",
fmt::format(frm, std::forward<T>(args)...).c_str());
}
template<typename...T>
inline void mu_critical(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_critical(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_CRITICAL, "%s",
fmt::format(frm, std::forward<T>(args)...).c_str());
}
template<typename...T>
inline void mu_error(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_error(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_ERROR, "%s",
fmt::format(frm, std::forward<T>(args)...).c_str());
}
Expand All @@ -91,19 +103,19 @@ inline void mu_error(fmt::format_string<T...> frm, T&&... args) noexcept {
*/

template<typename...T>
inline void mu_print(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_print(fmt::format_string<T...> frm, T&&... args) noexcept {
fmt::print(frm, std::forward<T>(args)...);
}
template<typename...T>
inline void mu_println(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_println(fmt::format_string<T...> frm, T&&... args) noexcept {
fmt::println(frm, std::forward<T>(args)...);
}
template<typename...T>
inline void mu_printerr(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_printerr(fmt::format_string<T...> frm, T&&... args) noexcept {
fmt::print(stderr, frm, std::forward<T>(args)...);
}
template<typename...T>
inline void mu_printerrln(fmt::format_string<T...> frm, T&&... args) noexcept {
void mu_printerrln(fmt::format_string<T...> frm, T&&... args) noexcept {
fmt::println(stderr, frm, std::forward<T>(args)...);
}

Expand All @@ -112,10 +124,16 @@ inline void mu_printerrln(fmt::format_string<T...> frm, T&&... args) noexcept {
* Fprmatting
*/
template<typename...T>
inline std::string mu_format(fmt::format_string<T...> frm, T&&... args) noexcept {
std::string mu_format(fmt::format_string<T...> frm, T&&... args) noexcept {
return fmt::format(frm, std::forward<T>(args)...);
}

template<typename Range>
auto mu_join(Range&& range, std::string_view sepa) {
return fmt::join(std::forward<Range>(range), sepa);
}


using StringVec = std::vector<std::string>;

/**
Expand Down
3 changes: 1 addition & 2 deletions mu/mu-cmd-add.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 4 additions & 5 deletions mu/mu-cmd-info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -251,8 +251,6 @@ topic_mu(const Options& opts)

std::cout << info << '\n';

// mu_println("{}", info);

return Ok();
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion mu/mu-cmd-remove.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 31f0c40

Please sign in to comment.