Thu Apr 28 2011 16:56:56

Asterisk developer's documentation


cdr_sqlite.c File Reference

Store CDR records in a SQLite database. More...

#include "asterisk.h"
#include <sqlite.h>
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "asterisk/paths.h"
Include dependency graph for cdr_sqlite.c:

Go to the source code of this file.

Defines

#define DATE_FORMAT   "%Y-%m-%d %T"
#define LOG_UNIQUEID   0
#define LOG_USERFIELD   0

Functions

static void __reg_module (void)
static void __unreg_module (void)
static void format_date (char *buffer, size_t length, struct timeval *when)
static int load_module (void)
static int sqlite_log (struct ast_cdr *cdr)
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 = "SQLite CDR Backend" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static struct ast_module_infoast_module_info = &__mod_info
static sqlite * db = NULL
static char * name = "sqlite"
static char sql_create_table [] = ");"
 SQL table format.
static ast_mutex_t sqlite_lock = AST_MUTEX_INIT_VALUE

Detailed Description

Store CDR records in a SQLite database.

Author:
Holger Schurig <hs4233@mail.mn-solutions.de>
ExtRef:
SQLite http://www.sqlite.org/

See also

Creates the database and table on-the-fly

Note:
This module has been marked deprecated in favor for cdr_sqlite3_custom

Definition in file cdr_sqlite.c.


Define Documentation

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

Definition at line 56 of file cdr_sqlite.c.

Referenced by format_date().

#define LOG_UNIQUEID   0

Definition at line 52 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_USERFIELD   0

Definition at line 53 of file cdr_sqlite.c.

Referenced by sqlite_log().


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 217 of file cdr_sqlite.c.

static void __unreg_module ( void  ) [static]

Definition at line 217 of file cdr_sqlite.c.

static void format_date ( char *  buffer,
size_t  length,
struct timeval *  when 
) [static]

Definition at line 90 of file cdr_sqlite.c.

References ast_localtime(), ast_strftime(), and DATE_FORMAT.

Referenced by sqlite_log().

{
   struct ast_tm tm;

   ast_localtime(when, &tm, NULL);
   ast_strftime(buffer, length, DATE_FORMAT, &tm);
}
static int load_module ( void  ) [static]

Definition at line 173 of file cdr_sqlite.c.

References ast_cdr_register(), ast_config_AST_LOG_DIR, AST_FILE_MODE, ast_free, ast_log(), ast_module_info::description, LOG_ERROR, LOG_NOTICE, and sqlite_log().

{
   char *zErr;
   char fn[PATH_MAX];
   int res;

   ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
      "using cdr_sqlite3_custom.\n");

   /* is the database there? */
   snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
   db = sqlite_open(fn, AST_FILE_MODE, &zErr);
   if (!db) {
      ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
      ast_free(zErr);
      return -1;
   }

   /* is the table there? */
   res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
   if (res) {
      res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
      if (res) {
         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
         ast_free(zErr);
         goto err;
      }

      /* TODO: here we should probably create an index */
   }

   res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
   if (res) {
      ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
      return -1;
   }
   return 0;

err:
   if (db)
      sqlite_close(db);
   return -1;
}
static int sqlite_log ( struct ast_cdr cdr) [static]

Definition at line 98 of file cdr_sqlite.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_free, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, ast_cdr::end, format_date(), ast_cdr::lastapp, ast_cdr::lastdata, LOG_ERROR, LOG_UNIQUEID, LOG_USERFIELD, sqlite_lock, ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_module().

{
   int res = 0;
   char *zErr = 0;
   char startstr[80], answerstr[80], endstr[80];
   int count;

   ast_mutex_lock(&sqlite_lock);

   format_date(startstr, sizeof(startstr), &cdr->start);
   format_date(answerstr, sizeof(answerstr), &cdr->answer);
   format_date(endstr, sizeof(endstr), &cdr->end);

   for(count=0; count<5; count++) {
      res = sqlite_exec_printf(db,
         "INSERT INTO cdr ("
            "clid,src,dst,dcontext,"
            "channel,dstchannel,lastapp,lastdata, "
            "start,answer,end,"
            "duration,billsec,disposition,amaflags, "
            "accountcode"
#           if LOG_UNIQUEID
            ",uniqueid"
#           endif
#           if LOG_USERFIELD
            ",userfield"
#           endif
         ") VALUES ("
            "'%q', '%q', '%q', '%q', "
            "'%q', '%q', '%q', '%q', "
            "'%q', '%q', '%q', "
            "%d, %d, %d, %d, "
            "'%q'"
#           if LOG_UNIQUEID
            ",'%q'"
#           endif
#           if LOG_USERFIELD
            ",'%q'"
#           endif
         ")", NULL, NULL, &zErr,
            cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
            cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
            startstr, answerstr, endstr,
            cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
            cdr->accountcode
#           if LOG_UNIQUEID
            ,cdr->uniqueid
#           endif
#           if LOG_USERFIELD
            ,cdr->userfield
#           endif
         );
      if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
         break;
      usleep(200);
   }

   if (zErr) {
      ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
      ast_free(zErr);
   }

   ast_mutex_unlock(&sqlite_lock);
   return res;
}
static int unload_module ( void  ) [static]

Definition at line 164 of file cdr_sqlite.c.

References ast_cdr_unregister().

{
   ast_cdr_unregister(name);
   if (db) {
      sqlite_close(db);
   }
   return 0;
}

Variable Documentation

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

Definition at line 217 of file cdr_sqlite.c.

Definition at line 217 of file cdr_sqlite.c.

char* name = "sqlite" [static]

Definition at line 58 of file cdr_sqlite.c.

char sql_create_table[] = ");" [static]

SQL table format.

Definition at line 64 of file cdr_sqlite.c.

ast_mutex_t sqlite_lock = AST_MUTEX_INIT_VALUE [static]

Definition at line 61 of file cdr_sqlite.c.

Referenced by sqlite_log().