Wed Apr 6 11:29:54 2011

Asterisk developer's documentation


cdr_custom.c File Reference

Custom Comma Separated Value CDR records. More...

#include "asterisk.h"
#include <time.h>
#include "asterisk/paths.h"
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/threadstorage.h"
#include "asterisk/strings.h"

Go to the source code of this file.

Data Structures

struct  cdr_config
struct  sinks

Defines

#define CONFIG   "cdr_custom.conf"
#define CUSTOM_LOG_DIR   "/cdr_custom"

Functions

static void __init_custom_buf (void)
static void __reg_module (void)
static void __unreg_module (void)
static int custom_log (struct ast_cdr *cdr)
static void free_config (void)
static int load_config (void)
static enum ast_module_load_result load_module (void)
static int reload (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Customizable Comma Separated Values CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_threadstorage custom_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_custom_buf , .custom_init = NULL , }
static const char name [] = "cdr-custom"


Detailed Description

Custom Comma Separated Value CDR records.

Author:
Mark Spencer <markster@digium.com>
Logs in LOG_DIR/cdr_custom

Definition in file cdr_custom.c.


Define Documentation

#define CONFIG   "cdr_custom.conf"

Definition at line 51 of file cdr_custom.c.

#define CUSTOM_LOG_DIR   "/cdr_custom"

Definition at line 50 of file cdr_custom.c.


Function Documentation

static void __init_custom_buf ( void   )  [static]

Definition at line 53 of file cdr_custom.c.

00057 {

static void __reg_module ( void   )  [static]

Definition at line 219 of file cdr_custom.c.

static void __unreg_module ( void   )  [static]

Definition at line 219 of file cdr_custom.c.

static int custom_log ( struct ast_cdr cdr  )  [static]

Definition at line 116 of file cdr_custom.c.

References ast_cdr_dup(), ast_channel_release(), ast_dummy_channel_alloc(), AST_LIST_TRAVERSE, ast_log(), ast_mutex_lock, ast_mutex_unlock, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_str_buffer(), ast_str_substitute_variables(), ast_str_thread_get(), config, custom_buf, dummy(), errno, cdr_config::list, LOG_ERROR, and str.

Referenced by load_module(), and unload_module().

00117 {
00118    struct ast_channel *dummy;
00119    struct ast_str *str;
00120    struct cdr_config *config;
00121 
00122    /* Batching saves memory management here.  Otherwise, it's the same as doing an allocation and free each time. */
00123    if (!(str = ast_str_thread_get(&custom_buf, 16))) {
00124       return -1;
00125    }
00126 
00127    dummy = ast_dummy_channel_alloc();
00128 
00129    if (!dummy) {
00130       ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
00131       return -1;
00132    }
00133 
00134    /* We need to dup here since the cdr actually belongs to the other channel,
00135       so when we release this channel we don't want the CDR getting cleaned
00136       up prematurely. */
00137    dummy->cdr = ast_cdr_dup(cdr);
00138 
00139    AST_RWLIST_RDLOCK(&sinks);
00140 
00141    AST_LIST_TRAVERSE(&sinks, config, list) {
00142       FILE *out;
00143 
00144       ast_str_substitute_variables(&str, 0, dummy, config->format);
00145 
00146       /* Even though we have a lock on the list, we could be being chased by
00147          another thread and this lock ensures that we won't step on anyone's
00148          toes.  Once each CDR backend gets it's own thread, this lock can be
00149          removed. */
00150       ast_mutex_lock(&config->lock);
00151 
00152       /* Because of the absolutely unconditional need for the
00153          highest reliability possible in writing billing records,
00154          we open write and close the log file each time */
00155       if ((out = fopen(config->filename, "a"))) {
00156          fputs(ast_str_buffer(str), out);
00157          fflush(out); /* be particularly anal here */
00158          fclose(out);
00159       } else {
00160          ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
00161       }
00162 
00163       ast_mutex_unlock(&config->lock);
00164    }
00165 
00166    AST_RWLIST_UNLOCK(&sinks);
00167 
00168    ast_channel_release(dummy);
00169 
00170    return 0;
00171 }

static void free_config ( void   )  [static]

Definition at line 68 of file cdr_custom.c.

References ast_free, ast_mutex_destroy, AST_RWLIST_REMOVE_HEAD, cdr_config::list, and cdr_config::lock.

00069 {
00070    struct cdr_config *sink;
00071    while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
00072       ast_mutex_destroy(&sink->lock);
00073       ast_free(sink);
00074    }
00075 }

