From 54ba51e39a2abcb50bd3a91c369bc2837aee9aaa Mon Sep 17 00:00:00 2001
From: John Crispin <john@phrozen.org>
Date: Mon, 31 Jan 2022 12:40:51 +0100
Subject: [PATCH] ubox: add log priority filtering

Signed-off-by: John Crispin <john@phrozen.org>
---
 package/system/ubox/files/log.init            |  2 +
 .../ubox/patches/000-log-priority.patch       | 50 +++++++++++++++++++
 2 files changed, 52 insertions(+)
 create mode 100644 package/system/ubox/patches/000-log-priority.patch

diff --git a/package/system/ubox/files/log.init b/package/system/ubox/files/log.init
index c4802d4199..76a1110698 100644
--- a/package/system/ubox/files/log.init
+++ b/package/system/ubox/files/log.init
@@ -20,6 +20,7 @@ validate_log_section()
 		'log_port:port:514' \
 		'log_proto:or("tcp", "udp"):udp' \
 		'log_trailer_null:bool:0' \
+		'log_priority:uinteger' \
 		'log_prefix:string'
 }
 
@@ -80,6 +81,7 @@ start_service_remote()
 		"tcp") [ "${log_trailer_null}" -eq 1 ] && procd_append_param command -0;;
 	esac
 	[ -z "${log_prefix}" ] || procd_append_param command -P "${log_prefix}"
+	[ -z "${log_priority}" ] || procd_append_param command -m "${log_priority}"
 	procd_close_instance
 }
 
diff --git a/package/system/ubox/patches/000-log-priority.patch b/package/system/ubox/patches/000-log-priority.patch
new file mode 100644
index 0000000000..736e224a34
--- /dev/null
+++ b/package/system/ubox/patches/000-log-priority.patch
@@ -0,0 +1,50 @@
+Index: ubox-2020-10-25-9ef88681/log/logread.c
+===================================================================
+--- ubox-2020-10-25-9ef88681.orig/log/logread.c
++++ ubox-2020-10-25-9ef88681/log/logread.c
+@@ -63,7 +63,7 @@ static struct uloop_fd sender;
+ static regex_t regexp_preg;
+ static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname, *regexp_pattern;
+ static int log_type = LOG_STDOUT;
+-static int log_size, log_udp, log_follow, log_trailer_null = 0;
++static int log_size, log_udp, log_follow, log_trailer_null = 0, log_priority = -1;
+ static int log_timestamp;
+ static int logd_conn_tries = LOGD_CONNECT_RETRY;
+ static int facility_include;
+@@ -147,6 +147,9 @@ static int log_notify(struct blob_attr *
+ 	}
+ 	p = blobmsg_get_u32(tb[LOG_PRIO]);
+ 
++	if (log_priority >= 0 && LOG_PRI(p) > log_priority)
++		return 0;
++
+ 	if (!check_facility_filter(LOG_FAC(p)))
+ 			return 0;
+ 
+@@ -229,6 +232,7 @@ static int usage(const char *prog)
+ 		"    -P	<prefix>	Prefix custom text to streamed messages\n"
+ 		"    -z	<facility>	handle only messages with given facility (0-23), repeatable\n"
+ 		"    -Z	<facility>	ignore messages with given facility (0-23), repeatable\n"
++		"    -m <priority>	filter messages by their maximum priority\n"
+ 		"    -f			Follow log messages\n"
+ 		"    -u			Use UDP as the protocol\n"
+ 		"    -t			Add an extra timestamp\n"
+@@ -307,7 +311,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:z:Z:r:F:p:S:P:h:e:tm:")) != -1) {
+ 		switch (ch) {
+ 		case 'u':
+ 			log_udp = 1;
+@@ -345,6 +349,9 @@ int main(int argc, char **argv)
+ 			id = strtoul(optarg, NULL, 0) & 0x1f;
+ 			facility_exclude |= (1 << id);
+ 			break;
++		case 'm':
++			log_priority = atoi(optarg);
++			break;
+ 		case 'S':
+ 			log_size = atoi(optarg);
+ 			if (log_size < 1)
-- 
2.25.1

