Mon Oct 8 12:39:12 2012

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"

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 = "ac1f6a56484a8820659555499174e588" , .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.

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

00099 {
00100    struct ast_tm tm;
00101 
00102    ast_localtime(when, &tm, NULL);
00103    ast_strftime(buffer, length, DATE_FORMAT, &tm);
00104 }

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

00201 {
00202    char *zErr;
00203    char fn[PATH_MAX];
00204    int res;
00205 
00206    ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
00207       "using cdr_sqlite3_custom.\n");
00208 
00209    /* is the database there? */
00210    snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
00211    db = sqlite_open(fn, AST_FILE_MODE, &zErr);
00212    if (!db) {
00213       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00214       ast_free(zErr);
00215       return AST_MODULE_LOAD_DECLINE;
00216    }
00217 
00218    /* is the table there? */
00219    res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
00220    if (res) {
00221       res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
00222       if (res) {
00223          ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
00224          ast_free(zErr);
00225          goto err;
00226       }
00227 
00228       /* TODO: here we should probably create an index */
00229    }
00230 
00231    res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
00232    if (res) {
00233       ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
00234       return AST_MODULE_LOAD_DECLINE;
00235    }
00236    return AST_MODULE_LOAD_SUCCESS;
00237 
00238 err:
00239    if (db)
00240       sqlite_close(db);
00241    return AST_MODULE_LOAD_DECLINE;
00242 }

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

00107 {
00108    int res = 0;
00109    char *zErr = 0;
00110    char startstr[80], answerstr[80], endstr[80];
00111    int count;
00112 #if LOG_HRTIME
00113    double hrbillsec = 0.0;
00114    double hrduration;
00115 #endif
00116 
00117    ast_mutex_lock(&sqlite_lock);
00118 
00119    format_date(startstr, sizeof(startstr), &cdr->start);
00120    format_date(answerstr, sizeof(answerstr), &cdr->answer);
00121    format_date(endstr, sizeof(endstr), &cdr->end);
00122 
00123 #if LOG_HRTIME
00124    if (!ast_tvzero(cdr->answer)) {
00125       hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
00126    }
00127    hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
00128 #endif
00129 
00130    for(count=0; count<5; count++) {
00131       res = sqlite_exec_printf(db,
00132          "INSERT INTO cdr ("
00133             "clid,src,dst,dcontext,"
00134             "channel,dstchannel,lastapp,lastdata, "
00135             "start,answer,end,"
00136             "duration,billsec,disposition,amaflags, "
00137             "accountcode"
00138 #           if LOG_UNIQUEID
00139             ",uniqueid"
00140 #           endif
00141 #           if LOG_USERFIELD
00142             ",userfield"
00143 #           endif
00144          ") VALUES ("
00145             "'%q', '%q', '%q', '%q', "
00146             "'%q', '%q', '%q', '%q', "
00147             "'%q', '%q', '%q', "
00148 #if LOG_HRTIME
00149             "%f, %f, %d, %d, "
00150 #else
00151             "%d, %d, %d, %d, "
00152 #endif
00153             "'%q'"
00154 #           if LOG_UNIQUEID
00155             ",'%q'"
00156 #           endif
00157 #           if LOG_USERFIELD
00158             ",'%q'"
00159 #           endif
00160          ")", NULL, NULL, &zErr,
00161             cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
00162             cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
00163             startstr, answerstr, endstr,
00164 #if LOG_HRTIME
00165             hrduration, hrbillsec, cdr->disposition, cdr->amaflags,
00166 #else
00167             cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
00168 #endif
00169             cdr->accountcode
00170 #           if LOG_UNIQUEID
00171             ,cdr->uniqueid
00172 #           endif
00173 #           if LOG_USERFIELD
00174             ,cdr->userfield
00175 #           endif
00176          );
00177       if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
00178          break;
00179       usleep(200);
00180    }
00181 
00182    if (zErr) {
00183       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00184       ast_free(zErr);
00185    }
00186 
00187    ast_mutex_unlock(&sqlite_lock);
00188    return res;
00189 }

static int unload_module ( void   )  [static]

Definition at line 191 of file cdr_sqlite.c.

References ast_cdr_unregister().

00192 {
00193    ast_cdr_unregister(name);
00194    if (db) {
00195       sqlite_close(db);
00196    }
00197    return 0;
00198 }


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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, } [static]

Definition at line 248 of file cdr_sqlite.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 248 of file cdr_sqlite.c.

sqlite* db = NULL [static]

Definition at line 62 of file cdr_sqlite.c.

Referenced by ast_config_internal_load(), ast_destroy_realtime(), ast_load_realtime_helper(), ast_load_realtime_multientry(), ast_realtime_require_field(), ast_store_realtime(), ast_unload_realtime(), ast_update2_realtime(), ast_update_realtime(), and my_swap_subchannels().

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


Generated on Mon Oct 8 12:39:12 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7