2 * Asterisk -- A telephony toolkit for Linux.
4 * Comma Separated Value CDR records.
6 * Copyright (C) 1999 - 2005, Digium, inc
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License.
13 * Includes code and algorithms from the Zapata library.
17 #include <sys/types.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/cdr.h>
20 #include <asterisk/module.h>
21 #include <asterisk/config.h>
22 #include <asterisk/channel_pvt.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/logger.h>
25 #include <asterisk/utils.h>
26 #include "../asterisk.h"
27 #include "../astconf.h"
29 #define CUSTOM_LOG_DIR "/cdr_custom"
31 #define DATE_FORMAT "%Y-%m-%d %T"
41 AST_MUTEX_DEFINE_STATIC(lock);
43 static char *desc = "Customizable Comma Separated Values CDR Backend";
45 static char *name = "cdr-custom";
47 static FILE *mf = NULL;
49 static char master[AST_CONFIG_MAX_PATH];
50 static char format[1024]="";
52 static int load_config(int reload)
54 struct ast_config *cfg;
55 struct ast_variable *var;
60 if((cfg = ast_config_load("cdr_custom.conf"))) {
61 var = ast_variable_browse(cfg, "mappings");
63 ast_mutex_lock(&lock);
64 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
65 strncpy(format, var->value, sizeof(format) - 2);
67 snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
68 ast_mutex_unlock(&lock);
70 ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
72 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);
75 ast_config_destroy(cfg);
83 static int custom_log(struct ast_cdr *cdr)
85 /* Make sure we have a big enough buf */
87 struct ast_channel dummy;
89 /* Abort if no master file is specified */
90 if (ast_strlen_zero(master))
93 memset(buf, 0 , sizeof(buf));
94 /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
95 memset(&dummy, 0, sizeof(dummy));
97 pbx_substitute_variables_helper(&dummy, format, buf, sizeof(buf) - 1);
99 /* because of the absolutely unconditional need for the
100 highest reliability possible in writing billing records,
101 we open write and close the log file each time */
102 mf = fopen(master, "a");
104 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
108 fflush(mf); /* be particularly anal here */
115 char *description(void)
120 int unload_module(void)
124 ast_cdr_unregister(name);
128 int load_module(void)
132 res = load_config(0);
134 res = ast_cdr_register(name, desc, custom_log);
136 ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
146 return load_config(1);
156 return ASTERISK_GPL_KEY;