Implement max syslog message length, as per RFC (1024 bytes total)

This commit is contained in:
Marko Zivanovic
2015-09-04 12:24:31 +02:00
parent 2dad24a22e
commit 464db0d1d2
3 changed files with 17 additions and 5 deletions
+7 -4
View File
@@ -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;
}
+1
View File
@@ -42,6 +42,7 @@ public:
std::string operator()();
private:
static const size_t MAX_LEN = 1024;
const SyslogMessage& _message;
};
+9 -1
View File
@@ -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");
}
}
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");
}