]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mon: helper function for string to log level
authorJohn Spray <john.spray@redhat.com>
Tue, 27 Jun 2017 10:28:47 +0000 (06:28 -0400)
committerJohn Spray <john.spray@redhat.com>
Tue, 27 Jun 2017 10:28:47 +0000 (06:28 -0400)
Signed-off-by: John Spray <john.spray@redhat.com>
src/common/LogEntry.cc
src/common/LogEntry.h
src/mon/LogMonitor.cc

index 40bdbea2b3ac72e929d6515ecabb684d0bf66b86..d0d3e93dd2049e7217487fff61c2ae1b94c8256f 100644 (file)
@@ -1,3 +1,6 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+//
 #include <syslog.h>
 #include <boost/algorithm/string/predicate.hpp>
 
@@ -36,6 +39,27 @@ void LogEntryKey::generate_test_instances(list<LogEntryKey*>& 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<LogSummary*>& o)
   o.push_back(new LogSummary);
   // more!
 }
+
index 4feea3d419fc7c15d8752cf77d71df1f020154d5..a25f9c38150d7a8fde1ca84449d94f2bf5681262 100644 (file)
@@ -115,6 +115,7 @@ struct LogEntry {
   void decode(bufferlist::iterator& bl);
   void dump(Formatter *f) const;
   static void generate_test_instances(list<LogEntry*>& o);
+  static clog_type str_to_level(std::string const &str);
 };
 WRITE_CLASS_ENCODER_FEATURES(LogEntry)
 
index 50ae98cea3b1fcc4690f0f25c5323e8ced03ed54..319196b6c5eaad837ae19605165c7d0ec7965e3c 100644 (file)
@@ -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()