[svn-commits] seanbright: branch group/newcdr r202106 - /team/group/newcdr/cel/cel_custom.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Jun 19 19:03:25 CDT 2009


Author: seanbright
Date: Fri Jun 19 19:03:21 2009
New Revision: 202106

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=202106
Log:
Bring cel_custom in line with recent changes to cdr_custom.  Multiple logging
sinks can now be specified.

Modified:
    team/group/newcdr/cel/cel_custom.c

Modified: team/group/newcdr/cel/cel_custom.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/newcdr/cel/cel_custom.c?view=diff&rev=202106&r1=202105&r2=202106
==============================================================================
--- team/group/newcdr/cel/cel_custom.c (original)
+++ team/group/newcdr/cel/cel_custom.c Fri Jun 19 19:03:21 2009
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2008, Digium, Inc.
+ * Copyright (C) 2009, Digium, Inc.
  *
  * Steve Murphy <murf at digium.com>
  * much borrowed from cdr code (cdr_custom.c), author Mark Spencer
@@ -33,172 +33,186 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <sys/types.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <time.h>
-
-#include "asterisk/paths.h"    /* use ast_config_AST_LOG_DIR */
+#include "asterisk/paths.h"
 #include "asterisk/channel.h"
 #include "asterisk/cel.h"
 #include "asterisk/module.h"
 #include "asterisk/config.h"
 #include "asterisk/pbx.h"
-#include "asterisk/logger.h"
 #include "asterisk/utils.h"
+#include "asterisk/lock.h"
+#include "asterisk/threadstorage.h"
+#include "asterisk/strings.h"
 
 #define CUSTOM_LOG_DIR "/cel_custom"
-
-#define DATE_FORMAT "%Y-%m-%d %T"
-
-AST_MUTEX_DEFINE_STATIC(lock);
-AST_MUTEX_DEFINE_STATIC(mf_lock);
-
-static char *name = "cel-custom";
-
-static FILE *mf = NULL;
-
-static char master[PATH_MAX];
-static char format[1024]="";
+#define CONFIG         "cel_custom.conf"
+
+AST_THREADSTORAGE(custom_buf);
+
+static const char name[] = "cel-custom";
+
+struct cel_config {
+	AST_DECLARE_STRING_FIELDS(
+		AST_STRING_FIELD(filename);
+		AST_STRING_FIELD(format);
+		);
+	ast_mutex_t lock;
+	AST_RWLIST_ENTRY(cel_config) list;
+};
+
 static struct ast_event_sub *event_sub = NULL;
 
