diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f5ca5ab..4b448c9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,7 +9,6 @@ add_library(slbu-lib Severity.cpp SyslogMessage.cpp UDPWriter.cpp - RFC3164FormattedSyslogMessage ) add_executable(syslog-bulk-uploader main.cpp) diff --git a/src/RFC3164FormattedSyslogMessage.cpp b/src/RFC3164FormattedSyslogMessage.cpp deleted file mode 100644 index 267a5df..0000000 --- a/src/RFC3164FormattedSyslogMessage.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - 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 -#include "RFC3164FormattedSyslogMessage.h" - -using namespace boost::posix_time; -using namespace std; - -static const time_facet* f = new time_facet("%b %e %H:%M:%S"); - -std::string RFC3164FormattedSyslogMessage::operator()() { - stringstream ss; - ss.imbue(locale(locale::classic(), f)); - - ss << "<" << to_string(_message.priority()) << ">"; - ss << _message.timestamp() << " "; - ss << _message.source() << " "; - ss << _message.message(); - - string ret = ss.str(); - if (ret.size() > MAX_LEN) { - ret = ret.substr(0, MAX_LEN); - } - return ret; -} diff --git a/src/RFC3164FormattedSyslogMessage.h b/src/RFC3164FormattedSyslogMessage.h index 3ced3af..ae94f53 100644 --- a/src/RFC3164FormattedSyslogMessage.h +++ b/src/RFC3164FormattedSyslogMessage.h @@ -31,19 +31,33 @@ class RFC3164FormattedSyslogMessage { public: RFC3164FormattedSyslogMessage(const SyslogMessage& msg) : _message(msg) { + // locale takes over ownership of facet, so, no need to keep track of it + _stream.imbue(std::locale(std::locale::classic(), new boost::posix_time::time_facet("%b %e %H:%M:%S"))); }; - RFC3164FormattedSyslogMessage(const RFC3164FormattedSyslogMessage& orig) : _message(orig._message) { + RFC3164FormattedSyslogMessage(const RFC3164FormattedSyslogMessage& orig) : RFC3164FormattedSyslogMessage(orig._message) { }; virtual ~RFC3164FormattedSyslogMessage() { }; - std::string operator()(); + std::string operator()() { + _stream.str(""); + _stream << "<" << std::to_string(_message.priority()) << ">"; + _stream << _message.timestamp() << " "; + _stream << _message.source() << " "; + _stream << _message.message(); + std::string ret = _stream.str(); + if (ret.size() > MAX_LEN) { + ret = ret.substr(0, MAX_LEN); + } + return ret; + }; private: static const size_t MAX_LEN = 1024; const SyslogMessage& _message; + std::stringstream _stream; }; #endif /* RFC3164FORMATTEDSYSLOGMESSAGE_H */ diff --git a/src/UDPWriter.cpp b/src/UDPWriter.cpp index 3d6aa9c..28d304f 100644 --- a/src/UDPWriter.cpp +++ b/src/UDPWriter.cpp @@ -28,9 +28,9 @@ SOFTWARE. using namespace boost::asio::ip; -UDPWriter::UDPWriter(const std::string& destination, const int port) : _destination(destination), _port(port) { +UDPWriter::UDPWriter(const std::string& destination, const int port) { udp::resolver resolver(_ios); - udp::resolver::query query(_destination, std::to_string(_port)); + udp::resolver::query query(destination, std::to_string(port)); udp::resolver::iterator it = resolver.resolve(query); if (it != udp::resolver::iterator()) { auto endpoint = *it; diff --git a/src/UDPWriter.h b/src/UDPWriter.h index 869a5c7..ce21366 100644 --- a/src/UDPWriter.h +++ b/src/UDPWriter.h @@ -37,8 +37,6 @@ public: virtual void sendMessage(std::shared_ptr); private: - const std::string& _destination; - const int _port; boost::asio::io_service _ios; boost::asio::ip::udp::socket _socket{_ios}; };