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

[incubator-nuttx] branch master updated: Added color output to syslog.

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

btashton 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 cb78dad  Added color output to syslog.
cb78dad is described below

commit cb78dadc4bb656467df950546c8b470e67535fc9
Author: Fotis Panagiotopoulos <f....@gmail.com>
AuthorDate: Sat Jan 16 16:20:49 2021 +0200

    Added color output to syslog.
---
 drivers/syslog/Kconfig   |  6 ++++++
 drivers/syslog/vsyslog.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/drivers/syslog/Kconfig b/drivers/syslog/Kconfig
index a098bf6..461a32c 100644
--- a/drivers/syslog/Kconfig
+++ b/drivers/syslog/Kconfig
@@ -142,6 +142,12 @@ config SYSLOG_TIMESTAMP_BUFFER
 	---help---
 		Buffer size to store syslog formatted timestamps.
 
+config SYSLOG_COLOR_OUTPUT
+	bool "Colored syslog output"
+	default n
+	---help---
+		Enables colored output in syslog, according to message priority.
+
 config SYSLOG_PRIORITY
 	bool "Prepend priority to syslog message"
 	default n
diff --git a/drivers/syslog/vsyslog.c b/drivers/syslog/vsyslog.c
index d2bb5c1..728db13 100644
--- a/drivers/syslog/vsyslog.c
+++ b/drivers/syslog/vsyslog.c
@@ -176,6 +176,44 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
   ret += lib_sprintf(&stream.public, "[%6s] ", g_priority_str[priority]);
 #endif
 
+#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
+  /* Set the terminal style according to message priority. */
+
+  switch (priority)
+    {
+      case LOG_EMERG:   /* Red, Bold, Blinking */
+        ret += lib_sprintf(&stream.public, "\e[31;1;5m");
+        break;
+
+      case LOG_ALERT:   /* Red, Bold */
+        ret += lib_sprintf(&stream.public, "\e[31;1m");
+        break;
+
+      case LOG_CRIT:    /* Red, Bold */
+        ret += lib_sprintf(&stream.public, "\e[31;1m");
+        break;
+
+      case LOG_ERR:     /* Red */
+        ret += lib_sprintf(&stream.public, "\e[31m");
+        break;
+
+      case LOG_WARNING: /* Yellow */
+        ret += lib_sprintf(&stream.public, "\e[33m");
+        break;
+
+      case LOG_NOTICE:  /* Bold */
+        ret += lib_sprintf(&stream.public, "\e[1m");
+        break;
+
+      case LOG_INFO:    /* Normal */
+        break;
+
+      case LOG_DEBUG:   /* Dim */
+        ret += lib_sprintf(&stream.public, "\e[2m");
+        break;
+    }
+#endif
+
 #if defined(CONFIG_SYSLOG_PREFIX)
   /* Pre-pend the prefix, if available */
 
@@ -186,6 +224,12 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
 
   ret += lib_vsprintf(&stream.public, fmt, *ap);
 
+#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
+  /* Reset the terminal style back to normal. */
+
+  ret += lib_sprintf(&stream.public, "\e[0m");
+#endif
+
 #ifdef CONFIG_SYSLOG_BUFFER
   /* Flush and destroy the syslog stream buffer */