static int load_config ( void   )  [static]

Definition at line 77 of file cdr_custom.c.

References ast_calloc_with_stringfields, ast_config_AST_LOG_DIR, ast_config_destroy(), ast_config_load, ast_log(), ast_mutex_init, AST_RWLIST_INSERT_TAIL, ast_string_field_build, ast_strlen_zero(), ast_variable_browse(), CONFIG, config_flags, CONFIG_STATUS_FILEINVALID, cdr_config::filename, cdr_config::list, cdr_config::lock, LOG_ERROR, LOG_NOTICE, and var.

00078 {
00079    struct ast_config *cfg;
00080    struct ast_variable *var;
00081    struct ast_flags config_flags = { 0 };
00082    int res = 0;
00083 
00084    cfg = ast_config_load(CONFIG, config_flags);
00085    if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
00086       ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
00087       return -1;
00088    }
00089 
00090    var = ast_variable_browse(cfg, "mappings");
00091    while (var) {
00092       if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
00093          struct cdr_config *sink = ast_calloc_with_stringfields(1, struct cdr_config, 1024);
00094 
00095          if (!sink) {
00096             ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
00097             res = -2;
00098             break;
00099          }
00100 
00101          ast_string_field_build(sink, format, "%s\n", var->value);
00102          ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
00103          ast_mutex_init(&sink->lock);
00104 
00105          AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
00106       } else {
00107          ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
00108       }
00109       var = var->next;
00110    }
00111    ast_config_destroy(cfg);
00112 
00113    return res;
00114 }

static enum ast_module_load_result load_module ( void   )  [static]

Definition at line 188 of file cdr_custom.c.

References ast_cdr_register(), ast_log(), AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, custom_log(), load_config(), and LOG_ERROR.

00189 {
00190    if (AST_RWLIST_WRLOCK(&sinks)) {
00191       ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
00192       return AST_MODULE_LOAD_FAILURE;
00193    }
00194 
00195    load_config();
00196    AST_RWLIST_UNLOCK(&sinks);
00197    ast_cdr_register(name, ast_module_info->description, custom_log);
00198    return AST_MODULE_LOAD_SUCCESS;
00199 }

static int reload ( void   )  [static]

Definition at line 201 of file cdr_custom.c.

References ast_log(), AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, free_config(), load_config(), and LOG_ERROR.

00202 {
00203    if (AST_RWLIST_WRLOCK(&sinks)) {
00204       ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
00205       return AST_MODULE_LOAD_FAILURE;
00206    }
00207 
00208    free_config();
00209    load_config();
00210    AST_RWLIST_UNLOCK(&sinks);
00211    return AST_MODULE_LOAD_SUCCESS;
00212 }

static int unload_module ( void   )  [static]

Definition at line 173 of file cdr_custom.c.

References ast_cdr_register(), ast_cdr_unregister(), ast_log(), AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, custom_log(), free_config(), and LOG_ERROR.

00174 {
00175    ast_cdr_unregister(name);
00176 
00177    if (AST_RWLIST_WRLOCK(&sinks)) {
00178       ast_cdr_register(name, ast_module_info->description, custom_log);
00179       ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
00180       return -1;
00181    }
00182 
00183    free_config();
00184    AST_RWLIST_UNLOCK(&sinks);
00185    return 0;
00186 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Customizable Comma Separated Values CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, } [static]

Definition at line 219 of file cdr_custom.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 219 of file cdr_custom.c.

struct ast_threadstorage custom_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_custom_buf , .custom_init = NULL , } [static]

Definition at line 53 of file cdr_custom.c.

Referenced by custom_log().

const char name[] = "cdr-custom" [static]

Definition at line 55 of file cdr_custom.c.


Generated on Wed Apr 6 11:29:54 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7