syslog/ramlog: initialize g_sysdev based on the current ramlog buffer

N/A
by ramlog_initbuf();
Algorithm: Scan the entire ramlog buffer, the position of the head
is the first byte is not empty and second byte is empty. The position
of the tail is the first byte is empty and second byte is not empty.

Change-Id: Ieb9161bd670481cd335e9a901287cd5e589f0849
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong
2021-05-20 20:55:11 +08:00
parent e318b040f8
commit 418923540d
+60 -2
View File
@@ -37,6 +37,7 @@
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <ctype.h>
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
@@ -136,8 +137,8 @@ static struct ramlog_dev_s g_sysdev =
#ifndef CONFIG_RAMLOG_NONBLOCKING
0, /* rl_nwaiters */
#endif
0, /* rl_head */
0, /* rl_tail */
CONFIG_RAMLOG_BUFSIZE, /* rl_head */
CONFIG_RAMLOG_BUFSIZE, /* rl_tail */
SEM_INITIALIZER(1), /* rl_exclsem */
#ifndef CONFIG_RAMLOG_NONBLOCKING
SEM_INITIALIZER(0), /* rl_waitsem */
@@ -385,6 +386,7 @@ static ssize_t ramlog_read(FAR struct file *filep, FAR char *buffer,
*/
ch = priv->rl_buffer[priv->rl_tail];
priv->rl_buffer[priv->rl_tail] = '\0';
/* Increment the tail index. */
@@ -679,6 +681,60 @@ errout:
return ret;
}
/****************************************************************************
* Name: ramlog_initbuf
*
* Description:
* Initialize g_sysdev based on the current system ramlog buffer.
*
****************************************************************************/
#ifdef CONFIG_RAMLOG_SYSLOG
static void ramlog_initbuf(void)
{
FAR struct ramlog_dev_s *priv = &g_sysdev;
char prev, cur;
size_t i;
if (priv->rl_head != CONFIG_RAMLOG_BUFSIZE ||
priv->rl_tail != CONFIG_RAMLOG_BUFSIZE)
{
return;
}
prev = priv->rl_buffer[priv->rl_bufsize - 1];
for (i = 0; i < priv->rl_bufsize; i++)
{
cur = priv->rl_buffer[i];
if (!isascii(cur))
{
goto out;
}
if (prev && !cur)
{
priv->rl_head = i;
}
if (!prev && cur)
{
priv->rl_tail = i;
}
prev = cur;
}
out:
if (i != priv->rl_bufsize)
{
priv->rl_head = priv->rl_tail = 0;
memset(priv->rl_buffer, 0, priv->rl_bufsize);
}
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -768,6 +824,8 @@ int ramlog_putc(FAR struct syslog_channel_s *channel, int ch)
UNUSED(channel);
ramlog_initbuf();
#ifdef CONFIG_RAMLOG_CRLF
/* Ignore carriage returns. But return success. */