[Freeswitch-branches] [commit] r5187 - freeswitch/branches/anthonyl/fs-branch/src/mod/loggers/mod_log2file

Freeswitch SVN anthonyl at freeswitch.org
Tue May 15 23:15:37 EDT 2007


Author: anthonyl
Date: Tue May 15 23:15:36 2007
New Revision: 5187

Modified:
   freeswitch/branches/anthonyl/fs-branch/src/mod/loggers/mod_log2file/mod_log2file.c

Log:
more apr integration. the size checking is now totally apr


Modified: freeswitch/branches/anthonyl/fs-branch/src/mod/loggers/mod_log2file/mod_log2file.c
==============================================================================
--- freeswitch/branches/anthonyl/fs-branch/src/mod/loggers/mod_log2file/mod_log2file.c	(original)
+++ freeswitch/branches/anthonyl/fs-branch/src/mod/loggers/mod_log2file/mod_log2file.c	Tue May 15 23:15:36 2007
@@ -1,6 +1,5 @@
 /* 
  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
- * Copyright (C) 2005/2006, James Martelletti <james at nerdc0re.com>
  *
  * Version: MPL 1.1
  *
@@ -38,7 +37,7 @@
 #define DEFAULT_LOGFILE  "/var/log/freeswitch"
 #define DEFAULT_LIMIT    0x7FFFFFFF
 #define WARM_FUZZY_OFFSET 64
-
+#define MAX_ROT 4096 /* why not */
 static const uint8_t STATIC_LEVELS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
 static const char modname[] = "mod_log2file";
 
@@ -53,13 +52,31 @@
 static struct {
     unsigned int log_fd;
     unsigned int log_size;   /* keep the log size in check for rotation */
+    unsigned int roll_size;  /* the size that we want to rotate the file at */
     unsigned char *logfile;
 	unsigned char *format;
+    apr_file_t    *log_afd;
 } globals;
 
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_logfile, globals.logfile)
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_format, globals.format)
-
+     
+/* i know this is strange but it's the fastest way i could think of managing log levels. */
+/* i'd rather not try to search something each time we get a message to log */
+struct level_set {
+    int level;
+    int on;
+}static levels[] = {
+    {SWITCH_LOG_EMERG,   0},   /* 0 */
+    {SWITCH_LOG_ALERT,   0},   /* 1 */
+    {SWITCH_LOG_CRIT,    0},   /* 2 */
+    {SWITCH_LOG_ERROR,   0},   /* 3 */
+    {SWITCH_LOG_WARNING, 0},   /* 4 */
+    {SWITCH_LOG_NOTICE,  0},   /* 5 */
+    {SWITCH_LOG_INFO,    0},   /* 6 */
+    {SWITCH_LOG_DEBUG,   0},   /* 7 */
+    {SWITCH_LOG_CONSOLE, 0},   /* 8 */
+};
 static switch_loadable_module_interface_t log2file_module_interface = {
 	/*.module_name */ modname,
 	/*.endpoint_interface */ NULL,
@@ -73,42 +90,98 @@
 	/*.directory_interface */ NULL
 };
 
