Refactor severity and facility parsing code

This commit is contained in:
Marko Zivanovic
2015-09-03 15:59:06 +02:00
parent a7ee585a9e
commit 2a0d9cfafe
5 changed files with 53 additions and 87 deletions
+6 -52
View File
@@ -23,6 +23,7 @@ SOFTWARE.
*/ */
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <map>
#include "Facility.h" #include "Facility.h"
const std::string Facility::readFromStream(std::istream& src) { const std::string Facility::readFromStream(std::istream& src) {
@@ -38,60 +39,13 @@ const std::string Facility::readFromStream(std::istream& src) {
return ret; return ret;
} }
const Facility::Value Facility::readFromString(const std::string& src) { const uint8_t Facility::readFromString(const std::string& src) {
Facility::Value ret; const auto &value = _values.find(boost::algorithm::to_lower_copy(src));
if (boost::iequals(src, std::string("Kern"))) { if (value != _values.end()) {
ret = Facility::Value::Kern; return value->second;
} else if (boost::iequals(src, std::string("User"))) {
ret = Facility::Value::User;
} else if (boost::iequals(src, std::string("Mail"))) {
ret = Facility::Value::Mail;
} else if (boost::iequals(src, std::string("Daemon"))) {
ret = Facility::Value::Daemon;
} else if (boost::iequals(src, std::string("Auth"))) {
ret = Facility::Value::Auth;
} else if (boost::iequals(src, std::string("Syslog"))) {
ret = Facility::Value::Syslog;
} else if (boost::iequals(src, std::string("Lpr"))) {
ret = Facility::Value::Lpr;
} else if (boost::iequals(src, std::string("News"))) {
ret = Facility::Value::News;
} else if (boost::iequals(src, std::string("Uucp"))) {
ret = Facility::Value::Uucp;
} else if (boost::iequals(src, std::string("Clock"))) {
ret = Facility::Value::Clock;
} else if (boost::iequals(src, std::string("Authpriv"))) {
ret = Facility::Value::Authpriv;
} else if (boost::iequals(src, std::string("Ftp"))) {
ret = Facility::Value::Ftp;
} else if (boost::iequals(src, std::string("Ntp"))) {
ret = Facility::Value::Ntp;
} else if (boost::iequals(src, std::string("LogAudit"))) {
ret = Facility::Value::LogAudit;
} else if (boost::iequals(src, std::string("LogAlert"))) {
ret = Facility::Value::LogAlert;
} else if (boost::iequals(src, std::string("Cron"))) {
ret = Facility::Value::Cron;
} else if (boost::iequals(src, std::string("Local0"))) {
ret = Facility::Value::Local0;
} else if (boost::iequals(src, std::string("Local1"))) {
ret = Facility::Value::Local1;
} else if (boost::iequals(src, std::string("Local2"))) {
ret = Facility::Value::Local2;
} else if (boost::iequals(src, std::string("Local3"))) {
ret = Facility::Value::Local3;
} else if (boost::iequals(src, std::string("Local4"))) {
ret = Facility::Value::Local4;
} else if (boost::iequals(src, std::string("Local5"))) {
ret = Facility::Value::Local5;
} else if (boost::iequals(src, std::string("Local6"))) {
ret = Facility::Value::Local6;
} else if (boost::iequals(src, std::string("Local7"))) {
ret = Facility::Value::Local7;
} else { } else {
throw "Illegal facility value: " + src; throw "Illegal facility value: " + src;
}; }
return ret;
} }
+27 -6
View File
@@ -60,14 +60,35 @@ public:
}; };
private: private:
enum Value { const std::map<std::string, uint8_t> _values{
Kern = 0, User = 1, Mail = 2, Daemon = 3, Auth = 4, Syslog = 5, Lpr = 6, News = 7, Uucp = 8, Clock = 9, {"kern", 0},
Authpriv = 10, Ftp = 11, Ntp = 12, LogAudit = 13, LogAlert = 14, Cron = 15, Local0 = 16, Local1 = 17, {"user", 1},
Local2 = 18, Local3 = 19, Local4 = 20, Local5 = 21, Local6 = 22, Local7 = 23 {"mail", 2},
{"daemon", 3},
{"auth", 4},
{"syslog", 5},
{"lpr", 6},
{"news", 7},
{"uucp", 8},
{"clock", 9},
{"authpriv", 10},
{"ftp", 11},
{"ntp", 12},
{"logaudit", 13},
{"logalert", 14},
{"cron", 15},
{"local0", 16},
{"local1", 17},
{"local2", 18},
{"local3", 19},
{"local4", 20},
{"local5", 21},
{"local6", 22},
{"local7", 23}
}; };
const Value _value; const uint8_t _value;
const std::string readFromStream(std::istream&); const std::string readFromStream(std::istream&);
const Value readFromString(const std::string&); const uint8_t readFromString(const std::string&);
}; };
#endif /* FACILITY_H */ #endif /* FACILITY_H */
+7 -21
View File
@@ -23,6 +23,7 @@ SOFTWARE.
*/ */
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <map>
#include "Severity.h" #include "Severity.h"
const std::string Severity::readFromStream(std::istream& src) { const std::string Severity::readFromStream(std::istream& src) {
@@ -38,28 +39,13 @@ const std::string Severity::readFromStream(std::istream& src) {
return ret; return ret;
} }
const Severity::Value Severity::readFromString(const std::string& src) { const uint8_t Severity::readFromString(const std::string& src) {
Severity::Value ret; const auto &value = _values.find(boost::algorithm::to_lower_copy(src));
if (boost::iequals(src, std::string("Emergency"))) { if (value != _values.end()) {
ret = Severity::Value::Emergency; return value->second;
} else if (boost::iequals(src, std::string("Alert"))) {
ret = Severity::Value::Alert;
} else if (boost::iequals(src, std::string("Critical"))) {
ret = Severity::Value::Critical;
} else if (boost::iequals(src, std::string("Error"))) {
ret = Severity::Value::Error;
} else if (boost::iequals(src, std::string("Warning"))) {
ret = Severity::Value::Warning;
} else if (boost::iequals(src, std::string("Notice"))) {
ret = Severity::Value::Notice;
} else if (boost::iequals(src, std::string("Informational"))) {
ret = Severity::Value::Informational;
} else if (boost::iequals(src, std::string("Debug"))) {
ret = Severity::Value::Debug;
} else { } else {
throw "Illegal severity value: " + src; throw "illegal severity: " + src;
}; }
return ret;
} }
+11 -6
View File
@@ -25,8 +25,6 @@ SOFTWARE.
#ifndef SEVERITY_H #ifndef SEVERITY_H
#define SEVERITY_H #define SEVERITY_H
#include <istream>
class Severity { class Severity {
public: public:
@@ -61,13 +59,20 @@ public:
private: private:
enum Value { const std::map<std::string, uint8_t> _values{
Emergency = 0, Alert = 1, Critical = 2, Error = 3, Warning = 4, Notice = 5, Informational = 6, Debug = 7 {"emergency", 0},
{"alert", 1},
{"critical", 2},
{"error", 3},
{"warning", 4},
{"notice", 5},
{"informational", 6},
{"debug", 7}
}; };
const Value _value; const uint8_t _value;
const std::string readFromStream(std::istream& source); const std::string readFromStream(std::istream& source);
const Value readFromString(const std::string& src); const uint8_t readFromString(const std::string& src);
}; };
#endif /* SEVERITY_H */ #endif /* SEVERITY_H */
+2 -2
View File
@@ -41,12 +41,12 @@ public:
} }
} }
private: private:
std::vector<std::string> _messages = { std::vector<std::string> _messages{
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query", "2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query",
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query", "2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query",
"2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query" "2015-09-02 13:33:11 Local4.Critical 192.168.0.1 Kiwi_Syslog_Server %ASA-2-106007: Deny inbound UDP from 1.2.3.4/22084 to 4.3.2.1/53 due to DNS Query"
}; };
size_t _pos; size_t _pos{};
}; };
class MockWriter : public Writer { class MockWriter : public Writer {