2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2009, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Includes code and algorithms from the Zapata library.
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
23 * \brief Custom Comma Separated Value CDR records.
25 * \author Mark Spencer <markster@digium.com>
27 * \arg See also \ref AstCDR
29 * Logs in LOG_DIR/cdr_custom
30 * \ingroup cdr_drivers
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
40 #include "asterisk/channel.h"
41 #include "asterisk/cdr.h"
42 #include "asterisk/module.h"
43 #include "asterisk/config.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/utils.h"
46 #include "asterisk/lock.h"
47 #include "asterisk/threadstorage.h"
48 #include "asterisk/strings.h"
50 #define CUSTOM_LOG_DIR "/cdr_custom"
51 #define CONFIG "cdr_custom.conf"
53 AST_THREADSTORAGE(custom_buf);
55 static const char name[] = "cdr-custom";
58 AST_DECLARE_STRING_FIELDS(
59 AST_STRING_FIELD(filename);
60 AST_STRING_FIELD(format);
63 AST_RWLIST_ENTRY(cdr_config) list;
66 static AST_RWLIST_HEAD_STATIC(sinks, cdr_config);
68 static void free_config(void)
70 struct cdr_config *sink;
71 while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
72 ast_mutex_destroy(&sink->lock);
77 static int load_config(void)
79 struct ast_config *cfg;
80 struct ast_variable *var;
81 struct ast_flags config_flags = { 0 };
84 cfg = ast_config_load(CONFIG, config_flags);
85 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
86 ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
90 var = ast_variable_browse(cfg, "mappings");
92 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
93 struct cdr_config *sink = ast_calloc_with_stringfields(1, struct cdr_config, 1024);
96 ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
101 ast_string_field_build(sink, format, "%s\n", var->value);
102 ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
103 ast_mutex_init(&sink->lock);
105 AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
107 ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
111 ast_config_destroy(cfg);
116 static int custom_log(struct ast_cdr *cdr)
118 struct ast_channel *dummy;
120 struct cdr_config *config;
122 /* Batching saves memory management here. Otherwise, it's the same as doing an allocation and free each time. */
123 if (!(str = ast_str_thread_get(&custom_buf, 16))) {
127 dummy = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Substitution/%p", cdr);
130 ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
134 /* We need to dup here since the cdr actually belongs to the other channel,
135 so when we release this channel we don't want the CDR getting cleaned
137 dummy->cdr = ast_cdr_dup(cdr);
139 AST_RWLIST_RDLOCK(&sinks);
141 AST_LIST_TRAVERSE(&sinks, config, list) {
144 ast_str_substitute_variables(&str, 0, dummy, config->format);
146 /* Even though we have a lock on the list, we could be being chased by
147 another thread and this lock ensures that we won't step on anyone's
148 toes. Once each CDR backend gets it's own thread, this lock can be
150 ast_mutex_lock(&config->lock);
152 /* Because of the absolutely unconditional need for the
153 highest reliability possible in writing billing records,
154 we open write and close the log file each time */
155 if ((out = fopen(config->filename, "a"))) {
156 fputs(ast_str_buffer(str), out);
157 fflush(out); /* be particularly anal here */
160 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
163 ast_mutex_unlock(&config->lock);
166 AST_RWLIST_UNLOCK(&sinks);
168 ast_channel_release(dummy);
173 static int unload_module(void)
175 ast_cdr_unregister(name);
177 if (AST_RWLIST_WRLOCK(&sinks)) {
178 ast_cdr_register(name, ast_module_info->description, custom_log);
179 ast_log(LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
184 AST_RWLIST_UNLOCK(&sinks);
188 static enum ast_module_load_result load_module(void)
190 if (AST_RWLIST_WRLOCK(&sinks)) {
191 ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
192 return AST_MODULE_LOAD_FAILURE;
196 AST_RWLIST_UNLOCK(&sinks);
197 ast_cdr_register(name, ast_module_info->description, custom_log);
198 return AST_MODULE_LOAD_SUCCESS;
201 static int reload(void)
203 if (AST_RWLIST_WRLOCK(&sinks)) {
204 ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
205 return AST_MODULE_LOAD_FAILURE;
210 AST_RWLIST_UNLOCK(&sinks);
211 return AST_MODULE_LOAD_SUCCESS;
214 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Customizable Comma Separated Values CDR Backend",
216 .unload = unload_module,