dect
/
asterisk
Archived
13
0
Fork 0

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
This commit is contained in:
file 2007-09-19 20:06:31 +00:00
parent f847a4fdb8
commit e346df68a2
1 changed files with 51 additions and 49 deletions

View File

@ -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));
} 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));
}
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 {
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;
}