static void event_handler(switch_event_t *event) { const char *sig = switch_event_get_header(event, "Trapped-Signal"); switch_hash_index_t *hi; void *val; const void *var; logfile_profile_t *profile; if (sig && !strcmp(sig, "HUP")) { if (globals.rotate) { for (hi = switch_core_hash_first(profile_hash); hi; hi = switch_core_hash_next(&hi)) { switch_core_hash_this(hi, &var, NULL, &val); profile = val; mod_logfile_rotate(profile); } } else { switch_mutex_lock(globals.mutex); for (hi = switch_core_hash_first(profile_hash); hi; hi = switch_core_hash_next(&hi)) { switch_core_hash_this(hi, &var, NULL, &val); profile = val; switch_file_close(profile->log_afd); if (mod_logfile_openlogfile(profile, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error Re-opening Log!\n"); } } switch_mutex_unlock(globals.mutex); } } }
/* write to the actual logfile */ static switch_status_t mod_logfile_raw_write(logfile_profile_t *profile, char *log_data) { switch_size_t len; switch_status_t status = SWITCH_STATUS_SUCCESS; len = strlen(log_data); if (len <= 0 || !profile->log_afd) { return SWITCH_STATUS_FALSE; } switch_mutex_lock(globals.mutex); if (switch_file_write(profile->log_afd, log_data, &len) != SWITCH_STATUS_SUCCESS) { switch_file_close(profile->log_afd); if ((status = mod_logfile_openlogfile(profile, SWITCH_TRUE)) == SWITCH_STATUS_SUCCESS) { len = strlen(log_data); switch_file_write(profile->log_afd, log_data, &len); } } switch_mutex_unlock(globals.mutex); if (status == SWITCH_STATUS_SUCCESS) { profile->log_size += len; if (profile->roll_size && profile->log_size >= profile->roll_size) { mod_logfile_rotate(profile); } } return status; }
/* rotate the log file */ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile) { unsigned int i = 0; char *filename = NULL; switch_status_t stat = 0; int64_t offset = 0; switch_memory_pool_t *pool = NULL; switch_time_exp_t tm; char date[80] = ""; switch_size_t retsize; switch_status_t status = SWITCH_STATUS_SUCCESS; switch_mutex_lock(globals.mutex); switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); profile->log_size = 0; stat = switch_file_seek(profile->log_afd, SWITCH_SEEK_SET, &offset); if (stat != SWITCH_STATUS_SUCCESS) { status = SWITCH_STATUS_FALSE; goto end; } switch_core_new_memory_pool(&pool); filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); 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) { continue; } switch_file_close(profile->log_afd); switch_file_rename(profile->logfile, filename, pool); if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error Rotating Log!\n"); goto end; } break; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n"); end: if (pool) { switch_core_destroy_memory_pool(&pool); } switch_mutex_unlock(globals.mutex); return status; }
static switch_status_t load_profile(switch_xml_t xml) { switch_xml_t param, settings; char *name = (char *) switch_xml_attr_soft(xml, "name"); logfile_profile_t *new_profile; new_profile = switch_core_alloc(module_pool, sizeof(*new_profile)); memset(new_profile, 0, sizeof(*new_profile)); switch_core_hash_init(&(new_profile->log_hash)); new_profile->name = switch_core_strdup(module_pool, switch_str_nil(name)); new_profile->suffix = 1; new_profile->log_uuid = SWITCH_TRUE; if ((settings = switch_xml_child(xml, "settings"))) { for (param = switch_xml_child(settings, "param"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); if (!strcmp(var, "logfile")) { new_profile->logfile = strdup(val); } else if (!strcmp(var, "rollover")) { new_profile->roll_size = switch_atoui(val); } else if (!strcmp(var, "maximum-rotate")) { new_profile->max_rot = switch_atoui(val); if (new_profile->max_rot == 0) { new_profile->max_rot = MAX_ROT; } } else if (!strcmp(var, "uuid")) { new_profile->log_uuid = switch_true(val); } } } if ((settings = switch_xml_child(xml, "mappings"))) { for (param = switch_xml_child(settings, "map"); param; param = param->next) { char *var = (char *) switch_xml_attr_soft(param, "name"); char *val = (char *) switch_xml_attr_soft(param, "value"); add_mapping(new_profile, var, val); } } if (zstr(new_profile->logfile)) { char logfile[512]; switch_snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch.log"); new_profile->logfile = strdup(logfile); } if (mod_logfile_openlogfile(new_profile, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) { return SWITCH_STATUS_GENERR; } switch_core_hash_insert_destructor(profile_hash, new_profile->name, (void *) new_profile, cleanup_profile); return SWITCH_STATUS_SUCCESS; }
/* rotate the log file */ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile) { unsigned int i = 0; char *filename = NULL; switch_status_t stat = 0; int64_t offset = 0; switch_memory_pool_t *pool = NULL; switch_time_exp_t tm; char date[80] = ""; switch_size_t retsize; switch_status_t status = SWITCH_STATUS_SUCCESS; switch_mutex_lock(globals.mutex); switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); profile->log_size = 0; stat = switch_file_seek(profile->log_afd, SWITCH_SEEK_SET, &offset); if (stat != SWITCH_STATUS_SUCCESS) { status = SWITCH_STATUS_FALSE; goto end; } 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 [%s]\n", from_filename, to_filename, strerror(errno)); 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 [%s]\n", to_filename, strerror(errno)); 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 [%s]\n", profile->logfile, to_filename, strerror(errno)); 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) { continue; } switch_file_close(profile->log_afd); switch_file_rename(profile->logfile, filename, pool); if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error Rotating Log!\n"); goto end; } break; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n"); end: if (pool) { switch_core_destroy_memory_pool(&pool); } switch_mutex_unlock(globals.mutex); return status; }