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 2023/07/12 16:47:29 UTC

[nuttx-apps] branch master updated: syste/setlogmask: add syslog channel filtering function

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/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new a7c6b20e8 syste/setlogmask: add syslog channel filtering function
a7c6b20e8 is described below

commit a7c6b20e895c597baa0c1b13a9c9287b360f32cf
Author: yinshengkai <yi...@xiaomi.com>
AuthorDate: Tue Jul 4 17:39:19 2023 +0800

    syste/setlogmask: add syslog channel filtering function
    
    Signed-off-by: yinshengkai <yi...@xiaomi.com>
---
 system/setlogmask/setlogmask.c | 97 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/system/setlogmask/setlogmask.c b/system/setlogmask/setlogmask.c
index 65f4682ac..009c915b6 100644
--- a/system/setlogmask/setlogmask.c
+++ b/system/setlogmask/setlogmask.c
@@ -24,11 +24,17 @@
 
 #include <nuttx/config.h>
 
+#include <fcntl.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
 #include <syslog.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+
+#include <nuttx/syslog/syslog.h>
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -50,9 +56,77 @@
  * Name: show_usage
  ****************************************************************************/
 
+#ifdef CONFIG_SYSLOG_IOCTL
+static int print_channels(void)
+{
+  struct syslog_channel_info_s info[CONFIG_SYSLOG_MAX_CHANNELS];
+  int ret;
+  int fd;
+  int i;
+
+  fd = open("/dev/log", O_WRONLY);
+  if (fd < 0)
+    {
+      perror("Failed to open /dev/log");
+      return EXIT_FAILURE;
+    }
+
+  memset(info, 0, sizeof(info));
+  ret = ioctl(fd, SYSLOGIOC_GETCHANNELS, &info);
+  if (ret < 0)
+    {
+      perror("Failed to get channels");
+      return EXIT_FAILURE;
+    }
+
+  printf("Channels:\n");
+  for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
+    {
+      if (info[i].sc_name[0] == '\0')
+        {
+          break;
+        }
+
+      printf("  %s: %s\n", info[i].sc_name,
+             info[i].sc_disable ? "disable" : "enable");
+    }
+
+  return ret;
+}
+
+static int disable_channel(FAR const char *name, bool disable)
+{
+  struct syslog_channel_info_s info;
+  int ret;
+  int fd;
+
+  fd = open("/dev/log", O_WRONLY);
+  if (fd < 0)
+    {
+      perror("Failed to open /dev/log");
+      return EXIT_FAILURE;
+    }
+
+  info.sc_disable = disable;
+  strlcpy(info.sc_name, name, sizeof(info.sc_name));
+  ret = ioctl(fd, SYSLOGIOC_SETFILTER, (unsigned long)&info);
+  if (ret < 0)
+    {
+      perror("Failed to set filter");
+    }
+
+  close(fd);
+  return ret;
+}
+#endif
+
 static void show_usage(FAR const char *progname, int exitcode)
 {
   printf("\nUsage: %s <d|i|n|w|e|c|a|r>\n", progname);
+#ifdef CONFIG_SYSLOG_IOCTL
+  printf("       %s list\n", progname);
+  printf("       %s <enable/disable> <channel>\n", progname);
+#endif
   printf("       %s -h\n", progname);
   printf("\nWhere:\n");
   printf("  d=DEBUG\n");
@@ -77,6 +151,29 @@ int main(int argc, FAR char *argv[])
       show_usage(argv[0], EXIT_FAILURE);
     }
 
+#ifdef CONFIG_SYSLOG_IOCTL
+  if (strcmp(argv[1], "list") == 0)
+    {
+      print_channels();
+      return EXIT_SUCCESS;
+    }
+  else if (argc == 3)
+    {
+      if (strcmp(argv[1], "enable") == 0)
+        {
+          return disable_channel(argv[2], false);
+        }
+      else if (strcmp(argv[1], "disable") == 0)
+        {
+          return disable_channel(argv[2], true);
+        }
+      else
+        {
+          show_usage(argv[0], EXIT_FAILURE);
+        }
+    }
+#endif
+
   switch (*argv[1])
     {
       case 'd':