mirror of
https://github.com/apache/nuttx.git
synced 2026-06-02 01:21:26 +08:00
Syslog buffering: Various corrections from early debug
This commit is contained in:
@@ -484,6 +484,7 @@ ssize_t syslog_dev_write(FAR const char *buffer, size_t buflen)
|
|||||||
FAR const char *endptr;
|
FAR const char *endptr;
|
||||||
ssize_t nwritten;
|
ssize_t nwritten;
|
||||||
size_t writelen;
|
size_t writelen;
|
||||||
|
size_t remaining;
|
||||||
int errcode;
|
int errcode;
|
||||||
int ret;
|
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 */
|
/* 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
|
nwritten = file_write(&g_syslog_dev.sl_file, buffer, writelen);
|
||||||
* return.
|
if (nwritten < 0)
|
||||||
*
|
|
||||||
* - 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);
|
errcode = -nwritten;
|
||||||
if (nwritten < 0)
|
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);
|
nwritten = file_write(&g_syslog_dev.sl_file, g_syscrlf, 2);
|
||||||
if (nwritten < 0)
|
if (nwritten < 0)
|
||||||
{
|
{
|
||||||
errcode = -nwritten;
|
errcode = -nwritten;
|
||||||
goto errout_with_sem;
|
goto errout_with_sem;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Adjust pointers */
|
|
||||||
|
|
||||||
writelen++; /* Skip the line feed */
|
|
||||||
buffer += writelen; /* Points past the line feed */
|
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
/* Adjust pointers */
|
||||||
break;
|
|
||||||
|
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.
|
* - 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);
|
writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ int syslog_remove_intbuffer(void)
|
|||||||
|
|
||||||
flags = enter_critical_section();
|
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;
|
inndx = (uint32_t)g_syslog_intbuffer.si_inndx;
|
||||||
outndx = (uint32_t)g_syslog_intbuffer.si_outndx;
|
outndx = (uint32_t)g_syslog_intbuffer.si_outndx;
|
||||||
@@ -211,7 +211,7 @@ int syslog_add_intbuffer(int ch)
|
|||||||
|
|
||||||
flags = enter_critical_section();
|
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;
|
inndx = (uint32_t)g_syslog_intbuffer.si_inndx;
|
||||||
outndx = (uint32_t)g_syslog_intbuffer.si_outndx;
|
outndx = (uint32_t)g_syslog_intbuffer.si_outndx;
|
||||||
|
|||||||
@@ -62,19 +62,24 @@ static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch)
|
|||||||
#ifdef CONFIG_SYSLOG_BUFFER
|
#ifdef CONFIG_SYSLOG_BUFFER
|
||||||
FAR struct lib_syslogstream_s *stream = (FAR struct lib_syslogstream_s *)this;
|
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;
|
if (ch != '\r')
|
||||||
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 */
|
/* 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
|
#else
|
||||||
int ret;
|
int ret;
|
||||||
@@ -123,8 +128,8 @@ static int syslogstream_flush(FAR struct lib_outstream_s *this)
|
|||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
int status = syslog_write(stream->buf, stream->nbuf);
|
ssize_t nbytes = syslog_write(stream->buf, stream->nbuf);
|
||||||
if (status < 0)
|
if (nbytes < 0)
|
||||||
{
|
{
|
||||||
ret = -get_errno();
|
ret = -get_errno();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,21 +108,21 @@ int _vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
|
|||||||
{
|
{
|
||||||
/* Use the SYSLOG emergency stream */
|
/* Use the SYSLOG emergency stream */
|
||||||
|
|
||||||
emergstream((FAR struct lib_outstream_s *)&stream);
|
emergstream(&stream.public);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Use the normal SYSLOG stream */
|
/* Use the normal SYSLOG stream */
|
||||||
|
|
||||||
syslogstream((FAR struct lib_syslogstream_s *)&stream);
|
syslogstream(&stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(CONFIG_SYSLOG_TIMESTAMP)
|
#if defined(CONFIG_SYSLOG_TIMESTAMP)
|
||||||
/* Pre-pend the message with the current time, if available */
|
/* Pre-pend the message with the current time, if available */
|
||||||
|
|
||||||
(void)lib_sprintf((FAR struct lib_outstream_s *)&stream,
|
(void)lib_sprintf(&stream.public, "[%6d.%06d]",
|
||||||
"[%6d.%06d]", ts.tv_sec, ts.tv_nsec/1000);
|
ts.tv_sec, ts.tv_nsec/1000);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, *ap);
|
return lib_vsprintf(&stream.public, fmt, *ap);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user