Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / cdr / cdr_custom.c
blob6ecef8af4242e8ef6f2b016dcb412f25aaa873c1
1 /*
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.
21 /*! \file
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
33 #include "asterisk.h"
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <time.h>
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"
48 #define CUSTOM_LOG_DIR "/cdr_custom"
50 #define DATE_FORMAT "%Y-%m-%d %T"
52 AST_MUTEX_DEFINE_STATIC(lock);
53 AST_MUTEX_DEFINE_STATIC(mf_lock);
55 static char *name = "cdr-custom";
57 static char master[PATH_MAX];
58 static char format[1024]="";
60 static int load_config(int reload)
62 struct ast_config *cfg;
63 struct ast_variable *var;
64 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
65 int res = -1;
67 if ((cfg = ast_config_load("cdr_custom.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
68 return 0;
70 strcpy(format, "");
71 strcpy(master, "");
72 ast_mutex_lock(&lock);
73 if (cfg) {
74 var = ast_variable_browse(cfg, "mappings");
75 while(var) {
76 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
77 if (strlen(var->value) > (sizeof(format) - 1))
78 ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
79 ast_copy_string(format, var->value, sizeof(format) - 1);
80 strcat(format,"\n");
81 snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
82 if (var->next) {
83 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);
84 break;
86 } else
87 ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
88 var = var->next;
90 ast_config_destroy(cfg);
91 res = 0;
92 } else {
93 if (reload)
94 ast_log(LOG_WARNING, "Failed to reload configuration file.\n");
95 else
96 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
98 ast_mutex_unlock(&lock);
100 return res;
105 static int custom_log(struct ast_cdr *cdr)
107 FILE *mf = NULL;
109 /* Make sure we have a big enough buf */
110 char buf[2048];
111 struct ast_channel dummy;
113 /* Abort if no master file is specified */
114 if (ast_strlen_zero(master))
115 return 0;
117 /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
118 memset(&dummy, 0, sizeof(dummy));
119 dummy.cdr = cdr;
120 pbx_substitute_variables_helper(&dummy, format, buf, sizeof(buf) - 1);
122 /* because of the absolutely unconditional need for the
123 highest reliability possible in writing billing records,
124 we open write and close the log file each time */
125 ast_mutex_lock(&mf_lock);
126 mf = fopen(master, "a");
127 if (mf) {
128 fputs(buf, mf);
129 fflush(mf); /* be particularly anal here */
130 fclose(mf);
131 mf = NULL;
132 ast_mutex_unlock(&mf_lock);
133 } else {
134 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
135 ast_mutex_unlock(&mf_lock);
138 return 0;
141 static int unload_module(void)
143 ast_cdr_unregister(name);
144 return 0;
147 static int load_module(void)
149 int res = 0;
151 if (!load_config(0)) {
152 res = ast_cdr_register(name, ast_module_info->description, custom_log);
153 if (res)
154 ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
155 return res;
156 } else
157 return AST_MODULE_LOAD_DECLINE;
160 static int reload(void)
162 return load_config(1);
165 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Customizable Comma Separated Values CDR Backend",
166 .load = load_module,
167 .unload = unload_module,
168 .reload = reload,