-static void del_mapping(char *var) {
-	if (!strcasecmp(var, "all")) {
-		all_level = -1;
-		return;
-	}
-	switch_core_hash_insert(log_hash, var, NULL);
+/* this may exist somewhere else in the code? */
+static void log_type2str(switch_log_level_t type)
+{
+    switch(type) {
+    case SWITCH_LOG_CONSOLE:
+        printf("[CONSOLE] ");
+        break;
+    case SWITCH_LOG_DEBUG:
+        printf("[DEBUG] ");
+        break;
+    case SWITCH_LOG_INFO:
+        printf("[INFO] ");
+        break;
+    case SWITCH_LOG_NOTICE:
+        printf("[NOTICE] ");
+        break;
+    case SWITCH_LOG_WARNING:
+        printf("[WARNING] ");
+        break;
+    case SWITCH_LOG_ERROR:
+        printf("[ERROR] ");
+        break;
+    case SWITCH_LOG_CRIT:
+        printf("[CRIT] ");
+        break;
+    case SWITCH_LOG_ALERT:
+        printf("[ALERT] ");
+        break;
+    case SWITCH_LOG_EMERG:
+        printf("[EMERG] ");
+        break;
+    default:
+        break;
+    }
+    return;
 }
 
-static void add_mapping(char *var, char *val)
+void process_levels(char *p)
 {
-	char *name;
-
-	if (!strcasecmp(var, "all")) {
-		all_level = (int8_t) switch_log_str2level(val);
-		return;
-	}
-
-	if (!(name = switch_core_hash_find(name_hash, var))) {
-		name = switch_core_strdup(module_pool, var);
-		switch_core_hash_insert(name_hash, name, name);
-	}
-
-	del_mapping(name);
-	switch_core_hash_insert(log_hash, name, (void *) &STATIC_LEVELS[(uint8_t)switch_log_str2level(val)]);
+    char *q;
+    
+    while (q = strsep(&p, ",")) {
+        if (!strncasecmp(q, "emerg", strlen(q))) {
+            levels[SWITCH_LOG_EMERG].on = 1;
+        } else if (!strncasecmp(q, "alert", strlen(q))) {
+            levels[SWITCH_LOG_ALERT].on = 1;
+        } else if (!strncasecmp(q, "crit", strlen(q))) {
+            levels[SWITCH_LOG_CRIT].on = 1;
+        } else if (!strncasecmp(q, "error", strlen(q))) {
+            levels[SWITCH_LOG_ERROR].on = 1;
+        } else if (!strncasecmp(q, "warn", strlen(q))) {
+            levels[SWITCH_LOG_WARNING].on = 1;
+        } else if (!strncasecmp(q, "notice", strlen(q))) {
+            levels[SWITCH_LOG_NOTICE].on = 1;
+        } else if (!strncasecmp(q, "info", strlen(q))) {
+            levels[SWITCH_LOG_INFO].on = 1;
+        } else if (!strncasecmp(q, "debug", strlen(q))) {
+            levels[SWITCH_LOG_DEBUG].on = 1;
+        } else if (!strncasecmp(q, "console", strlen(q))) {
+            levels[SWITCH_LOG_CONSOLE].on = 1;
+        } else if (!strncasecmp(q, "all", strlen(q))) {
+            int i;
+            printf("all option!\n");
+            for (i=0; i < (sizeof(levels) / sizeof(struct level_set)); i++) {
+                levels[i].on = 1;
+            }
+        }
+                   
+    }
+    return;
 }
-
 static switch_status_t mod_log2file_openlogfile(void)
 {
     int fd;
+    apr_file_t *afd;
+    apr_status_t stat;
 
+    stat = apr_file_open(&afd, globals.logfile,
+                         APR_READ | APR_WRITE | APR_APPEND, NULL, NULL);
+
+    if (stat != APR_SUCCESS) {
+        return SWITCH_STATUS_FALSE;
+    }
+    
     /* locate where we want to log file to be */
     fd = fopen(globals.logfile, "a+");    
+
     if (!fd) {
         return SWITCH_STATUS_FALSE;
-    }    
-    globals.log_fd = fd;
+    }
+
+    globals.log_afd = afd;
     mod_log2file_check();
     return SWITCH_STATUS_SUCCESS;
 }
@@ -139,24 +212,25 @@
         return SWITCH_STATUS_FALSE;
     }
     memset(p, '\0', strlen(globals.logfile)+WARM_FUZZY_OFFSET);
