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

@ -1,289 +1,339 @@
/* /*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* *
* Version: MPL 1.1 * Version: MPL 1.1
* *
* The contents of this file are subject to the Mozilla Public License Version * The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with * 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/ * http://www.mozilla.org/MPL/
* *
* Software distributed under the License is distributed on an "AS IS" basis, * Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the * for the specific language governing rights and limitations under the
* License. * License.
* *
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* *
* The Initial Developer of the Original Code is * The Initial Developer of the Original Code is
* Anthony Minessale II <anthmct@yahoo.com> * Anthony Minessale II <anthmct@yahoo.com>
* Portions created by the Initial Developer are Copyright (C) * Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved. * the Initial Developer. All Rights Reserved.
* *
* Contributor(s): * Contributor(s):
* *
* Anthony LaMantia <anthony@petabit.net> * Anthony LaMantia <anthony@petabit.net>
* Michael Jerris <mike@jerris.com> * Michael Jerris <mike@jerris.com>
* *
* *
* mod_logfile.c -- Filesystem Logging * mod_logfile.c -- Filesystem Logging
* *
*/ */
#include <switch.h> #include <switch.h>
SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load); 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};
static switch_status_t load_config(void); static switch_status_t load_config(void);
static switch_memory_pool_t *module_pool = NULL; 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;
} globals; switch_mutex_t *mutex;
switch_file_t *log_afd;
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_logfile, globals.logfile); } globals;
/* i know this is strange but it's the fastest way i could think of managing log levels. */ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_logfile, globals.logfile);
/* i'd rather not try to search something each time we get a message to log */
struct level_set { /* i know this is strange but it's the fastest way i could think of managing log levels. */
int level; /* i'd rather not try to search something each time we get a message to log */
int on; struct level_set {
} static levels[] = { int level;
{SWITCH_LOG_CONSOLE, 0}, /* 0 */ int on;
{SWITCH_LOG_ALERT, 0}, /* 1 */ } static levels[] = {
{SWITCH_LOG_CRIT, 0}, /* 2 */ {SWITCH_LOG_CONSOLE, 0}, /* 0 */
{SWITCH_LOG_ERROR, 0}, /* 3 */ {SWITCH_LOG_ALERT, 0}, /* 1 */
{SWITCH_LOG_WARNING, 0}, /* 4 */ {SWITCH_LOG_CRIT, 0}, /* 2 */
{SWITCH_LOG_NOTICE, 0}, /* 5 */ {SWITCH_LOG_ERROR, 0}, /* 3 */
{SWITCH_LOG_INFO, 0}, /* 6 */ {SWITCH_LOG_WARNING, 0}, /* 4 */
{SWITCH_LOG_DEBUG, 0}, /* 7 */ {SWITCH_LOG_NOTICE, 0}, /* 5 */
}; {SWITCH_LOG_INFO, 0}, /* 6 */
{SWITCH_LOG_DEBUG, 0}, /* 7 */
void process_levels(char *p) };
{
int x, argc = 0; void process_levels(char *p)
char *argv[10] = { 0 }; {
int x, argc = 0;
if ((argc = switch_separate_string(p, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) { char *argv[10] = { 0 };
for (x = 0; x < argc; x++) {
if (!strncasecmp(argv[x], "alert", strlen(argv[x]))) { if ((argc = switch_separate_string(p, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
levels[SWITCH_LOG_ALERT].on = 1; for (x = 0; x < argc; x++) {
} else if (!strncasecmp(argv[x], "crit", strlen(argv[x]))) { if (!strncasecmp(argv[x], "alert", strlen(argv[x]))) {
levels[SWITCH_LOG_CRIT].on = 1; levels[SWITCH_LOG_ALERT].on = 1;
} else if (!strncasecmp(argv[x], "error", strlen(argv[x]))) { } else if (!strncasecmp(argv[x], "crit", strlen(argv[x]))) {
levels[SWITCH_LOG_ERROR].on = 1; levels[SWITCH_LOG_CRIT].on = 1;
} else if (!strncasecmp(argv[x], "warn", strlen(argv[x]))) { } else if (!strncasecmp(argv[x], "error", strlen(argv[x]))) {
levels[SWITCH_LOG_WARNING].on = 1; levels[SWITCH_LOG_ERROR].on = 1;
} else if (!strncasecmp(argv[x], "notice", strlen(argv[x]))) { } else if (!strncasecmp(argv[x], "warn", strlen(argv[x]))) {
levels[SWITCH_LOG_NOTICE].on = 1; levels[SWITCH_LOG_WARNING].on = 1;
} else if (!strncasecmp(argv[x], "info", strlen(argv[x]))) { } else if (!strncasecmp(argv[x], "notice", strlen(argv[x]))) {
levels[SWITCH_LOG_INFO].on = 1; levels[SWITCH_LOG_NOTICE].on = 1;
} else if (!strncasecmp(argv[x], "debug", strlen(argv[x]))) { } else if (!strncasecmp(argv[x], "info", strlen(argv[x]))) {
levels[SWITCH_LOG_DEBUG].on = 1; levels[SWITCH_LOG_INFO].on = 1;
} else if (!strncasecmp(argv[x], "console", strlen(argv[x]))) { } else if (!strncasecmp(argv[x], "debug", strlen(argv[x]))) {
levels[SWITCH_LOG_CONSOLE].on = 1; levels[SWITCH_LOG_DEBUG].on = 1;
} else if (!strncasecmp(argv[x], "all", strlen(argv[x]))) { } else if (!strncasecmp(argv[x], "console", strlen(argv[x]))) {
int i; levels[SWITCH_LOG_CONSOLE].on = 1;
for (i=0; i < (sizeof(levels) / sizeof(struct level_set)); i++) { } else if (!strncasecmp(argv[x], "all", strlen(argv[x]))) {
levels[i].on = 1; int i;
} for (i=0; i < (sizeof(levels) / sizeof(struct level_set)); i++) {
} levels[i].on = 1;
} }
} }
return; }
} }
return;
static switch_status_t mod_logfile_rotate(void); }
static switch_status_t mod_logfile_openlogfile(switch_bool_t check) static switch_status_t mod_logfile_rotate(void);
{
unsigned int flags = 0; static switch_status_t mod_logfile_openlogfile(switch_bool_t check)
switch_file_t *afd; {
switch_status_t stat; unsigned int flags = 0;
switch_file_t *afd;
flags |= SWITCH_FOPEN_CREATE; switch_status_t stat;
flags |= SWITCH_FOPEN_READ;
flags |= SWITCH_FOPEN_WRITE; flags |= SWITCH_FOPEN_CREATE;
flags |= SWITCH_FOPEN_APPEND; flags |= SWITCH_FOPEN_READ;
flags |= SWITCH_FOPEN_WRITE;
stat = switch_file_open(&afd, globals.logfile, flags, SWITCH_FPROT_UREAD|SWITCH_FPROT_UWRITE, module_pool); flags |= SWITCH_FOPEN_APPEND;
if (stat != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_FALSE; 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); globals.log_afd = afd;
if (check && globals.log_size >= globals.roll_size) { globals.log_size = switch_file_get_size(globals.log_afd);
mod_logfile_rotate();
} if (check && globals.roll_size && globals.log_size >= globals.roll_size) {
mod_logfile_rotate();
return SWITCH_STATUS_SUCCESS; }
}
return SWITCH_STATUS_SUCCESS;
/* rotate the log file */ }
static switch_status_t mod_logfile_rotate(void)
{ /* rotate the log file */
unsigned int i = 0; static switch_status_t mod_logfile_rotate(void)
char *p = NULL; {
switch_status_t stat = 0; unsigned int i = 0;
int64_t offset = 0; char *p = NULL;
switch_memory_pool_t *pool; switch_status_t stat = 0;
switch_time_exp_t tm; int64_t offset = 0;
char date[80] = ""; switch_memory_pool_t *pool;
switch_size_t retsize; switch_time_exp_t tm;
char date[80] = "";
switch_time_exp_lt(&tm, switch_time_now()); switch_size_t retsize;
switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); switch_status_t status = SWITCH_STATUS_SUCCESS;
globals.log_size = 0; switch_mutex_lock(globals.mutex);
stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset); switch_time_exp_lt(&tm, switch_time_now());
switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm);
if (stat != SWITCH_STATUS_SUCCESS)
return SWITCH_STATUS_FALSE; globals.log_size = 0;
p = malloc(strlen(globals.logfile)+WARM_FUZZY_OFFSET); stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset);
assert(p);
if (stat != SWITCH_STATUS_SUCCESS) {
memset(p, '\0', strlen(globals.logfile)+WARM_FUZZY_OFFSET); status = SWITCH_STATUS_FALSE;
goto end;
switch_core_new_memory_pool(&pool); }
for (i=1; i < MAX_ROT; i++) {
sprintf((char *)p, "%s.%s.%i", globals.logfile, date, i); p = malloc(strlen(globals.logfile)+WARM_FUZZY_OFFSET);
if (switch_file_exists(p, pool) == SWITCH_STATUS_SUCCESS) { assert(p);
continue;
} memset(p, '\0', strlen(globals.logfile)+WARM_FUZZY_OFFSET);
switch_file_close(globals.log_afd); switch_core_new_memory_pool(&pool);
switch_file_rename(globals.logfile, p, pool);
mod_logfile_openlogfile(SWITCH_FALSE); for (i=1; i < MAX_ROT; i++) {
break; sprintf((char *)p, "%s.%s.%i", globals.logfile, date, i);
} if (switch_file_exists(p, pool) == SWITCH_STATUS_SUCCESS) {
continue;
free(p); }
switch_core_destroy_memory_pool(&pool); switch_file_close(globals.log_afd);
switch_file_rename(globals.logfile, p, pool);
return SWITCH_STATUS_SUCCESS; mod_logfile_openlogfile(SWITCH_FALSE);
} break;
}
/* write to the actual logfile */
static switch_status_t mod_logfile_raw_write(char *log_data) free(p);
{
switch_size_t len; switch_core_destroy_memory_pool(&pool);
len = strlen(log_data);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n");
if (len <= 0)
return SWITCH_STATUS_FALSE; end:
if (switch_file_write(globals.log_afd, log_data, &len) != SWITCH_STATUS_SUCCESS) { switch_mutex_unlock(globals.mutex);
switch_file_close(globals.log_afd);
mod_logfile_openlogfile(SWITCH_TRUE); return status;
} }
globals.log_size += len; /* write to the actual logfile */
static switch_status_t mod_logfile_raw_write(char *log_data)
if (globals.log_size >= globals.roll_size) { {
mod_logfile_rotate(); switch_size_t len;
} len = strlen(log_data);
return SWITCH_STATUS_SUCCESS; if (len <= 0) {
} return SWITCH_STATUS_FALSE;
}
static switch_status_t mod_logfile_logger(const switch_log_node_t *node, switch_log_level_t level)
{ switch_mutex_lock(globals.mutex);
if (levels[node->level].on) { if (switch_file_write(globals.log_afd, log_data, &len) != SWITCH_STATUS_SUCCESS) {
mod_logfile_raw_write(node->data); switch_file_close(globals.log_afd);
} mod_logfile_openlogfile(SWITCH_TRUE);
len = strlen(log_data);
return SWITCH_STATUS_SUCCESS; switch_file_write(globals.log_afd, log_data, &len);
} }
static switch_status_t load_config(void) switch_mutex_unlock(globals.mutex);
{
char *cf = "logfile.conf"; globals.log_size += len;
switch_xml_t cfg, xml, settings, param;
if (globals.roll_size && globals.log_size >= globals.roll_size) {
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { mod_logfile_rotate();
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf); }
} else {
if ((settings = switch_xml_child(cfg, "settings"))) { return SWITCH_STATUS_SUCCESS;
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"); static switch_status_t mod_logfile_logger(const switch_log_node_t *node, switch_log_level_t level)
if (!strcmp(var, "logfile")) { {
set_global_logfile(val);
} else if (!strcmp(var, "level")) { if (levels[node->level].on) {
process_levels(val); mod_logfile_raw_write(node->data);
} else if (!strcmp(var, "rollover")) { }
globals.roll_size = atoi(val);
} return SWITCH_STATUS_SUCCESS;
} }
}
switch_xml_free(xml); static switch_status_t load_config(void)
} {
if (globals.roll_size == 0) { char *cf = "logfile.conf";
globals.roll_size = DEFAULT_LIMIT; switch_xml_t cfg, xml, settings, param;
}
if (switch_strlen_zero(globals.logfile)) { if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
char logfile[512]; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch.log"); } else {
set_global_logfile(logfile); if ((settings = switch_xml_child(cfg, "settings"))) {
} for (param = switch_xml_child(settings, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name");
return 0; char *val = (char *) switch_xml_attr_soft(param, "value");
} if (!strcmp(var, "logfile")) {
set_global_logfile(val);
SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load) } else if (!strcmp(var, "rotate")) {
{ globals.rotate = switch_true(val);
switch_status_t status; } else if (!strcmp(var, "level")) {
process_levels(val);
module_pool = pool; } else if (!strcmp(var, "rollover")) {
globals.roll_size = atoi(val);
/* connect my internal structure to the blank pointer passed to me */ if (globals.roll_size < 0) {
*module_interface = switch_loadable_module_create_module_interface(pool, modname); globals.roll_size = 0;
}
if ((status=load_config()) != SWITCH_STATUS_SUCCESS) { }
return status; }
} }
switch_xml_free(xml);
mod_logfile_openlogfile(SWITCH_TRUE); }
switch_log_bind_logger(mod_logfile_logger, SWITCH_LOG_DEBUG); if (switch_strlen_zero(globals.logfile)) {
char logfile[512];
return SWITCH_STATUS_SUCCESS; snprintf(logfile, sizeof(logfile), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, "freeswitch.log");
} set_global_logfile(logfile);
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_logfile_shutdown)
{ return 0;
/* TODO: Need to finish processing pending log messages before we close the file handle */ }
//switch_file_close(globals.log_afd);
return SWITCH_STATUS_SUCCESS; static void event_handler(switch_event_t *event)
} {
const char *sig = switch_event_get_header(event, "Trapped-Signal");
/* For Emacs:
* Local Variables: if (sig && !strcmp(sig, "HUP")) {
* mode:c if (globals.rotate) {
* indent-tabs-mode:nil mod_logfile_rotate();
* tab-width:4 } else {
* c-basic-offset:4 switch_mutex_lock(globals.mutex);
* End: switch_file_close(globals.log_afd);
* For VIM: mod_logfile_openlogfile(SWITCH_TRUE);
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: switch_mutex_unlock(globals.mutex);
*/ }
}
}
SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load)
{
switch_status_t status;
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 */
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
if ((status=load_config()) != SWITCH_STATUS_SUCCESS) {
return status;
}
mod_logfile_openlogfile(SWITCH_TRUE);
switch_log_bind_logger(mod_logfile_logger, SWITCH_LOG_DEBUG);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_logfile_shutdown)
{
/* TODO: Need to finish processing pending log messages before we close the file handle */
//switch_file_close(globals.log_afd);
return SWITCH_STATUS_SUCCESS;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:nil
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* 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