update to use fmt-based apis

Not complete, but a first big stab converting users of Mu::Error and
various g_warning & friends, format to the new libfmt-based APIs.
This commit is contained in:
Dirk-Jan C. Binnema
2023-07-05 23:10:13 +03:00
parent 742ca33740
commit 4920b56671
46 changed files with 435 additions and 449 deletions

View File

@ -137,7 +137,7 @@ static void
output_mutt_address_book(ItemType itype, OptContact contact, const Options& opts)
{
if (itype == ItemType::Header)
g_print ("Matching addresses in the mu database:\n");
mu_print ("Matching addresses in the mu database:\n");
if (!contact)
return;
@ -176,8 +176,8 @@ static void
output_bbdb(ItemType itype, OptContact contact, const Options& opts)
{
if (itype == ItemType::Header)
g_print (";; -*-coding: utf-8-emacs;-*-\n"
";;; file-version: 6\n");
mu_println (";; -*-coding: utf-8-emacs;-*-\n"
";;; file-version: 6");
if (!contact)
return;
@ -185,13 +185,9 @@ output_bbdb(ItemType itype, OptContact contact, const Options& opts)
const auto now{time_to_string("%Y-%m-%d", ::time(NULL))};
const auto timestamp{time_to_string("%Y-%m-%d", contact->message_date)};
g_print("[\"%s\" \"%s\" nil nil nil nil (\"%s\") "
"((creation-date . \"%s\") (time-stamp . \"%s\")) nil]\n",
names.first.c_str(),
names.second.c_str(),
contact->email.c_str(),
now.c_str(),
timestamp.c_str());
mu_println("[\"{}\" \"{}\" nil nil nil nil (\"{}\") "
"((creation-date . \"{}\") (time-stamp . \"{}\")) nil]",
names.first, names.second, contact->email, now, timestamp);
}
static void
@ -209,10 +205,10 @@ static void
output_json(ItemType itype, OptContact contact, const Options& opts)
{
if (itype == ItemType::Header)
g_print("[\n");
mu_println("[");
if (contact) {
g_print("%s", itype == ItemType::Header ? "" : ",\n");
g_print (" {\n");
mu_print("{}", itype == ItemType::Header ? "" : ",\n");
mu_println (" {{");
const std::string name = contact->name.empty() ? "null" : Mu::quote(contact->name);
print_encoded(
@ -230,11 +226,11 @@ output_json(ItemType itype, OptContact contact, const Options& opts)
time_to_string("%FT%TZ", contact->message_date, true/*utc*/).c_str(),
contact->personal ? "true" : "false",
contact->frequency);
g_print (" }");
mu_print (" }}");
}
if (itype == ItemType::Footer)
g_print("\n]\n");
mu_println("\n]");
}
static OutputFunc
@ -260,7 +256,7 @@ find_output_func(Format format)
case Format::Json:
return output_json;
default:
g_warning("unsupported format");
mu_warning("unsupported format");
return {};
}
#pragma GCC diagnostic pop

View File

