ell.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH] log: Add a max_log_level and limit l_log
@ 2022-05-18 14:58 Michael Johnson
  0 siblings, 0 replies; only message in thread
From: Michael Johnson @ 2022-05-18 14:58 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1796 bytes --]

This allows the user of ell to limit the logging verbosity to whatever
value they want by calling l_log_set_max_level. The current behavior of
no limit is maintained.
---
 ell/log.c | 16 ++++++++++++++++
 ell/log.h |  1 +
 2 files changed, 17 insertions(+)

diff --git a/ell/log.c b/ell/log.c
index 05af3e5..89e4ffc 100644
--- a/ell/log.c
+++ b/ell/log.c
@@ -37,6 +37,7 @@
 #include "queue.h"
 #include "log.h"
 #include "private.h"
+#include "useful.h"
 
 struct debug_section {
 	struct l_debug_desc *start;
@@ -67,6 +68,7 @@ static l_log_func_t log_func = log_null;
 static const char *log_ident = "";
 static int log_fd = -1;
 static unsigned long log_pid;
+static int max_log_level = L_LOG_DEBUG;
 
 static inline void close_log(void)
 {
@@ -275,6 +277,17 @@ LIB_EXPORT void l_log_set_journal(void)
 	log_func = log_journal;
 }
 
+/**
+ * l_log_set_max_level:
+ * @priority: max level
+ *
+ * Set the maximum logging level.
+ */
+LIB_EXPORT void l_log_set_max_level(int priority)
+{
+	max_log_level = priority;
+}
+
 /**
  * l_log_with_location:
  * @priority: priority level
@@ -292,6 +305,9 @@ LIB_EXPORT void l_log_with_location(int priority,
 {
 	va_list ap;
 
+	if (unlikely(priority > max_log_level))
+		return;
+
 	va_start(ap, format);
 	log_func(priority, file, line, func, format, ap);
 	va_end(ap);
diff --git a/ell/log.h b/ell/log.h
index 9ae40c0..3fc3867 100644
--- a/ell/log.h
+++ b/ell/log.h
@@ -44,6 +44,7 @@ void l_log_set_null(void);
 void l_log_set_stderr(void);
 void l_log_set_syslog(void);
 void l_log_set_journal(void);
+void l_log_set_max_level(int priority);
 
 void l_log_with_location(int priority, const char *file, const char *line,
 				const char *func, const char *format, ...)
-- 
2.25.1

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-18 14:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-18 14:58 [PATCH] log: Add a max_log_level and limit l_log Michael Johnson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).