Remove some unused variables
[asterisk/asterisk.git] / cdr / cdr_custom.c
index e89f022..6ecef8a 100644 (file)
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <sys/types.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-
-#include <stdlib.h>
-#include <unistd.h>
 #include <time.h>
 
+#include "asterisk/paths.h"    /* use ast_config_AST_LOG_DIR */
 #include "asterisk/channel.h"
 #include "asterisk/cdr.h"
 #include "asterisk/module.h"
 #include "asterisk/config.h"
 #include "asterisk/pbx.h"
-#include "asterisk/logger.h"
 #include "asterisk/utils.h"
+#include "asterisk/lock.h"
 
 #define CUSTOM_LOG_DIR "/cdr_custom"
 
 #define DATE_FORMAT "%Y-%m-%d %T"
 
 AST_MUTEX_DEFINE_STATIC(lock);
+AST_MUTEX_DEFINE_STATIC(mf_lock);
 
 static char *name = "cdr-custom";
 
-static FILE *mf = NULL;
-
 static char master[PATH_MAX];
 static char format[1024]="";
 
@@ -68,25 +61,30 @@ static int load_config(int reload)
 {
        struct ast_config *cfg;
        struct ast_variable *var;
+       struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
        int res = -1;
 
+       if ((cfg = ast_config_load("cdr_custom.conf", config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
+               return 0;
+
        strcpy(format, "");
        strcpy(master, "");
-       if((cfg = ast_config_load("cdr_custom.conf"))) {
+       ast_mutex_lock(&lock);
+       if (cfg) {
                var = ast_variable_browse(cfg, "mappings");
                while(var) {
-                       ast_mutex_lock(&lock);
                        if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
-                               if (strlen(var->value) > (sizeof(format) - 2))
+                               if (strlen(var->value) > (sizeof(format) - 1))
                                        ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
-                               strncpy(format, var->value, sizeof(format) - 2);
+                               ast_copy_string(format, var->value, sizeof(format) - 1);
                                strcat(format,"\n");
                                snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
-                               ast_mutex_unlock(&lock);
+                               if (var->next) {
+                                       ast_log(LOG_NOTICE, "Sorry, only one mapping is supported at this time, mapping '%s' will be ignored at line %d.\n", var->next->name, var->next->lineno); 
+                                       break;
+                               }
                        } else
                                ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
-                       if (var->next)
-                               ast_log(LOG_NOTICE, "Sorry, only one mapping is supported at this time, mapping '%s' will be ignored at line %d.\n", var->next->name, var->next->lineno); 
                        var = var->next;
                }
                ast_config_destroy(cfg);
@@ -97,6 +95,7 @@ static int load_config(int reload)
                else
                        ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
        }
+       ast_mutex_unlock(&lock);
        
        return res;
 }
@@ -105,6 +104,8 @@ static int load_config(int reload)
 
 static int custom_log(struct ast_cdr *cdr)
 {
+       FILE *mf = NULL;
+
        /* Make sure we have a big enough buf */
        char buf[2048];
        struct ast_channel dummy;
@@ -113,7 +114,6 @@ static int custom_log(struct ast_cdr *cdr)
        if (ast_strlen_zero(master))
                return 0;
 
-       memset(buf, 0 , sizeof(buf));
        /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
        memset(&dummy, 0, sizeof(dummy));
        dummy.cdr = cdr;
@@ -122,23 +122,24 @@ static int custom_log(struct ast_cdr *cdr)
        /* because of the absolutely unconditional need for the
           highest reliability possible in writing billing records,
           we open write and close the log file each time */
+       ast_mutex_lock(&mf_lock);
        mf = fopen(master, "a");
-       if (!mf) {
-               ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
-       }
        if (mf) {
                fputs(buf, mf);
                fflush(mf); /* be particularly anal here */
                fclose(mf);
                mf = NULL;
+               ast_mutex_unlock(&mf_lock);
+       } else {
+               ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
+               ast_mutex_unlock(&mf_lock);
        }
+
        return 0;
 }
 
 static int unload_module(void)
 {
-       if (mf)
-               fclose(mf);
        ast_cdr_unregister(name);
        return 0;
 }
@@ -151,10 +152,9 @@ static int load_module(void)
                res = ast_cdr_register(name, ast_module_info->description, custom_log);
                if (res)
                        ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
-               if (mf)
-                       fclose(mf);
-       }
-       return res;
+               return res;
+       } else 
+               return AST_MODULE_LOAD_DECLINE;
 }
 
 static int reload(void)