Skip to content

Commit

Permalink
[dnf5 plugin] config-manager: Write warnings to stderr too
Browse files Browse the repository at this point in the history
Previously, warnings were only logged.
  • Loading branch information
jrohel committed Feb 28, 2024
1 parent 822ca5e commit 1cbe4ca
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
3 changes: 2 additions & 1 deletion dnf5-plugins/config-manager_plugin/addrepo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion dnf5-plugins/config-manager_plugin/setopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ std::set<std::string> 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) {
Expand Down
9 changes: 9 additions & 0 deletions dnf5-plugins/config-manager_plugin/shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include <libdnf5/conf/config_main.hpp>
#include <libdnf5/conf/const.hpp>
#include <libdnf5/utils/bgettext/bgettext-lib.h>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>

#include <filesystem>
Expand All @@ -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 <typename... Args>
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
Expand Down
41 changes: 26 additions & 15 deletions dnf5-plugins/config-manager_plugin/unsetopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
}
}
Expand Down
12 changes: 8 additions & 4 deletions dnf5-plugins/config-manager_plugin/unsetvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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(
Expand Down

0 comments on commit 1cbe4ca

Please sign in to comment.