Merge "BuildSystem: For consistency, avoid double-checking via if clauses."
[asterisk/asterisk.git] / cel / cel_custom.c
index 54b1e4f..9c81ee1 100644 (file)
@@ -35,8 +35,6 @@
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/paths.h"
 #include "asterisk/channel.h"
 #include "asterisk/cel.h"
@@ -64,15 +62,17 @@ struct cel_config {
        AST_RWLIST_ENTRY(cel_config) list;
 };
 
-static struct ast_event_sub *event_sub = NULL;
+#define CUSTOM_BACKEND_NAME "CEL Custom CSV Logging"
 
 static AST_RWLIST_HEAD_STATIC(sinks, cel_config);
 
 static void free_config(void)
 {
        struct cel_config *sink;
+
        while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
                ast_mutex_destroy(&sink->lock);
+               ast_string_field_free_memory(sink);
                ast_free(sink);
        }
 }
@@ -82,6 +82,7 @@ static int load_config(void)
        struct ast_config *cfg;
        struct ast_variable *var;
        struct ast_flags config_flags = { 0 };
+       int mappings = 0;
        int res = 0;
 
        cfg = ast_config_load(CONFIG, config_flags);
@@ -90,7 +91,10 @@ static int load_config(void)
                return -1;
        }
 
-       var = ast_variable_browse(cfg, "mappings");
+       if (!(var = ast_variable_browse(cfg, "mappings"))) {
+               ast_log(LOG_NOTICE, "No mappings found in " CONFIG ". Not logging CEL to custom CSVs.\n");
+       }
+
        while (var) {
                if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
                        struct cel_config *sink = ast_calloc_with_stringfields(1, struct cel_config, 1024);
@@ -105,6 +109,8 @@ static int load_config(void)
                        ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
                        ast_mutex_init(&sink->lock);
 
+                       ast_verb(3, "Added CEL CSV mapping for '%s'.\n", sink->filename);
+                       mappings += 1;
                        AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
                } else {
                        ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
@@ -113,10 +119,12 @@ static int load_config(void)
        }
        ast_config_destroy(cfg);
 
+       ast_verb(1, "Added CEL CSV mapping for %d files.\n", mappings);
+
        return res;
 }
 
-static void custom_log(const struct ast_event *event, void *userdata)
+static void custom_log(struct ast_event *event)
 {
        struct ast_channel *dummy;
        struct ast_str *str;
@@ -128,7 +136,6 @@ static void custom_log(const struct ast_event *event, void *userdata)
        }
 
        dummy = ast_cel_fabricate_channel_from_event(event);
-
        if (!dummy) {
                ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
                return;
@@ -163,24 +170,20 @@ static void custom_log(const struct ast_event *event, void *userdata)
 
        AST_RWLIST_UNLOCK(&sinks);
 
-       ast_channel_release(dummy);
+       ast_channel_unref(dummy);
 }
 
 static int unload_module(void)
 {
-       if (event_sub) {
-               event_sub = ast_event_unsubscribe(event_sub);
-       }
 
        if (AST_RWLIST_WRLOCK(&sinks)) {
-               event_sub = ast_event_subscribe(AST_EVENT_CEL, custom_log, "CEL Custom CSV Logging",
-                       NULL, AST_EVENT_IE_END);
                ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
                return -1;
        }
 
        free_config();
        AST_RWLIST_UNLOCK(&sinks);
+       ast_cel_backend_unregister(CUSTOM_BACKEND_NAME);
        return 0;
 }
 
@@ -188,14 +191,16 @@ static enum ast_module_load_result load_module(void)
 {
        if (AST_RWLIST_WRLOCK(&sinks)) {
                ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
 
        load_config();
        AST_RWLIST_UNLOCK(&sinks);
 
-       event_sub = ast_event_subscribe(AST_EVENT_CEL, custom_log, "CEL Custom CSV Logging",
-               NULL, AST_EVENT_IE_END);
+       if (ast_cel_backend_register(CUSTOM_BACKEND_NAME, custom_log)) {
+               free_config();
+               return AST_MODULE_LOAD_DECLINE;
+       }
        return AST_MODULE_LOAD_SUCCESS;
 }
 
@@ -203,7 +208,7 @@ static int reload(void)
 {
        if (AST_RWLIST_WRLOCK(&sinks)) {
                ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
 
        free_config();
@@ -213,9 +218,9 @@ static int reload(void)
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CEL Backend",
+       .support_level = AST_MODULE_SUPPORT_CORE,
        .load = load_module,
        .unload = unload_module,
        .reload = reload,
        .load_pri = AST_MODPRI_CDR_DRIVER,
-       );
-
+);