[asterisk-commits] juggie: branch group/NoLossCDR-Redux2 r109829 - /team/group/NoLossCDR-Redux2/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Mar 18 18:55:47 CDT 2008


Author: juggie
Date: Tue Mar 18 18:55:46 2008
New Revision: 109829

URL: http://svn.digium.com/view/asterisk?view=rev&rev=109829
Log:
revert change that russell tells me should be in its own branch, new branch to come shortly

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=109829&r1=109828&r2=109829
==============================================================================
--- team/group/NoLossCDR-Redux2/cdr/cdr_csv.c (original)
+++ team/group/NoLossCDR-Redux2/cdr/cdr_csv.c Tue Mar 18 18:55:46 2008
@@ -132,65 +132,120 @@
 	return 1;
 }
 
-static char * csv_date(char * tmp, struct timeval tv)
-{
-	tmp = '\0';
+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] = "";
 	struct ast_tm tm;
 
+	if (strlen(buf) > bufsize - 3)
+		return -1;
+
 	if (ast_tvzero(tv)) {
-		return tmp;
+		strncat(buf, ",", bufsize - strlen(buf) - 1);
+		return 0;
 	}
 
 	ast_localtime(&tv, &tm, usegmtime ? "GMT" : NULL);
 	ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
 
-	return tmp;
-}
-
-static int build_csv_record(struct ast_str *buf, struct ast_cdr *cdr)
-{
-	char tmp[80];
-
+	return append_string(buf, tmp, bufsize);
+}
+
+static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
+{
+
+	buf[0] = '\0';
 	/* Account code */
-	ast_str_set(&buf, 0, "%s", cdr->accountcode);
+	append_string(buf, cdr->accountcode, bufsize);
 	/* Source */
-	ast_str_append(&buf, 0, ",%s", cdr->src);
+	append_string(buf, cdr->src, bufsize);
 	/* Destination */
-	ast_str_append(&buf, 0, ",%s", cdr->dst);
+	append_string(buf, cdr->dst, bufsize);
 	/* Destination context */
-	ast_str_append(&buf, 0, ",%s", cdr->dcontext);
+	append_string(buf, cdr->dcontext, bufsize);
 	/* Caller*ID */
-	ast_str_append(&buf, 0, ",%s", cdr->clid);
+	append_string(buf, cdr->clid, bufsize);
 	/* Channel */
-	ast_str_append(&buf, 0, ",%s", cdr->channel);
+	append_string(buf, cdr->channel, bufsize);
 	/* Destination Channel */
-	ast_str_append(&buf, 0, ",%s", cdr->dstchannel);
+	append_string(buf, cdr->dstchannel, bufsize);
 	/* Last Application */
-	ast_str_append(&buf, 0, ",%s", cdr->lastapp);
+	append_string(buf, cdr->lastapp, bufsize);
 	/* Last Data */
-	ast_str_append(&buf, 0, ",%s", cdr->lastdata);
+	append_string(buf, cdr->lastdata, bufsize);
 	/* Start Time */
-	ast_str_append(&buf, 0, ",%s", csv_date(tmp,cdr->start));
+	append_date(buf, cdr->start, bufsize);
 	/* Answer Time */
-	ast_str_append(&buf, 0, ",%s", csv_date(tmp,cdr->answer));
+	append_date(buf, cdr->answer, bufsize);
 	/* End Time */
-	ast_str_append(&buf, 0, ",%s", csv_date(tmp,cdr->end));
+	append_date(buf, cdr->end, bufsize);
 	/* Duration */
-	ast_str_append(&buf, 0, ",%li", cdr->duration);
+	append_int(buf, cdr->duration, bufsize);
 	/* Billable seconds */
-	ast_str_append(&buf, 0, ",%li", cdr->billsec);
+	append_int(buf, cdr->billsec, bufsize);
 	/* Disposition */
-	ast_str_append(&buf, 0, ",%s", ast_cdr_disp2str(cdr->disposition));
+	append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
 	/* AMA Flags */
-	ast_str_append(&buf, 0, ",%s", ast_cdr_flags2str(cdr->amaflags));
+	append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
 	/* Unique ID */
 	if (loguniqueid)
-		ast_str_append(&buf, 0, ",%s", cdr->uniqueid);
+		append_string(buf, cdr->uniqueid, bufsize);
 	/* append the user field */
 	if(loguserfield)
-		ast_str_append(&buf, 0, ",%s", cdr->userfield);	
-
-	return 0;
+		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;
 }
 
 static int writefile(char *s, char *acc)
@@ -224,14 +279,14 @@
 {
 	FILE *mf = NULL;
 	/* Make sure we have a big enough buf */
-	struct ast_str *buf=ast_str_create(512);
+	char buf[1024];
 	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, cdr)) {
+	if (build_csv_record(buf, sizeof(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;
 	}
@@ -241,7 +296,7 @@
 	   we open write and close the log file each time */
 	ast_mutex_lock(&mf_lock);
 	if ((mf = fopen(csvmaster, "a"))) {
-		fputs(buf->str, mf);
+		fputs(buf, mf);
 		fflush(mf); /* be particularly anal here */
 		fclose(mf);
 		mf = NULL;
@@ -254,7 +309,7 @@
 	}
 	
 	if (!ast_strlen_zero(cdr->accountcode)) {
-		if (writefile(buf->str, cdr->accountcode))
+		if (writefile(buf, 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