Implement RFC3164 formatting for syslog message; Implement UDP writer.
This commit is contained in:
+2
-1
@@ -1,10 +1,11 @@
|
|||||||
cmake_minimum_required(VERSION 2.8)
|
cmake_minimum_required(VERSION 2.8)
|
||||||
project(syslog-bulk-uploader)
|
project(syslog-bulk-uploader)
|
||||||
set(CMAKE_CXX_FLAGS "-g -Wall")
|
set(CMAKE_CXX_FLAGS "-g -Wall -Werror")
|
||||||
add_definitions(-std=c++11)
|
add_definitions(-std=c++11)
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
enable_testing()
|
enable_testing()
|
||||||
add_test(NAME SyslogBulkUploaderTests COMMAND SyslogBulkUploaderTests)
|
add_test(NAME SyslogBulkUploaderTests COMMAND SyslogBulkUploaderTests)
|
||||||
add_test(NAME SyslogMessageTests COMMAND SyslogMessageTests)
|
add_test(NAME SyslogMessageTests COMMAND SyslogMessageTests)
|
||||||
|
add_test(NAME RFC3164FormattedSyslogMessageTests COMMAND RFC3164FormattedSyslogMessageTests)
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ add_library(slbu-lib
|
|||||||
Facility.cpp
|
Facility.cpp
|
||||||
Severity.cpp
|
Severity.cpp
|
||||||
SyslogMessage.cpp
|
SyslogMessage.cpp
|
||||||
|
UDPWriter.cpp
|
||||||
|
RFC3164FormattedSyslogMessage
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(syslog-bulk-uploader main.cpp)
|
add_executable(syslog-bulk-uploader main.cpp)
|
||||||
|
|||||||
+7
-2
@@ -42,6 +42,9 @@ public:
|
|||||||
Facility(const Facility & orig) : _value(orig._value) {
|
Facility(const Facility & orig) : _value(orig._value) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
virtual ~Facility() {
|
||||||
|
};
|
||||||
|
|
||||||
bool operator!=(const Facility & right) const {
|
bool operator!=(const Facility & right) const {
|
||||||
bool result = !(*this == right); // Reuse equals operator
|
bool result = !(*this == right); // Reuse equals operator
|
||||||
return result;
|
return result;
|
||||||
@@ -56,8 +59,10 @@ public:
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~Facility() {
|
uint8_t as_int() const {
|
||||||
};
|
return _value;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
const std::map<std::string, uint8_t> _values{
|
const std::map<std::string, uint8_t> _values{
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Marko Živanović
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <boost/date_time.hpp>
|
||||||
|
#include "RFC3164FormattedSyslogMessage.h"
|
||||||
|
|
||||||
|
using namespace boost::posix_time;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
static time_facet* f = new time_facet("%b %e %H:%M:%S");
|
||||||
|
static stringstream ss;
|
||||||
|
|
||||||
|
std::string RFC3164FormattedSyslogMessage::operator()() {
|
||||||
|
ss.str("");
|
||||||
|
ss.imbue(locale(locale::classic(), f));
|
||||||
|
|
||||||
|
ss << "<" << to_string(_message.priority()) << ">";
|
||||||
|
ss << _message.timestamp() << " ";
|
||||||
|
ss << _message.source() << " ";
|
||||||
|
ss << _message.message();
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Marko Živanović
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RFC3164FORMATTEDSYSLOGMESSAGE_H
|
||||||
|
#define RFC3164FORMATTEDSYSLOGMESSAGE_H
|
||||||
|
|
||||||
|
#include "SyslogMessage.h"
|
||||||
|
|
||||||
|
class RFC3164FormattedSyslogMessage {
|
||||||
|
public:
|
||||||
|
|
||||||
|
RFC3164FormattedSyslogMessage(const SyslogMessage& msg) : _message(msg) {
|
||||||
|
};
|
||||||
|
|
||||||
|
RFC3164FormattedSyslogMessage(const RFC3164FormattedSyslogMessage& orig) : _message(orig._message) {
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual ~RFC3164FormattedSyslogMessage() {
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string operator()();
|
||||||
|
|
||||||
|
private:
|
||||||
|
const SyslogMessage& _message;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* RFC3164FORMATTEDSYSLOGMESSAGE_H */
|
||||||
|
|
||||||
+7
-3
@@ -40,6 +40,9 @@ public:
|
|||||||
Severity(const Severity& orig) : _value(orig._value) {
|
Severity(const Severity& orig) : _value(orig._value) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
virtual ~Severity() {
|
||||||
|
};
|
||||||
|
|
||||||
bool operator!=(const Severity& right) const {
|
bool operator!=(const Severity& right) const {
|
||||||
bool result = !(*this == right); // Reuse equals operator
|
bool result = !(*this == right); // Reuse equals operator
|
||||||
return result;
|
return result;
|
||||||
@@ -49,14 +52,15 @@ public:
|
|||||||
return _value == right._value;
|
return _value == right._value;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~Severity() {
|
|
||||||
};
|
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& os, const Severity& obj) {
|
friend std::ostream& operator<<(std::ostream& os, const Severity& obj) {
|
||||||
os << obj._value;
|
os << obj._value;
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t as_int() const {
|
||||||
|
return _value;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
const std::map<std::string, uint8_t> _values{
|
const std::map<std::string, uint8_t> _values{
|
||||||
|
|||||||
@@ -87,3 +87,4 @@ const std::string SyslogMessage::readMessage(std::istream & src) {
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,10 @@ public:
|
|||||||
return _timestamp;
|
return _timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const uint8_t priority() const {
|
||||||
|
return (_facility.as_int() * 8) +_severity.as_int();
|
||||||
|
};
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& os, const SyslogMessage& obj) {
|
friend std::ostream& operator<<(std::ostream& os, const SyslogMessage& obj) {
|
||||||
os << "SyslogMessage{timestamp:" << obj._timestamp << ", facility:" << obj._facility <<
|
os << "SyslogMessage{timestamp:" << obj._timestamp << ", facility:" << obj._facility <<
|
||||||
", severity:" << obj._severity << ",source:" << obj._source << ",message:" << obj._message << "}";
|
", severity:" << obj._severity << ",source:" << obj._source << ",message:" << obj._message << "}";
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Marko Živanović
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "UDPWriter.h"
|
||||||
|
#include "RFC3164FormattedSyslogMessage.h"
|
||||||
|
|
||||||
|
using namespace boost::asio::ip;
|
||||||
|
|
||||||
|
UDPWriter::UDPWriter(const std::string& destination, const int port) : _destination(destination), _port(port) {
|
||||||
|
udp::resolver resolver(_ios);
|
||||||
|
udp::resolver::query query(_destination, std::to_string(_port));
|
||||||
|
udp::resolver::iterator it = resolver.resolve(query);
|
||||||
|
if (it != udp::resolver::iterator()) {
|
||||||
|
auto endpoint = *it;
|
||||||
|
_socket.connect(endpoint);
|
||||||
|
} else {
|
||||||
|
throw "Destination not found: " + destination;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void UDPWriter::sendMessage(std::shared_ptr<SyslogMessage> message) {
|
||||||
|
if (_socket.is_open()) {
|
||||||
|
auto formatted = RFC3164FormattedSyslogMessage{*message};
|
||||||
|
auto buff = ::boost::asio::buffer(formatted());
|
||||||
|
_socket.send(buff);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Marko Živanović
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef UDPWRITER_H
|
||||||
|
#define UDPWRITER_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include "Writer.h"
|
||||||
|
|
||||||
|
class UDPWriter : public Writer {
|
||||||
|
public:
|
||||||
|
|
||||||
|
UDPWriter(const std::string&, const int);
|
||||||
|
|
||||||
|
virtual void sendMessage(std::shared_ptr<SyslogMessage>);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const std::string& _destination;
|
||||||
|
const int _port;
|
||||||
|
boost::asio::io_service _ios;
|
||||||
|
boost::asio::ip::udp::socket _socket{_ios};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* UDPWRITER_H */
|
||||||
|
|
||||||
@@ -26,6 +26,7 @@ SOFTWARE.
|
|||||||
#ifndef WRITER_H
|
#ifndef WRITER_H
|
||||||
#define WRITER_H
|
#define WRITER_H
|
||||||
|
|
||||||
|
#include <boost/noncopyable.hpp>
|
||||||
#include "SyslogMessage.h"
|
#include "SyslogMessage.h"
|
||||||
|
|
||||||
class Writer : private boost::noncopyable {
|
class Writer : private boost::noncopyable {
|
||||||
|
|||||||
@@ -19,3 +19,10 @@ target_link_libraries(SyslogMessageTests
|
|||||||
${Boost_DATE_TIME_LIBRARY}
|
${Boost_DATE_TIME_LIBRARY}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(RFC3164FormattedSyslogMessageTests RFC3164FormattedSyslogMessageTests.cpp)
|
||||||
|
target_link_libraries(RFC3164FormattedSyslogMessageTests
|
||||||
|
slbu-lib
|
||||||
|
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
|
||||||
|
${Boost_DATE_TIME_LIBRARY}
|
||||||
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015 Marko Živanović
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define BOOST_TEST_MODULE RFC3164FormattedSyslogMessageTests
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include "../src/SyslogMessage.h"
|
||||||
|
#include "../src/RFC3164FormattedSyslogMessage.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(format) {
|
||||||
|
stringstream ss("2015-09-02 13:33:11 Local4.Critical 192.168.0.1 test message");
|
||||||
|
SyslogMessage m(ss);
|
||||||
|
RFC3164FormattedSyslogMessage f(m);
|
||||||
|
string s = f();
|
||||||
|
BOOST_CHECK_EQUAL(s, "<162>Sep 2 13:33:11 192.168.0.1 test message");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user