@ -86,7 +86,7 @@ save_parts(const Message& message, const std::string& filename_rx,
if (saved_num == 0)
return Err(Error::Code::File,
"no %s extracted from this message",
"no {} extracted from this message",
opts.extract.save_attachments ? "attachments" : "parts");
else
return Ok();
@ -102,7 +102,7 @@ static void
show_part(const MessagePart& part, size_t index, bool color)
{
/* index */
g_print(" %zu ", index);
mu_print(" {} ", index);
/* filename */
color_maybe(MU_COLOR_GREEN);
@ -122,7 +122,7 @@ show_part(const MessagePart& part, size_t index, bool color)
/* size */
if (part.size() > 0) {
color_maybe(MU_COLOR_CYAN);
g_print(" (%zu bytes)", part.size());
mu_print(" ({} bytes)", part.size());
}
color_maybe(MU_COLOR_DEFAULT);
@ -133,7 +133,7 @@ static Mu::Result<void>
show_parts(const Message& message, const Options& opts)
{
size_t index{};
g_print("MIME-parts in this message:\n");
mu_println("MIME-parts in this message:");
for (auto&& part: message.parts())
show_part(part, ++index, !opts.nocolor);
@ -164,8 +164,8 @@ Mu::mu_cmd_extract(const Options& opts)
if (!check_dir(opts.extract.targetdir, false/*!readable*/, true/*writeable*/))
return Err(Error::Code::File,
"target '%s' is not a writable directory",
opts.extract.targetdir.c_str());
"target '{}' is not a writable directory",
opts.extract.targetdir);
return save_parts(*message, opts.extract.filename_rx, opts);
}

View File

@ -116,14 +116,14 @@ resolve_bookmark(const Options& opts)
auto bm = mu_bookmarks_new(bmfile.c_str());
if (!bm)
return Err(Error::Code::File,
"failed to open bookmarks file '%s'", bmfile.c_str());
"failed to open bookmarks file '{}'", bmfile);
const auto bookmark{opts.find.bookmark};
const auto val = mu_bookmarks_lookup(bm, bookmark.c_str());
if (!val) {
mu_bookmarks_destroy(bm);
return Err(Error::Code::NoMatches,
"bookmark '%s' not found", bookmark.c_str());
"bookmark '{}' not found", bookmark);
}
mu_bookmarks_destroy(bm);
@ -263,9 +263,9 @@ print_summary(const Message& msg, const Options& opts)
const auto summ{summarize(body->c_str(), opts.find.summary_len.value_or(0))};
g_print("Summary: ");
mu_print("Summary: ");
fputs_encoded(summ, stdout);
g_print("\n");
mu_println("");
}
static void
@ -361,21 +361,19 @@ static bool
output_json(const Option<Message>& msg, const OutputInfo& info, const Options& opts, GError** err)
{
if (info.header) {
g_print("[\n");
mu_println("[");
return true;
}
if (info.footer) {
g_print("]\n");
mu_println("]");
return true;
}
if (!msg)
return true;
g_print("%s%s\n",
msg->sexp().to_json_string().c_str(),
info.last ? "" : ",");
mu_println("{}{}", msg->sexp().to_json_string(), info.last ? "" : ",");
return true;
}
@ -387,34 +385,34 @@ print_attr_xml(const std::string& elm, const std::string& str)
return; /* empty: don't include */
auto&& esc{to_string_opt_gchar(g_markup_escape_text(str.c_str(), -1))};
g_print("\t\t<%s>%s</%s>\n", elm.c_str(), esc.value_or("").c_str(), elm.c_str());
mu_println("\t\t<{}>{}</{}>", elm, esc.value_or(""), elm);
}
static bool
output_xml(const Option<Message>& msg, const OutputInfo& info, const Options& opts, GError** err)
{
if (info.header) {
g_print("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
g_print("<messages>\n");
mu_println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
mu_println("<messages>");
return true;
}
if (info.footer) {
g_print("</messages>\n");
mu_println("</messages>");
return true;
}
g_print("\t<message>\n");
mu_println("\t<message>");
print_attr_xml("from", to_string(msg->from()));
print_attr_xml("to", to_string(msg->to()));
print_attr_xml("cc", to_string(msg->cc()));
print_attr_xml("subject", msg->subject());
g_print("\t\t<date>%u</date>\n", (unsigned)msg->date());
g_print("\t\t<size>%u</size>\n", (unsigned)msg->size());
mu_println("\t\t<date>{}</date>", (unsigned)msg->date());
mu_println("\t\t<size>{}</size>", (unsigned)msg->size());
print_attr_xml("msgid", msg->message_id());
print_attr_xml("path", msg->path());
print_attr_xml("maildir", msg->maildir());
g_print("\t</message>\n");
mu_println("\t</message>");
return true;
}

View File

@ -55,7 +55,7 @@ install_sig_handler(void)
for (i = 0; i != G_N_ELEMENTS(sigs); ++i)
if (sigaction(sigs[i], &action, NULL) != 0)
g_critical("set sigaction for %d failed: %s",
mu_critical("set sigaction for {} failed: {}",
sigs[i], g_strerror(errno));
}
@ -79,7 +79,7 @@ Mu::mu_cmd_index(Store& store, const Options& opts)
{
const auto mdir{store.root_maildir()};
if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0))
return Err(Error::Code::File, "'%s' is not readable: %s",
return Err(Error::Code::File, "'{}' is not readable: {}",
mdir.c_str(), g_strerror(errno));
MaybeAnsi col{!opts.nocolor};

View File

@ -28,11 +28,10 @@ Mu::mu_cmd_remove(Mu::Store& store, const Options& opts)
for (auto&& file: opts.remove.files) {
const auto res = store.remove_message(file);
if (!res)
return Err(Error::Code::File, "failed to remove %s", file.c_str());
return Err(Error::Code::File, "failed to remove {}", file.c_str());
else
g_debug("removed message @ %s", file.c_str());
}
return Ok();
}

