libdlog: add a priority based pre-filter 66/226766/6
authorMichal Bloch <m.bloch@samsung.com>
Wed, 4 Mar 2020 16:37:06 +0000 (17:37 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 23 Jun 2020 11:53:03 +0000 (13:53 +0200)
Change-Id: I7850cdbfc06c1cbc5313a050bf8582fc8c9a4178
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
Makefile.am
include/dlog-internal.h
src/libdlog/log.c
src/tests/libdlog_prio_filter_pos.c [new file with mode: 0644]

index b8705c2..286fa82 100644 (file)
@@ -288,6 +288,7 @@ check_PROGRAMS = \
        src/tests/libdlog_android_monotonic_neg \
        src/tests/libdlog_base_pos \
        src/tests/libdlog_base_neg \
+       src/tests/libdlog_prio_filter_pos \
        src/tests/queued_entry_pos \
        src/tests/queued_entry_neg \
        src/tests/queued_entry_monotonic_pos \
@@ -415,6 +416,10 @@ src_tests_libdlog_base_neg_SOURCES = src/tests/libdlog_base_neg.c src/libdlog/lo
 src_tests_libdlog_base_neg_CFLAGS = $(check_CFLAGS) -pthread
 src_tests_libdlog_base_neg_LDFLAGS = $(AM_LDFLAGS) -lpthread -Wl,--wrap=log_config_read,--wrap=snprintf
 
+src_tests_libdlog_prio_filter_pos_SOURCES = src/tests/libdlog_prio_filter_pos.c src/libdlog/log.c src/shared/logcommon.c src/shared/logconfig.c src/shared/parsers.c
+src_tests_libdlog_prio_filter_pos_CFLAGS = $(check_CFLAGS) -pthread
+src_tests_libdlog_prio_filter_pos_LDFLAGS = $(AM_LDFLAGS) -lpthread -Wl,--wrap=log_config_read
+
 src_tests_log_file_SOURCES = src/tests/log_file.c src/shared/log_file.c
 src_tests_log_file_CFLAGS = $(check_CFLAGS)
 src_tests_log_file_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=strdup,--wrap=free,--wrap=memcpy,--wrap=snprintf,--wrap=open,--wrap=open64,--wrap=fstat,--wrap=fstat64,--wrap=rename,--wrap=dlogutil_entry_get_timestamp,--wrap=log_print_log_line,--wrap=dlogutil_entry_get_tag,--wrap=isatty
index 02943bf..9f8b003 100644 (file)
@@ -438,6 +438,36 @@ int __dlog_critical_print(log_id_t log_id, int prio, const char *tag, const char
  * @endcode
   */
 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap);
+
+/**
+ * @brief Blocks low priority logs
+ * @details Allows filtering low priority logs globally, think a runtime "--verbose" mode,
+ *          except you can choose which priorities to filter.
+ *          Using this (as opposed to filtering later, when retrieving logs) is that it
+ *          helps conserve system logging resources (buffer space etc.)
+ * @if MOBILE @since_tizen 6.0 @elseif WEARABLE @since_tizen 6.0 @endif
+ * @param[in] prio minimum priority level (of type #log_priority)
+ * @returns A DLOG_ERROR code
+ * @retval DLOG_ERROR_NONE Success
+ * @retval DLOG_ERROR_INVALID_PARAMETER Invalid priority parameter given
+ * @pre none
+ * @post none
+ * @see dlog_print
+ *
+ * @code
+#include<dlog.h>
+int main(void)
+{
+       dlog_print(DLOG_INFO, "TAG", "no filter so this log goes through");
+       dlog_set_minimum_priority(DLOG_WARN);
+       dlog_print(DLOG_INFO, "TAG", "WARN is a higher level than INFO so this log gets filtered");
+       dlog_print(DLOG_WARN, "TAG", "but this one still goes through");
+       dlog_print(DLOG_ERROR, "TAG", "as does this one");
+}
+ * @endcode
+ */
+int dlog_set_minimum_priority(int prio);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 70e37d0..66d213a 100644 (file)
@@ -56,6 +56,7 @@ static bool enable_secure_logs = true;
 static int debugmode;
 static int fatal_assert;
 static int limiter_apply_to_all_buffers;
+static _Atomic log_priority priority_filter_level = DLOG_VERBOSE;
 
 static void __configure_limiter(struct log_config *config)
 {
@@ -308,6 +309,9 @@ static int dlog_check_limiter(log_id_t log_id, int prio, const char *tag)
 
 static int __write_to_log_critical_section(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap, bool check_should_log)
 {
+       if (check_should_log && prio < priority_filter_level)
+               return DLOG_ERROR_NONE;
+
        if ((check_should_log || limiter_apply_to_all_buffers) && (dlog_check_limiter(log_id, prio, tag) < 0))
                return DLOG_ERROR_NONE;
 
@@ -521,6 +525,15 @@ int __dlog_critical_print(log_id_t log_id, int prio, const char *tag, const char
 }
 #endif
 
+int dlog_set_minimum_priority(int priority)
+{
+       if (priority < DLOG_DEFAULT || priority > DLOG_PRIO_MAX)
+               return DLOG_ERROR_INVALID_PARAMETER;
+
+       priority_filter_level = priority;
+       return DLOG_ERROR_NONE;
+}
+
 /**
  * @brief Print log
  * @details Print a log line
diff --git a/src/tests/libdlog_prio_filter_pos.c b/src/tests/libdlog_prio_filter_pos.c
new file mode 100644 (file)
index 0000000..fa7cdc9
--- /dev/null
@@ -0,0 +1,94 @@
+/* Copyright (c) 2020, Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. */
+
+// C
+#include <assert.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+// DLog
+#include <dlog.h>
+#include <libdlog.h>
+#include <logconfig.h>
+
+int wtl(log_id_t buf_id, log_priority pri, const char *tag, const char *msg) { return 0xABCD; }
+void __dlog_init_pipe(const struct log_config *conf) { write_to_log = wtl; }
+
+int __wrap_log_config_read(struct log_config *config)
+{
+       config->begin = config->last = NULL;
+       log_config_set(config, "backend", "pipe");
+       log_config_set(config, "debugmode", "1");
+       return 0;
+}
+
+// lobotomize various mechanisms I don't want to deal with
+void __log_limiter_update(struct log_config *config) { }
+void __log_limiter_destroy(void) { }
+void __dynamic_config_destroy() { }
+int __log_limiter_pass_log(const char* tag, int prio) { return 1; }
+int __log_limiter_create(struct log_config *config) { return 1; }
+bool __dynamic_config_create(struct log_config *config) { return false; }
+void __dynamic_config_update() { }
+void __dlog_init_android(const struct log_config *conf) { }
+
+
+int main()
+{
+#define   VALID_FILTER(x) assert(dlog_set_minimum_priority(x) == DLOG_ERROR_NONE)
+#define INVALID_FILTER(x) assert(dlog_set_minimum_priority(x) == DLOG_ERROR_INVALID_PARAMETER)
+#define ALLOW(x) assert(__dlog_print(LOG_ID_MAIN, x, "tag", "msg") == 0xABCD)
+#define BLOCK(x) assert(__dlog_print(LOG_ID_MAIN, x, "tag", "msg") == 0)
+
+       // Nothing should be filtered if there hasn't been a minimum set
+       for (int p = DLOG_VERBOSE; p < DLOG_PRIO_MAX; ++p)
+               ALLOW(p);
+
+       // The DEFAULT level should also not filter anything
+       VALID_FILTER(DLOG_DEFAULT);
+       for (int p = DLOG_VERBOSE; p < DLOG_PRIO_MAX; ++p)
+               ALLOW(p);
+
+       // The MAX filter should filter everything.
+       VALID_FILTER(DLOG_PRIO_MAX);
+       for (int p = DLOG_VERBOSE; p <= DLOG_SILENT; ++p)
+               BLOCK(p);
+
+       for (int l = DLOG_VERBOSE; l < DLOG_PRIO_MAX; ++l) {
+               VALID_FILTER(l);
+               for (int p = DLOG_VERBOSE; p < DLOG_PRIO_MAX; ++p)
+                       if (p < l)
+                               BLOCK(p);
+                       else
+                               ALLOW(p);
+       }
+
+       // Named priorities
+       INVALID_FILTER(DLOG_UNKNOWN);
+
+       // Adjacent to the allowed values
+       INVALID_FILTER(DLOG_DEFAULT  - 1);
+       INVALID_FILTER(DLOG_PRIO_MAX + 1);
+
+       // Misc arbitrary values
+       INVALID_FILTER((log_priority) -1);
+       INVALID_FILTER((log_priority)  0);
+       INVALID_FILTER((log_priority) 57);
+
+#undef BLOCK
+#undef ALLOW
+#undef INVALID_FILTER
+#undef   VALID_FILTER
+}