You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/07/10 18:10:20 UTC

[incubator-nuttx] 01/02: ramlog: init head and tail when buffer is empty on boot

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 4bb48892d0bb1d1ea3affbf1f59517cea7cb0c45
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Wed Jul 7 18:10:29 2021 +0800

    ramlog: init head and tail when buffer is empty on boot
    
    N/A
    
    Change-Id: I6963ef6256c3a93ac7a63115c303033ceafa321f
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 drivers/syslog/ramlog.c | 118 +++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 57 deletions(-)

diff --git a/drivers/syslog/ramlog.c b/drivers/syslog/ramlog.c
index 0a41c80..d19c613 100644
--- a/drivers/syslog/ramlog.c
+++ b/drivers/syslog/ramlog.c
@@ -209,6 +209,60 @@ static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv,
 }
 
 /****************************************************************************
+ * 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;
+  bool is_empty = true;
+  char prev;
+  char 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))
+        {
+          memset(priv->rl_buffer, 0, priv->rl_bufsize);
+          break;
+        }
+      else if (prev && !cur)
+        {
+          priv->rl_head = i;
+          is_empty = false;
+        }
+      else if (!prev && cur)
+        {
+          priv->rl_tail = i;
+        }
+
+      prev = cur;
+    }
+
+  if (i != priv->rl_bufsize || is_empty)
+    {
+      priv->rl_head = priv->rl_tail = 0;
+    }
+}
+#endif
+
+/****************************************************************************
  * Name: ramlog_addchar
  ****************************************************************************/
 
@@ -217,6 +271,13 @@ static int ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch)
   irqstate_t flags;
   size_t nexthead;
 
+#ifdef CONFIG_RAMLOG_SYSLOG
+  if (priv == &g_sysdev)
+    {
+      ramlog_initbuf();
+    }
+#endif
+
   /* Disable interrupts (in case we are NOT called from interrupt handler) */
 
   flags = enter_critical_section();
@@ -697,61 +758,6 @@ errout:
 }
 
 /****************************************************************************
- * 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
  ****************************************************************************/
 
@@ -840,8 +846,6 @@ int ramlog_putc(FAR struct syslog_channel_s *channel, int ch)
 
   UNUSED(channel);
 
-  ramlog_initbuf();
-
   /* Add the character to the RAMLOG */
 
   ret = ramlog_addchar(priv, ch);