Mon Mar 12 2012 21:27:21

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_HRTIME   0
#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 __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SQLite 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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, }
static struct ast_module_infoast_module_info = &__mod_info
static sqlite * db = NULL
static const char name [] = "sqlite"
static const char sql_create_table [] = ");"
 SQL table format.
static ast_mutex_t sqlite_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 }

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 59 of file cdr_sqlite.c.

Referenced by format_date().

#define LOG_HRTIME   0

Definition at line 56 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_UNIQUEID   0

Definition at line 54 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_USERFIELD   0

Definition at line 55 of file cdr_sqlite.c.

Referenced by sqlite_log().


Function Documentation

static void __reg_module ( void  ) [static]

Definition at line 248 of file cdr_sqlite.c.

static void __unreg_module ( void  ) [static]

Definition at line 248 of file cdr_sqlite.c.

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

Definition at line 98 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 200 of file cdr_sqlite.c.

References ast_cdr_register(), ast_config_AST_LOG_DIR, AST_FILE_MODE, ast_free, ast_log(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, 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 AST_MODULE_LOAD_DECLINE;
   }

   /* 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 AST_MODULE_LOAD_DECLINE;
   }
   return AST_MODULE_LOAD_SUCCESS;

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

Definition at line 106 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_tvdiff_us(), ast_tvzero(), 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_HRTIME, 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;
#if LOG_HRTIME
   double hrbillsec = 0.0;
   double hrduration;
#endif

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

#if LOG_HRTIME
   if (!ast_tvzero(cdr->answer)) {
      hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
   }
   hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
#endif

   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', "
#if LOG_HRTIME
            "%f, %f, %d, %d, "
#else
            "%d, %d, %d, %d, "
#endif
            "'%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,
#if LOG_HRTIME
            hrduration, hrbillsec, cdr->disposition, cdr->amaflags,
#else
            cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
#endif
            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 191 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 __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SQLite 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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, } [static]

Definition at line 248 of file cdr_sqlite.c.

Definition at line 248 of file cdr_sqlite.c.

const char name[] = "sqlite" [static]

Definition at line 61 of file cdr_sqlite.c.

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

SQL table format.

Definition at line 67 of file cdr_sqlite.c.

ast_mutex_t sqlite_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 } [static]

Definition at line 64 of file cdr_sqlite.c.

Referenced by sqlite_log().