[asterisk-commits] juggie: branch group/NoLossCDR-Redux2 r109759 - /team/group/NoLossCDR-Redux2/...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Mar 18 16:58:57 CDT 2008
Author: juggie
Date: Tue Mar 18 16:58:57 2008
New Revision: 109759
URL: http://svn.digium.com/view/asterisk?view=rev&rev=109759
Log:
Checkpoint commit for cdr_csv upgrade to ast_str.
Explosion Potential: High
Modified:
team/group/NoLossCDR-Redux2/cdr/cdr_csv.c
Modified: team/group/NoLossCDR-Redux2/cdr/cdr_csv.c
URL: http://svn.digium.com/view/asterisk/team/group/NoLossCDR-Redux2/cdr/cdr_csv.c?view=diff&rev=109759&r1=109758&r2=109759
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_csv.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_csv.c Tue Mar 18 16:58:57 2008
@@ -132,120 +132,65 @@
return 1;
}
-static int append_string(char *buf, char *s, size_t bufsize)
-{
- int pos = strlen(buf), spos = 0, error = -1;
-
- if (pos >= bufsize - 4)
- return -1;
-
- buf[pos++] = '\"';
-
- while(pos < bufsize - 3) {
- if (!s[spos]) {
- error = 0;
- break;
- }
- if (s[spos] == '\"')
- buf[pos++] = '\"';
- buf[pos++] = s[spos];
- spos++;
- }
-
- buf[pos++] = '\"';
- buf[pos++] = ',';
- buf[pos++] = '\0';
-
- return error;
-}
-
-static int append_int(char *buf, int s, size_t bufsize)
-{
- char tmp[32];
- int pos = strlen(buf);
-
- snprintf(tmp, sizeof(tmp), "%d", s);
-
- if (pos + strlen(tmp) > bufsize - 3)
- return -1;
-
- strncat(buf, tmp, bufsize - strlen(buf) - 1);
- pos = strlen(buf);
- buf[pos++] = ',';
- buf[pos++] = '\0';
-
- return 0;
-}
-
-static int append_date(char *buf, struct timeval tv, size_t bufsize)
-{
- char tmp[80] = "";
+static char * csv_date(char * tmp, struct timeval tv)
+{
+ tmp = '\0';
struct ast_tm tm;
- if (strlen(buf) > bufsize - 3)
- return -1;
-
if (ast_tvzero(tv)) {
- strncat(buf, ",", bufsize - strlen(buf) - 1);
- return 0;
+ return tmp;
}
ast_localtime(&tv, &tm, usegmtime ? "GMT" : NULL);
ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
- return append_string(buf, tmp, bufsize);
-}
-
-static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
-{
-
- buf[0] = '\0';
+ return tmp;
+}
+
+static int build_csv_record(struct ast_str *buf, struct ast_cdr *cdr)
+{
+ char tmp[80];
+
/* Account code */
- append_string(buf, cdr->accountcode, bufsize);
+ ast_str_set(&buf, 0, "%s", cdr->accountcode);
/* Source */
- append_string(buf, cdr->src, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->src);
/* Destination */
- append_string(buf, cdr->dst, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->dst);
/* Destination context */
- append_string(buf, cdr->dcontext, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->dcontext);
/* Caller*ID */
- append_string(buf, cdr->clid, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->clid);
/* Channel */
- append_string(buf, cdr->channel, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->channel);
/* Destination Channel */
- append_string(buf, cdr->dstchannel, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->dstchannel);
/* Last Application */
- append_string(buf, cdr->lastapp, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->lastapp);
/* Last Data */
- append_string(buf, cdr->lastdata, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->lastdata);
/* Start Time */
- append_date(buf, cdr->start, bufsize);
+ ast_str_append(&buf, 0, ",%s", csv_date(tmp,cdr->start));
/* Answer Time */
- append_date(buf, cdr->answer, bufsize);
+ ast_str_append(&buf, 0, ",%s", csv_date(tmp,cdr->answer));
/* End Time */
- append_date(buf, cdr->end, bufsize);
+ ast_str_append(&buf, 0, ",%s", csv_date(tmp,cdr->end));
/* Duration */
- append_int(buf, cdr->duration, bufsize);
+ ast_str_append(&buf, 0, ",%li", cdr->duration);
/* Billable seconds */
- append_int(buf, cdr->billsec, bufsize);
+ ast_str_append(&buf, 0, ",%li", cdr->billsec);
/* Disposition */
- append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
+ ast_str_append(&buf, 0, ",%s", ast_cdr_disp2str(cdr->disposition));
/* AMA Flags */
- append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
+ ast_str_append(&buf, 0, ",%s", ast_cdr_flags2str(cdr->amaflags));
/* Unique ID */
if (loguniqueid)
- append_string(buf, cdr->uniqueid, bufsize);
+ ast_str_append(&buf, 0, ",%s", cdr->uniqueid);
/* append the user field */
if(loguserfield)
- append_string(buf, cdr->userfield,bufsize);
- /* If we hit the end of our buffer, log an error */
- if (strlen(buf) < bufsize - 5) {
- /* Trim off trailing comma */
- buf[strlen(buf) - 1] = '\0';
- strncat(buf, "\n", bufsize - strlen(buf) - 1);
- return 0;
- }
- return -1;
+ ast_str_append(&buf, 0, ",%s", cdr->userfield);
+
+ return 0;
}
static int writefile(char *s, char *acc)
@@ -279,14 +224,14 @@
{
FILE *mf = NULL;
/* Make sure we have a big enough buf */
- char buf[1024];
+ struct ast_str *buf=ast_str_create(512);
char csvmaster[PATH_MAX];
int res = 0;;
snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
#if 0
printf("[CDR] %s ('%s' -> '%s') Dur: %ds Bill: %ds Disp: %s Flags: %s Account: [%s]\n", cdr->channel, cdr->src, cdr->dst, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_cdr_flags2str(cdr->amaflags), cdr->accountcode);
#endif
- if (build_csv_record(buf, sizeof(buf), cdr)) {
+ if (build_csv_record(buf, cdr)) {
ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
return AST_CDR_POST_FAILED;
}
@@ -296,7 +241,7 @@
we open write and close the log file each time */
ast_mutex_lock(&mf_lock);
if ((mf = fopen(csvmaster, "a"))) {
- fputs(buf, mf);
+ fputs(buf->str, mf);
fflush(mf); /* be particularly anal here */
fclose(mf);
mf = NULL;
@@ -309,7 +254,7 @@
}
if (!ast_strlen_zero(cdr->accountcode)) {
- if (writefile(buf, cdr->accountcode))
+ if (writefile(buf->str, cdr->accountcode))
ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
}
More information about the asterisk-commits
mailing list