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 level_type parse_level(std::string_view level);
static std::string all_level_names();
private: private:
std::string policy_name_; // TODO: const? std::string policy_name_; // TODO: const?
}; };

View File

@ -24,9 +24,14 @@
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <boost/program_options.hpp>
namespace dwarfs { namespace dwarfs {
std::string std::string
tool_header(std::string_view tool_name, std::string_view extra_info = ""); 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 } // 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)); 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, stream_logger::stream_logger(std::ostream& os, level_type threshold,
bool with_context) bool with_context)
: os_(os) : os_(os)

View File

@ -21,6 +21,7 @@
#include <fmt/format.h> #include <fmt/format.h>
#include "dwarfs/logger.h"
#include "dwarfs/tool.h" #include "dwarfs/tool.h"
#include "dwarfs/version.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); 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 } // namespace dwarfs

View File

@ -969,7 +969,7 @@ void usage(std::filesystem::path const& progname) {
<< " -o readonly show read-only file system\n" << " -o readonly show read-only file system\n"
<< " -o (no_)cache_image (don't) keep image in kernel cache\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 (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_strategy=NAME (none)|time|swap\n"
<< " -o tidy_interval=TIME interval for cache tidying (5m)\n" << " -o tidy_interval=TIME interval for cache tidying (5m)\n"
<< " -o tidy_max_age=TIME tidy blocks after this time (10m)\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", ("export-metadata",
po::value<std::string>(&export_metadata), po::value<std::string>(&export_metadata),
"export raw metadata as JSON to file") "export raw metadata as JSON to file")
("log-level", ;
po::value<std::string>(&log_level)->default_value("info"), add_common_options(opts, log_level);
"log level (error, warn, info, debug, trace)")
("help,h",
"output help message and exit");
// clang-format on // clang-format on
po::positional_options_description pos; po::positional_options_description pos;

View File

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

View File

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