2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, 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 * \arg See also \ref AstCDR
27 * Logs in LOG_DIR/cdr_custom
28 * \ingroup cdr_drivers
31 #include <sys/types.h>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/channel.h"
38 #include "asterisk/cdr.h"
39 #include "asterisk/module.h"
40 #include "asterisk/config.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/utils.h"
45 #define CUSTOM_LOG_DIR "/cdr_custom"
47 #define DATE_FORMAT "%Y-%m-%d %T"
57 AST_MUTEX_DEFINE_STATIC(lock);
59 static char *desc = "Customizable Comma Separated Values CDR Backend";
61 static char *name = "cdr-custom";
63 static FILE *mf = NULL;
65 static char master[AST_CONFIG_MAX_PATH];
66 static char format[1024]="";
68 static int load_config(int reload)
70 struct ast_config *cfg;
71 struct ast_variable *var;
76 if((cfg = ast_config_load("cdr_custom.conf"))) {
77 var = ast_variable_browse(cfg, "mappings");
79 ast_mutex_lock(&lock);
80 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
81 if (strlen(var->value) > (sizeof(format) - 2))
82 ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
83 strncpy(format, var->value, sizeof(format) - 2);
85 snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
86 ast_mutex_unlock(&lock);
88 ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
90 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);
93 ast_config_destroy(cfg);
97 ast_log(LOG_WARNING, "Failed to reload configuration file.\n");
99 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
107 static int custom_log(struct ast_cdr *cdr)
109 /* Make sure we have a big enough buf */
111 struct ast_channel dummy;
113 /* Abort if no master file is specified */
114 if (ast_strlen_zero(master))
117 memset(buf, 0 , sizeof(buf));
118 /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
119 memset(&dummy, 0, sizeof(dummy));
121 pbx_substitute_variables_helper(&dummy, format, buf, sizeof(buf) - 1);
123 /* because of the absolutely unconditional need for the
124 highest reliability possible in writing billing records,
125 we open write and close the log file each time */
126 mf = fopen(master, "a");
128 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
132 fflush(mf); /* be particularly anal here */
139 char *description(void)
144 int unload_module(void)
148 ast_cdr_unregister(name);
152 int load_module(void)
156 if (!load_config(0)) {
157 res = ast_cdr_register(name, desc, custom_log);
159 ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
168 return load_config(1);
178 return ASTERISK_GPL_KEY;