[Freeswitch-svn] [commit] r6504 - in freeswitch/trunk/src/mod: applications/mod_voicemail loggers/mod_logfile loggers/mod_syslog

Freeswitch SVN anthm at freeswitch.org
Tue Dec 4 17:36:05 EST 2007


Author: anthm
Date: Tue Dec  4 17:36:05 2007
New Revision: 6504

Modified:
   freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c
   freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c
   freeswitch/trunk/src/mod/loggers/mod_syslog/mod_syslog.c

Log:
update logger code

Modified: freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c	Tue Dec  4 17:36:05 2007
@@ -1140,7 +1140,7 @@
                     message_count(profile, cbt->user, cbt->domain, cbt->in_folder, &total_new_messages, &total_saved_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);
 
                     snprintf(tmp,sizeof(tmp), "%d", total_new_messages);

Modified: freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c
==============================================================================
--- freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c	(original)
+++ freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c	Tue Dec  4 17:36:05 2007
@@ -38,7 +38,7 @@
 
 #define DEFAULT_FORMAT   "${data}" 
 #define DEFAULT_LIMIT    0x7FFFFFFF
-#define WARM_FUZZY_OFFSET 64
+#define WARM_FUZZY_OFFSET 256
 #define MAX_ROT 4096 /* why not */
 static const uint8_t STATIC_LEVELS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
 
@@ -107,21 +107,52 @@
 	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 */
 static switch_status_t mod_logfile_rotate(void)
 {
 	unsigned int i = 0;
-	unsigned int flags = 0;
 	char *p = NULL;
-	char *q = NULL;
 	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;
+    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;
-	switch_core_new_memory_pool(&pool);   
 
 	stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset);
 
@@ -129,78 +160,34 @@
 		return SWITCH_STATUS_FALSE;
 
 	p = malloc(strlen(globals.logfile)+WARM_FUZZY_OFFSET);
-	if (!p) {
-		return SWITCH_STATUS_FALSE;
-	}
-
-	q = malloc(1024);
-	if (!q) {
-		free(p);
-		return SWITCH_STATUS_FALSE;
-	}
+    assert(p);
 
 	memset(p, '\0', strlen(globals.logfile)+WARM_FUZZY_OFFSET);
 
+    switch_core_new_memory_pool(&pool);
+    
 	for (i=1; i < MAX_ROT; i++) {
-		sprintf((char *)p, "%s.%i", globals.logfile, i);
-
-		stat = switch_file_open(&l_afd, p, SWITCH_FOPEN_READ, SWITCH_FPROT_UREAD, pool);
-
-		if (stat == SWITCH_STATUS_SUCCESS) {
-			switch_file_close(l_afd);
-			continue;
-		}
-		flags |= SWITCH_FOPEN_READ;
-		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;
+		sprintf((char *)p, "%s.%s.%i", globals.logfile, date, i);
+        if (switch_file_exists(p, pool) == SWITCH_STATUS_SUCCESS) {
+            continue;
+        }
+        
+        switch_file_close(globals.log_afd);
+        switch_file_rename(globals.logfile, p, pool);
+        mod_logfile_openlogfile(SWITCH_FALSE);
+        break;
 	}
 
-	offset = 0;
-	stat = switch_file_seek(globals.log_afd, SWITCH_SEEK_SET, &offset);
 	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) {
-		mod_logfile_rotate();
-	}
+    switch_core_destroy_memory_pool(&pool);
 
 	return SWITCH_STATUS_SUCCESS;
 }
 
+
+#if 0
 /* write to the actual logfile */
 static switch_status_t mod_logfile_write(char *fmt, ...) 
 {
@@ -219,47 +206,43 @@
 
 	globals.log_size += len;
 
-	/* we may want to hold back on this for preformance reasons */
 	if (globals.log_size >= globals.roll_size) {
 		mod_logfile_rotate();
 	}
 
 	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];
-	char date[80] = "";
-	switch_time_exp_t time;
-	switch_size_t retsize;
-	char *message = NULL;
-
-	if (!levels[node->level].on) {
-		return SWITCH_STATUS_SUCCESS;
-	}
+	switch_size_t len;
+	len = strlen(log_data);
 
-	message = (char *)malloc(strlen(globals.format)+2);
-	switch_copy_string(message, globals.format, strlen(globals.format)+1);
+	if (len <= 0)
+		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) {
-		return SWITCH_STATUS_FALSE;
+	if (globals.log_size >= globals.roll_size) {
+		mod_logfile_rotate();
 	}
 
-	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);
+	return SWITCH_STATUS_SUCCESS;
+}
 
-	snprintf(line_no, sizeof(line_no), "%d", node->line);
-	message = switch_string_replace(message, "${line}", line_no);
+static switch_status_t mod_logfile_logger(const switch_log_node_t *node, switch_log_level_t level)
+{
 
-	if (!switch_strlen_zero(message)) {
-		mod_logfile_write("%s", message);
-	}
+	if (levels[node->level].on) {
+        mod_logfile_raw_write(node->data);
+    }
 
 	return SWITCH_STATUS_SUCCESS;
 }
@@ -316,7 +299,7 @@
 		return status;
 	}
 
-	mod_logfile_openlogfile();
+	mod_logfile_openlogfile(SWITCH_TRUE);
 
 	switch_log_bind_logger(mod_logfile_logger, SWITCH_LOG_DEBUG);
 

Modified: freeswitch/trunk/src/mod/loggers/mod_syslog/mod_syslog.c
==============================================================================
--- freeswitch/trunk/src/mod/loggers/mod_syslog/mod_syslog.c	(original)
+++ freeswitch/trunk/src/mod/loggers/mod_syslog/mod_syslog.c	Tue Dec  4 17:36:05 2007
@@ -69,31 +69,8 @@
 static switch_status_t mod_syslog_logger(const switch_log_node_t *node, switch_log_level_t level)
 {
 	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;
 
-	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) {
 		case SWITCH_LOG_DEBUG:
 			syslog_level = LOG_DEBUG;
@@ -122,10 +99,10 @@
 	}
 
 	if (!switch_strlen_zero(message)) {
-		syslog(syslog_level, "%s", message);
+		syslog(syslog_level, "%s", node->data);
 	}
 
-	free(message);
+
 
 	return SWITCH_STATUS_SUCCESS;
 }



More information about the Freeswitch-svn mailing list