libdlog: separate limiter and dynamic config 32/190032/3
authorMichal Bloch <m.bloch@samsung.com>
Mon, 24 Sep 2018 17:27:49 +0000 (19:27 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Fri, 28 Sep 2018 11:33:38 +0000 (13:33 +0200)
Other dynamic features no longer require limiter to be enabled.

Change-Id: I0cbb8b4b2055397afffb23a0132acbb15b319420
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
include/dynamic_config.h
src/libdlog/dynamic_config.c
src/libdlog/log.c

index 770e679..705d478 100644 (file)
 extern "C" {
 #endif
 
+#include <stdbool.h>
+
 #include <logconfig.h>
 
 #define DYNAMIC_CONFIG_CONF_KEY "dynamic_config_path"
 #define DYNAMIC_CONFIG_FILENAME "FILTERS" // legacy name; should ideally become "CONFIG" at some point
 
-void __dynamic_config_create(struct log_config *config);
+bool __dynamic_config_create(struct log_config *config);
 void __dynamic_config_destroy();
 void __dynamic_config_update();
 
index 33563e5..9b48396 100644 (file)
@@ -32,6 +32,7 @@ static typeof(((struct inotify_event *)0)->wd) inotify_wd = -1;
 static char *inotify_path;
 
 extern pthread_mutex_t log_init_lock;
+extern bool limiter;
 
 static void __setup_runtime_watch(char const *path)
 {
@@ -54,34 +55,35 @@ static void __setup_runtime_watch(char const *path)
        }
 }
 
-static void __update_filters()
+static void __apply_update()
 {
        assert(inotify_path);
 
-       struct log_config config = {NULL, NULL};
+       __attribute__((cleanup(log_config_free))) struct log_config config = {NULL, NULL};
        const int r = log_config_read_file(&config, inotify_path);
-       if (!r)
-               __log_limiter_update(&config);
-       else
+       if (r) {
                syslog_critical_failure("DLog: keeping current runtime filters, can't read from \"%s\" (%d)", inotify_path, -r);// LCOV_EXCL_LINE
+               return;
+       }
 
-       log_config_free(&config);
+       if (limiter)
+               __log_limiter_update(&config);
 }
 
 /// caller has to guarantee exclusive access
-void __dynamic_config_create(struct log_config *config)
+bool __dynamic_config_create(struct log_config *config)
 {
        assert(config);
        assert(!inotify_path);
 
        const char *const extra_config_path = log_config_get(config, DYNAMIC_CONFIG_CONF_KEY);
        if (!extra_config_path)
-               return;
+               return false;
 
        if (asprintf(&inotify_path, "%s/%s", extra_config_path, DYNAMIC_CONFIG_FILENAME) < 0) {
                inotify_path = NULL;// LCOV_EXCL_LINE
                syslog_critical_failure("DLog: out of memory");// LCOV_EXCL_LINE
-               return;// LCOV_EXCL_LINE
+               return false; // LCOV_EXCL_LINE
        }
 
        /* missing filters config file at startup
@@ -91,6 +93,8 @@ void __dynamic_config_create(struct log_config *config)
        log_config_read_file(config, inotify_path);
 
        __setup_runtime_watch(extra_config_path);
+
+       return true;
 }
 
 /// caller has to guarantee exclusive access
@@ -131,6 +135,6 @@ void __dynamic_config_update()
        assert(ievent.ie.wd == inotify_wd);
 
        pthread_mutex_lock(&log_init_lock);
-       __update_filters();
+       __apply_update();
        pthread_mutex_unlock(&log_init_lock);
 }
index 75f90cd..e9f270b 100644 (file)
@@ -28,7 +28,7 @@
 #include <assert.h>
 #include <unistd.h>
 
-#define DEFAULT_CONFIG_LIMITER 0
+#define DEFAULT_CONFIG_LIMITER false
 #define DEFAULT_CONFIG_PLOG 1
 #define DEFAULT_CONFIG_DEBUGMODE 0
 #define DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS 0
@@ -50,7 +50,9 @@ pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
 extern void __dlog_init_pipe();
 extern void __dlog_init_android();
 
-static int limiter;
+bool limiter;
+bool dynamic_config;
+
 static int plog;
 static int debugmode;
 static int fatal_assert;
@@ -77,11 +79,7 @@ static void __configure_limiter(struct log_config *config)
        if (!limiter)
                return;
 
-       __dynamic_config_create(config);
        limiter = __log_limiter_create(config);
-
-       if (!limiter)
-               __dynamic_config_destroy(); // LCOV_EXCL_LINE
 }
 
 static int __configure_backend(struct log_config *config)
@@ -109,7 +107,7 @@ static void __configure_parameters(struct log_config *config)
        plog = log_config_get_int(config, "plog", DEFAULT_CONFIG_PLOG);
        debugmode = log_config_get_int(config, "debugmode", DEFAULT_CONFIG_DEBUGMODE);
        fatal_assert = access(DEBUGMODE_FILE, F_OK) != -1;
-       limiter = log_config_get_int(config, "limiter", DEFAULT_CONFIG_LIMITER);
+       limiter = log_config_get_boolean(config, "limiter", DEFAULT_CONFIG_LIMITER);
        limiter_apply_to_all_buffers = log_config_get_int(config,
                                                                        "limiter_apply_to_all_buffers",
                                                                        DEFAULT_CONFIG_LIMITER_APPLY_TO_ALL_BUFFERS);
@@ -126,6 +124,8 @@ static void __configure(void)
        if (log_config_read(&config) < 0)
                goto failure;
 
+       dynamic_config = __dynamic_config_create(&config);
+
        __configure_parameters(&config);
 
        if (!__configure_backend(&config))
@@ -196,20 +196,22 @@ static int dlog_should_log(log_id_t log_id, int prio, const char *tag)
        if (log_id != LOG_ID_APPS && !plog)
                return DLOG_ERROR_NOT_PERMITTED;
 
-       if (limiter) {
+       if (dynamic_config) {
                __dynamic_config_update();
 
                // LCOV_EXCL_START : disabled feature (limiter)
-               pthread_mutex_lock(&log_init_lock);
-               int should_log = __log_limiter_pass_log(tag, prio);
-               pthread_mutex_unlock(&log_init_lock);
-
-               if (!should_log) {
-                       return DLOG_ERROR_NOT_PERMITTED;
-               } else if (should_log < 0) {
-                       write_to_log(log_id, prio, tag,
-                                       "Your log has been blocked due to limit of log lines per minute.");
-                       return DLOG_ERROR_NOT_PERMITTED;
+               if (limiter) {
+                       pthread_mutex_lock(&log_init_lock);
+                       int should_log = __log_limiter_pass_log(tag, prio);
+                       pthread_mutex_unlock(&log_init_lock);
+
+                       if (!should_log) {
+                               return DLOG_ERROR_NOT_PERMITTED;
+                       } else if (should_log < 0) {
+                               write_to_log(log_id, prio, tag,
+                                               "Your log has been blocked due to limit of log lines per minute.");
+                               return DLOG_ERROR_NOT_PERMITTED;
+                       }
                }
                // LCOV_EXCL_STOP
        }