You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by da...@apache.org on 2021/06/17 13:23:13 UTC

[incubator-nuttx] branch master updated: syslog/ramlog: Remove the duplication of \n->\r\n conversion

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 80157b8  syslog/ramlog: Remove the duplication of \n->\r\n conversion
80157b8 is described below

commit 80157b8782802b3e72f06ca709ab47d7c39f0ba8
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Wed Jun 16 16:43:50 2021 +0800

    syslog/ramlog: Remove the duplication of \n->\r\n conversion
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Change-Id: I5497f3ab40a55c5c08ba893e1186b93915cef03a
---
 drivers/syslog/ramlog.c | 78 +++++++++++++++++++------------------------------
 1 file changed, 30 insertions(+), 48 deletions(-)

diff --git a/drivers/syslog/ramlog.c b/drivers/syslog/ramlog.c
index 538df47..7ca5358 100644
--- a/drivers/syslog/ramlog.c
+++ b/drivers/syslog/ramlog.c
@@ -87,7 +87,7 @@ static int     ramlog_readnotify(FAR struct ramlog_dev_s *priv);
 #endif
 static void    ramlog_pollnotify(FAR struct ramlog_dev_s *priv,
                                  pollevent_t eventset);
-static ssize_t ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch);
+static int     ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch);
 
 /* Character driver methods */
 
@@ -209,7 +209,7 @@ static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv,
  * Name: ramlog_addchar
  ****************************************************************************/
 
-static ssize_t ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch)
+static int ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch)
 {
   irqstate_t flags;
   size_t nexthead;
@@ -218,6 +218,25 @@ static ssize_t ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch)
 
   flags = enter_critical_section();
 
+#ifdef CONFIG_RAMLOG_CRLF
+  /* Ignore carriage returns */
+
+  if (ch == '\r')
+    {
+      leave_critical_section(flags);
+      return OK;
+    }
+
+  /* Pre-pend a carriage before a linefeed */
+
+  if (ch == '\n')
+    {
+      ch = '\r';
+    }
+
+again:
+#endif
+
   /* Calculate the write index AFTER the next byte is written */
 
   nexthead = priv->rl_head + 1;
@@ -251,6 +270,15 @@ static ssize_t ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch)
 
   priv->rl_buffer[priv->rl_head] = ch;
   priv->rl_head = nexthead;
+
+#ifdef CONFIG_RAMLOG_CRLF
+  if (ch == '\r')
+    {
+      ch = '\n';
+      goto again;
+    }
+#endif
+
   leave_critical_section(flags);
   return OK;
 }
@@ -454,30 +482,6 @@ static ssize_t ramlog_write(FAR struct file *filep, FAR const char *buffer,
 
       ch = buffer[nwritten];
 
-      /* Ignore carriage returns */
-
-#ifdef CONFIG_RAMLOG_CRLF
-      if (ch == '\r')
-        {
-          continue;
-        }
-
-      /* Pre-pend a carriage before a linefeed */
-
-      if (ch == '\n')
-        {
-          ret = ramlog_addchar(priv, '\r');
-          if (ret < 0)
-            {
-              /* The buffer is full and nothing was saved.  The remaining
-               * data to be written is dropped on the floor.
-               */
-
-              break;
-            }
-        }
-#endif
-
       /* Then output the character */
 
       ret = ramlog_addchar(priv, ch);
@@ -789,28 +793,6 @@ int ramlog_putc(FAR struct syslog_channel_s *channel, int ch)
 
   ramlog_initbuf();
 
-#ifdef CONFIG_RAMLOG_CRLF
-  /* Ignore carriage returns.  But return success. */
-
-  if (ch == '\r')
-    {
-      return ch;
-    }
-
-  /* Pre-pend a newline with a carriage return */
-
-  if (ch == '\n')
-    {
-      ret = ramlog_addchar(priv, '\r');
-      if (ret < 0)
-        {
-          /* The buffer is full and nothing was saved. */
-
-          return ret;
-        }
-    }
-#endif
-
   /* Add the character to the RAMLOG */
 
   ret = ramlog_addchar(priv, ch);