Allow setting format of log filename in format_cdr

This commit allows you to set a `log-file` string parameter in a
format_cdr profile.  This string is a template that may (and should!)
contain variables.  This template will be expanded and used as the
file name of the CDR to be written.  This parameter should contain
only the template for the file name itself; the path is relative to
the `log-dir`.
This commit is contained in:
Travis Cross 2014-06-25 22:17:24 +00:00
parent f7b4ec617a
commit d5fa28417e
2 changed files with 21 additions and 5 deletions

View File

@ -29,6 +29,8 @@
<!-- optional: if not present we do not log every record to disk -->
<!-- either an absolute path, a relative path assuming ${prefix}/logs or a blank value will default to ${prefix}/logs/format_cdr -->
<param name="log-dir" value=""/>
<!-- template for the filename of the saved CDR -->
<!-- <param name="log-file" value="${uuid}.cdr.xml"/> -->
<!-- optional: if not present we do log the b leg -->
<!-- true or false if we should create a cdr for the b leg of a call-->

View File

@ -63,6 +63,7 @@ struct cdr_profile {
char *log_dir;
char *err_log_dir[MAX_ERR_DIRS];
int err_dir_count;
char *log_file;
uint32_t delay;
uint32_t retries;
uint32_t enable_cacert_check;
@ -198,6 +199,7 @@ static switch_status_t my_on_reporting_cb(switch_core_session_t *session, cdr_pr
switch_xml_t xml_cdr = NULL;
cJSON *json_cdr = NULL;
char *cdr_text = NULL;
char *lfile = NULL;
char *dpath = NULL;
char *path = NULL;
char *curl_cdr_text = NULL;
@ -265,9 +267,14 @@ static switch_status_t my_on_reporting_cb(switch_core_session_t *session, cdr_pr
}
if (!zstr(logdir) && (profile->log_http_and_disk || !profile->url_count)) {
if (profile->log_file) {
lfile = switch_channel_expand_variables(channel, profile->log_file);
} else {
lfile = switch_mprintf("%s%s.cdr.%s", a_prefix, switch_core_session_get_uuid(session), profile->format);
}
dpath = switch_mprintf("%s%s%s", logdir, SWITCH_PATH_SEPARATOR, a_prefix);
path = switch_mprintf("%s%s%s%s.cdr.%s", logdir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session),
profile->format);
path = switch_mprintf("%s%s%s", logdir, SWITCH_PATH_SEPARATOR, lfile);
if (lfile != profile->log_file) switch_safe_free(lfile);
switch_thread_rwlock_unlock(profile->log_path_lock);
if (path) {
if (switch_directory_exists(dpath, profile->pool) != SWITCH_STATUS_SUCCESS) {
@ -427,9 +434,14 @@ static switch_status_t my_on_reporting_cb(switch_core_session_t *session, cdr_pr
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to post to web server, writing to file\n");
switch_thread_rwlock_rdlock(profile->log_path_lock);
dpath = switch_mprintf("%s%s%s", profile->err_log_dir, SWITCH_PATH_SEPARATOR, a_prefix);
path = switch_mprintf("%s%s%s%s.cdr.%s", profile->err_log_dir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session),
profile->format);
if (profile->log_file) {
lfile = switch_channel_expand_variables(channel, profile->log_file);
} else {
lfile = switch_mprintf("%s%s.cdr.%s", a_prefix, switch_core_session_get_uuid(session), profile->format);
}
dpath = switch_mprintf("%s%s%s", logdir, SWITCH_PATH_SEPARATOR, a_prefix);
path = switch_mprintf("%s%s%s", logdir, SWITCH_PATH_SEPARATOR, lfile);
if (lfile != profile->log_file) switch_safe_free(lfile);
switch_thread_rwlock_unlock(profile->log_path_lock);
if (path) {
if (switch_directory_exists(dpath, profile->pool) != SWITCH_STATUS_SUCCESS) {
@ -616,6 +628,8 @@ switch_status_t mod_format_cdr_load_profile_xml(switch_xml_t xprofile)
profile->retries = switch_atoui(val);
} else if (!strcasecmp(var, "rotate") && !zstr(val)) {
profile->rotate = switch_true(val);
} else if (!strcasecmp(var, "log-file") && !zstr(val)) {
profile->log_file = switch_core_strdup(profile->pool, val);
} else if (!strcasecmp(var, "log-dir")) {
if (zstr(val)) {
profile->base_log_dir = switch_core_sprintf(profile->pool, "%s%sformat_cdr", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);