View File

@ -36,7 +36,7 @@ Mu::mu_cmd_script(const Options& opts)
if (script_it == scriptinfos.cend())
return Err(Error::Code::InvalidArgument,
"cannot find script '%s'", opts.script.name.c_str());
"cannot find script '{}'", opts.script.name);
std::vector<std::string> params{opts.script.params};
if (!opts.muhome.empty()) {

View File

@ -57,7 +57,7 @@ install_sig_handler(void)
for (i = 0; i != G_N_ELEMENTS(sigs); ++i)
if (sigaction(sigs[i], &action, NULL) != 0)
g_critical("set sigaction for %d failed: %s",
mu_critical("set sigaction for {} failed: {}",
sigs[i], g_strerror(errno));
}
@ -92,7 +92,7 @@ output_sexp_stdout(const Sexp& sexp, Server::OutputFlags flags)
const auto str{sexp.to_string(fopts)};
cookie(str.size() + 1);
if (G_UNLIKELY(::puts(str.c_str()) < 0)) {
g_critical("failed to write output '%s'", str.c_str());
mu_critical("failed to write output '{}'", str);
::raise(SIGTERM); /* terminate ourselves */
}
@ -118,12 +118,11 @@ Mu::mu_cmd_server(const Mu::Options& opts) try {
return Err(store.error());
Server server{*store, output_sexp_stdout};
g_message("created server with store @ %s; maildir @ %s; debug-mode %s;"
"readline: %s",
store->path().c_str(),
store->root_maildir().c_str(),
opts.debug ? "yes" : "no",
have_readline() ? "yes" : "no");
mu_message("created server with store @ {}; maildir @ {}; debug-mode {};"
"readline: {}",
store->path(), store->root_maildir(),
opts.debug ? "yes" : "no",
have_readline() ? "yes" : "no");
tty = ::isatty(::fileno(stdout));
const auto eval = std::string{opts.server.commands ? "(help :full t)" : opts.server.eval};
@ -153,18 +152,17 @@ Mu::mu_cmd_server(const Mu::Options& opts) try {
}
if (MuTerminate != 0)
g_message ("shutting down due to signal %d", MuTerminate.load());
mu_message ("shutting down due to signal {}", MuTerminate.load());
shutdown_readline();
return Ok();
} catch (const Error& er) {
/* note: user-level error, "OK" for mu */
} catch (const Error& er) { /* note: user-level error, "OK" for mu */
report_error(er);
g_warning("server caught exception: %s", er.what());
mu_warning("server caught exception: {}", er.what());
return Ok();
} catch (...) {
g_critical("server caught exception");
mu_critical("server caught exception");
return Err(Error::Code::Internal, "caught exception");
}

View File

@ -34,13 +34,11 @@ key_val(const Mu::MaybeAnsi& col, const std::string& key, T val)
{
using Color = Mu::MaybeAnsi::Color;
std::cout << col.fg(Color::BrightBlue) << std::left << std::setw(18) << key << col.reset()
<< ": ";
std::cout << col.fg(Color::Green) << val << col.reset() << "\n";
mu_println("{}{:<18}{}: {}{}{}",
col.fg(Color::BrightBlue), key, col.reset(),
col.fg(Color::Green), val, col.reset());
}
static void
print_signature(const Mu::MimeSignature& sig, const Options& opts)
{
@ -90,7 +88,7 @@ verify(const MimeMultipartSigned& sigpart, const Options& opts)
if (!sigs || sigs->empty()) {
if (!opts.quiet)
g_print("cannot find signatures in part\n");
mu_println("cannot find signatures in part");
return true;
}
@ -119,7 +117,7 @@ verify_message(const Message& message, const Options& opts, const std::string& n
{
if (none_of(message.flags() & Flags::Signed)) {
if (!opts.quiet)
g_print("%s: no signed parts found\n", name.c_str());
mu_println("{}: no signed parts found", name);
return false;
}
@ -155,7 +153,7 @@ Mu::mu_cmd_verify(const Options& opts)
return Err(message.error());
if (!opts.quiet && opts.verify.files.size() > 1)
g_print("verifying %s\n", file.c_str());
mu_println("verifying {}", file);
if (!verify_message(*message, opts, file))
all_ok = false;

View File

@ -93,11 +93,10 @@ body_or_summary(const Message& message, const Options& opts)
if (!body || body->empty()) {
if (any_of(message.flags() & Flags::Encrypted)) {
color_maybe(MU_COLOR_CYAN);
g_print("[No text body found; "
"message has encrypted parts]\n");
mu_println("[No text body found; message has encrypted parts]");
} else {
color_maybe(MU_COLOR_MAGENTA);
g_print("[No text body found]\n");
mu_println("[No text body found]");
}
color_maybe(MU_COLOR_DEFAULT);
return;
@ -109,7 +108,7 @@ body_or_summary(const Message& message, const Options& opts)
} else {
print_encoded("%s", body->c_str());
if (!g_str_has_suffix(body->c_str(), "\n"))
g_print("\n");
mu_println("");
}
}
@ -148,7 +147,7 @@ handle_msg(const Message& message, const Options& opts)
case Format::Sexp:
return view_msg_sexp(message, opts);
default:
g_critical("bug: should not be reached");
mu_critical("bug: should not be reached");
return Err(Error::Code::Internal, "error");
}
}
@ -166,7 +165,7 @@ Mu::mu_cmd_view(const Options& opts)
return res;
/* add a separator between two messages? */
if (opts.view.terminate)
g_print("%c", VIEW_TERMINATOR);
mu_print("{}", VIEW_TERMINATOR);
}
// no files? read from stding

