You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/02/04 09:21:25 UTC

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #5408: drivers/syslog/syslog_device: fix flushing data when end of line is detected

xiaoxiang781216 commented on a change in pull request #5408:
URL: https://github.com/apache/incubator-nuttx/pull/5408#discussion_r799292189



##########
File path: drivers/syslog/syslog_device.c
##########
@@ -451,38 +451,58 @@ static ssize_t syslog_dev_write(FAR struct syslog_channel_s *channel,
 
       if (*endptr == '\r' || *endptr == '\n')
         {
+          /* Write everything up to the position of the special
+           * character.
+           *
+           * - buffer points to next byte to output.
+           * - endptr points to the special character.
+           */
+
+          writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);
+          if (writelen > 0)
+            {
+              nwritten = file_write(&syslog_dev->sl_file,
+                                    buffer, writelen);
+              if (nwritten < 0)
+                {
+                  ret = (int)nwritten;
+                  goto errout_with_sem;
+                }
+            }
+
           /* 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 */
+              nwritten = file_write(&syslog_dev->sl_file,
+                                    g_syscrlf, 2);
 
-              endptr++;
-              remaining--;
-            }
-          else
-            {
-              /* Write everything up to the position of the special
-               * character.
-               *
-               * - buffer points to next byte to output.
-               * - endptr points to the special character.
+              /* Synchronize the file when each CR-LF is encountered
+               * (i.e., implements line buffering always).
                */
 
-              writelen = (size_t)((uintptr_t)endptr - (uintptr_t)buffer);
-              if (writelen > 0)
+#ifndef CONFIG_DISABLE_MOUNTPOINT
+              if (nwritten > 0)
                 {
-                  nwritten = file_write(&syslog_dev->sl_file,
-                                        buffer, writelen);
-                  if (nwritten < 0)
-                    {
-                      ret = (int)nwritten;
-                      goto errout_with_sem;
-                    }
+                  syslog_dev_flush(channel);

Review comment:
       we can merge the most code in if and else block:
   ```
   nwritten = file_write(&syslog_dev->sl_file, g_syscrlf, 2);
   if (nwritten < 0)
     {
       ret = (int)nwritten;
       goto errout_with_sem;
     }
   
   syslog_dev_flush(channel);
   
   if (remaining > 1 &&
       ((endptr[0] == '\r' && endptr[1] == '\n') ||
       (endptr[0] == '\n' && endptr[1] == '\r')))
     {
       endptr++;
       remaining--;
     }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org