-static int load_config(int reload)
+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_free(sink);
+	}
+}
+
+static int load_config(void)
 {
 	struct ast_config *cfg;
 	struct ast_variable *var;
-	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
-	int res = -1;
-
-	if ((cfg = ast_config_load("cel_custom.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
-		return 0;
-	}
-	strcpy(format, "");
-	strcpy(master, "");
-	ast_mutex_lock(&lock);
-
-	if (cfg) {
-		var = ast_variable_browse(cfg, "mappings");
-		while (var) {
-			if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
-				if (strlen(var->value) > (sizeof(format) - 1)) {
-					ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
-				}
-				ast_copy_string(format, var->value, sizeof(format) - 1);
-				strcat(format,"\n");
-				snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
-			} else {
-				ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
+	struct ast_flags config_flags = { 0 };
+	int res = 0;
+
+	cfg = ast_config_load(CONFIG, config_flags);
+	if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
+		ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging CEL to custom CSVs.\n");
+		return -1;
+	}
+
+	var = ast_variable_browse(cfg, "mappings");
+	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);
+
+			if (!sink) {
+				ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
+				res = -2;
+				break;
 			}
-			if (var->next) {
-				ast_log(LOG_NOTICE, "Sorry, only one mapping is supported at this time, mapping '%s' will be ignored at line %d.\n", var->next->name, var->next->lineno); 
-			}
-			var = var->next;
+
+			ast_string_field_build(sink, format, "%s\n", var->value);
+			ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
+			ast_mutex_init(&sink->lock);
+
+			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);
 		}
-		ast_config_destroy(cfg);
-		res = 0;
-	} else {
-		if (reload) {
-			ast_log(LOG_WARNING, "Failed to reload configuration file.\n");
+		var = var->next;
+	}
+	ast_config_destroy(cfg);
+
+	return res;
+}
+
+static void custom_log(const struct ast_event *event, void *userdata)
+{
+	struct ast_channel *dummy;
+	struct ast_str *str;
+	struct cel_config *config;
+
+	/* Batching saves memory management here.  Otherwise, it's the same as doing an allocation and free each time. */
+	if (!(str = ast_str_thread_get(&custom_buf, 16))) {
+		return;
+	}
+
+	dummy = ast_cel_fabricate_channel_from_event(event);
+
+	if (!dummy) {
+		ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
+		return;
+	}
+
+	AST_RWLIST_RDLOCK(&sinks);
+
+	AST_LIST_TRAVERSE(&sinks, config, list) {
+		FILE *out;
+
+		ast_str_substitute_variables(&str, 0, dummy, config->format);
+
+		/* Even though we have a lock on the list, we could be being chased by
+		   another thread and this lock ensures that we won't step on anyone's
+		   toes.  Once each CEL backend gets it's own thread, this lock can be
+		   removed. */
+		ast_mutex_lock(&config->lock);
+
+		/* Because of the absolutely unconditional need for the
+		   highest reliability possible in writing billing records,
+		   we open write and close the log file each time */
+		if ((out = fopen(config->filename, "a"))) {
+			fputs(ast_str_buffer(str), out);
+			fflush(out); /* be particularly anal here */
+			fclose(out);
 		} else {
-			ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
+			ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
 		}
-	}
-	ast_mutex_unlock(&lock);
-
-	return res;
-}
-
-static void custom_log(const struct ast_event *event, void *userdata)
-{
-	char buf[2048]; /* to store the log line */
-	struct ast_channel *tchan;
-
-	/* Abort if no master file is specified */
-	if (ast_strlen_zero(master)) {
-		ast_log(LOG_WARNING,"master not defined. Logging aborted.\n");
-		return;
-	}
-
-	tchan = ast_cel_fabricate_channel_from_event(event);
-	if (!tchan) {
-		ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event\n");
-		return;
-	}
-
-	memset(buf, 0 , sizeof(buf));
-	pbx_substitute_variables_helper(tchan, format, buf, sizeof(buf) - 1);
-
-	tchan = ast_channel_release(tchan);
-
-	/* because of the absolutely unconditional need for the
-	   highest reliability possible in writing billing records,
-	   we open write and close the log file each time */
-	ast_mutex_lock(&mf_lock);
-	mf = fopen(master, "a");
-	if (!mf) {
-		ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
-	} else {
-		fputs(buf, mf);
-		fflush(mf); /* be particularly anal here */
-		fclose(mf);
-		mf = NULL;
-	}
-	ast_mutex_unlock(&mf_lock);
+
+		ast_mutex_unlock(&config->lock);
+	}
+
+	AST_RWLIST_UNLOCK(&sinks);
+
+	ast_channel_release(dummy);
+
 	return;
 }
 
 static int unload_module(void)
 {
-	if (mf) {
-		fclose(mf);
-		mf = NULL;
-	}
 	if (event_sub) {
-		ast_event_unsubscribe(event_sub);
-	}
-	event_sub = NULL;
+		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);
+	return 0;
+}
+
+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;
+	}
+
+	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);
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
-static int load_module(void)
-{
-
-	if (load_config(0)) {
-		return AST_MODULE_LOAD_DECLINE;
-	}
-
-	event_sub = ast_event_subscribe(AST_EVENT_CEL, custom_log, "Custom CSV logging", NULL, AST_EVENT_IE_END);
-
-	if (mf) {
-		fclose(mf);
-		mf = NULL;
-	}
-	if (!event_sub) {
-		ast_log(LOG_ERROR, "Unable to register custom CEL handling\n");
-		return AST_MODULE_LOAD_DECLINE;
-	} else {
-		ast_log(LOG_NOTICE, "Subscribed to Custom-CSV CEL events\n");
-	}
+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;
+	}
+
+	free_config();
+	load_config();
+	AST_RWLIST_UNLOCK(&sinks);
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
-static int reload(void)
-{
-	if (load_config(1)) {
-		return AST_MODULE_LOAD_DECLINE;
-	} else {
-		return AST_MODULE_LOAD_SUCCESS;
-	}
-
-}
-
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Customizable Comma Separated Values CEL Backend",
-		.load = load_module,
-		.unload = unload_module,
-		.reload = reload,
-	       );
-
+	.load = load_module,
+	.unload = unload_module,
+	.reload = reload,
+	);
+




More information about the svn-commits mailing list