[svn-commits] file: trunk r83183 - /trunk/cdr/cdr_csv.c
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Wed Sep 19 15:06:31 CDT 2007
Author: file
Date: Wed Sep 19 15:06:31 2007
New Revision: 83183
URL: http://svn.digium.com/view/asterisk?view=rev&rev=83183
Log:
Clean up code in cdr_csv. (Are you sensing a theme for me today?)
Modified:
trunk/cdr/cdr_csv.c
Modified: trunk/cdr/cdr_csv.c
URL: http://svn.digium.com/view/asterisk/trunk/cdr/cdr_csv.c?view=diff&rev=83183&r1=83182&r2=83183
==============================================================================
--- trunk/cdr/cdr_csv.c (original)
+++ trunk/cdr/cdr_csv.c Wed Sep 19 15:06:31 2007
@@ -106,43 +106,34 @@
usegmtime = 0;
loguniqueid = 0;
loguserfield = 0;
+
+ if (!(cfg = ast_config_load(config, config_flags))) {
+ ast_log(LOG_WARNING, "unable to load config: %s\n", config);
+ return 0;
+ } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
+ return 0;
+
+ if (!(var = ast_variable_browse(cfg, "csv"))) {
+ ast_config_destroy(cfg);
+ return 0;
+ }
- cfg = ast_config_load(config, config_flags);
-
- if (!cfg) {
- ast_log(LOG_WARNING, "unable to load config: %s\n", config);
- return 0;
- } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
- return 0;
-
- var = ast_variable_browse(cfg, "csv");
- if (!var) {
- ast_config_destroy(cfg);
- return 0;
- }
-
- tmp = ast_variable_retrieve(cfg, "csv", "usegmtime");
- if (tmp) {
+ if ((tmp = ast_variable_retrieve(cfg, "csv", "usegmtime"))) {
usegmtime = ast_true(tmp);
- if (usegmtime) {
+ if (usegmtime)
ast_debug(1, "logging time in GMT\n");
- }
- }
-
- tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid");
- if (tmp) {
+ }
+
+ if ((tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid"))) {
loguniqueid = ast_true(tmp);
- if (loguniqueid) {
+ if (loguniqueid)
ast_debug(1, "logging CDR field UNIQUEID\n");
- }
- }
-
- tmp = ast_variable_retrieve(cfg, "csv", "loguserfield");
- if (tmp) {
+ }
+
+ if ((tmp = ast_variable_retrieve(cfg, "csv", "loguserfield"))) {
loguserfield = ast_true(tmp);
- if (loguserfield) {
+ if (loguserfield)
ast_debug(1, "logging CDR user-defined field\n");
- }
}
ast_config_destroy(cfg);
@@ -151,13 +142,13 @@
static int append_string(char *buf, char *s, size_t bufsize)
{
- int pos = strlen(buf);
- int spos = 0;
- int error = 0;
+ int pos = strlen(buf), spos = 0, error = -1;
+
if (pos >= bufsize - 4)
return -1;
+
buf[pos++] = '\"';
- error = -1;
+
while(pos < bufsize - 3) {
if (!s[spos]) {
error = 0;
@@ -168,9 +159,11 @@
buf[pos++] = s[spos];
spos++;
}
+
buf[pos++] = '\"';
buf[pos++] = ',';
buf[pos++] = '\0';
+
return error;
}
@@ -178,13 +171,17 @@
{
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;
}
@@ -192,14 +189,18 @@
{
char tmp[80] = "";
struct ast_tm tm;
+
if (strlen(buf) > bufsize - 3)
return -1;
+
if (ast_tvzero(tv)) {
strncat(buf, ",", bufsize - strlen(buf) - 1);
return 0;
}
+
ast_localtime(&tv, &tm, usegmtime ? "GMT" : NULL);
ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
+
return append_string(buf, tmp, bufsize);
}
@@ -259,15 +260,16 @@
{
char tmp[PATH_MAX];
FILE *f;
+
if (strchr(acc, '/') || (acc[0] == '.')) {
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
return -1;
}
+
snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
ast_mutex_lock(&acf_lock);
- f = fopen(tmp, "a");
- if (!f) {
+ if (!(f = fopen(tmp, "a"))) {
ast_mutex_unlock(&acf_lock);
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", tmp, strerror(errno));
return -1;
@@ -293,28 +295,29 @@
#endif
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 0;
+ }
+
+ /* because of the absolutely unconditional need for the
+ highest reliability possible in writing billing records,
+ we open write and close the log file each time */
+ ast_mutex_lock(&mf_lock);
+ if ((mf = fopen(csvmaster, "a"))) {
+ fputs(buf, mf);
+ fflush(mf); /* be particularly anal here */
+ fclose(mf);
+ mf = NULL;
+ ast_mutex_unlock(&mf_lock);
} else {
- /* because of the absolutely unconditional need for the
- highest reliability possible in writing billing records,
- we open write and close the log file each time */
- ast_mutex_lock(&mf_lock);
- mf = fopen(csvmaster, "a");
- if (mf) {
- fputs(buf, mf);
- fflush(mf); /* be particularly anal here */
- fclose(mf);
- mf = NULL;
- ast_mutex_unlock(&mf_lock);
- } else {
- ast_mutex_unlock(&mf_lock);
- ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
- }
-
- if (!ast_strlen_zero(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));
- }
- }
+ ast_mutex_unlock(&mf_lock);
+ ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
+ }
+
+ if (!ast_strlen_zero(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));
+ }
+
return 0;
}
@@ -331,10 +334,9 @@
if(!load_config(0))
return AST_MODULE_LOAD_DECLINE;
- res = ast_cdr_register(name, ast_module_info->description, csv_log);
- if (res) {
+ if ((res = ast_cdr_register(name, ast_module_info->description, csv_log)))
ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
- }
+
return res;
}
More information about the svn-commits
mailing list