From d5fa28417e8fb52e7708fe63727ec226e7aa5bc8 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Wed, 25 Jun 2014 22:17:24 +0000 Subject: [PATCH] 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`. --- .../conf/autoload_configs/format_cdr.conf.xml | 2 ++ .../mod_format_cdr/mod_format_cdr.c | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/mod/event_handlers/mod_format_cdr/conf/autoload_configs/format_cdr.conf.xml b/src/mod/event_handlers/mod_format_cdr/conf/autoload_configs/format_cdr.conf.xml index 227bcb4722..3e5cdaf153 100644 --- a/src/mod/event_handlers/mod_format_cdr/conf/autoload_configs/format_cdr.conf.xml +++ b/src/mod/event_handlers/mod_format_cdr/conf/autoload_configs/format_cdr.conf.xml @@ -29,6 +29,8 @@ + + diff --git a/src/mod/event_handlers/mod_format_cdr/mod_format_cdr.c b/src/mod/event_handlers/mod_format_cdr/mod_format_cdr.c index 57d3e1ba56..6852e8d078 100644 --- a/src/mod/event_handlers/mod_format_cdr/mod_format_cdr.c +++ b/src/mod/event_handlers/mod_format_cdr/mod_format_cdr.c @@ -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);