diff --git a/src/RFC3164FormattedSyslogMessage.cpp b/src/RFC3164FormattedSyslogMessage.cpp index a9e6b11..267a5df 100644 --- a/src/RFC3164FormattedSyslogMessage.cpp +++ b/src/RFC3164FormattedSyslogMessage.cpp @@ -28,11 +28,10 @@ SOFTWARE. using namespace boost::posix_time; using namespace std; -static time_facet* f = new time_facet("%b %e %H:%M:%S"); -static stringstream ss; +static const time_facet* f = new time_facet("%b %e %H:%M:%S"); std::string RFC3164FormattedSyslogMessage::operator()() { - ss.str(""); + stringstream ss; ss.imbue(locale(locale::classic(), f)); ss << "<" << to_string(_message.priority()) << ">"; @@ -40,5 +39,9 @@ std::string RFC3164FormattedSyslogMessage::operator()() { ss << _message.source() << " "; ss << _message.message(); - return ss.str(); + 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 562fd4a..3ced3af 100644 --- a/src/RFC3164FormattedSyslogMessage.h +++ b/src/RFC3164FormattedSyslogMessage.h @@ -42,6 +42,7 @@ public: std::string operator()(); private: + static const size_t MAX_LEN = 1024; const SyslogMessage& _message; }; diff --git a/test/RFC3164FormattedSyslogMessageTests.cpp b/test/RFC3164FormattedSyslogMessageTests.cpp index 8d2677b..59d34b6 100644 --- a/test/RFC3164FormattedSyslogMessageTests.cpp +++ b/test/RFC3164FormattedSyslogMessageTests.cpp @@ -35,4 +35,12 @@ BOOST_AUTO_TEST_CASE(format) { RFC3164FormattedSyslogMessage f(m); string s = f(); BOOST_CHECK_EQUAL(s, "<162>Sep 2 13:33:11 192.168.0.1 test message"); -} \ No newline at end of file +} + +BOOST_AUTO_TEST_CASE(long_message_clipping) { + stringstream ss("2015-09-02 13:33:11 Local4.Critical 192.168.0.1 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); + SyslogMessage m(ss); + RFC3164FormattedSyslogMessage f(m); + string s = f(); + BOOST_CHECK_EQUAL(s, "<162>Sep 2 13:33:11 192.168.0.1 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901"); +}