diff mbox series

ubox: logread add option to filter priority (log level)

Message ID CACDmYyft9wxJm+5a-4qDu-OTSV9rsF9LkvbzcViy_0Ks3opEKQ@mail.gmail.com
State New
Headers show
Series ubox: logread add option to filter priority (log level) | expand

Commit Message

Isaev Ruslan July 22, 2023, 1:40 p.m. UTC
From 071cfc2853dd7a4f9fb59a1650de8d5c874ce4f2 Mon Sep 17 00:00:00 2001
From:
Date: Sat, 22 Jul 2023 01:28:05 +0300
Subject: [PATCH] logread: add option to filter priority (log level)

This adds an ability to filter log messages priority:
        -v      <log level>     handle only messages with given log
level (0-7), repeatable
        -V      <log level>     ignore messages with given log level
(0-7), repeatable

Signed-off-by: Isaev Ruslan <legale.legale@gmail.com>
---
 log/logread.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

                "    -z <facility>      handle only messages with
given facility (0-23), repeatable\n"
                "    -Z <facility>      ignore messages with given
facility (0-23), repeatable\n"
                "    -f                 Follow log messages\n"
@@ -313,7 +330,7 @@ int main(int argc, char **argv)

        signal(SIGPIPE, SIG_IGN);

-       while ((ch = getopt(argc, argv, "u0fcs:l:z:Z:r:F:p:S:P:h:e:t")) != -1) {
+       while ((ch = getopt(argc, argv,
"u0fcs:l:v:V:z:Z:r:F:p:S:P:h:e:t")) != -1) {
                switch (ch) {
                case 'u':
                        log_udp = 1;
@@ -343,6 +360,14 @@ int main(int argc, char **argv)
                case 'l':
                        lines = atoi(optarg);
                        break;
+               case 'v':
+                       id = strtoul(optarg, NULL, 0) & 0x1f;
+                       loglevel_include |= (1 << id);
+                       break;
+               case 'V':
+                       id = strtoul(optarg, NULL, 0) & 0x1f;
+                       loglevel_exclude |= (1 << id);
+                       break;
                case 'z':
                        id = strtoul(optarg, NULL, 0) & 0x1f;
                        facility_include |= (1 << id);

Comments

Paul D July 24, 2023, 10:19 a.m. UTC | #1
For those executing this at the command line, how does one 'repeat'?

-v 1 -v 2, or -v1 -v2 or -v123 or -v 1,2,3?

I had to think for a bit since it wasn't immediately obvious.

Perhaps a hint string with "(repeatable eg -v 1 -v 2)"?



On 2023-07-22 14:40, Legale Legale wrote:
>  From 071cfc2853dd7a4f9fb59a1650de8d5c874ce4f2 Mon Sep 17 00:00:00 2001
> From:
> Date: Sat, 22 Jul 2023 01:28:05 +0300
> Subject: [PATCH] logread: add option to filter priority (log level)
> 
> This adds an ability to filter log messages priority:
>          -v      <log level>     handle only messages with given log
> level (0-7), repeatable
>          -V      <log level>     ignore messages with given log level
> (0-7), repeatable
> 
> Signed-off-by: Isaev Ruslan <legale.legale@gmail.com>
> ---
>   log/logread.c | 27 ++++++++++++++++++++++++++-
>   1 file changed, 26 insertions(+), 1 deletion(-)
> 
> diff --git a/log/logread.c b/log/logread.c
> index f48dd4b..91aae00 100644
> --- a/log/logread.c
> +++ b/log/logread.c
> @@ -66,6 +66,8 @@ static int log_type = LOG_STDOUT;
>   static int log_size, log_udp, log_follow, log_trailer_null = 0;
>   static int log_timestamp;
>   static int logd_conn_tries = LOGD_CONNECT_RETRY;
> +static int loglevel_include;
> +static int loglevel_exclude;
>   static int facility_include;
>   static int facility_exclude;
> 
> @@ -79,6 +81,16 @@ static int check_facility_filter(int f)
>          return 1;
>   }
> 
> +/* check for loglevel filter; return 0 if message shall be dropped */
> +static int check_loglevel_filter(int f)
> +{
> +       if (loglevel_include)
> +               return !!(loglevel_include & (1 << f));
> +       if (loglevel_exclude)
> +               return !(loglevel_exclude & (1 << f));
> +       return 1;
> +}
> +
>   static const char* getcodetext(int value, CODE *codetable) {
>          CODE *i;
> 
> @@ -155,6 +167,9 @@ static int log_notify(struct blob_attr *msg)
> 
>          if (!check_facility_filter(LOG_FAC(p)))
>                          return 0;
> +
> +  if (!check_loglevel_filter(LOG_PRI(p)))
> +    return 0;
> 
>          m = blobmsg_get_string(tb[LOG_MSG]);
>          if (regexp_pattern &&
> @@ -233,6 +248,8 @@ static int usage(const char *prog)
>                  "    -p <file>          PID file\n"
>                  "    -h <hostname>      Add hostname to the message\n"
>                  "    -P <prefix>        Prefix custom text to streamed
> messages\n"
> +               "    -v <log level>     handle only messages with
> given log level (0-7), repeatable\n"
> +               "    -V <log level>     ignore messages with given log
> level (0-7), repeatable\n"
>                  "    -z <facility>      handle only messages with
> given facility (0-23), repeatable\n"
>                  "    -Z <facility>      ignore messages with given
> facility (0-23), repeatable\n"
>                  "    -f                 Follow log messages\n"
> @@ -313,7 +330,7 @@ int main(int argc, char **argv)
> 
>          signal(SIGPIPE, SIG_IGN);
> 
> -       while ((ch = getopt(argc, argv, "u0fcs:l:z:Z:r:F:p:S:P:h:e:t")) != -1) {
> +       while ((ch = getopt(argc, argv,
> "u0fcs:l:v:V:z:Z:r:F:p:S:P:h:e:t")) != -1) {
>                  switch (ch) {
>                  case 'u':
>                          log_udp = 1;
> @@ -343,6 +360,14 @@ int main(int argc, char **argv)
>                  case 'l':
>                          lines = atoi(optarg);
>                          break;
> +               case 'v':
> +                       id = strtoul(optarg, NULL, 0) & 0x1f;
> +                       loglevel_include |= (1 << id);
> +                       break;
> +               case 'V':
> +                       id = strtoul(optarg, NULL, 0) & 0x1f;
> +                       loglevel_exclude |= (1 << id);
> +                       break;
>                  case 'z':
>                          id = strtoul(optarg, NULL, 0) & 0x1f;
>                          facility_include |= (1 << id);
Hannu Nyman July 24, 2023, 3:20 p.m. UTC | #2
Paul D kirjoitti 24.7.2023 klo 13.19:
> For those executing this at the command line, how does one 'repeat'?
>
> -v 1 -v 2, or -v1 -v2 or -v123 or -v 1,2,3?
>
> I had to think for a bit since it wasn't immediately obvious.
>
> Perhaps a hint string with "(repeatable eg -v 1 -v 2)"?
>
>
>
> On 2023-07-22 14:40, Legale Legale wrote:
>>  From 071cfc2853dd7a4f9fb59a1650de8d5c874ce4f2 Mon Sep 17 00:00:00 2001
>> From:
>> Date: Sat, 22 Jul 2023 01:28:05 +0300
>> Subject: [PATCH] logread: add option to filter priority (log level)
>>
>> This adds an ability to filter log messages priority:
>>          -v      <log level>     handle only messages with given log
>> level (0-7), repeatable
>>          -V      <log level>     ignore messages with given log level
>> (0-7), repeatable


I think that a more normal approach would be same as with kernel log:
defining the minimum seriousness (max level), and printing the items more 
serious than that.

https://www.kernel.org/doc/html/next/core-api/printk-basics.html

 > The log level specifies the importance of a message. The kernel decides 
whether to show the message immediately (printing it to the current console) 
depending on its log level and the current console_loglevel (a kernel 
variable). If the message priority is higher (lower log level value) than the 
console_loglevel the message will be printed to the console.

Simiilar approach of "show log items at least as serious as X" (=loglevel 
0-X) would look more natural to me.

There might be also filtering by individual level (with repeatable options), 
but the use case is not that obvious.
Isaev Ruslan Aug. 21, 2023, 11:49 a.m. UTC | #3
This will print only DEBUG PRIO.

root@AP_44D1FABC0E5C:~# logread -f -v 7
Mon Aug 21 14:48:24 2023 user.debug root: HELLO!!!

On Mon, 24 Jul 2023 at 18:22, Hannu Nyman <hannu.nyman@iki.fi> wrote:
>
> Paul D kirjoitti 24.7.2023 klo 13.19:
> > For those executing this at the command line, how does one 'repeat'?
> >
> > -v 1 -v 2, or -v1 -v2 or -v123 or -v 1,2,3?
> >
> > I had to think for a bit since it wasn't immediately obvious.
> >
> > Perhaps a hint string with "(repeatable eg -v 1 -v 2)"?
> >
> >
> >
> > On 2023-07-22 14:40, Legale Legale wrote:
> >>  From 071cfc2853dd7a4f9fb59a1650de8d5c874ce4f2 Mon Sep 17 00:00:00 2001
> >> From:
> >> Date: Sat, 22 Jul 2023 01:28:05 +0300
> >> Subject: [PATCH] logread: add option to filter priority (log level)
> >>
> >> This adds an ability to filter log messages priority:
> >>          -v      <log level>     handle only messages with given log
> >> level (0-7), repeatable
> >>          -V      <log level>     ignore messages with given log level
> >> (0-7), repeatable
>
>
> I think that a more normal approach would be same as with kernel log:
> defining the minimum seriousness (max level), and printing the items more
> serious than that.
>
> https://www.kernel.org/doc/html/next/core-api/printk-basics.html
>
>  > The log level specifies the importance of a message. The kernel decides
> whether to show the message immediately (printing it to the current console)
> depending on its log level and the current console_loglevel (a kernel
> variable). If the message priority is higher (lower log level value) than the
> console_loglevel the message will be printed to the console.
>
> Simiilar approach of "show log items at least as serious as X" (=loglevel
> 0-X) would look more natural to me.
>
> There might be also filtering by individual level (with repeatable options),
> but the use case is not that obvious.
>
>
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel@lists.openwrt.org
> https://lists.openwrt.org/mailman/listinfo/openwrt-devel
diff mbox series

Patch

diff --git a/log/logread.c b/log/logread.c
index f48dd4b..91aae00 100644
--- a/log/logread.c
+++ b/log/logread.c
@@ -66,6 +66,8 @@  static int log_type = LOG_STDOUT;
 static int log_size, log_udp, log_follow, log_trailer_null = 0;
 static int log_timestamp;
 static int logd_conn_tries = LOGD_CONNECT_RETRY;
+static int loglevel_include;
+static int loglevel_exclude;
 static int facility_include;
 static int facility_exclude;

@@ -79,6 +81,16 @@  static int check_facility_filter(int f)
        return 1;
 }

+/* check for loglevel filter; return 0 if message shall be dropped */
+static int check_loglevel_filter(int f)
+{
+       if (loglevel_include)
+               return !!(loglevel_include & (1 << f));
+       if (loglevel_exclude)
+               return !(loglevel_exclude & (1 << f));
+       return 1;
+}
+
 static const char* getcodetext(int value, CODE *codetable) {
        CODE *i;

@@ -155,6 +167,9 @@  static int log_notify(struct blob_attr *msg)

        if (!check_facility_filter(LOG_FAC(p)))
                        return 0;
+
+  if (!check_loglevel_filter(LOG_PRI(p)))
+    return 0;

        m = blobmsg_get_string(tb[LOG_MSG]);
        if (regexp_pattern &&
@@ -233,6 +248,8 @@  static int usage(const char *prog)
                "    -p <file>          PID file\n"
                "    -h <hostname>      Add hostname to the message\n"
                "    -P <prefix>        Prefix custom text to streamed
messages\n"
+               "    -v <log level>     handle only messages with
given log level (0-7), repeatable\n"
+               "    -V <log level>     ignore messages with given log
level (0-7), repeatable\n"