fix: ensure common options (like --log-level) are consistent

This commit is contained in:
Marcus Holland-Moritz 2023-12-18 21:45:33 +01:00
parent 447320b15a
commit ad9bae8618
8 changed files with 35 additions and 16 deletions

View File

@ -73,6 +73,8 @@ class logger {
static level_type parse_level(std::string_view level);
static std::string all_level_names();
private:
std::string policy_name_; // TODO: const?
};

View File

@ -24,9 +24,14 @@
#include <string>
#include <string_view>
#include <boost/program_options.hpp>
namespace dwarfs {
std::string
tool_header(std::string_view tool_name, std::string_view extra_info = "");
void add_common_options(boost::program_options::options_description& opts,
std::string& log_level_str);
} // namespace dwarfs

View File

@ -65,6 +65,10 @@ logger::level_type logger::parse_level(std::string_view level) {
DWARFS_THROW(runtime_error, fmt::format("invalid logger level: {}", level));
}
std::string logger::all_level_names() {
return "error, warn, info, verbose, debug, trace";
}
stream_logger::stream_logger(std::ostream& os, level_type threshold,
bool with_context)
: os_(os)

View File

@ -21,6 +21,7 @@
#include <fmt/format.h>
#include "dwarfs/logger.h"
#include "dwarfs/tool.h"
#include "dwarfs/version.h"
@ -39,4 +40,19 @@ tool_header(std::string_view tool_name, std::string_view extra_info) {
tool_name, PRJ_GIT_ID, extra_info, PRJ_BUILD_ID);
}
void add_common_options(boost::program_options::options_description& opts,
std::string& log_level_str) {
auto log_level_desc = "log level (" + logger::all_level_names() + ")";
// clang-format off
opts.add_options()
("log-level",
boost::program_options::value<std::string>(&log_level_str)
->default_value("info"),
log_level_desc.c_str())
("help,h",
"output help message and exit");
// clang-format on
}
} // namespace dwarfs

View File

@ -969,7 +969,7 @@ void usage(std::filesystem::path const& progname) {
<< " -o readonly show read-only file system\n"
<< " -o (no_)cache_image (don't) keep image in kernel cache\n"
<< " -o (no_)cache_files (don't) keep files in kernel cache\n"
<< " -o debuglevel=NAME error, warn, info, debug, trace\n"
<< " -o debuglevel=NAME " << logger::all_level_names() << "\n"
<< " -o tidy_strategy=NAME (none)|time|swap\n"
<< " -o tidy_interval=TIME interval for cache tidying (5m)\n"
<< " -o tidy_max_age=TIME tidy blocks after this time (10m)\n"

View File

@ -83,11 +83,8 @@ int dwarfsck_main(int argc, sys_char** argv) {
("export-metadata",
po::value<std::string>(&export_metadata),
"export raw metadata as JSON to file")
("log-level",
po::value<std::string>(&log_level)->default_value("info"),
"log level (error, warn, info, debug, trace)")
("help,h",
"output help message and exit");
;
add_common_options(opts, log_level);
// clang-format on
po::positional_options_description pos;

View File

@ -83,16 +83,13 @@ int dwarfsextract_main(int argc, sys_char** argv) {
("cache-size,s",
po::value<std::string>(&cache_size_str)->default_value("512m"),
"block cache size")
("log-level,l",
po::value<std::string>(&log_level)->default_value("warn"),
"log level (error, warn, info, debug, trace)")
#if DWARFS_PERFMON_ENABLED
("perfmon",
po::value<std::string>(&perfmon_str),
"enable performance monitor")
#endif
("help,h",
"output help message and exit");
;
add_common_options(opts, log_level);
// clang-format on
po::variables_map vm;

View File

@ -333,11 +333,9 @@ int mkdwarfs_main(int argc, sys_char** argv) {
("compress-level,l",
po::value<unsigned>(&level)->default_value(default_level),
"compression level (0=fast, 9=best, please see man page for details)")
("log-level",
po::value<std::string>(&log_level_str)->default_value("info"),
"log level (error, warn, info, verbose, debug, trace)")
("help,h",
"output help message and exit")
;
add_common_options(basic_opts, log_level_str);
basic_opts.add_options()
("long-help,H",
"output full help message and exit")
;