Wed Mar 2 18:55:11 2011

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 __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .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, }
static struct ast_module_info * ast_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_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP )


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.

#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().

00091 {
00092    struct ast_tm tm;
00093 
00094    ast_localtime(when, &tm, NULL);
00095    ast_strftime(buffer, length, DATE_FORMAT, &tm);
00096 }

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(), LOG_ERROR, LOG_NOTICE, and sqlite_log().

00174 {
00175    char *zErr;
00176    char fn[PATH_MAX];
00177    int res;
00178 
00179    ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
00180       "using cdr_sqlite3_custom.\n");
00181 
00182    /* is the database there? */
00183    snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
00184    db = sqlite_open(fn, AST_FILE_MODE, &zErr);
00185    if (!db) {
00186       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00187       ast_free(zErr);
00188       return -1;
00189    }
00190 
00191    /* is the table there? */
00192    res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
00193    if (res) {
00194       res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
00195       if (res) {
00196          ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
00197          ast_free(zErr);
00198          goto err;
00199       }
00200 
00201       /* TODO: here we should probably create an index */
00202    }
00203 
00204    res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
00205    if (res) {
00206       ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
00207       return -1;
00208    }
00209    return 0;
00210 
00211 err:
00212    if (db)
00213       sqlite_close(db);
00214    return -1;
00215 }

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().

00099 {
00100    int res = 0;
00101    char *zErr = 0;
00102    char startstr[80], answerstr[80], endstr[80];
00103    int count;
00104 
00105    ast_mutex_lock(&sqlite_lock);
00106 
00107    format_date(startstr, sizeof(startstr), &cdr->start);
00108    format_date(answerstr, sizeof(answerstr), &cdr->answer);
00109    format_date(endstr, sizeof(endstr), &cdr->end);
00110 
00111    for(count=0; count<5; count++) {
00112       res = sqlite_exec_printf(db,
00113          "INSERT INTO cdr ("
00114             "clid,src,dst,dcontext,"
00115             "channel,dstchannel,lastapp,lastdata, "
00116             "start,answer,end,"
00117             "duration,billsec,disposition,amaflags, "
00118             "accountcode"
00119 #           if LOG_UNIQUEID
00120             ",uniqueid"
00121 #           endif
00122 #           if LOG_USERFIELD
00123             ",userfield"
00124 #           endif
00125          ") VALUES ("
00126             "'%q', '%q', '%q', '%q', "
00127             "'%q', '%q', '%q', '%q', "
00128             "'%q', '%q', '%q', "
00129             "%d, %d, %d, %d, "
00130             "'%q'"
00131 #           if LOG_UNIQUEID
00132             ",'%q'"
00133 #           endif
00134 #           if LOG_USERFIELD
00135             ",'%q'"
00136 #           endif
00137          ")", NULL, NULL, &zErr,
00138             cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
00139             cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
00140             startstr, answerstr, endstr,
00141             cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
00142             cdr->accountcode
00143 #           if LOG_UNIQUEID
00144             ,cdr->uniqueid
00145 #           endif
00146 #           if LOG_USERFIELD
00147             ,cdr->userfield
00148 #           endif
00149          );
00150       if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
00151          break;
00152       usleep(200);
00153    }
00154 
00155    if (zErr) {
00156       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00157       ast_free(zErr);
00158    }
00159 
00160    ast_mutex_unlock(&sqlite_lock);
00161    return res;
00162 }

static int unload_module ( void   )  [static]

Definition at line 164 of file cdr_sqlite.c.

References ast_cdr_unregister().

00165 {
00166    ast_cdr_unregister(name);
00167    if (db) {
00168       sqlite_close(db);
00169    }
00170    return 0;
00171 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .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, } [static]

Definition at line 217 of file cdr_sqlite.c.

Definition at line 217 of file cdr_sqlite.c.

sqlite* db = NULL [static]

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_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP ) [static]

Definition at line 61 of file cdr_sqlite.c.

Referenced by sqlite_log().


Generated on Wed Mar 2 18:55:11 2011 for Asterisk - the Open Source PBX by  doxygen 1.5.9