Fix memory management issues in RFC 3164 formatted syslog message
This commit is contained in:
@@ -9,7 +9,6 @@ add_library(slbu-lib
|
||||
Severity.cpp
|
||||
SyslogMessage.cpp
|
||||
UDPWriter.cpp
|
||||
RFC3164FormattedSyslogMessage
|
||||
)
|
||||
|
||||
add_executable(syslog-bulk-uploader main.cpp)
|
||||
|
||||
@@ -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 <boost/date_time.hpp>
|
||||
#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;
|
||||
}
|
||||
@@ -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 */
|
||||
|
||||
+2
-2
@@ -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;
|
||||
|
||||
@@ -37,8 +37,6 @@ public:
|
||||
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};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user