diff mbox series

[v3] ubox: libubox: logread: add filter logread messages by priority rsyslog related

Message ID a165db995004d37e30690bcc68dcc010.legale.legale@gmail.com
State New
Headers show
Series [v3] ubox: libubox: logread: add filter logread messages by priority rsyslog related | expand

Commit Message

Isaev Ruslan April 5, 2024, 3:36 a.m. UTC
branch: master d413903

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

This function is primarily needed to limit the messages sent to rsyslog from
the system log (performed using logread) by message priority levels, for
example, by sending messages only up to the LOG_ERR or LOG_WARNING levels.

changes:
v1: initial patch
v2: whitespaces cleanup
v3: change commit message topic

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

Patch

diff --git a/log/logread.c b/log/logread.c
index f48dd4b..87ec873 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;
 
@@ -156,6 +168,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 &&
 	    regexec(&regexp_preg, m, 0, NULL, 0) == REG_NOMATCH)
@@ -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);