View File

@ -47,13 +47,14 @@ using namespace Mu;
static Result<void>
cmd_fields(const Options& opts)
{
g_printerr("the 'mu fields' command has been superseded by 'mu info'; try:\n"
mu_printerrln("the 'mu fields' command has been superseded by 'mu info'; try:\n"
" mu info fields\n"
" mu info flags\n");
" mu info flags");
return Ok();
}
static Result<void>
cmd_find(const Options& opts)
{
@ -64,14 +65,15 @@ cmd_find(const Options& opts)
return mu_cmd_find(*store, opts);
}
static void
show_usage(void)
{
g_print("usage: mu command [options] [parameters]\n");
g_print("where command is one of index, find, cfind, view, mkdir, "
"extract, add, remove, script, verify or server\n");
g_print("see the mu, mu-<command> or mu-easy manpages for "
"more information\n");
mu_println("usage: mu command [options] [parameters]");
mu_println("where command is one of index, find, cfind, view, mkdir, "
"extract, add, remove, script, verify or server");
mu_println("see the mu, mu-<command> or mu-easy manpages for "
"more information");
}
@ -157,9 +159,9 @@ Mu::mu_cmd_execute(const Options& opts) try {
} catch (const Mu::Error& er) {
return Err(er);
} catch (const std::runtime_error& re) {
return Err(Error::Code::Internal, "runtime-error: %s", re.what());
return Err(Error::Code::Internal, "runtime-error: {}", re.what());
} catch (const std::exception& ex) {
return Err(Error::Code::Internal, "error: %s", ex.what());
return Err(Error::Code::Internal, "error: {}", ex.what());
} catch (...) {
return Err(Error::Code::Internal, "caught exception");
}

