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

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 committed by Gustavo Henrique Nihei
parent ec38d0ce98
commit aeb7ed8c53
2 changed files with 64 additions and 4 deletions

View File

@ -39,8 +39,8 @@ config RAMLOG_BUFFER_SECTION
default ".bss" default ".bss"
depends on RAMLOG_SYSLOG depends on RAMLOG_SYSLOG
---help--- ---help---
The section where ramlog buffer is located, this section cannot The section where ramlog buffer is located.
be initialized each time when the system boot. The section shall not be initialized on system boot.
config RAMLOG_BUFSIZE config RAMLOG_BUFSIZE
int "RAMLOG buffer size" int "RAMLOG buffer size"

View File

@ -37,6 +37,7 @@
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <debug.h> #include <debug.h>
#include <ctype.h>
#include <nuttx/arch.h> #include <nuttx/arch.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
@ -133,8 +134,8 @@ static struct ramlog_dev_s g_sysdev =
#ifndef CONFIG_RAMLOG_NONBLOCKING #ifndef CONFIG_RAMLOG_NONBLOCKING
0, /* rl_nwaiters */ 0, /* rl_nwaiters */
#endif #endif
0, /* rl_head */ CONFIG_RAMLOG_BUFSIZE, /* rl_head */
0, /* rl_tail */ CONFIG_RAMLOG_BUFSIZE, /* rl_tail */
SEM_INITIALIZER(1), /* rl_exclsem */ SEM_INITIALIZER(1), /* rl_exclsem */
#ifndef CONFIG_RAMLOG_NONBLOCKING #ifndef CONFIG_RAMLOG_NONBLOCKING
SEM_INITIALIZER(0), /* rl_waitsem */ SEM_INITIALIZER(0), /* rl_waitsem */
@ -232,6 +233,7 @@ static ssize_t ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch)
#ifdef CONFIG_RAMLOG_OVERWRITE #ifdef CONFIG_RAMLOG_OVERWRITE
/* Yes... Overwrite with the latest log in the circular buffer */ /* Yes... Overwrite with the latest log in the circular buffer */
priv->rl_buffer[priv->rl_tail] = '\0';
priv->rl_tail += 1; priv->rl_tail += 1;
if (priv->rl_tail >= priv->rl_bufsize) if (priv->rl_tail >= priv->rl_bufsize)
{ {
@ -382,6 +384,7 @@ static ssize_t ramlog_read(FAR struct file *filep, FAR char *buffer,
*/ */
ch = priv->rl_buffer[priv->rl_tail]; ch = priv->rl_buffer[priv->rl_tail];
priv->rl_buffer[priv->rl_tail] = '\0';
/* Increment the tail index. */ /* Increment the tail index. */
@ -640,6 +643,61 @@ errout:
return ret; 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 = 0;
priv->rl_tail = 0;
memset(priv->rl_buffer, 0, priv->rl_bufsize);
}
}
#endif
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -729,6 +787,8 @@ int ramlog_putc(FAR struct syslog_channel_s *channel, int ch)
UNUSED(channel); UNUSED(channel);
ramlog_initbuf();
#ifdef CONFIG_RAMLOG_CRLF #ifdef CONFIG_RAMLOG_CRLF
/* Ignore carriage returns. But return success. */ /* Ignore carriage returns. But return success. */