diff --git a/src/Facility.cpp b/src/Facility.cpp index 968aa39..b155f5f 100644 --- a/src/Facility.cpp +++ b/src/Facility.cpp @@ -23,6 +23,7 @@ SOFTWARE. */ #include +#include #include "Facility.h" const std::string Facility::readFromStream(std::istream& src) { @@ -38,60 +39,13 @@ const std::string Facility::readFromStream(std::istream& src) { return ret; } -const Facility::Value Facility::readFromString(const std::string& src) { - Facility::Value ret; - if (boost::iequals(src, std::string("Kern"))) { - ret = Facility::Value::Kern; - } 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; +const uint8_t Facility::readFromString(const std::string& src) { + const auto &value = _values.find(boost::algorithm::to_lower_copy(src)); + if (value != _values.end()) { + return value->second; } else { throw "Illegal facility value: " + src; - }; - return ret; + } } diff --git a/src/Facility.h b/src/Facility.h index 651efe1..f0e73cb 100644 --- a/src/Facility.h +++ b/src/Facility.h @@ -60,14 +60,35 @@ public: }; private: - enum Value { - Kern = 0, User = 1, 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 std::map _values{ + {"kern", 0}, + {"user", 1}, + {"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 Value readFromString(const std::string&); + const uint8_t readFromString(const std::string&); }; #endif /* FACILITY_H */ \ No newline at end of file diff --git a/src/Severity.cpp b/src/Severity.cpp index d4057fc..2374cd1 100644 --- a/src/Severity.cpp +++ b/src/Severity.cpp @@ -23,6 +23,7 @@ SOFTWARE. */ #include +#include #include "Severity.h" const std::string Severity::readFromStream(std::istream& src) { @@ -38,28 +39,13 @@ const std::string Severity::readFromStream(std::istream& src) { return ret; } -const Severity::Value Severity::readFromString(const std::string& src) { - Severity::Value ret; - if (boost::iequals(src, std::string("Emergency"))) { - ret = Severity::Value::Emergency; - } 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; +const uint8_t Severity::readFromString(const std::string& src) { + const auto &value = _values.find(boost::algorithm::to_lower_copy(src)); + if (value != _values.end()) { + return value->second; } else { - throw "Illegal severity value: " + src; - }; - return ret; + throw "illegal severity: " + src; + } } diff --git a/src/Severity.h b/src/Severity.h index 44b08cb..1851dad 100644 --- a/src/Severity.h +++ b/src/Severity.h @@ -25,8 +25,6 @@ SOFTWARE. #ifndef SEVERITY_H #define SEVERITY_H -#include - class Severity { public: @@ -61,13 +59,20 @@ public: private: - enum Value { - Emergency = 0, Alert = 1, Critical = 2, Error = 3, Warning = 4, Notice = 5, Informational = 6, Debug = 7 + const std::map _values{ + {"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 Value readFromString(const std::string& src); + const uint8_t readFromString(const std::string& src); }; #endif /* SEVERITY_H */ \ No newline at end of file diff --git a/test/SyslogBulkUploaderTests.cpp b/test/SyslogBulkUploaderTests.cpp index 4e0feaf..8069d4d 100644 --- a/test/SyslogBulkUploaderTests.cpp +++ b/test/SyslogBulkUploaderTests.cpp @@ -41,12 +41,12 @@ public: } } private: - std::vector _messages = { + std::vector _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" }; - size_t _pos; + size_t _pos{}; }; class MockWriter : public Writer {