Clean up code in cdr_csv. (Are you sensing a theme for me today?)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@83183 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.6.0
Joshua Colp 18 years ago
parent c1d1440e59
commit 2f0dba3010

@ -107,43 +107,34 @@ static int load_config(int reload)
loguniqueid = 0; loguniqueid = 0;
loguserfield = 0; loguserfield = 0;
cfg = ast_config_load(config, config_flags); if (!(cfg = ast_config_load(config, config_flags))) {
if (!cfg) {
ast_log(LOG_WARNING, "unable to load config: %s\n", config); ast_log(LOG_WARNING, "unable to load config: %s\n", config);
return 0; return 0;
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
return 0; return 0;
var = ast_variable_browse(cfg, "csv"); if (!(var = ast_variable_browse(cfg, "csv"))) {
if (!var) {
ast_config_destroy(cfg); ast_config_destroy(cfg);
return 0; return 0;
} }
tmp = ast_variable_retrieve(cfg, "csv", "usegmtime"); if ((tmp = ast_variable_retrieve(cfg, "csv", "usegmtime"))) {
if (tmp) {
usegmtime = ast_true(tmp); usegmtime = ast_true(tmp);
if (usegmtime) { if (usegmtime)
ast_debug(1, "logging time in GMT\n"); ast_debug(1, "logging time in GMT\n");
} }
}
tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid"); if ((tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid"))) {
if (tmp) {
loguniqueid = ast_true(tmp); loguniqueid = ast_true(tmp);
if (loguniqueid) { if (loguniqueid)
ast_debug(1, "logging CDR field UNIQUEID\n"); ast_debug(1, "logging CDR field UNIQUEID\n");
} }
}
tmp = ast_variable_retrieve(cfg, "csv", "loguserfield"); if ((tmp = ast_variable_retrieve(cfg, "csv", "loguserfield"))) {
if (tmp) {
loguserfield = ast_true(tmp); loguserfield = ast_true(tmp);
if (loguserfield) { if (loguserfield)
ast_debug(1, "logging CDR user-defined field\n"); ast_debug(1, "logging CDR user-defined field\n");
} }
}
ast_config_destroy(cfg); ast_config_destroy(cfg);
return 1; return 1;
@ -151,13 +142,13 @@ static int load_config(int reload)
static int append_string(char *buf, char *s, size_t bufsize) static int append_string(char *buf, char *s, size_t bufsize)
{ {
int pos = strlen(buf); int pos = strlen(buf), spos = 0, error = -1;
int spos = 0;
int error = 0;
if (pos >= bufsize - 4) if (pos >= bufsize - 4)
return -1; return -1;
buf[pos++] = '\"'; buf[pos++] = '\"';
error = -1;
while(pos < bufsize - 3) { while(pos < bufsize - 3) {
if (!s[spos]) { if (!s[spos]) {
error = 0; error = 0;
@ -168,9 +159,11 @@ static int append_string(char *buf, char *s, size_t bufsize)
buf[pos++] = s[spos]; buf[pos++] = s[spos];
spos++; spos++;
} }
buf[pos++] = '\"'; buf[pos++] = '\"';
buf[pos++] = ','; buf[pos++] = ',';
buf[pos++] = '\0'; buf[pos++] = '\0';
return error; return error;
} }
@ -178,13 +171,17 @@ static int append_int(char *buf, int s, size_t bufsize)
{ {
char tmp[32]; char tmp[32];
int pos = strlen(buf); int pos = strlen(buf);
snprintf(tmp, sizeof(tmp), "%d", s); snprintf(tmp, sizeof(tmp), "%d", s);
if (pos + strlen(tmp) > bufsize - 3) if (pos + strlen(tmp) > bufsize - 3)
return -1; return -1;
strncat(buf, tmp, bufsize - strlen(buf) - 1); strncat(buf, tmp, bufsize - strlen(buf) - 1);
pos = strlen(buf); pos = strlen(buf);
buf[pos++] = ','; buf[pos++] = ',';
buf[pos++] = '\0'; buf[pos++] = '\0';
return 0; return 0;
} }
@ -192,14 +189,18 @@ static int append_date(char *buf, struct timeval tv, size_t bufsize)
{ {
char tmp[80] = ""; char tmp[80] = "";
struct ast_tm tm; struct ast_tm tm;
if (strlen(buf) > bufsize - 3) if (strlen(buf) > bufsize - 3)
return -1; return -1;
if (ast_tvzero(tv)) { if (ast_tvzero(tv)) {
strncat(buf, ",", bufsize - strlen(buf) - 1); strncat(buf, ",", bufsize - strlen(buf) - 1);
return 0; return 0;
} }
ast_localtime(&tv, &tm, usegmtime ? "GMT" : NULL); ast_localtime(&tv, &tm, usegmtime ? "GMT" : NULL);
ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm); ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
return append_string(buf, tmp, bufsize); return append_string(buf, tmp, bufsize);
} }
@ -259,15 +260,16 @@ static int writefile(char *s, char *acc)
{ {
char tmp[PATH_MAX]; char tmp[PATH_MAX];
FILE *f; FILE *f;
if (strchr(acc, '/') || (acc[0] == '.')) { if (strchr(acc, '/') || (acc[0] == '.')) {
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc); ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
return -1; return -1;
} }
snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc); snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
ast_mutex_lock(&acf_lock); ast_mutex_lock(&acf_lock);
f = fopen(tmp, "a"); if (!(f = fopen(tmp, "a"))) {
if (!f) {
ast_mutex_unlock(&acf_lock); ast_mutex_unlock(&acf_lock);
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", tmp, strerror(errno)); ast_log(LOG_ERROR, "Unable to open file %s : %s\n", tmp, strerror(errno));
return -1; return -1;
@ -293,13 +295,14 @@ static int csv_log(struct ast_cdr *cdr)
#endif #endif
if (build_csv_record(buf, sizeof(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)); ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
} else { return 0;
}
/* because of the absolutely unconditional need for the /* because of the absolutely unconditional need for the
highest reliability possible in writing billing records, highest reliability possible in writing billing records,
we open write and close the log file each time */ we open write and close the log file each time */
ast_mutex_lock(&mf_lock); ast_mutex_lock(&mf_lock);
mf = fopen(csvmaster, "a"); if ((mf = fopen(csvmaster, "a"))) {
if (mf) {
fputs(buf, mf); fputs(buf, mf);
fflush(mf); /* be particularly anal here */ fflush(mf); /* be particularly anal here */
fclose(mf); fclose(mf);
@ -314,7 +317,7 @@ static int csv_log(struct ast_cdr *cdr)
if (writefile(buf, 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_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
} }
}
return 0; return 0;
} }
@ -331,10 +334,9 @@ static int load_module(void)
if(!load_config(0)) if(!load_config(0))
return AST_MODULE_LOAD_DECLINE; return AST_MODULE_LOAD_DECLINE;
res = ast_cdr_register(name, ast_module_info->description, csv_log); if ((res = ast_cdr_register(name, ast_module_info->description, csv_log)))
if (res) {
ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n"); ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
}
return res; return res;
} }

Loading…
Cancel
Save