提交 26fccbdd authored 作者: Anthony Minessale's avatar Anthony Minessale

update logger code

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6504 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 c5046d63
...@@ -1140,7 +1140,7 @@ static switch_status_t listen_file(switch_core_session_t *session, vm_profile_t ...@@ -1140,7 +1140,7 @@ static switch_status_t listen_file(switch_core_session_t *session, vm_profile_t
message_count(profile, cbt->user, cbt->domain, cbt->in_folder, &total_new_messages, &total_saved_messages, message_count(profile, cbt->user, cbt->domain, cbt->in_folder, &total_new_messages, &total_saved_messages,
&total_new_urgent_messages, &total_saved_urgent_messages); &total_new_urgent_messages, &total_saved_urgent_messages);
switch_time_exp_lt(&tm, atoi(cbt->created_epoch)); switch_time_exp_lt(&tm, atoi(cbt->created_epoch) * 1000000);
switch_strftime(date, &retsize, sizeof(date), profile->date_fmt, &tm); switch_strftime(date, &retsize, sizeof(date), profile->date_fmt, &tm);
snprintf(tmp,sizeof(tmp), "%d", total_new_messages); snprintf(tmp,sizeof(tmp), "%d", total_new_messages);
......
...@@ -38,7 +38,7 @@ SWITCH_MODULE_DEFINITION(mod_logfile, mod_logfile_load, mod_logfile_shutdown, NU ...@@ -38,7 +38,7 @@ SWITCH_MODULE_DEFINITION(mod_logfile, mod_logfile_load, mod_logfile_shutdown, NU
#define DEFAULT_FORMAT "${data}" #define DEFAULT_FORMAT "${data}"
#define DEFAULT_LIMIT 0x7FFFFFFF #define DEFAULT_LIMIT 0x7FFFFFFF
#define WARM_FUZZY_OFFSET 64 #define WARM_FUZZY_OFFSET 256
#define MAX_ROT 4096 /* why not */ #define MAX_ROT 4096 /* why not */
static const uint8_t STATIC_LEVELS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; static const uint8_t STATIC_LEVELS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
...@@ -107,21 +107,52 @@ void process_levels(char *p) ...@@ -107,21 +107,52 @@ void process_levels(char *p)
return; return;
} }
static switch_status_t mod_logfile_rotate(void);
static switch_status_t mod_logfile_openlogfile(switch_bool_t check)
{
unsigned int flags = 0;
switch_file_t *afd;
switch_status_t stat;
flags |= SWITCH_FOPEN_CREATE;
flags |= SWITCH_FOPEN_READ;
flags |= SWITCH_FOPEN_WRITE;
flags |= SWITCH_FOPEN_APPEND;
stat = switch_file_open(&afd, globals.logfile, flags, SWITCH_FPROT_UREAD|SWITCH_FPROT_UWRITE, module_pool);
if (stat != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
globals.log_afd = afd;
globals.log_size = switch_file_get_size(globals.log_afd);
if (check && globals.log_size >= globals.roll_size) {
mod_logfile_rotate();
}
return SWITCH_STATUS_SUCCESS;
}
/* rotate the log file */ /* rotate the log file */
static switch_status_t mod_logfile_rotate(void) static switch_status_t mod_logfile_rotate(void)
{ {
unsigned int i = 0; unsigned int i = 0;
unsigned int flags = 0;
char *p = NULL; char *p = NULL;
char *q = NULL;
switch_status_t stat = 0; switch_status_t stat = 0;
switch_size_t nbytes = 1024;
switch_file_t *l_afd = NULL;
switch_memory_pool_t *pool = NULL;
int64_t offset = 0; int64_t offset = 0;
switch_memory_pool_t *pool;
switch_time_exp_t tm;
char date[80] = "";
switch_size_t retsize;
switch_time_exp_lt(&tm, switch_time_now());
switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm);
globals.log_size = 0; globals.log_size = 0;
switch_core_new_memory_pool(&pool);
stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset); stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset);
...@@ -129,78 +160,34 @@ static switch_status_t mod_logfile_rotate(void) ...@@ -129,78 +160,34 @@ static switch_status_t mod_logfile_rotate(void)
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
p = malloc(strlen(globals.logfile)+WARM_FUZZY_OFFSET); p = malloc(strlen(globals.logfile)+WARM_FUZZY_OFFSET);
if (!p) { assert(p);
return SWITCH_STATUS_FALSE;
}
q = malloc(1024);
if (!q) {
free(p);
return SWITCH_STATUS_FALSE;
}
memset(p, '\0', strlen(globals.logfile)+WARM_FUZZY_OFFSET); memset(p, '\0', strlen(globals.logfile)+WARM_FUZZY_OFFSET);
switch_core_new_memory_pool(&pool);
for (i=1; i < MAX_ROT; i++) { for (i=1; i < MAX_ROT; i++) {
sprintf((char *)p, "%s.%i", globals.logfile, i); sprintf((char *)p, "%s.%s.%i", globals.logfile, date, i);
if (switch_file_exists(p, pool) == SWITCH_STATUS_SUCCESS) {
stat = switch_file_open(&l_afd, p, SWITCH_FOPEN_READ, SWITCH_FPROT_UREAD, pool); continue;
}
if (stat == SWITCH_STATUS_SUCCESS) {
switch_file_close(l_afd); switch_file_close(globals.log_afd);
continue; switch_file_rename(globals.logfile, p, pool);
} mod_logfile_openlogfile(SWITCH_FALSE);
flags |= SWITCH_FOPEN_READ; break;
flags |= SWITCH_FOPEN_WRITE;
flags |= SWITCH_FOPEN_CREATE;
stat = switch_file_open(&l_afd, p, flags , SWITCH_FPROT_UREAD | SWITCH_FPROT_UWRITE ,pool);
if (stat == SWITCH_STATUS_SUCCESS)
break;
} }
while ((stat = switch_file_read(globals.log_afd, q, &nbytes)) == SWITCH_STATUS_SUCCESS) {
switch_file_write(l_afd, q, &nbytes);
memset(q, '\0', 1024);
nbytes = 1024;
}
offset = 0;
stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset);
free(p); free(p);
free(q);
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t mod_logfile_openlogfile(void)
{
unsigned int flags = 0;
switch_file_t *afd;
switch_status_t stat;
switch_memory_pool_t *pool;
flags |= SWITCH_FOPEN_CREATE;
flags |= SWITCH_FOPEN_READ;
flags |= SWITCH_FOPEN_WRITE;
flags |= SWITCH_FOPEN_APPEND;
switch_core_new_memory_pool(&pool);
stat = switch_file_open(&afd, globals.logfile,flags,SWITCH_FPROT_UREAD|SWITCH_FPROT_UWRITE, pool);
if (stat != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
globals.log_afd = afd;
globals.log_size = switch_file_get_size(globals.log_afd);
if (globals.log_size >= globals.roll_size) { switch_core_destroy_memory_pool(&pool);
mod_logfile_rotate();
}
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
#if 0
/* write to the actual logfile */ /* write to the actual logfile */
static switch_status_t mod_logfile_write(char *fmt, ...) static switch_status_t mod_logfile_write(char *fmt, ...)
{ {
...@@ -219,47 +206,43 @@ static switch_status_t mod_logfile_write(char *fmt, ...) ...@@ -219,47 +206,43 @@ static switch_status_t mod_logfile_write(char *fmt, ...)
globals.log_size += len; globals.log_size += len;
/* we may want to hold back on this for preformance reasons */
if (globals.log_size >= globals.roll_size) { if (globals.log_size >= globals.roll_size) {
mod_logfile_rotate(); mod_logfile_rotate();
} }
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
#endif
static switch_status_t mod_logfile_logger(const switch_log_node_t *node, switch_log_level_t level) /* write to the actual logfile */
static switch_status_t mod_logfile_raw_write(char *log_data)
{ {
char line_no[sizeof(int)*8+1]; switch_size_t len;
char date[80] = ""; len = strlen(log_data);
switch_time_exp_t time;
switch_size_t retsize;
char *message = NULL;
if (!levels[node->level].on) {
return SWITCH_STATUS_SUCCESS;
}
message = (char *)malloc(strlen(globals.format)+2); if (len <= 0)
switch_copy_string(message, globals.format, strlen(globals.format)+1); return SWITCH_STATUS_FALSE;
message = switch_string_replace(message, "${message}", node->content); if (switch_file_write(globals.log_afd, log_data, &len) != SWITCH_STATUS_SUCCESS) {
switch_file_close(globals.log_afd);
mod_logfile_openlogfile(SWITCH_TRUE);
}
globals.log_size += len;
if (switch_time_exp_lt(&time, node->timestamp) != SWITCH_STATUS_SUCCESS) { if (globals.log_size >= globals.roll_size) {
return SWITCH_STATUS_FALSE; mod_logfile_rotate();
} }
switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &time); return SWITCH_STATUS_SUCCESS;
}
message = switch_string_replace(message, "${time}", date);
message = switch_string_replace(message, "${file}", node->file);
message = switch_string_replace(message, "${func}", node->func);
snprintf(line_no, sizeof(line_no), "%d", node->line); static switch_status_t mod_logfile_logger(const switch_log_node_t *node, switch_log_level_t level)
message = switch_string_replace(message, "${line}", line_no); {
if (!switch_strlen_zero(message)) { if (levels[node->level].on) {
mod_logfile_write("%s", message); mod_logfile_raw_write(node->data);
} }
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
...@@ -316,7 +299,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load) ...@@ -316,7 +299,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load)
return status; return status;
} }
mod_logfile_openlogfile(); mod_logfile_openlogfile(SWITCH_TRUE);
switch_log_bind_logger(mod_logfile_logger, SWITCH_LOG_DEBUG); switch_log_bind_logger(mod_logfile_logger, SWITCH_LOG_DEBUG);
......
...@@ -69,31 +69,8 @@ static switch_loadable_module_interface_t console_module_interface = { ...@@ -69,31 +69,8 @@ static switch_loadable_module_interface_t console_module_interface = {
static switch_status_t mod_syslog_logger(const switch_log_node_t *node, switch_log_level_t level) static switch_status_t mod_syslog_logger(const switch_log_node_t *node, switch_log_level_t level)
{ {
char *message = NULL; char *message = NULL;
char line_no[sizeof(int) * 8 + 1];
char date[80] = "";
switch_time_exp_t time;
switch_size_t retsize;
int syslog_level; int syslog_level;
message = (char *) malloc(strlen(globals.format) + 2);
switch_copy_string(message, globals.format, strlen(globals.format) + 1);
message = switch_string_replace(message, "${message}", node->content);
if (switch_time_exp_lt(&time, node->timestamp) != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE;
}
switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &time);
message = switch_string_replace(message, "${time}", date);
message = switch_string_replace(message, "${file}", node->file);
message = switch_string_replace(message, "${func}", node->func);
snprintf(line_no, sizeof(line_no), "%d", node->line);
message = switch_string_replace(message, "${line}", line_no);
switch (level) { switch (level) {
case SWITCH_LOG_DEBUG: case SWITCH_LOG_DEBUG:
syslog_level = LOG_DEBUG; syslog_level = LOG_DEBUG;
...@@ -122,10 +99,10 @@ static switch_status_t mod_syslog_logger(const switch_log_node_t *node, switch_l ...@@ -122,10 +99,10 @@ static switch_status_t mod_syslog_logger(const switch_log_node_t *node, switch_l
} }
if (!switch_strlen_zero(message)) { if (!switch_strlen_zero(message)) {
syslog(syslog_level, "%s", message); syslog(syslog_level, "%s", node->data);
} }
free(message);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论