Logger/CLI/etc.: Fix some aesthetic issues; reduce chatty verbose messages
[asterisk/asterisk.git] / cdr / cdr_custom.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2009, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Includes code and algorithms from the Zapata library.
9  *
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.
15  *
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.
19  */
20
21 /*!
22  * \file
23  * \brief Custom Comma Separated Value CDR records.
24  *
25  * \author Mark Spencer <markster@digium.com>
26  *
27  * \arg See also \ref AstCDR
28  *
29  * Logs in LOG_DIR/cdr_custom
30  * \ingroup cdr_drivers
31  */
32
33 /*! \li \ref cdr_custom.c uses the configuration file \ref cdr_custom.conf
34  * \addtogroup configuration_file Configuration Files
35  */
36
37 /*!
38  * \page cdr_custom.conf cdr_custom.conf
39  * \verbinclude cdr_custom.conf.sample
40  */
41
42 /*** MODULEINFO
43         <support_level>core</support_level>
44  ***/
45
46 #include "asterisk.h"
47
48 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49
50 #include <time.h>
51
52 #include "asterisk/paths.h"     /* use ast_config_AST_LOG_DIR */
53 #include "asterisk/channel.h"
54 #include "asterisk/cdr.h"
55 #include "asterisk/module.h"
56 #include "asterisk/config.h"
57 #include "asterisk/pbx.h"
58 #include "asterisk/utils.h"
59 #include "asterisk/lock.h"
60 #include "asterisk/threadstorage.h"
61 #include "asterisk/strings.h"
62
63 #define CUSTOM_LOG_DIR "/cdr_custom"
64 #define CONFIG         "cdr_custom.conf"
65
66 AST_THREADSTORAGE(custom_buf);
67
68 static const char name[] = "cdr-custom";
69
70 struct cdr_custom_config {
71         AST_DECLARE_STRING_FIELDS(
72                 AST_STRING_FIELD(filename);
73                 AST_STRING_FIELD(format);
74                 );
75         ast_mutex_t lock;
76         AST_RWLIST_ENTRY(cdr_custom_config) list;
77 };
78
79 static AST_RWLIST_HEAD_STATIC(sinks, cdr_custom_config);
80
81 static void free_config(void)
82 {
83         struct cdr_custom_config *sink;
84         while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
85                 ast_mutex_destroy(&sink->lock);
86                 ast_free(sink);
87         }
88 }
89
90 static int load_config(void)
91 {
92         struct ast_config *cfg;
93         struct ast_variable *var;
94         struct ast_flags config_flags = { 0 };
95         int res = 0;
96
97         cfg = ast_config_load(CONFIG, config_flags);
98         if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
99                 ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
100                 return -1;
101         }
102
103         var = ast_variable_browse(cfg, "mappings");
104         while (var) {
105                 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
106                         struct cdr_custom_config *sink = ast_calloc_with_stringfields(1, struct cdr_custom_config, 1024);
107
108                         if (!sink) {
109                                 ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
110                                 res = -2;
111                                 break;
112                         }
113
114                         ast_string_field_build(sink, format, "%s\n", var->value);
115                         ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
116                         ast_mutex_init(&sink->lock);
117
118                         AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
119                 } else {
120                         ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
121                 }
122                 var = var->next;
123         }
124         ast_config_destroy(cfg);
125
126         return res;
127 }
128
129 static int custom_log(struct ast_cdr *cdr)
130 {
131         struct ast_channel *dummy;
132         struct ast_str *str;
133         struct cdr_custom_config *config;
134
135         /* Batching saves memory management here.  Otherwise, it's the same as doing an allocation and free each time. */
136         if (!(str = ast_str_thread_get(&custom_buf, 16))) {
137                 return -1;
138         }
139
140         dummy = ast_dummy_channel_alloc();
141         if (!dummy) {
142                 ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
143                 return -1;
144         }
145
146         /* We need to dup here since the cdr actually belongs to the other channel,
147            so when we release this channel we don't want the CDR getting cleaned
148            up prematurely. */
149         ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
150
151         AST_RWLIST_RDLOCK(&sinks);
152
153         AST_LIST_TRAVERSE(&sinks, config, list) {
154                 FILE *out;
155
156                 ast_str_substitute_variables(&str, 0, dummy, config->format);
157
158                 /* Even though we have a lock on the list, we could be being chased by
159                    another thread and this lock ensures that we won't step on anyone's
160                    toes.  Once each CDR backend gets it's own thread, this lock can be
161                    removed. */
162                 ast_mutex_lock(&config->lock);
163
164                 /* Because of the absolutely unconditional need for the
165                    highest reliability possible in writing billing records,
166                    we open write and close the log file each time */
167                 if ((out = fopen(config->filename, "a"))) {
168                         fputs(ast_str_buffer(str), out);
169                         fflush(out); /* be particularly anal here */
170                         fclose(out);
171                 } else {
172                         ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
173                 }
174
175                 ast_mutex_unlock(&config->lock);
176         }
177
178         AST_RWLIST_UNLOCK(&sinks);
179
180         ast_channel_unref(dummy);
181
182         return 0;
183 }
184
185 static int unload_module(void)
186 {
187         if (ast_cdr_unregister(name)) {
188                 return -1;
189         }
190
191         if (AST_RWLIST_WRLOCK(&sinks)) {
192                 ast_cdr_register(name, ast_module_info->description, custom_log);
193                 ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
194                 return -1;
195         }
196
197         free_config();
198         AST_RWLIST_UNLOCK(&sinks);
199         return 0;
200 }
201
202 static enum ast_module_load_result load_module(void)
203 {
204         if (AST_RWLIST_WRLOCK(&sinks)) {
205                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
206                 return AST_MODULE_LOAD_FAILURE;
207         }
208
209         load_config();
210         AST_RWLIST_UNLOCK(&sinks);
211         ast_cdr_register(name, ast_module_info->description, custom_log);
212         return AST_MODULE_LOAD_SUCCESS;
213 }
214
215 static int reload(void)
216 {
217         if (AST_RWLIST_WRLOCK(&sinks)) {
218                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
219                 return AST_MODULE_LOAD_FAILURE;
220         }
221
222         free_config();
223         load_config();
224         AST_RWLIST_UNLOCK(&sinks);
225         return AST_MODULE_LOAD_SUCCESS;
226 }
227
228 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CDR Backend",
229                 .load = load_module,
230                 .unload = unload_module,
231                 .reload = reload,
232                 .load_pri = AST_MODPRI_CDR_DRIVER,
233                );
234