Thu Apr 28 2011 17:15:37

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 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

static void __reg_module (void)
static void __unreg_module (void)
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 struct ast_module_info
__MODULE_INFO_SECTION 
__mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Customizable Comma Separated Values CDR Backend" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, }
static struct ast_module_infoast_module_info = &__mod_info
static char format [1024] = ""
static ast_mutex_t lock = AST_MUTEX_INIT_VALUE
static char master [PATH_MAX]
static ast_mutex_t mf_lock = AST_MUTEX_INIT_VALUE
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 48 of file cdr_custom.c.

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

Definition at line 50 of file cdr_custom.c.


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 174 of file cdr_custom.c.

static void __unreg_module ( void  ) [static]

Definition at line 174 of file cdr_custom.c.

static int custom_log ( struct ast_cdr cdr) [static]

Definition at line 110 of file cdr_custom.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), buf, ast_channel::cdr, errno, LOG_ERROR, mf_lock, and pbx_substitute_variables_helper().

Referenced by load_module().

{
   FILE *mf = NULL;

   /* Make sure we have a big enough buf */
   char buf[2048];
   struct ast_channel dummy;

   /* Abort if no master file is specified */
   if (ast_strlen_zero(master))
      return 0;

   /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
   memset(&dummy, 0, sizeof(dummy));
   dummy.cdr = cdr;
   pbx_substitute_variables_helper(&dummy, format, buf, sizeof(buf) - 1);

   /* because of the absolutely unconditional need for the
      highest reliability possible in writing billing records,
      we open write and close the log file each time */
   ast_mutex_lock(&mf_lock);
   mf = fopen(master, "a");
   if (mf) {
      fputs(buf, mf);
      fflush(mf); /* be particularly anal here */
      fclose(mf);
      mf = NULL;
      ast_mutex_unlock(&mf_lock);
   } else {
      ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
      ast_mutex_unlock(&mf_lock);
   }

   return 0;
}
static int load_config ( int  reload) [static]

Definition at line 60 of file cdr_custom.c.

References ast_config_AST_LOG_DIR, ast_config_destroy(), ast_config_load, ast_copy_string(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), ast_variable_browse(), CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, ast_variable::lineno, lock, LOG_ERROR, LOG_NOTICE, LOG_WARNING, ast_variable::name, ast_variable::next, ast_variable::value, and var.

Referenced by load_module(), and reload().

{
   struct ast_config *cfg;
   struct ast_variable *var;
   struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
   int res = -1;

   if ((cfg = ast_config_load("cdr_custom.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
      return 0;

   if (cfg == CONFIG_STATUS_FILEINVALID) {
      ast_log(LOG_ERROR, "Invalid config file\n");
      return 1;
   }

   strcpy(format, "");
   strcpy(master, "");
   ast_mutex_lock(&lock);
   if (cfg) {
      var = ast_variable_browse(cfg, "mappings");
      while(var) {
         if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
            if (strlen(var->value) > (sizeof(format) - 1))
               ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
            ast_copy_string(format, var->value, sizeof(format) - 1);
            strcat(format,"\n");
            snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
            if (var->next) {
               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);
               break;
            }
         } else
            ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
         var = var->next;
      }
      ast_config_destroy(cfg);
      res = 0;
   } else {
      if (reload)
         ast_log(LOG_WARNING, "Failed to reload configuration file.\n");
      else
         ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
   }
   ast_mutex_unlock(&lock);

   return res;
}
static int load_module ( void  ) [static]

Definition at line 152 of file cdr_custom.c.

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

{
   int res = 0;

   if (!load_config(0)) {
      res = ast_cdr_register(name, ast_module_info->description, custom_log);
      if (res)
         ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
      return res;
   } else
      return AST_MODULE_LOAD_DECLINE;
}
static int reload ( void  ) [static]

Definition at line 165 of file cdr_custom.c.

References load_config().

{
   return load_config(1);
}
static int unload_module ( void  ) [static]

Definition at line 146 of file cdr_custom.c.

References ast_cdr_unregister().

{
   ast_cdr_unregister(name);
   return 0;
}

Variable Documentation

struct ast_module_info __MODULE_INFO_SECTION __mod_info = { __MODULE_INFO_GLOBALS .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Customizable Comma Separated Values CDR Backend" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, } [static]

Definition at line 174 of file cdr_custom.c.

Definition at line 174 of file cdr_custom.c.

ast_mutex_t lock = AST_MUTEX_INIT_VALUE [static]

Definition at line 52 of file cdr_custom.c.

char master[PATH_MAX] [static]

Definition at line 57 of file cdr_custom.c.

Referenced by __unload_module(), dahdi_bridge(), dahdi_link(), dahdi_restart(), and setup_dahdi().

ast_mutex_t mf_lock = AST_MUTEX_INIT_VALUE [static]

Definition at line 53 of file cdr_custom.c.

Referenced by custom_log().

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

Definition at line 55 of file cdr_custom.c.