View File

@ -623,7 +623,7 @@ cmd_help(const CLI::App& app, Options& opts)
return show_manpage(opts, "mu-" + opts.help.command);
return Err(Error::Code::Command,
"no help available for '%s'", opts.help.command.c_str());
"no help available for '{}'", opts.help.command);
}
bool
@ -660,7 +660,7 @@ Options::make(int argc, char *argv[])
CLI::App app{"mu mail indexer/searcher", "mu"};
app.description(R"(mu mail indexer/searcher
Copyright (C) 2008-2022 Dirk-Jan C. Binnema
Copyright (C) 2008-2023 Dirk-Jan C. Binnema
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
@ -743,7 +743,7 @@ There is NO WARRANTY, to the extent permitted by law.
} catch (const CLI::CallForVersion&) {
std::cout << "version " << PACKAGE_VERSION << "\n";
} catch (const CLI::ParseError& pe) {
return Err(Error::Code::InvalidArgument, "%s", pe.what());
return Err(Error::Code::InvalidArgument, "{}", pe.what());
} catch (...) {
return Err(Error::Code::Internal, "error parsing arguments");
}

View File

@ -284,7 +284,6 @@ struct Options {
std::string runtime_path(RuntimePath path) const {
return Mu::runtime_path(path, muhome);
}
};
} // namepace Mu

View File

@ -56,7 +56,7 @@ make_database(const std::string& testdir)
MU_PROGRAM, testdir, MU_PROGRAM)};
if (g_test_verbose())
g_printerr("\n%s\n", cmdline.c_str());
mu_printerrln("\n{}", cmdline);
g_assert(g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, NULL));
auto xpath = mu_format("{}{}{}",
@ -106,7 +106,7 @@ run_and_count_matches(const std::string& xpath,
if (g_test_verbose())
g_print("'%s' => %zu\n", expr.c_str(), qres->size());
mu_println("'{}' => {}\n", expr, qres->size());
return qres->size();
}
@ -247,7 +247,7 @@ test_mu_query_accented_chars_01(void)
const auto msg{qres->begin().message()};
if (!msg) {
g_warning("error getting message");
mu_warning("error getting message");
g_assert_not_reached();
}
@ -269,7 +269,7 @@ test_mu_query_accented_chars_02(void)
for (i = 0; i != G_N_ELEMENTS(queries); ++i) {
auto count = run_and_count_matches(DB_PATH1, queries[i].query);
if (count != queries[i].count)
g_warning("query '%s'; expect %zu but got %zu",
mu_warning("query '{}'; expected {} but got {}",
queries[i].query, queries[i].count, count);
g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query),
==,
@ -292,7 +292,7 @@ test_mu_query_accented_chars_fraiche(void)
for (i = 0; i != G_N_ELEMENTS(queries); ++i) {
if (g_test_verbose())
g_print("'%s'\n", queries[i].query);
mu_println("{}", queries[i].query);
g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query),
==,
@ -439,7 +439,7 @@ test_mu_query_attach(void)
for (i = 0; i != G_N_ELEMENTS(queries); ++i) {
if (g_test_verbose())
g_print("query: %s\n", queries[i].query);
mu_println("query: {}", queries[i].query);
g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query),
==,
queries[i].count);
@ -462,7 +462,7 @@ test_mu_query_msgid(void)
for (i = 0; i != G_N_ELEMENTS(queries); ++i) {
if (g_test_verbose())
g_print("query: %s\n", queries[i].query);
mu_println("query: {}", queries[i].query);
g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query),
==,
queries[i].count);