-    for (i=1;i<MAX_UINT; i++) {
+    for (i=1;i<MAX_ROT; i++) {
         sprintf(p, "%s.%i", globals.logfile, i);
-        fd = fopen(p, 'w+');
-        if (fd)
+        fd = fopen(p, "r");
+        if (fd != NULL) {
+            fclose(fd);
+            continue;
+        }
+        if(fd = fopen(p, "w")) 
             break;
+        
     }
-    
-    do {
-        memset(q, '\0', 1024);
-        ret = fread(q, 1024, 1, globals.log_fd);
-        fwrite(q, 1024, 1, fd); 
-        globals.log_size = off;
-    } while(ret);
-
+    while( ret =  fread(q, 1024, 1, globals.log_fd)) {
+        fwrite(q, 1024, 1, fd);        
+        memset(q, '\0', 1024); 
+    }
+    /* now we need to resize the original file to 0 */
+    /* or just remove it */
     ret = fseek(globals.log_fd, 0, SEEK_SET);
-    
     fclose(fd);
-    
     free(p);
     free(q);
     return SWITCH_STATUS_SUCCESS;
@@ -165,13 +239,16 @@
 /* check the size of the file for rotation */
 static switch_status_t mod_log2file_check(void)
 {
+    apr_status_t stat;
+    apr_finfo_t  finfo;
     off_t off;
-    
-    if((off = ftello(globals.log_fd)) < 0) {
+
+    stat = apr_file_info_get(&finfo, APR_FINFO_SIZE, &globals.log_afd);
+
+    if (stat != APR_SUCCESS)
         return SWITCH_STATUS_FALSE;
-    }
-    
-    if (off >= DEFAULT_LIMIT) {
+
+    if (finfo.size >= globals.roll_size) {
         return mod_log2file_rotate();
     }
     
@@ -203,48 +280,57 @@
 
 static switch_status_t mod_log2file_logger(const switch_log_node_t *node, switch_log_level_t level)
 {
-	char *message = NULL;
+    uint8_t *lookup = NULL;
 	char line_no[sizeof(int)*8+1];
 	char date[80] = "";
-    uint8_t *lookup = NULL;
-    switch_log_level_t level = SWITCH_LOG_DEBUG;	
 	switch_time_exp_t time;
 	switch_size_t retsize;
-   
-	message = (char *)malloc(strlen(globals.format)+2);
+    char *message = NULL;
 
-    if (!message) {
-        return SWITCH_STATUS_FALSE;
+    if (!levels[node->level].on) {
+        return SWITCH_STATUS_SUCCESS;
     }
     
-	switch_copy_string(message, globals.format, strlen(globals.format)+1);
-   
-    message = switch_string_replace(message, "${data}", node->data);
-	message = switch_string_replace(message, "${message}", node->content);
-	
-	if (switch_time_exp_lt(&time, node->timestamp) != SWITCH_STATUS_SUCCESS) {
-        free(message);
-        return SWITCH_STATUS_FALSE;
+	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);
-	
+
+    /* work with the log type */
+    log_type2str(node->level);
+    printf("\n");
+    printf("Type: (%i)\n", node->level);
+    if (node->file)
+        printf("File: %s\n", node->file);
+    if (node->line)
+        printf("Line: %i\n", node->line);
+    if (node->func)
+        printf("Func: %s\n", node->func);
+    if (node->data)
+        printf("Data: %s\n", node->data);
+    if (node->content)
+        printf("Cont: %s\n", node->content);
+
+   	
 	if (!switch_strlen_zero(message)) {
         mod_log2file_write("%s", message);
     }
-
-	free(message);
-	
+    
 	return SWITCH_STATUS_SUCCESS;
 }
-
 static switch_status_t load_config(void)
 {
 	char *cf = "log2file.conf";
@@ -261,36 +347,43 @@
 					set_global_logfile(val);
 				} else if (!strcmp(var, "format")) {
 					set_global_format(val);
-				} 
-  
+				} else if (!strcmp(var, "level")) {
+                    process_levels(val);
+                } else if (!strcmp(var, "rollover")) {
+                    globals.roll_size = atoi(val);
+                }  
 			}
 		}
 		switch_xml_free(xml);
 	}
-    
+    if (globals.roll_size == 0) {
+        globals.roll_size = DEFAULT_LIMIT;
+    }
     if (switch_strlen_zero(globals.logfile)) {
         set_global_logfile(DEFAULT_LOGFILE);
     }
-
 	if (switch_strlen_zero(globals.format)) {
 		set_global_format(DEFAULT_FORMAT);
 	}
-
 	return 0;
 }
 
 SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **interface, char *filename)
 {
 	switch_status_t status;
-	*interface = &log2file_module_interface;
 
+	if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "OH OH no pool\n");
+		return SWITCH_STATUS_TERM;
+	}
+   
+	*interface = &log2file_module_interface; 
+    
 	if ((status=load_config()) != SWITCH_STATUS_SUCCESS) {
 		return status;
 	}
-
     mod_log2file_openlogfile();
 	switch_log_bind_logger(mod_log2file_logger, SWITCH_LOG_DEBUG);
-    
    	return SWITCH_STATUS_SUCCESS;
 }
 



More information about the Freeswitch-branches mailing list