Syslog buffering: Various corrections from early debug

This commit is contained in:
Gregory Nutt 2017-05-10 16:13:21 -06:00
parent 00075acada
commit d8a83f16be
4 changed files with 55 additions and 72 deletions

View file

@ -484,6 +484,7 @@ ssize_t syslog_dev_write(FAR const char *buffer, size_t buflen)
FAR const char *endptr;
ssize_t nwritten;
size_t writelen;
size_t remaining;
int errcode;
int ret;
@ -513,80 +514,57 @@ ssize_t syslog_dev_write(FAR const char *buffer, size_t buflen)
/* Loop until we have output all characters */
for (endptr = buffer; *endptr != '\n'; endptr++)
for (endptr = buffer, remaining = buflen;
remaining > 0;
endptr++, remaining--)
{
switch (*endptr)
/* Special case carriage return and line feed */
if (*endptr == '\r' || *endptr == '\n')
{
case '\r':
/* Write everything up to this point, ignore the special
* character.
*
* - buffer points to next byte to output.
* - endptr points to the special character.
*/
writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);
if (writelen > 0)
{
/* Write everything up to this point, ignore the carriage
* return.
*
* - buffer points to next byte to output.
* - endptr points to the carriage return.
*/
writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);
if (writelen > 0)
nwritten = file_write(&g_syslog_dev.sl_file, buffer, writelen);
if (nwritten < 0)
{
nwritten = file_write(&g_syslog_dev.sl_file, buffer, writelen);
if (nwritten < 0)
{
errcode = -nwritten;
goto errout_with_sem;
}
errcode = -nwritten;
goto errout_with_sem;
}
/* Adjust pointers */
writelen++; /* Skip the carriage return */
buffer += writelen; /* Points past the carriage return */
}
break;
case '\n':
/* Ignore the carriage return, but for the linefeed, output
* both a carriage return and a linefeed.
*/
if (*endptr == '\n')
{
/* Write everything up to this point, then add a carriage
* return and linefeed;
*
* - buffer points to next byte to output.
* - endptr points to the new line.
*/
writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);
if (writelen > 0)
{
nwritten = file_write(&g_syslog_dev.sl_file, buffer, writelen);
if (nwritten < 0)
{
errcode = -nwritten;
goto errout_with_sem;
}
}
nwritten = file_write(&g_syslog_dev.sl_file, g_syscrlf, 2);
if (nwritten < 0)
{
errcode = -nwritten;
goto errout_with_sem;
}
/* Adjust pointers */
writelen++; /* Skip the line feed */
buffer += writelen; /* Points past the line feed */
}
break;
default:
break;
/* Adjust pointers */
writelen++; /* Skip the special character */
buffer += writelen; /* Points past the special character */
}
}
/* Write any data at the end of the buffer.
/* Write any unterminated data at the end of the buffer.
*
* - buffer points to next byte to output.
* - endptr points to the NULL terminator.
* - endptr points to the end of the buffer plus 1.
*/
writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);

View file

@ -133,7 +133,7 @@ int syslog_remove_intbuffer(void)
flags = enter_critical_section();
/* How much space is left in the inbuffer? */
/* How much space is left in the intbuffer? */
inndx = (uint32_t)g_syslog_intbuffer.si_inndx;
outndx = (uint32_t)g_syslog_intbuffer.si_outndx;
@ -211,7 +211,7 @@ int syslog_add_intbuffer(int ch)
flags = enter_critical_section();
/* How much space is left in the inbuffer? */
/* How much space is left in the intbuffer? */
inndx = (uint32_t)g_syslog_intbuffer.si_inndx;
outndx = (uint32_t)g_syslog_intbuffer.si_outndx;

View file

@ -62,19 +62,24 @@ static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch)
#ifdef CONFIG_SYSLOG_BUFFER
FAR struct lib_syslogstream_s *stream = (FAR struct lib_syslogstream_s *)this;
/* Add the incoming character to the buffer */
/* Discard carriage returns */
stream->buf[stream->nbuf] = ch;
stream->nbuf++;
this->nput++;
/* Is the buffer full? Did we encounter a new line? */
if (stream->nbuf >= CONFIG_SYSLOG_BUFSIZE || ch == '\n')
if (ch != '\r')
{
/* Yes.. then flush the buffer */
/* Add the incoming character to the buffer */
(void)this->flush(this);
stream->buf[stream->nbuf] = ch;
stream->nbuf++;
this->nput++;
/* Is the buffer full? Did we encounter a new line? */
if (stream->nbuf >= CONFIG_SYSLOG_BUFSIZE || ch == '\n')
{
/* Yes.. then flush the buffer */
(void)this->flush(this);
}
}
#else
int ret;
@ -123,8 +128,8 @@ static int syslogstream_flush(FAR struct lib_outstream_s *this)
do
{
int status = syslog_write(stream->buf, stream->nbuf);
if (status < 0)
ssize_t nbytes = syslog_write(stream->buf, stream->nbuf);
if (nbytes < 0)
{
ret = -get_errno();
}

View file

@ -108,21 +108,21 @@ int _vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
{
/* Use the SYSLOG emergency stream */
emergstream((FAR struct lib_outstream_s *)&stream);
emergstream(&stream.public);
}
else
{
/* Use the normal SYSLOG stream */
syslogstream((FAR struct lib_syslogstream_s *)&stream);
syslogstream(&stream);
}
#if defined(CONFIG_SYSLOG_TIMESTAMP)
/* Pre-pend the message with the current time, if available */
(void)lib_sprintf((FAR struct lib_outstream_s *)&stream,
"[%6d.%06d]", ts.tv_sec, ts.tv_nsec/1000);
(void)lib_sprintf(&stream.public, "[%6d.%06d]",
ts.tv_sec, ts.tv_nsec/1000);
#endif
return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, *ap);
return lib_vsprintf(&stream.public, fmt, *ap);
}