aboutsummaryrefslogtreecommitdiffstats
path: root/cdr
diff options
context:
space:
mode:
authorfile <file@f38db490-d61c-443f-a65b-d21fe96a405b>2007-09-19 20:06:31 +0000
committerfile <file@f38db490-d61c-443f-a65b-d21fe96a405b>2007-09-19 20:06:31 +0000
commite346df68a26ee68e3a5612add3ce1514146e1c4f (patch)
tree77dc92ca4c13e19e822b02ae3ce1d52312886719 /cdr
parentf847a4fdb80bd790b5860cbfbf19de9c9fa79fe8 (diff)
Clean up code in cdr_csv. (Are you sensing a theme for me today?)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@83183 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'cdr')
-rw-r--r--cdr/cdr_csv.c98
1 files changed, 50 insertions, 48 deletions
diff --git a/cdr/cdr_csv.c b/cdr/cdr_csv.c
index 72f0c1877..1a0fcaa30 100644
--- a/cdr/cdr_csv.c
+++ b/cdr/cdr_csv.c
@@ -106,43 +106,34 @@ static int load_config(int reload)
usegmtime = 0;
loguniqueid = 0;
loguserfield = 0;
-
- cfg = ast_config_load(config, config_flags);
-
- if (!cfg) {
+
+ 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;
-
- var = ast_variable_browse(cfg, "csv");
- if (!var) {
+
+ if (!(var = ast_variable_browse(cfg, "csv"))) {
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 load_config(int reload)
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 @@ static int append_string(char *buf, char *s, size_t bufsize)
buf[pos++] = s[spos];
spos++;
}
+
buf[pos++] = '\"';
buf[pos++] = ',';
buf[pos++] = '\0';
+
return error;
}
@@ -178,13 +171,17 @@ 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;
}
@@ -192,14 +189,18 @@ 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)) {
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 @@ static int writefile(char *s, char *acc)
{
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 @@ static int csv_log(struct ast_cdr *cdr)
#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 @@ static int load_module(void)
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;
}