Mon Mar 19 11:30:37 2012

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 = "88eaa8f5c1bd988bedd71113385e0886" , .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 55 of file cdr_custom.c.

#define CUSTOM_LOG_DIR   "/cdr_custom"

Definition at line 54 of file cdr_custom.c.


Function Documentation

static void __init_custom_buf ( void   )  [static]

Definition at line 57 of file cdr_custom.c.

00061 {

static void __reg_module ( void   )  [static]

Definition at line 222 of file cdr_custom.c.

static void __unreg_module ( void   )  [static]

Definition at line 222 of file cdr_custom.c.

static int custom_log ( struct ast_cdr cdr  )  [static]

Definition at line 120 of file cdr_custom.c.

References ast_cdr_dup(), ast_channel_unref, 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().

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

static void free_config ( void   )  [static]

Definition at line 72 of file cdr_custom.c.

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

00073 {
00074    struct cdr_config *sink;
00075    while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
00076       ast_mutex_destroy(&sink->lock);
00077       ast_free(sink);
00078    }
00079 }

static int load_config ( void   )  [static]

Definition at line 81 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.

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

static enum ast_module_load_result load_module ( void   )  [static]

Definition at line 191 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.

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

static int reload ( void   )  [static]

Definition at line 204 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.

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

static int unload_module ( void   )  [static]

Definition at line 176 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.

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


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 = "88eaa8f5c1bd988bedd71113385e0886" , .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, } [static]

Definition at line 222 of file cdr_custom.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 222 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 57 of file cdr_custom.c.

Referenced by custom_log().

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

Definition at line 59 of file cdr_custom.c.


Generated on Mon Mar 19 11:30:37 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7