mon: LogMonitor: handle boolean options consistently

'mon_cluster_log_to_syslog' gets a string of key/value pairs, values
in the pair being booleans, and keys being optional (one can simply
specify the value). However, we weren't being consistent with the
boolean behavior when handling option values. e.g., the user expects
'1' and '0' to mean 'true' and 'false' respectively, and expects
'mon_cluster_log_to_syslog' to understand both '1' and '0', alongside
with 'true' and 'false'.

All values not 'true' or '1' will be considered 'false'.

Fixes: #12325

Signed-off-by: Joao Eduardo Luis <joao@suse.de>
This commit is contained in:
Joao Eduardo Luis 2015-07-27 21:17:31 +01:00
parent b6cc488d56
commit cfcacb89a1
2 changed files with 31 additions and 4 deletions

View File

@ -12,6 +12,8 @@
*
*/
#include <boost/algorithm/string/predicate.hpp>
#include <sstream>
#include <syslog.h>
@ -28,6 +30,7 @@
#include "osd/osd_types.h"
#include "common/errno.h"
#include "common/config.h"
#include "common/strtol.h"
#include "include/assert.h"
#include "include/str_list.h"
#include "include/str_map.h"
@ -673,6 +676,33 @@ string LogMonitor::log_channel_info::expand_channel_meta(
return s;
}
bool LogMonitor::log_channel_info::do_log_to_syslog(const string &channel) {
string v = get_str_map_key(log_to_syslog, channel,
&CLOG_CONFIG_DEFAULT_KEY);
// We expect booleans, but they are in k/v pairs, kept
// as strings, in 'log_to_syslog'. We must ensure
// compatibility with existing boolean handling, and so
// we are here using a modified version of how
// md_config_t::set_val_raw() handles booleans. We will
// accept both 'true' and 'false', but will also check for
// '1' and '0'. The main distiction between this and the
// original code is that we will assume everything not '1',
// '0', 'true' or 'false' to be 'false'.
bool ret = false;
if (boost::iequals(v, "false")) {
ret = false;
} else if (boost::iequals(v, "true")) {
ret = true;
} else {
std::string err;
int b = strict_strtol(v.c_str(), 10, &err);
ret = (err.empty() && b == 1);
}
return ret;
}
void LogMonitor::handle_conf_change(const struct md_config_t *conf,
const std::set<std::string> &changed)
{

View File

@ -69,10 +69,7 @@ private:
string expand_channel_meta(const string &input,
const string &change_to);
bool do_log_to_syslog(const string &channel) {
return (get_str_map_key(log_to_syslog, channel,
&CLOG_CONFIG_DEFAULT_KEY) == "true");
}
bool do_log_to_syslog(const string &channel);
string get_facility(const string &channel) {
return get_str_map_key(syslog_facility, channel,