Mon May 14 04:45:47 2007

Asterisk developer's documentation


cdr_custom.c File Reference

Custom Comma Separated Value CDR records. More...

#include "asterisk.h"
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"

Include dependency graph for cdr_custom.c:

Go to the source code of this file.

Defines

#define CUSTOM_LOG_DIR   "/cdr_custom"
#define DATE_FORMAT   "%Y-%m-%d %T"

Functions

 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,"Customizable Comma Separated Values CDR Backend",.load=load_module,.unload=unload_module,.reload=reload,)
 AST_MUTEX_DEFINE_STATIC (lock)
static int custom_log (struct ast_cdr *cdr)
static int load_config (int reload)
static int load_module (void)
static int reload (void)
static int unload_module (void)

Variables

static char format [1024] = ""
static char master [PATH_MAX]
static FILE * mf = NULL
static 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 CUSTOM_LOG_DIR   "/cdr_custom"

Definition at line 54 of file cdr_custom.c.

#define DATE_FORMAT   "%Y-%m-%d %T"

Definition at line 56 of file cdr_custom.c.


Function Documentation

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_DEFAULT  ,
"Customizable Comma Separated Values CDR Backend"  ,
load = load_module,
unload = unload_module,
reload = reload 
)

AST_MUTEX_DEFINE_STATIC ( lock   ) 

static int custom_log ( struct ast_cdr cdr  )  [static]

Definition at line 106 of file cdr_custom.c.

References ast_log(), ast_strlen_zero(), ast_channel::cdr, LOG_ERROR, and pbx_substitute_variables_helper().

Referenced by load_module().

00107 {
00108    /* Make sure we have a big enough buf */
00109    char buf[2048];
00110    struct ast_channel dummy;
00111 
00112    /* Abort if no master file is specified */
00113    if (ast_strlen_zero(master))
00114       return 0;
00115 
00116    memset(buf, 0 , sizeof(buf));
00117    /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
00118    memset(&dummy, 0, sizeof(dummy));
00119    dummy.cdr = cdr;
00120    pbx_substitute_variables_helper(&dummy, format, buf, sizeof(buf) - 1);
00121 
00122    /* because of the absolutely unconditional need for the
00123       highest reliability possible in writing billing records,
00124       we open write and close the log file each time */
00125    mf = fopen(master, "a");
00126    if (!mf) {
00127       ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
00128    }
00129    if (mf) {
00130       fputs(buf, mf);
00131       fflush(mf); /* be particularly anal here */
00132       fclose(mf);
00133       mf = NULL;
00134    }
00135    return 0;
00136 }

static int load_config ( int  reload  )  [static]

Definition at line 67 of file cdr_custom.c.

References ast_config_AST_LOG_DIR, ast_config_destroy(), ast_config_load(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), ast_variable_browse(), lock, LOG_NOTICE, LOG_WARNING, and var.

00068 {
00069    struct ast_config *cfg;
00070    struct ast_variable *var;
00071    int res = -1;
00072 
00073    strcpy(format, "");
00074    strcpy(master, "");
00075    if((cfg = ast_config_load("cdr_custom.conf"))) {
00076       var = ast_variable_browse(cfg, "mappings");
00077       while(var) {
00078          ast_mutex_lock(&lock);
00079          if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
00080             if (strlen(var->value) > (sizeof(format) - 1))
00081                ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
00082             ast_copy_string(format, var->value, sizeof(format) - 1);
00083             strcat(format,"\n");
00084             snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
00085             ast_mutex_unlock(&lock);
00086          } else
00087             ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
00088          if (var->next)
00089             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); 
00090          var = var->next;
00091       }
00092       ast_config_destroy(cfg);
00093       res = 0;
00094    } else {
00095       if (reload)
00096          ast_log(LOG_WARNING, "Failed to reload configuration file.\n");
00097       else
00098          ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
00099    }
00100    
00101    return res;
00102 }

static int load_module ( void   )  [static]

Definition at line 146 of file cdr_custom.c.

References ast_cdr_register(), ast_log(), AST_MODULE_LOAD_DECLINE, custom_log(), load_config(), and LOG_ERROR.

00147 {
00148    int res = 0;
00149 
00150    if (!load_config(0)) {
00151       res = ast_cdr_register(name, ast_module_info->description, custom_log);
00152       if (res)
00153          ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
00154       if (mf)
00155          fclose(mf);
00156       return res;
00157    } else 
00158       return AST_MODULE_LOAD_DECLINE;
00159 }

static int reload ( void   )  [static]

Definition at line 161 of file cdr_custom.c.

References load_config().

00162 {
00163    return load_config(1);
00164 }

static int unload_module ( void   )  [static]

Definition at line 138 of file cdr_custom.c.

References ast_cdr_unregister().

00139 {
00140    if (mf)
00141       fclose(mf);
00142    ast_cdr_unregister(name);
00143    return 0;
00144 }


Variable Documentation

char format[1024] = "" [static]

Definition at line 65 of file cdr_custom.c.

char master[PATH_MAX] [static]

Definition at line 64 of file cdr_custom.c.

FILE* mf = NULL [static]

Definition at line 62 of file cdr_custom.c.

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

Definition at line 60 of file cdr_custom.c.


Generated on Mon May 14 04:45:48 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1