From f9f29c87a6c15bdcbf9138ee43d58786cee922b1 Mon Sep 17 00:00:00 2001 From: John Spray Date: Tue, 27 Jun 2017 06:28:47 -0400 Subject: [PATCH] mon: helper function for string to log level Signed-off-by: John Spray --- src/common/LogEntry.cc | 25 +++++++++++++++++++++++++ src/common/LogEntry.h | 1 + src/mon/LogMonitor.cc | 29 +++++++---------------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/common/LogEntry.cc b/src/common/LogEntry.cc index 40bdbea2b3a..d0d3e93dd20 100644 --- a/src/common/LogEntry.cc +++ b/src/common/LogEntry.cc @@ -1,3 +1,6 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +// #include #include @@ -36,6 +39,27 @@ void LogEntryKey::generate_test_instances(list& o) o.push_back(new LogEntryKey(entity_inst_t(), utime_t(1,2), 34)); } +clog_type LogEntry::str_to_level(std::string const &str) +{ + std::string level_str = str; + std::transform(level_str.begin(), level_str.end(), level_str.begin(), + [](char c) {return std::tolower(c);}); + + if (level_str == "debug") { + return CLOG_DEBUG; + } else if (level_str == "info") { + return CLOG_INFO; + } else if (level_str == "sec") { + return CLOG_SEC; + } else if (level_str == "warn" | level_str == "warning") { + return CLOG_WARN; + } else if (level_str == "error" | level_str == "err") { + return CLOG_ERROR; + } else { + return CLOG_UNKNOWN; + } +} + // ---- int clog_type_to_syslog_level(clog_type t) @@ -274,3 +298,4 @@ void LogSummary::generate_test_instances(list& o) o.push_back(new LogSummary); // more! } + diff --git a/src/common/LogEntry.h b/src/common/LogEntry.h index 4feea3d419f..a25f9c38150 100644 --- a/src/common/LogEntry.h +++ b/src/common/LogEntry.h @@ -115,6 +115,7 @@ struct LogEntry { void decode(bufferlist::iterator& bl); void dump(Formatter *f) const; static void generate_test_instances(list& o); + static clog_type str_to_level(std::string const &str); }; WRITE_CLASS_ENCODER_FEATURES(LogEntry) diff --git a/src/mon/LogMonitor.cc b/src/mon/LogMonitor.cc index 50ae98cea3b..319196b6c5e 100644 --- a/src/mon/LogMonitor.cc +++ b/src/mon/LogMonitor.cc @@ -407,17 +407,8 @@ bool LogMonitor::preprocess_command(MonOpRequestRef op) std::string level_str; clog_type level; if (cmd_getval(g_ceph_context, cmdmap, "level", level_str)) { - if (level_str == "debug") { - level = CLOG_DEBUG; - } else if (level_str == "info") { - level = CLOG_INFO; - } else if (level_str == "sec") { - level = CLOG_SEC; - } else if (level_str == "warn") { - level = CLOG_WARN; - } else if (level_str == "error") { - level = CLOG_ERROR; - } else { + level = LogEntry::str_to_level(level_str); + if (level == CLOG_UNKNOWN) { ss << "Invalid severity '" << level_str << "'"; mon->reply_command(op, -EINVAL, ss.str(), get_last_committed()); return true; @@ -525,17 +516,11 @@ bool LogMonitor::prepare_command(MonOpRequestRef op) int LogMonitor::sub_name_to_id(const string& n) { - if (n == "log-debug") - return CLOG_DEBUG; - if (n == "log-info") - return CLOG_INFO; - if (n == "log-sec") - return CLOG_SEC; - if (n == "log-warn") - return CLOG_WARN; - if (n == "log-error") - return CLOG_ERROR; - return CLOG_UNKNOWN; + if (n.substr(0, 4) == "log-" && n.size() > 4) { + return LogEntry::str_to_level(n.substr(4)); + } else { + return CLOG_UNKNOWN; + } } void LogMonitor::check_subs() -- 2.39.5