drivers/syslog/syslog_device: fix flushing data when end of line is detected

Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
This commit is contained in:
Petro Karashchenko 2022-02-03 18:56:29 +02:00 committed by Xiang Xiao
parent a597e66070
commit 74a4394352

View File

@ -450,19 +450,6 @@ static ssize_t syslog_dev_write(FAR struct syslog_channel_s *channel,
/* Check for carriage return or line feed */ /* Check for carriage return or line feed */
if (*endptr == '\r' || *endptr == '\n') if (*endptr == '\r' || *endptr == '\n')
{
/* Check for pre-formatted CR-LF sequence */
if (remaining > 1 &&
((endptr[0] == '\r' && endptr[1] == '\n') ||
(endptr[0] == '\n' && endptr[1] == '\r')))
{
/* Just skip over pre-formatted CR-LF or LF-CR sequence */
endptr++;
remaining--;
}
else
{ {
/* Write everything up to the position of the special /* Write everything up to the position of the special
* character. * character.
@ -483,14 +470,42 @@ static ssize_t syslog_dev_write(FAR struct syslog_channel_s *channel,
} }
} }
/* Check for pre-formatted CR-LF sequence */
if (remaining > 1 &&
((endptr[0] == '\r' && endptr[1] == '\n') ||
(endptr[0] == '\n' && endptr[1] == '\r')))
{
writelen = sizeof(g_syscrlf);
/* Skip over pre-formatted CR-LF or LF-CR sequence */
endptr++;
remaining--;
}
else
{
/* Ignore the carriage return, but for the linefeed, output /* Ignore the carriage return, but for the linefeed, output
* both a carriage return and a linefeed. * both a carriage return and a linefeed.
*/ */
if (*endptr == '\n') writelen = *endptr == '\n' ? sizeof(g_syscrlf) : 0;
}
if (writelen > 0)
{ {
nwritten = file_write(&syslog_dev->sl_file, nwritten = file_write(&syslog_dev->sl_file,
g_syscrlf, 2); g_syscrlf, writelen);
/* Synchronize the file when each CR-LF is encountered
* (i.e., implements line buffering always).
*/
if (nwritten > 0)
{
syslog_dev_flush(channel);
}
if (nwritten < 0) if (nwritten < 0)
{ {
ret = (int)nwritten; ret = (int)nwritten;
@ -500,9 +515,7 @@ static ssize_t syslog_dev_write(FAR struct syslog_channel_s *channel,
/* Adjust pointers */ /* Adjust pointers */
writelen++; /* Skip the special character */ buffer = endptr + 1;
buffer += writelen; /* Points past the special character */
}
} }
} }
@ -599,12 +612,10 @@ static int syslog_dev_putc(FAR struct syslog_channel_s *channel, int ch)
* implements line buffering always). * implements line buffering always).
*/ */
#ifndef CONFIG_DISABLE_MOUNTPOINT
if (nbytes > 0) if (nbytes > 0)
{ {
syslog_dev_flush(channel); syslog_dev_flush(channel);
} }
#endif
} }
else else
{ {