<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14300] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14300">14300</a></dd>
<dt>Author</dt> <dd>mrene</dd>
<dt>Date</dt> <dd>2009-07-20 09:52:55 -0500 (Mon, 20 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre><a href="http://jira.freeswitch.org/browse/XML-6">XML-6</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodxml_intmod_xml_cdrmod_xml_cdrc">freeswitch/trunk/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodxml_intmod_xml_cdrmod_xml_cdrc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c (14299 => 14300)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c        2009-07-20 06:10:15 UTC (rev 14299)
+++ freeswitch/trunk/src/mod/xml_int/mod_xml_cdr/mod_xml_cdr.c        2009-07-20 14:52:55 UTC (rev 14300)
</span><span class="lines">@@ -40,6 +40,9 @@
</span><span class="cx">         char *urls[MAX_URLS+1];
</span><span class="cx">         int url_count;
</span><span class="cx">         int url_index;
</span><ins>+        switch_thread_rwlock_t *log_path_lock;
+        char *base_log_dir;
+        char *base_err_log_dir;
</ins><span class="cx">         char *log_dir;
</span><span class="cx">         char *err_log_dir;
</span><span class="cx">         uint32_t delay;
</span><span class="lines">@@ -56,6 +59,7 @@
</span><span class="cx">         int log_b;
</span><span class="cx">         int prefix_a;
</span><span class="cx">         int disable100continue;
</span><ins>+        int rotate;
</ins><span class="cx">         switch_memory_pool_t *pool;
</span><span class="cx"> } globals;
</span><span class="cx"> 
</span><span class="lines">@@ -72,6 +76,106 @@
</span><span class="cx">         return size * nitems;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static switch_status_t set_xml_cdr_log_dirs()
+{
+        switch_time_exp_t tm;
+        char *path = NULL;
+        char date[80] = &quot;&quot;;
+        switch_size_t retsize;
+        switch_status_t status = SWITCH_STATUS_SUCCESS, dir_status;
+
+        switch_time_exp_lt(&amp;tm, switch_micro_time_now());
+        switch_strftime_nocheck(date, &amp;retsize, sizeof(date), &quot;%Y-%m-%d-%H-%M-%S&quot;, &amp;tm);
+
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;Rotating log file paths\n&quot;);
+
+        if (!switch_strlen_zero(globals.base_log_dir)) {
+                if (globals.rotate) {
+                        if ((path = switch_mprintf(&quot;%s%s%s&quot;, globals.base_log_dir, SWITCH_PATH_SEPARATOR, date))) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;Rotating log file path to %s\n&quot;, path);
+
+                                dir_status = SWITCH_STATUS_SUCCESS;
+                                if (switch_directory_exists(path, globals.pool) != SWITCH_STATUS_SUCCESS) {
+                                        dir_status = switch_dir_make(path, SWITCH_FPROT_OS_DEFAULT, globals.pool);
+                                }
+
+                                if (dir_status == SWITCH_STATUS_SUCCESS) {
+                                        switch_thread_rwlock_wrlock(globals.log_path_lock);
+                                        switch_safe_free(globals.log_dir);
+                                        globals.log_dir = path;
+                                        switch_thread_rwlock_unlock(globals.log_path_lock);
+                                }
+                                else {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Failed to create new mod_xml_cdr log_dir path\n&quot;);
+                                        switch_safe_free(path);
+                                        status = SWITCH_STATUS_FALSE;
+                                }
+                        }
+                        else {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Failed to generate new mod_xml_cdr log_dir path\n&quot;);
+                                status = SWITCH_STATUS_FALSE;
+                        }
+                }
+                else {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;Setting log file path to %s\n&quot;, globals.base_log_dir);
+                        if ((path = switch_safe_strdup(globals.base_log_dir))) {
+                                switch_thread_rwlock_wrlock(globals.log_path_lock);
+                                switch_safe_free(globals.log_dir);
+                                globals.log_dir = path;
+                                switch_thread_rwlock_unlock(globals.log_path_lock);
+                        }
+                        else {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Failed to set log_dir path\n&quot;);
+                                status = SWITCH_STATUS_FALSE;
+                        }
+                }
+        }
+
+        if (!switch_strlen_zero(globals.base_err_log_dir)) {
+                if (globals.rotate) {
+                        if ((path = switch_mprintf(&quot;%s%s%s&quot;, globals.base_err_log_dir, SWITCH_PATH_SEPARATOR, date))) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;Rotating err log file path to %s\n&quot;, path);
+
+                                dir_status = SWITCH_STATUS_SUCCESS;
+                                if (switch_directory_exists(path, globals.pool) != SWITCH_STATUS_SUCCESS) {
+                                        dir_status = switch_dir_make(path, SWITCH_FPROT_OS_DEFAULT, globals.pool);
+                                }
+
+                                if (dir_status == SWITCH_STATUS_SUCCESS) {
+                                        switch_thread_rwlock_wrlock(globals.log_path_lock);
+                                        switch_safe_free(globals.err_log_dir);
+                                        globals.err_log_dir = path;
+                                        switch_thread_rwlock_unlock(globals.log_path_lock);
+                                }
+                                else {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Failed to create new mod_xml_cdr err_log_dir path\n&quot;);
+                                        switch_safe_free(path);
+                                        status = SWITCH_STATUS_FALSE;
+                                }
+                        }
+                        else {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Failed to generate new mod_xml_cdr err_log_dir path\n&quot;);
+                                status = SWITCH_STATUS_FALSE;
+                        }
+                }
+                else {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;Setting err log file path to %s\n&quot;, globals.base_err_log_dir);
+                        if ((path = switch_safe_strdup(globals.base_err_log_dir))) {
+                                switch_thread_rwlock_wrlock(globals.log_path_lock);
+                                switch_safe_free(globals.err_log_dir);
+                                globals.err_log_dir = path;
+                                switch_thread_rwlock_unlock(globals.log_path_lock);
+                        }
+                        else {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Failed to set err_log_dir path\n&quot;);
+                                status = SWITCH_STATUS_FALSE;
+                        }
+                }
+        }
+
+        return status;
+}
+
</ins><span class="cx"> static switch_status_t my_on_reporting(switch_core_session_t *session)
</span><span class="cx"> {
</span><span class="cx">         switch_xml_t cdr;
</span><span class="lines">@@ -113,12 +217,16 @@
</span><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        switch_thread_rwlock_rdlock(globals.log_path_lock);
+
</ins><span class="cx">         if (!(logdir = switch_channel_get_variable(channel, &quot;xml_cdr_base&quot;))) {
</span><span class="cx">                 logdir = globals.log_dir;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (!switch_strlen_zero(logdir)) {
</span><del>-                if ((path = switch_mprintf(&quot;%s%s%s%s.cdr.xml&quot;, logdir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session)))) {
</del><ins>+                path = switch_mprintf(&quot;%s%s%s%s.cdr.xml&quot;, logdir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session));
+                switch_thread_rwlock_unlock(globals.log_path_lock);
+                if (path) {
</ins><span class="cx"> #ifdef _MSC_VER
</span><span class="cx">                         if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) &gt; -1) {
</span><span class="cx"> #else
</span><span class="lines">@@ -140,6 +248,9 @@
</span><span class="cx">                         switch_safe_free(path);
</span><span class="cx">                 }
</span><span class="cx">         }
</span><ins>+        else {
+                switch_thread_rwlock_unlock(globals.log_path_lock);
+        }
</ins><span class="cx"> 
</span><span class="cx">         /* try to post it to the web server */
</span><span class="cx">         if (globals.url_count) {
</span><span class="lines">@@ -259,7 +370,10 @@
</span><span class="cx">                 /* if we are here the web post failed for some reason */
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Unable to post to web server, writing to file\n&quot;);
</span><span class="cx"> 
</span><del>-                if ((path = switch_mprintf(&quot;%s%s%s%s.cdr.xml&quot;, globals.err_log_dir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session)))) {
</del><ins>+                switch_thread_rwlock_rdlock(globals.log_path_lock);
+                path = switch_mprintf(&quot;%s%s%s%s.cdr.xml&quot;, globals.err_log_dir, SWITCH_PATH_SEPARATOR, a_prefix, switch_core_session_get_uuid(session));
+                switch_thread_rwlock_unlock(globals.log_path_lock);
+                if (path) {
</ins><span class="cx"> #ifdef _MSC_VER
</span><span class="cx">                         if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) &gt; -1) {
</span><span class="cx"> #else
</span><span class="lines">@@ -302,6 +416,17 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static void event_handler(switch_event_t *event)
+{
+        const char *sig = switch_event_get_header(event, &quot;Trapped-Signal&quot;);
+
+        if (sig &amp;&amp; !strcmp(sig, &quot;HUP&quot;)) {
+                if (globals.rotate) {
+                        set_xml_cdr_log_dirs();
+                }
+        }
+}
+
</ins><span class="cx"> static switch_state_handler_table_t state_handlers = {
</span><span class="cx">         /*.on_init */ NULL,
</span><span class="cx">         /*.on_routing */ NULL,
</span><span class="lines">@@ -327,10 +452,16 @@
</span><span class="cx"> 
</span><span class="cx">         *module_interface = switch_loadable_module_create_module_interface(pool, modname);
</span><span class="cx"> 
</span><ins>+        if (switch_event_bind(modname, SWITCH_EVENT_TRAP, SWITCH_EVENT_SUBCLASS_ANY, event_handler, NULL) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't bind!\n&quot;);
+                return SWITCH_STATUS_GENERR;
+        }
+
</ins><span class="cx">         memset(&amp;globals, 0, sizeof(globals));
</span><span class="cx">         globals.log_b = 1;
</span><span class="cx">         globals.disable100continue = 0;
</span><span class="cx">         globals.pool = pool;
</span><ins>+        switch_thread_rwlock_create(&amp;globals.log_path_lock, pool);
</ins><span class="cx"> 
</span><span class="cx">         /* parse the config */
</span><span class="cx">         if (!(xml = switch_xml_open_cfg(cf, &amp;cfg, NULL))) {
</span><span class="lines">@@ -367,24 +498,26 @@
</span><span class="cx">                                 }
</span><span class="cx">                         } else if (!strcasecmp(var, &quot;retries&quot;) &amp;&amp; !switch_strlen_zero(val)) {
</span><span class="cx">                                 globals.retries = (uint32_t) atoi(val);
</span><ins>+                        } else if (!strcasecmp(var, &quot;rotate&quot;) &amp;&amp; !switch_strlen_zero(val)) {
+                                globals.rotate = atoi(val);
</ins><span class="cx">                         } else if (!strcasecmp(var, &quot;log-dir&quot;)) {
</span><span class="cx">                                 if (switch_strlen_zero(val)) {
</span><del>-                                        globals.log_dir = switch_mprintf(&quot;%s%sxml_cdr&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
</del><ins>+                                        globals.base_log_dir = switch_mprintf(&quot;%s%sxml_cdr&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         if (switch_is_file_path(val)) {
</span><del>-                                                globals.log_dir = switch_core_strdup(globals.pool, val);
</del><ins>+                                                globals.base_log_dir = switch_core_strdup(globals.pool, val);
</ins><span class="cx">                                         } else {
</span><del>-                                                globals.log_dir = switch_mprintf(&quot;%s%s%s&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, val);
</del><ins>+                                                globals.base_log_dir = switch_mprintf(&quot;%s%s%s&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, val);
</ins><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="cx">                         } else if (!strcasecmp(var, &quot;err-log-dir&quot;)) {
</span><span class="cx">                                 if (switch_strlen_zero(val)) {
</span><del>-                                        globals.err_log_dir = switch_mprintf(&quot;%s%sxml_cdr&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
</del><ins>+                                        globals.base_err_log_dir = switch_mprintf(&quot;%s%sxml_cdr&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         if (switch_is_file_path(val)) {
</span><del>-                                                globals.err_log_dir = switch_core_strdup(globals.pool, val);
</del><ins>+                                                globals.base_err_log_dir = switch_core_strdup(globals.pool, val);
</ins><span class="cx">                                         } else {
</span><del>-                                                globals.err_log_dir = switch_mprintf(&quot;%s%s%s&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, val);
</del><ins>+                                                globals.base_err_log_dir = switch_mprintf(&quot;%s%s%s&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, val);
</ins><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="cx">                         } else if (!strcasecmp(var, &quot;enable-cacert-check&quot;) &amp;&amp; switch_true(val)) {
</span><span class="lines">@@ -403,11 +536,11 @@
</span><span class="cx">                                 globals.enable_ssl_verifyhost = 1;
</span><span class="cx">                         }
</span><span class="cx">                 
</span><del>-                        if (switch_strlen_zero(globals.err_log_dir)) {
-                                if (!switch_strlen_zero(globals.log_dir)) {
-                                        globals.err_log_dir = switch_core_strdup(globals.pool, globals.log_dir);
</del><ins>+                        if (switch_strlen_zero(globals.base_err_log_dir)) {
+                                if (!switch_strlen_zero(globals.base_log_dir)) {
+                                        globals.base_err_log_dir = switch_core_strdup(globals.pool, globals.base_log_dir);
</ins><span class="cx">                                 } else {
</span><del>-                                        globals.err_log_dir = switch_core_sprintf(globals.pool, &quot;%s%sxml_cdr&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
</del><ins>+                                        globals.base_err_log_dir = switch_core_sprintf(globals.pool, &quot;%s%sxml_cdr&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="lines">@@ -424,6 +557,8 @@
</span><span class="cx"> 
</span><span class="cx">         globals.retries++;
</span><span class="cx"> 
</span><ins>+        set_xml_cdr_log_dirs();
+
</ins><span class="cx">         switch_xml_free(xml);
</span><span class="cx">         return status;
</span><span class="cx"> }
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>