mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-30 10:33:13 +00:00
cdr/cdr_csv.c: Refactor, function to write content of csv file.
Create a function for write content of CDR on csv files. Before used same code for write two distinct files (account and master cdr) instead use a function for thats. Reduced to one lock when files are written. Change-Id: Idce707f4c108083252e0aeb948f421d924953e65
This commit is contained in:
@@ -93,8 +93,7 @@ static const char config[] = "cdr.conf";
|
|||||||
|
|
||||||
static char *name = "csv";
|
static char *name = "csv";
|
||||||
|
|
||||||
AST_MUTEX_DEFINE_STATIC(mf_lock);
|
AST_MUTEX_DEFINE_STATIC(f_lock);
|
||||||
AST_MUTEX_DEFINE_STATIC(acf_lock);
|
|
||||||
|
|
||||||
static int load_config(int reload)
|
static int load_config(int reload)
|
||||||
{
|
{
|
||||||
@@ -251,36 +250,37 @@ static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int writefile(char *s, char *acc)
|
static int writefile(char *s, char *file_path)
|
||||||
{
|
{
|
||||||
char tmp[PATH_MAX];
|
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
/* because of the absolutely unconditional need for the
|
||||||
if (strchr(acc, '/') || (acc[0] == '.')) {
|
highest reliability possible in writing billing records,
|
||||||
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
|
we open write and close the log file each time */
|
||||||
return -1;
|
if (!(f = fopen(file_path, "a"))) {
|
||||||
}
|
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path, strerror(errno));
|
||||||
|
|
||||||
snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
|
|
||||||
|
|
||||||
ast_mutex_lock(&acf_lock);
|
|
||||||
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;
|
return -1;
|
||||||
}
|
}
|
||||||
fputs(s, f);
|
fputs(s, f);
|
||||||
fflush(f);
|
fflush(f); /* be particularly anal here */
|
||||||
fclose(f);
|
fclose(f);
|
||||||
ast_mutex_unlock(&acf_lock);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int writefile_account(char *s, char *acc)
|
||||||
|
{
|
||||||
|
char file_account[PATH_MAX];
|
||||||
|
if (strchr(acc, '/') || (acc[0] == '.')) {
|
||||||
|
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
snprintf(file_account, sizeof(file_account), "%s/%s/%s.csv", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
|
||||||
|
return writefile(s, file_account);
|
||||||
|
}
|
||||||
|
|
||||||
static int csv_log(struct ast_cdr *cdr)
|
static int csv_log(struct ast_cdr *cdr)
|
||||||
{
|
{
|
||||||
FILE *mf = NULL;
|
|
||||||
/* Make sure we have a big enough buf */
|
/* Make sure we have a big enough buf */
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
char csvmaster[PATH_MAX];
|
char csvmaster[PATH_MAX];
|
||||||
@@ -290,26 +290,15 @@ static int csv_log(struct ast_cdr *cdr)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* because of the absolutely unconditional need for the
|
ast_mutex_lock(&f_lock);
|
||||||
highest reliability possible in writing billing records,
|
if (writefile(buf, csvmaster))
|
||||||
we open write and close the log file each time */
|
ast_log(LOG_WARNING, "Unable to write CSV record to master '%s' : %s\n", csvmaster, strerror(errno));
|
||||||
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 (accountlogs && !ast_strlen_zero(cdr->accountcode)) {
|
if (accountlogs && !ast_strlen_zero(cdr->accountcode)) {
|
||||||
if (writefile(buf, cdr->accountcode))
|
if (writefile_account(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));
|
||||||
}
|
}
|
||||||
|
ast_mutex_unlock(&f_lock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user