update logger stuff

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6510 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2007-12-04 23:47:27 +00:00
parent 7b34d76091
commit a1e9233eff
3 changed files with 345 additions and 295 deletions

View File

@ -2,7 +2,9 @@
<settings> <settings>
<!-- File to log to --> <!-- File to log to -->
<!--<param name="logfile" value="/var/log/freeswitch.log"/>--> <!--<param name="logfile" value="/var/log/freeswitch.log"/>-->
<!-- At this length in bytes rotate the log file --> <!-- true to auto rotate on HUP, false to open/close -->
<param name="rotate" value="true"/>
<!-- At this length in bytes rotate the log file (0 for never) -->
<!--<param name="rollover" value="10485760"/>--> <!--<param name="rollover" value="10485760"/>-->
<!-- The level of the message to log --> <!-- The level of the message to log -->
<!--<param name="level" value="debug,info,warning,notice,error,crit,alert"/>--> <!--<param name="level" value="debug,info,warning,notice,error,crit,alert"/>-->

View File

@ -36,7 +36,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_logfile_shutdown); SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_logfile_shutdown);
SWITCH_MODULE_DEFINITION(mod_logfile, mod_logfile_load, mod_logfile_shutdown, NULL); SWITCH_MODULE_DEFINITION(mod_logfile, mod_logfile_load, mod_logfile_shutdown, NULL);
#define DEFAULT_LIMIT 0xA00000 /* About 10 MB */ #define DEFAULT_LIMIT 0xA00000 /* About 10 MB */
#define WARM_FUZZY_OFFSET 256 #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};
@ -46,10 +46,12 @@ static switch_memory_pool_t *module_pool = NULL;
static struct { static struct {
unsigned int log_fd; unsigned int log_fd;
switch_size_t log_size; /* keep the log size in check for rotation */ 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 roll_size; /* the size that we want to rotate the file at */
char *logfile; char *logfile;
switch_file_t *log_afd; int rotate;
switch_mutex_t *mutex;
switch_file_t *log_afd;
} globals; } globals;
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_logfile, globals.logfile); SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_logfile, globals.logfile);
@ -126,7 +128,7 @@ static switch_status_t mod_logfile_openlogfile(switch_bool_t check)
globals.log_size = switch_file_get_size(globals.log_afd); globals.log_size = switch_file_get_size(globals.log_afd);
if (check && globals.log_size >= globals.roll_size) { if (check && globals.roll_size && globals.log_size >= globals.roll_size) {
mod_logfile_rotate(); mod_logfile_rotate();
} }
@ -144,6 +146,9 @@ static switch_status_t mod_logfile_rotate(void)
switch_time_exp_t tm; switch_time_exp_t tm;
char date[80] = ""; char date[80] = "";
switch_size_t retsize; switch_size_t retsize;
switch_status_t status = SWITCH_STATUS_SUCCESS;
switch_mutex_lock(globals.mutex);
switch_time_exp_lt(&tm, switch_time_now()); switch_time_exp_lt(&tm, switch_time_now());
switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm);
@ -152,8 +157,11 @@ static switch_status_t mod_logfile_rotate(void)
stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset); stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset);
if (stat != SWITCH_STATUS_SUCCESS) if (stat != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE; status = SWITCH_STATUS_FALSE;
goto end;
}
p = malloc(strlen(globals.logfile)+WARM_FUZZY_OFFSET); p = malloc(strlen(globals.logfile)+WARM_FUZZY_OFFSET);
assert(p); assert(p);
@ -178,7 +186,13 @@ static switch_status_t mod_logfile_rotate(void)
switch_core_destroy_memory_pool(&pool); switch_core_destroy_memory_pool(&pool);
return SWITCH_STATUS_SUCCESS; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n");
end:
switch_mutex_unlock(globals.mutex);
return status;
} }
/* write to the actual logfile */ /* write to the actual logfile */
@ -187,17 +201,24 @@ static switch_status_t mod_logfile_raw_write(char *log_data)
switch_size_t len; switch_size_t len;
len = strlen(log_data); len = strlen(log_data);
if (len <= 0) if (len <= 0) {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_FALSE;
}
switch_mutex_lock(globals.mutex);
if (switch_file_write(globals.log_afd, log_data, &len) != SWITCH_STATUS_SUCCESS) { if (switch_file_write(globals.log_afd, log_data, &len) != SWITCH_STATUS_SUCCESS) {
switch_file_close(globals.log_afd); switch_file_close(globals.log_afd);
mod_logfile_openlogfile(SWITCH_TRUE); mod_logfile_openlogfile(SWITCH_TRUE);
len = strlen(log_data);
switch_file_write(globals.log_afd, log_data, &len);
} }
switch_mutex_unlock(globals.mutex);
globals.log_size += len; globals.log_size += len;
if (globals.log_size >= globals.roll_size) { if (globals.roll_size && globals.log_size >= globals.roll_size) {
mod_logfile_rotate(); mod_logfile_rotate();
} }
@ -228,18 +249,21 @@ static switch_status_t load_config(void)
char *val = (char *) switch_xml_attr_soft(param, "value"); char *val = (char *) switch_xml_attr_soft(param, "value");
if (!strcmp(var, "logfile")) { if (!strcmp(var, "logfile")) {
set_global_logfile(val); set_global_logfile(val);
} else if (!strcmp(var, "rotate")) {
globals.rotate = switch_true(val);
} else if (!strcmp(var, "level")) { } else if (!strcmp(var, "level")) {
process_levels(val); process_levels(val);
} else if (!strcmp(var, "rollover")) { } else if (!strcmp(var, "rollover")) {
globals.roll_size = atoi(val); globals.roll_size = atoi(val);
if (globals.roll_size < 0) {
globals.roll_size = 0;
}
} }
} }
} }
switch_xml_free(xml); switch_xml_free(xml);
} }
if (globals.roll_size == 0) {
globals.roll_size = DEFAULT_LIMIT;
}
if (switch_strlen_zero(globals.logfile)) { if (switch_strlen_zero(globals.logfile)) {
char logfile[512]; char logfile[512];
snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch.log"); snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch.log");
@ -249,11 +273,37 @@ static switch_status_t load_config(void)
return 0; return 0;
} }
static void event_handler(switch_event_t *event)
{
const char *sig = switch_event_get_header(event, "Trapped-Signal");
if (sig && !strcmp(sig, "HUP")) {
if (globals.rotate) {
mod_logfile_rotate();
} else {
switch_mutex_lock(globals.mutex);
switch_file_close(globals.log_afd);
mod_logfile_openlogfile(SWITCH_TRUE);
switch_mutex_unlock(globals.mutex);
}
}
}
SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load) SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load)
{ {
switch_status_t status; switch_status_t status;
module_pool = pool; module_pool = pool;
memset(&globals, 0, sizeof(globals));
switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, module_pool);
if (switch_event_bind((char *) modname, SWITCH_EVENT_TRAP, SWITCH_EVENT_SUBCLASS_ANY, event_handler, NULL) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
return SWITCH_STATUS_GENERR;
}
/* connect my internal structure to the blank pointer passed to me */ /* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname); *module_interface = switch_loadable_module_create_module_interface(pool, modname);
@ -278,12 +328,12 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_logfile_shutdown)
} }
/* For Emacs: /* For Emacs:
* Local Variables: * Local Variables:
* mode:c * mode:c
* indent-tabs-mode:nil * indent-tabs-mode:nil
* tab-width:4 * tab-width:4
* c-basic-offset:4 * c-basic-offset:4
* End: * End:
* For VIM: * For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
*/ */

View File

@ -62,7 +62,7 @@ static HANDLE shutdown_event;
/* signal handler for when freeswitch is running in background mode. /* signal handler for when freeswitch is running in background mode.
* signal triggers the shutdown of freeswitch * signal triggers the shutdown of freeswitch
*/ */
static void handle_SIGHUP(int sig) static void handle_SIGTERM(int sig)
{ {
int32_t arg = 0; int32_t arg = 0;
if (sig); if (sig);
@ -390,11 +390,9 @@ int main(int argc, char *argv[])
return 255; return 255;
} }
signal(SIGTERM, handle_SIGTERM);
if (nc) { if (nc) {
signal(SIGHUP, handle_SIGHUP);
signal(SIGTERM, handle_SIGHUP);
#ifdef WIN32 #ifdef WIN32
FreeConsole(); FreeConsole();
#else #else