]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-3763 --resolve
authorMarc Olivier Chouinard <mochouinard@moctel.com>
Sun, 18 Dec 2011 22:04:11 +0000 (17:04 -0500)
committerMarc Olivier Chouinard <mochouinard@moctel.com>
Sun, 18 Dec 2011 22:04:11 +0000 (17:04 -0500)
conf/autoload_configs/logfile.conf.xml
src/mod/loggers/mod_logfile/mod_logfile.c

index 57f276c4eaf746ad61d95941197c4342b6dbc836..65bea552bd51d6796a27c525ad6c4ad8b371014f 100644 (file)
@@ -10,6 +10,9 @@
        <!--<param name="logfile" value="/var/log/freeswitch.log"/>-->
         <!-- At this length in bytes rotate the log file (0 for never) -->
         <param name="rollover" value="10485760"/>
+               <!-- Maximum number of log files to keep before wrapping -->
+               <!-- If this parameter is enabled, the log filenames will not include a date stamp -->
+               <!-- <param name="maximum-rotate" value="32"/> -->
                <!-- Uncomment to prefix all log lines by the session's uuid  -->
                <!-- <param name="uuid" value="true" /> -->
       </settings>
index 63de81b24dc66d08a9449f7a8b396d9b5d1d134e..b768d6bff0704047aca35c282147f4d74989a5f7 100644 (file)
@@ -53,10 +53,12 @@ struct logfile_profile {
        char *name;
        switch_size_t log_size;         /* keep the log size in check for rotation */
        switch_size_t roll_size;        /* the size that we want to rotate the file at */
+       switch_size_t max_rot;          /* number of log files to keep within the rotation */
        char *logfile;
        switch_file_t *log_afd;
        switch_hash_t *log_hash;
        uint32_t all_level;
+       uint32_t suffix;                        /* suffix of the highest logfile name */
        switch_bool_t log_uuid;
 };
 
@@ -140,6 +142,58 @@ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile)
        switch_core_new_memory_pool(&pool);
        filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET);
 
+       if (profile->max_rot) {
+               char *from_filename = NULL;
+               char *to_filename = NULL;
+
+               from_filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET);
+               to_filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET);
+
+               for (i=profile->suffix; i>1; i--) {
+                       sprintf((char *) to_filename, "%s.%i", profile->logfile, i);
+                       sprintf((char *) from_filename, "%s.%i", profile->logfile, i-1);
+
+                       if (switch_file_exists(to_filename, pool) == SWITCH_STATUS_SUCCESS) {
+                               if ((status = switch_file_remove(to_filename, pool)) != SWITCH_STATUS_SUCCESS) {
+                                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error removing log %s\n",to_filename);
+                                       goto end;
+                               }
+                       }
+
+                       if ((status = switch_file_rename(from_filename, to_filename, pool)) != SWITCH_STATUS_SUCCESS) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error renaming log from %s to %s\n",from_filename, to_filename);
+                               goto end;
+                       }
+               }
+
+               sprintf((char *) to_filename, "%s.%i", profile->logfile, i);
+                       
+               if (switch_file_exists(to_filename, pool) == SWITCH_STATUS_SUCCESS) {
+                       if ((status = switch_file_remove(to_filename, pool)) != SWITCH_STATUS_SUCCESS) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error removing log %s\n",to_filename);
+                               goto end;
+                       }
+               }
+
+               switch_file_close(profile->log_afd);
+               if ((status = switch_file_rename(profile->logfile, to_filename, pool)) != SWITCH_STATUS_SUCCESS) {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error renaming log from %s to %s\n", profile->logfile, to_filename);
+                       goto end;
+               }
+
+               if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error reopening log %s\n",profile->logfile);
+               }
+               if (profile->suffix < profile->max_rot) {
+                       profile->suffix++;
+               }
+
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n");
+
+               goto end;
+       }
+
+       /* XXX This have no real value EXCEPT making sure if we rotate within the same second, the end index will increase */
        for (i = 1; i < MAX_ROT; i++) {
                sprintf((char *) filename, "%s.%s.%i", profile->logfile, date, i);
                if (switch_file_exists(filename, pool) == SWITCH_STATUS_SUCCESS) {
@@ -269,6 +323,7 @@ static switch_status_t load_profile(switch_xml_t xml)
        switch_core_hash_init(&(new_profile->log_hash), module_pool);
        new_profile->name = switch_core_strdup(module_pool, switch_str_nil(name));
 
+       new_profile->suffix = 1;
 
        if ((settings = switch_xml_child(xml, "settings"))) {
                for (param = switch_xml_child(settings, "param"); param; param = param->next) {
@@ -281,6 +336,11 @@ static switch_status_t load_profile(switch_xml_t xml)
                                if (new_profile->roll_size < 0) {
                                        new_profile->roll_size = 0;
                                }
+                       } else if (!strcmp(var, "maximum-rotate")) {
+                               new_profile->max_rot = atoi(val);
+                               if (new_profile->max_rot < 0) {
+                                       new_profile->max_rot = MAX_ROT;
+                               }
                        } else if (!strcmp(var, "uuid") && switch_true(val)) {
                                new_profile->log_uuid = SWITCH_TRUE;
                        }