void SyslogAppender::append()

in src/main/cpp/syslogappender.cpp [274:373]


void SyslogAppender::append(const spi::LoggingEventPtr& event, Pool& p)
{
	if  (!isAsSevereAsThreshold(event->getLevel()))
	{
		return;
	}

	LogString msg;
	std::string encoded;
	_priv->layout->format(msg, event, p);

	Transcoder::encode(msg, encoded);

	// Split up the message if it is over maxMessageLength in size.
	// According to RFC 3164, the max message length is 1024, however
	// newer systems(such as syslog-ng) can go up to 8k in size for their
	// messages.  We will append (x/y) at the end of each message
	// to indicate how far through the message we are
	std::vector<LogString> packets;

	if ( msg.size() > _priv->maxMessageLength )
	{
		LogString::iterator start = msg.begin();

		while ( start != msg.end() )
		{
			LogString::iterator end = start + _priv->maxMessageLength - 12;

			if ( end > msg.end() )
			{
				end = msg.end();
			}

			LogString newMsg = LogString( start, end );
			packets.push_back( newMsg );
			start = end;
		}

		int current = 1;

		for ( std::vector<LogString>::iterator it = packets.begin();
			it != packets.end();
			it++, current++ )
		{
			char buf[12];
			apr_snprintf( buf, sizeof(buf), "(%d/%d)", current, (int)packets.size() );
			LOG4CXX_DECODE_CHAR(str, buf);
			it->append( str );
		}
	}
	else
	{
		packets.push_back( msg );
	}

	// On the local host, we can directly use the system function 'syslog'
	// if it is available
#if LOG4CXX_HAVE_SYSLOG

	if (_priv->sw == 0)
	{
		for ( std::vector<LogString>::iterator it = packets.begin();
			it != packets.end();
			it++ )
		{
			// use of "%s" to avoid a security hole
			::syslog(_priv->syslogFacility | event->getLevel()->getSyslogEquivalent(),
				"%s", it->c_str());
		}

		return;
	}

#endif

	// We must not attempt to append if sw is null.
	if (_priv->sw == 0)
	{
		_priv->errorHandler->error(LOG4CXX_STR("No syslog host is set for SyslogAppedender named \"") +
			_priv->name + LOG4CXX_STR("\"."));
		return;
	}

	for ( std::vector<LogString>::iterator it = packets.begin();
		it != packets.end();
		it++ )
	{
		LogString sbuf(1, 0x3C /* '<' */);
		StringHelper::toString((_priv->syslogFacility | event->getLevel()->getSyslogEquivalent()), p, sbuf);
		sbuf.append(1, (logchar) 0x3E /* '>' */);

		if (_priv->facilityPrinting)
		{
			sbuf.append(_priv->facilityStr);
		}

		sbuf.append(*it);
		_priv->sw->write(sbuf);
	}
}