lib: remove mu-runtime
Remove runtime; update the logger for that, and move the runtime-paths stuff to utils.
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
** Copyright (C) 2020-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or modify it
|
||||
** under the terms of the GNU General Public License as published by the
|
||||
@ -32,10 +32,10 @@
|
||||
|
||||
using namespace Mu;
|
||||
|
||||
static bool MuLogInitialized = false;
|
||||
static Mu::LogOptions MuLogOptions;
|
||||
static std::ofstream MuStream;
|
||||
static auto MaxLogFileSize = 1000 * 1024;
|
||||
static bool MuLogInitialized = false;
|
||||
static Mu::Logger::Options MuLogOptions;
|
||||
static std::ofstream MuStream;
|
||||
static auto MaxLogFileSize = 1000 * 1024;
|
||||
|
||||
static std::string MuLogPath;
|
||||
|
||||
@ -114,16 +114,21 @@ log_journal(GLogLevelFlags level, const GLogField* fields, gsize n_fields, gpoin
|
||||
return g_log_writer_journald(level, fields, n_fields, user_data);
|
||||
}
|
||||
|
||||
void
|
||||
Mu::log_init(const std::string& path, Mu::LogOptions opts)
|
||||
{
|
||||
if (MuLogInitialized) {
|
||||
g_error("logging is already initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
Result<Logger>
|
||||
Mu::Logger::make(const std::string& path, Mu::Logger::Options opts)
|
||||
{
|
||||
if (MuLogInitialized)
|
||||
return Err(Error::Code::Internal, "logging already initialized");
|
||||
|
||||
return Ok(Logger(path, opts));
|
||||
}
|
||||
|
||||
|
||||
Mu::Logger::Logger(const std::string& path, Mu::Logger::Options opts)
|
||||
{
|
||||
if (g_getenv("MU_LOG_STDOUTERR"))
|
||||
opts |= LogOptions::StdOutErr;
|
||||
opts |= Logger::Options::StdOutErr;
|
||||
|
||||
MuLogOptions = opts;
|
||||
MuLogPath = path;
|
||||
@ -132,12 +137,12 @@ Mu::log_init(const std::string& path, Mu::LogOptions opts)
|
||||
[](GLogLevelFlags level, const GLogField* fields, gsize n_fields, gpointer user_data) {
|
||||
// filter out debug-level messages?
|
||||
if (level == G_LOG_LEVEL_DEBUG &&
|
||||
(none_of(MuLogOptions & Mu::LogOptions::Debug)))
|
||||
(none_of(MuLogOptions & Options::Debug)))
|
||||
return G_LOG_WRITER_HANDLED;
|
||||
|
||||
// log criticals to stdout / err or if asked
|
||||
if (level == G_LOG_LEVEL_CRITICAL ||
|
||||
any_of(MuLogOptions & Mu::LogOptions::StdOutErr)) {
|
||||
any_of(MuLogOptions & Options::StdOutErr)) {
|
||||
log_stdouterr(level, fields, n_fields, user_data);
|
||||
}
|
||||
|
||||
@ -151,14 +156,13 @@ Mu::log_init(const std::string& path, Mu::LogOptions opts)
|
||||
NULL);
|
||||
|
||||
g_message("logging initialized; debug: %s, stdout/stderr: %s",
|
||||
any_of(log_get_options() & LogOptions::Debug) ? "yes" : "no",
|
||||
any_of(log_get_options() & LogOptions::StdOutErr) ? "yes" : "no");
|
||||
any_of(opts & Options::Debug) ? "yes" : "no",
|
||||
any_of(opts & Options::StdOutErr) ? "yes" : "no");
|
||||
|
||||
MuLogInitialized = true;
|
||||
}
|
||||
|
||||
void
|
||||
Mu::log_uninit()
|
||||
Logger::~Logger()
|
||||
{
|
||||
if (!MuLogInitialized)
|
||||
return;
|
||||
@ -168,15 +172,3 @@ Mu::log_uninit()
|
||||
|
||||
MuLogInitialized = false;
|
||||
}
|
||||
|
||||
void
|
||||
Mu::log_set_options(Mu::LogOptions opts)
|
||||
{
|
||||
MuLogOptions = opts;
|
||||
}
|
||||
|
||||
Mu::LogOptions
|
||||
Mu::log_get_options()
|
||||
{
|
||||
return MuLogOptions;
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
** Copyright (C) 2020-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or modify it
|
||||
** under the terms of the GNU General Public License as published by the
|
||||
@ -21,54 +21,54 @@
|
||||
#define MU_LOGGER_HH__
|
||||
|
||||
#include <string>
|
||||
#include "utils/mu-utils.hh"
|
||||
#include <utils/mu-utils.hh>
|
||||
#include <utils/mu-result.hh>
|
||||
|
||||
namespace Mu {
|
||||
|
||||
/**
|
||||
* Logging options
|
||||
* RAII object for handling logging (through g_(debug|warning|...))
|
||||
*
|
||||
*/
|
||||
enum struct LogOptions {
|
||||
None = 0, /**< Nothing specific */
|
||||
StdOutErr = 1 << 1, /**< Log to stdout/stderr */
|
||||
Debug = 1 << 2, /**< Include debug-level logs */
|
||||
struct Logger {
|
||||
|
||||
/**
|
||||
* Logging options
|
||||
*
|
||||
*/
|
||||
enum struct Options {
|
||||
None = 0, /**< Nothing specific */
|
||||
StdOutErr = 1 << 1, /**< Log to stdout/stderr */
|
||||
Debug = 1 << 2, /**< Include debug-level logs */
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the logging system.
|
||||
*
|
||||
* Note that the path is only used if structured logging fails --
|
||||
* practically, it goes to the file if there's no systemd/journald.
|
||||
*
|
||||
* if the environment variable MU_LOG_STDOUTERR is set,
|
||||
* LogOptions::StdoutErr is implied.
|
||||
*
|
||||
* @param path path to the log file
|
||||
* @param opts logging options
|
||||
*/
|
||||
static Result<Logger> make(const std::string& path, Options opts=Options::None);
|
||||
|
||||
/**
|
||||
* DTOR
|
||||
*
|
||||
*/
|
||||
~Logger();
|
||||
|
||||
private:
|
||||
Logger(const std::string& path, Options opts);
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the logging system. Note that the path is only used if structured
|
||||
* logging fails -- practically, it goes to the file if there's
|
||||
* systemd/journald.
|
||||
*
|
||||
* if the environment variable MU_LOG_STDOUTERR is set, LogOptions::StdoutErr is
|
||||
* implied.
|
||||
*
|
||||
* @param path path to the log file
|
||||
* @param opts logging options
|
||||
*/
|
||||
void log_init(const std::string& path, LogOptions opts);
|
||||
|
||||
/**
|
||||
* Uninitialize the logging system
|
||||
*
|
||||
*/
|
||||
void log_uninit();
|
||||
|
||||
/**
|
||||
* Change the logging options.
|
||||
*
|
||||
* @param opts options
|
||||
*/
|
||||
void log_set_options(LogOptions opts);
|
||||
|
||||
/**
|
||||
* Get the current log options
|
||||
*
|
||||
* @return the log options
|
||||
*/
|
||||
LogOptions log_get_options();
|
||||
MU_ENABLE_BITOPS(Logger::Options);
|
||||
|
||||
} // namespace Mu
|
||||
MU_ENABLE_BITOPS(Mu::LogOptions);
|
||||
|
||||
#endif /* MU_LOGGER_HH__ */
|
||||
|
||||
@ -638,3 +638,36 @@ Mu::timezone_available(const std::string& tz)
|
||||
|
||||
return have_tz;
|
||||
}
|
||||
|
||||
|
||||
std::string
|
||||
Mu::runtime_path(Mu::RuntimePath path, const std::string& muhome)
|
||||
{
|
||||
auto [mu_cache, mu_config] =
|
||||
std::invoke([&]()->std::pair<std::string, std::string> {
|
||||
|
||||
static std::string mu{"/mu"};
|
||||
if (muhome.empty())
|
||||
return { g_get_user_cache_dir() + mu,
|
||||
g_get_user_config_dir() + mu };
|
||||
else
|
||||
return { muhome, muhome };
|
||||
});
|
||||
|
||||
switch (path) {
|
||||
case Mu::RuntimePath::Cache:
|
||||
return mu_cache;
|
||||
case Mu::RuntimePath::XapianDb:
|
||||
return mu_cache + "/xapian";
|
||||
case Mu::RuntimePath::LogFile:
|
||||
return mu_cache + "/mu.log";
|
||||
case Mu::RuntimePath::Bookmarks:
|
||||
return mu_config + "/bookmarks";
|
||||
case Mu::RuntimePath::Config:
|
||||
return mu_config;
|
||||
case Mu::RuntimePath::Scripts:
|
||||
return mu_config + "/scripts";
|
||||
default:
|
||||
throw std::logic_error("unknown path");
|
||||
}
|
||||
}
|
||||
|
||||
@ -175,6 +175,31 @@ bool timezone_available(const std::string& tz);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Well-known runtime paths
|
||||
*
|
||||
*/
|
||||
enum struct RuntimePath {
|
||||
XapianDb,
|
||||
Cache,
|
||||
LogFile,
|
||||
Config,
|
||||
Scripts,
|
||||
Bookmarks
|
||||
};
|
||||
|
||||
/**
|
||||
* Get some well-known Path for internal use when don't have
|
||||
* access to the command-line
|
||||
*
|
||||
* @param path the RuntimePath to find
|
||||
* @param muhome path to muhome directory, or empty for the default.
|
||||
*
|
||||
* @return the path name
|
||||
*/
|
||||
std::string runtime_path(RuntimePath path, const std::string& muhome="");
|
||||
|
||||
|
||||
// https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member
|
||||
template <auto fn>
|
||||
struct deleter_from_fn {
|
||||
@ -436,7 +461,7 @@ to_second(const P& p, typename P::value_type::first_type f)
|
||||
|
||||
|
||||
/**
|
||||
* Convert string view in something printable with %*s
|
||||
* Convert string view in something printable with %.*s
|
||||
*/
|
||||
#define STR_V(sv__) static_cast<int>((sv__).size()), (sv__).data()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user