From 48c6d34418ec3535286ea6e2eaa26da03af85659 Mon Sep 17 00:00:00 2001 From: John Spray Date: Wed, 12 Jul 2017 08:49:43 -0400 Subject: [PATCH] common: pass up error strings from set_val This is so that we can use it as a replacment for the ugly injectargs. Signed-off-by: John Spray --- src/common/config.cc | 30 +++++++++++++++++++++++------- src/common/config.h | 8 +++++--- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/common/config.cc b/src/common/config.cc index 12f21ea7db6c1..4b720f355bdf5 100644 --- a/src/common/config.cc +++ b/src/common/config.cc @@ -751,13 +751,17 @@ void md_config_t::set_val_or_die(const std::string &key, const char *val) assert(ret == 0); } -int md_config_t::set_val(const std::string &key, const char *val, bool meta) +int md_config_t::set_val(const std::string &key, const char *val, + bool meta, std::stringstream *err_ss) { Mutex::Locker l(lock); - if (key.empty()) + if (key.empty()) { + if (err_ss) *err_ss << "No key specified"; return -EINVAL; - if (!val) + } + if (!val) { return -EINVAL; + } std::string v(val); if (meta) @@ -773,13 +777,17 @@ int md_config_t::set_val(const std::string &key, const char *val, bool meta) int log, gather; int r = sscanf(v.c_str(), "%d/%d", &log, &gather); if (r >= 1) { - if (r < 2) + if (r < 2) { gather = log; - // cout << "subsys " << subsys.get_name(o) << " log " << log << " gather " << gather << std::endl; + } subsys.set_log_level(o, log); subsys.set_gather_level(o, gather); + if (err_ss) *err_ss << "Set " << k << " to " << log << "/" << gather; return 0; } + if (err_ss) { + *err_ss << "Invalid debug level, should be or /"; + } return -EINVAL; } } @@ -793,15 +801,23 @@ int md_config_t::set_val(const std::string &key, const char *val, bool meta) if (observers.find(opt.name) == observers.end()) { // And there is no observer to safely change it... // You lose. + if (err_ss) *err_ss << "Configuration option '" << key << "' may " + "not be modified at runtime"; return -ENOSYS; } } std::string error_message; - return set_val_impl(v, opt, &error_message); + int r = set_val_impl(v, opt, &error_message); + if (r == 0) { + if (err_ss) *err_ss << "Set " << opt.name << " to " << v; + } else { + if (err_ss) *err_ss << error_message; + } + return r; } - // couldn't find a configuration option with key 'key' + if (err_ss) *err_ss << "Configuration option not found: '" << key << "'"; return -ENOENT; } diff --git a/src/common/config.h b/src/common/config.h index cb5a9a4e91c1c..e12228a2b27b8 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -148,9 +148,11 @@ public: // Set a configuration value. // Metavariables will be expanded. - int set_val(const std::string &key, const char *val, bool meta=true); - int set_val(const std::string &key, const string& s, bool meta=true) { - return set_val(key, s.c_str(), meta); + int set_val(const std::string &key, const char *val, bool meta=true, + std::stringstream *err_ss=nullptr); + int set_val(const std::string &key, const string& s, bool meta=true, + std::stringstream *err_ss=nullptr) { + return set_val(key, s.c_str(), meta, err_ss); } // Get a configuration value. -- 2.39.5