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

Freeswitch SVN anthonyl at freeswitch.org
Fri Jun 1 17:20:24 EDT 2007


Author: anthonyl
Date: Fri Jun  1 17:20:24 2007
New Revision: 5244

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

Log:
update. converted all of the calls to switch_file_write that we need.


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	Fri Jun  1 17:20:24 2007
@@ -55,7 +55,7 @@
     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;
+    switch_file_t    *log_afd;
 } globals;
 
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_logfile, globals.logfile)
@@ -79,15 +79,15 @@
 };
 static switch_loadable_module_interface_t log2file_module_interface = {
 	/*.module_name */ modname,
-	/*.endpoint_interface */ NULL,
-	/*.timer_interface */ NULL,
-	/*.dialplan_interface */ NULL,
-	/*.codec_interface */ NULL,
+	/*.endpoint_interface    */ NULL,
+	/*.timer_interface       */ NULL,
+	/*.dialplan_interface    */ NULL,
+	/*.codec_interface       */ NULL,
 	/*.application_interface */ NULL,
-	/*.api_interface */ NULL,
-	/*.file_interface */ NULL,
-	/*.speech_interface */ NULL,
-	/*.directory_interface */ NULL
+	/*.api_interface         */ NULL,
+	/*.file_interface        */ NULL,
+	/*.speech_interface      */ NULL,
+	/*.directory_interface   */ NULL
 };
 
 /* this may exist somewhere else in the code? */
@@ -164,13 +164,13 @@
 static switch_status_t mod_log2file_openlogfile(void)
 {
     int fd;
-    apr_file_t *afd;
-    apr_status_t stat;
+    switch_file_t *afd;
+    switch_status_t stat;
 
-    stat = apr_file_open(&afd, globals.logfile,
+    stat = switch_file_open(&afd, globals.logfile,
                          APR_CREATE| APR_READ | APR_WRITE | APR_APPEND, NULL, NULL);
 
-    if (stat != APR_SUCCESS) {
+    if (stat != SWITCH_STATUS_SUCCESS) {
         return SWITCH_STATUS_FALSE;
     }
     globals.log_afd = afd;
@@ -186,13 +186,14 @@
     unsigned int i;
     unsigned char *p;
     unsigned char *q;
-    apr_status_t stat;
-    apr_file_t *l_afd;
+    switch_status_t stat;
+    switch_size_t   nbytes;
+    switch_file_t *l_afd;
     
     globals.log_size = 0;
-    stat = apr_file_seek(globals.log_afd, APR_SET, 0);
+    stat = switch_file_seek(globals.log_afd, APR_SET, 0);
 
-    if (stat != APR_SUCCESS)
+    if (stat != SWITCH_STATUS_SUCCESS)
         return SWITCH_STATUS_FALSE;
     
     p = malloc(strlen(globals.logfile)+WARM_FUZZY_OFFSET);
@@ -207,23 +208,21 @@
     memset(p, '\0', strlen(globals.logfile)+WARM_FUZZY_OFFSET);
     for (i=1;i<MAX_ROT; i++) {
         sprintf(p, "%s.%i", globals.logfile, i);
-        stat = apr_file_open(l_afd, p, APR_READ, NULL, NULL);
-        if (stat == APR_SUCCESS) {
-            apr_file_close(l_afd);
+        stat = switch_file_open(l_afd, p, APR_READ, NULL, NULL);
+        if (stat == SWITCH_STATUS_SUCCESS) {
+            switch_file_close(l_afd);
             continue;
         }
-        stat = apr_file_open(l_afd, p, APR_READ | APR_WRITE | APR_CREATE , NULL, NULL);
-        if (stat == APR_SUCCESS)
+        stat = switch_file_open(l_afd, p, APR_READ | APR_WRITE | APR_CREATE , NULL, NULL);
+        if (stat == SWITCH_STATUS_SUCCESS)
             break;
     }
-    while( ret =  fread(q, 1024, 1, globals.log_fd)) {
-        fwrite(q, 1024, 1, fd);        
-        memset(q, '\0', 1024); 
+    nbytes = 1024;
+    while ((stat = switch_file_read(&globals.log_afd, q, &nbytes)) == SWITCH_STATUS_SUCCESS) {
+        switch_file_write(l_afd, q, &nbytes);
+        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;
@@ -232,16 +231,14 @@
 /* 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;
-
-    stat = apr_file_info_get(&finfo, APR_FINFO_SIZE, &globals.log_afd);
-
-    if (stat != APR_SUCCESS)
+    switch_status_t stat;
+    switch_size_t   fsize;
+    fsize = switch_file_get_size(&globals.log_afd);
+       
+    if (stat != SWITCH_STATUS_SUCCESS)
         return SWITCH_STATUS_FALSE;
 
-    if (finfo.size >= globals.roll_size) {
+    if (fsize >= globals.roll_size) {
         return mod_log2file_rotate();
     }
     
@@ -251,21 +248,23 @@
 /* write to the actual logfile */
 static switch_status_t mod_log2file_write(char *fmt, ...) 
 {
-    int ret;
     unsigned int len;
-    char log_data[MAX_LENGTH]; /* seems like a waste of stack space no ;), i'll fix this later */   
+    char *log_data;
+    switch_status_t stat;
     va_list args;
     
     va_start(args, fmt);
 #ifdef WIN32
-    _vsnprintf(&log_data, sizeof(log_data), fmt, args);
+    len = _vasprintf(&log_data, fmt, args);
 #else
-    vsnprintf((char *)&log_data, sizeof(log_data), fmt, args);
+    len = vasprintf(&log_data, fmt, args);
 #endif
-    if ((ret = fprintf(globals.log_fd, "%s" , log_data))<0) {
+    if (len <= 0)
         return SWITCH_STATUS_FALSE;
-    }
-    globals.log_size += ret;
+    stat = switch_file_write(&globals.log_afd, log_data, len);
+    if (stat != SWITCH_STATUS_SUCCESS)
+        return SWITCH_STATUS_FALSE;
+    globals.log_size += len;
     /* we may want to hold back on this for preformance reasons */
     mod_log2file_check();
     return SWITCH_STATUS_SUCCESS;



More information about the Freeswitch-branches mailing list