From 1cbe4ca247f309cbce44d8fd3f07ad403f50165a Mon Sep 17 00:00:00 2001 From: Jaroslav Rohel Date: Wed, 28 Feb 2024 12:53:41 +0100 Subject: [PATCH] [dnf5 plugin] config-manager: Write warnings to stderr too Previously, warnings were only logged. --- .../config-manager_plugin/addrepo.cpp | 3 +- dnf5-plugins/config-manager_plugin/setopt.cpp | 3 +- dnf5-plugins/config-manager_plugin/shared.hpp | 9 ++++ .../config-manager_plugin/unsetopt.cpp | 41 ++++++++++++------- .../config-manager_plugin/unsetvar.cpp | 12 ++++-- 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/dnf5-plugins/config-manager_plugin/addrepo.cpp b/dnf5-plugins/config-manager_plugin/addrepo.cpp index a32e5c765d..ddcbb932d0 100644 --- a/dnf5-plugins/config-manager_plugin/addrepo.cpp +++ b/dnf5-plugins/config-manager_plugin/addrepo.cpp @@ -493,7 +493,8 @@ void ConfigManagerAddRepoCommand::test_if_ids_not_already_exist( std::error_code ec; std::filesystem::directory_iterator di(dir, ec); if (ec) { - logger->warning("Cannot read repositories from directory \"{}\": {}", dir.string(), ec.message()); + write_warning( + *logger, M_("Cannot read repositories from directory \"{}\": {}"), dir.string(), ec.message()); continue; } for (auto & dentry : di) { diff --git a/dnf5-plugins/config-manager_plugin/setopt.cpp b/dnf5-plugins/config-manager_plugin/setopt.cpp index 7a117c38a7..0ac63e0ec8 100644 --- a/dnf5-plugins/config-manager_plugin/setopt.cpp +++ b/dnf5-plugins/config-manager_plugin/setopt.cpp @@ -259,7 +259,8 @@ std::set ConfigManagerSetOptCommand::load_existing_repo_ids() const std::error_code ec; std::filesystem::directory_iterator di(dir, ec); if (ec) { - logger->warning("Cannot read repositories from directory \"{}\": {}", dir.string(), ec.message()); + write_warning( + *logger, M_("Cannot read repositories from directory \"{}\": {}"), dir.string(), ec.message()); continue; } for (auto & dentry : di) { diff --git a/dnf5-plugins/config-manager_plugin/shared.hpp b/dnf5-plugins/config-manager_plugin/shared.hpp index 4a7333c55a..c2fa13535d 100644 --- a/dnf5-plugins/config-manager_plugin/shared.hpp +++ b/dnf5-plugins/config-manager_plugin/shared.hpp @@ -23,6 +23,7 @@ along with libdnf. If not, see . #include #include +#include #include #include @@ -37,6 +38,14 @@ struct ConfigManagerError : public libdnf5::Error { const char * get_name() const noexcept override { return "ConfigManagerError"; } }; +// Writes the warning message to the log and to stderr. +// The original message is written to the log and translated version to stderr. +template +void write_warning(libdnf5::Logger & log, BgettextMessage msg, Args &&... args) { + log.warning(b_gettextmsg_get_id(msg), args...); + std::cerr << libdnf5::utils::sformat(TM_(msg, 1), args...) << std::endl; +} + // Checks if the `path` directory exists. If not, according to the `create_missing_dirs` argument, // the directories (missing paths elements) are created or `ConfigManagerError` exception is thrown. // The ConfigManagerError exception is also thrown when the path exists but is not a directory diff --git a/dnf5-plugins/config-manager_plugin/unsetopt.cpp b/dnf5-plugins/config-manager_plugin/unsetopt.cpp index fd0d9e5ecb..85f42dba82 100644 --- a/dnf5-plugins/config-manager_plugin/unsetopt.cpp +++ b/dnf5-plugins/config-manager_plugin/unsetopt.cpp @@ -88,8 +88,10 @@ void ConfigManagerUnsetOptCommand::set_argument_parser() { try { tmp_repo_conf.opt_binds().at(repo_key); } catch (const OptionBindsOptionNotFoundError & ex) { - ctx.base.get_logger()->warning( - "config-manager: Request to remove unknown repository option from config file: {}", key); + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove unknown repository option from config file: {}"), + key); } in_repos_opts_to_remove[repo_id].insert(repo_key); @@ -98,8 +100,10 @@ void ConfigManagerUnsetOptCommand::set_argument_parser() { try { tmp_config.opt_binds().at(key); } catch (const OptionBindsOptionNotFoundError & ex) { - ctx.base.get_logger()->warning( - "config-manager: Request to remove unknown main option from config file: {}", key); + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove unknown main option from config file: {}"), + key); } // Save the global option for later removing from the file. @@ -131,8 +135,10 @@ void ConfigManagerUnsetOptCommand::configure() { // Generate warning for unused options for (const auto & key : main_opts_to_remove) { if (!used_keys.contains(key)) { - ctx.base.get_logger()->warning( - "config-manager: Request to remove main option but it is not present in the config file: {}", + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove main option but it is not present in the config file: " + "{}"), key); } } @@ -141,8 +147,10 @@ void ConfigManagerUnsetOptCommand::configure() { parser.write(cfg_filepath, false); } } else { - ctx.base.get_logger()->warning( - "config-manager: Request to remove main option but config file not found: {}", cfg_filepath.string()); + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove main option but config file not found: {}"), + cfg_filepath.string()); } } @@ -173,15 +181,17 @@ void ConfigManagerUnsetOptCommand::configure() { for (const auto & [in_repoid, keys] : in_repos_opts_to_remove) { if (const auto used_repoid_opts = used_repos_opts.find(in_repoid); used_repoid_opts == used_repos_opts.end()) { - ctx.base.get_logger()->warning( - "config-manager: Request to remove repository option but repoid not found in overrides: {}", + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove repository option but repoid not found in overrides: {}"), in_repoid); } else { for (const auto & key : keys) { if (!used_repoid_opts->second.contains(key)) - ctx.base.get_logger()->warning( - "config-manager: Request to remove repository option but it is not present " - "in the overrides: {}.{}", + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove repository option but it is not present " + "in the overrides: {}.{}"), in_repoid, key); } @@ -198,8 +208,9 @@ void ConfigManagerUnsetOptCommand::configure() { parser.write(repos_override_file_path, false); } } else { - ctx.base.get_logger()->warning( - "config-manager: Request to remove repository option but config file not found: {}", + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove repository option but config file not found: {}"), repos_override_file_path.string()); } } diff --git a/dnf5-plugins/config-manager_plugin/unsetvar.cpp b/dnf5-plugins/config-manager_plugin/unsetvar.cpp index a2cab1546f..d18d29a902 100644 --- a/dnf5-plugins/config-manager_plugin/unsetvar.cpp +++ b/dnf5-plugins/config-manager_plugin/unsetvar.cpp @@ -64,8 +64,10 @@ void ConfigManagerUnsetVarCommand::configure() { } if (!std::filesystem::exists(vars_dir)) { - ctx.base.get_logger()->warning( - "config-manager: Request to remove variable but vars directory not found: {}", vars_dir.string()); + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove variable but vars directory not found: {}"), + vars_dir.string()); return; } @@ -75,8 +77,10 @@ void ConfigManagerUnsetVarCommand::configure() { if (std::filesystem::exists(filepath)) { std::filesystem::remove(filepath); } else { - ctx.base.get_logger()->warning( - "config-manager: Request to remove not present variable in vars directory: {}", name); + write_warning( + *ctx.base.get_logger(), + M_("config-manager: Request to remove not present variable in vars directory: {}"), + name); } } catch (const std::filesystem::filesystem_error & e) { throw ConfigManagerError(