mirror of https://github.com/djcb/mu.git
mu/mu: tweak logging / exception handling
This commit is contained in:
parent
0a12b70d7b
commit
3ee2ce9647
|
@ -7,6 +7,7 @@ otherwise.
|
||||||
2. no matches found. Try a different query
|
2. no matches found. Try a different query
|
||||||
11. database schema mismatch. You need to re-initialize ~mu~, see *mu-init(1)*
|
11. database schema mismatch. You need to re-initialize ~mu~, see *mu-init(1)*
|
||||||
19. failed to acquire lock. Some other program has exclusive access to the mu database
|
19. failed to acquire lock. Some other program has exclusive access to the mu database
|
||||||
|
99. caught an exception
|
||||||
|
|
||||||
# Local Variables:
|
# Local Variables:
|
||||||
# mode: org
|
# mode: org
|
||||||
|
|
46
mu/mu.cc
46
mu/mu.cc
|
@ -40,10 +40,9 @@ output_error(const std::string& what, bool use_color)
|
||||||
using Color = MaybeAnsi::Color;
|
using Color = MaybeAnsi::Color;
|
||||||
MaybeAnsi col{use_color};
|
MaybeAnsi col{use_color};
|
||||||
|
|
||||||
std::cerr << col.fg(Color::Red) << "error" << col.reset() << ": "
|
mu_printerrln("{}error{}: {}{}{}",
|
||||||
<< col.fg(Color::BrightYellow)
|
col.fg(Color::Red), col.reset(),
|
||||||
<< what << col.reset() << "\n";
|
col.fg(Color::BrightYellow), what, col.reset());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -59,36 +58,35 @@ handle_result(const Result<void>& res, const Mu::Options& opts)
|
||||||
if (!res.error().is_soft_error())
|
if (!res.error().is_soft_error())
|
||||||
output_error(res.error().what(), !opts.nocolor);
|
output_error(res.error().what(), !opts.nocolor);
|
||||||
else
|
else
|
||||||
std::cerr << col.fg(Color::BrightBlue) << res.error().what() << '\n';
|
mu_printerrln("{}{}{}",
|
||||||
|
col.fg(Color::BrightBlue), res.error().what(), col.reset());
|
||||||
|
|
||||||
std::cerr << col.fg(Color::Green);
|
mu_printerr("{}", col.fg(Color::Green));
|
||||||
|
|
||||||
// perhaps give some useful hint on how to solve it.
|
// perhaps give some useful hint on how to solve it.
|
||||||
switch (res.error().code()) {
|
switch (res.error().code()) {
|
||||||
case Error::Code::InvalidArgument:
|
case Error::Code::InvalidArgument:
|
||||||
break;
|
break;
|
||||||
case Error::Code::StoreLock:
|
case Error::Code::StoreLock:
|
||||||
std::cerr << "Perhaps mu is already running?\n";
|
mu_printerrln("Perhaps mu is already running?");
|
||||||
break;
|
break;
|
||||||
case Error::Code::SchemaMismatch:
|
case Error::Code::SchemaMismatch:
|
||||||
std::cerr << "Please (re)initialize mu with 'mu init'; "
|
mu_printerrln("Please (re)initialize with 'mu init'; see mu-init(1) for details");
|
||||||
<< "see mu-init(1) for details\n";
|
|
||||||
break;
|
break;
|
||||||
case Error::Code::CannotReinit:
|
case Error::Code::CannotReinit:
|
||||||
std::cerr << "Invoke 'mu init' without '--reinit'; "
|
mu_printerrln("Invoke 'mu init' without '--reinit'; see mu-init(1) for details");
|
||||||
<< "see mu-init(1) for details\n";
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break; /* nothing to do */
|
break; /* nothing to do */
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << col.reset();
|
mu_printerr("{}", col.reset());
|
||||||
|
|
||||||
return res.error().exit_code();
|
return res.error().exit_code();
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char* argv[])
|
main(int argc, char* argv[]) try
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* We handle this through explicit options
|
* We handle this through explicit options
|
||||||
|
@ -112,23 +110,16 @@ main(int argc, char* argv[])
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// setup logging
|
||||||
* there's a subcommand
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* set up logging
|
|
||||||
*/
|
|
||||||
Logger::Options lopts{Logger::Options::None};
|
Logger::Options lopts{Logger::Options::None};
|
||||||
if (opts->log_stderr)
|
if (opts->log_stderr)
|
||||||
lopts |= Logger::Options::StdOutErr;
|
lopts |= Logger::Options::StdOutErr;
|
||||||
if (opts->debug)
|
if (opts->debug)
|
||||||
lopts |= Logger::Options::Debug;
|
lopts |= Logger::Options::Debug;
|
||||||
if (g_getenv("MU_TEST"))
|
if (!!g_getenv("MU_TEST"))
|
||||||
lopts |= Logger::Options::File;
|
lopts |= Logger::Options::File;
|
||||||
|
|
||||||
const auto logger = Logger::make(opts->runtime_path(RuntimePath::LogFile),
|
const auto logger{Logger::make(opts->runtime_path(RuntimePath::LogFile), lopts)};
|
||||||
lopts);
|
|
||||||
if (!logger) {
|
if (!logger) {
|
||||||
output_error(logger.error().what(), !opts->nocolor);
|
output_error(logger.error().what(), !opts->nocolor);
|
||||||
return logger.error().exit_code();
|
return logger.error().exit_code();
|
||||||
|
@ -138,4 +129,13 @@ main(int argc, char* argv[])
|
||||||
* handle sub command
|
* handle sub command
|
||||||
*/
|
*/
|
||||||
return handle_result(mu_cmd_execute(*opts), *opts);
|
return handle_result(mu_cmd_execute(*opts), *opts);
|
||||||
|
|
||||||
|
// exceptions should have been handled earlier, but catch them here,
|
||||||
|
// just in case...
|
||||||
|
} catch (const std::runtime_error& re) {
|
||||||
|
mu_printerrln("caught runtime-error: {}", re.what());
|
||||||
|
return 99;
|
||||||
|
} catch (...) {
|
||||||
|
mu_printerrln("caught exception");
|
||||||
|
return 99;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue