minor simplification of a conditional statement
[asterisk/asterisk.git] / cdr / cdr_pgsql.c
index e8f5b73..4ccd4aa 100644 (file)
@@ -25,6 +25,7 @@
  * \brief PostgreSQL CDR logger 
  * 
  * \author Matthew D. Hardeman <mhardemn@papersoft.com> 
+ * \extref PostgreSQL http://www.postgresql.org/
  *
  * See also
  * \arg \ref Config_cdr
@@ -67,18 +68,18 @@ static int connected = 0;
 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
 
 static PGconn  *conn = NULL;
-static PGresult        *result = NULL;
 
 static int pgsql_log(struct ast_cdr *cdr)
 {
-       struct tm tm;
+       struct ast_tm tm;
        char sqlcmd[2048] = "", timestr[128];
        char *pgerror;
+       PGresult *result;
 
        ast_mutex_lock(&pgsql_lock);
 
-       localtime_r(&cdr->start.tv_sec,&tm);
-       strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
+       ast_localtime(&cdr->start, &tm, NULL);
+       ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 
        if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
                conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
@@ -87,7 +88,9 @@ static int pgsql_log(struct ast_cdr *cdr)
                } else {
                        pgerror = PQerrorMessage(conn);
                        ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
-                        ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
+                       ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
+                       PQfinish(conn);
+                       conn = NULL;
                }
        }
 
@@ -120,8 +123,7 @@ static int pgsql_log(struct ast_cdr *cdr)
                        return -1;
                }
 
-               if (option_debug > 1)
-                       ast_log(LOG_DEBUG, "cdr_pgsql: inserting a CDR record.\n");
+               ast_debug(2, "cdr_pgsql: inserting a CDR record.\n");
 
                snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,"
                                 "lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES"
@@ -129,8 +131,7 @@ static int pgsql_log(struct ast_cdr *cdr)
                                 table,timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,
                                 cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
                
-               if (option_debug > 2)
-                       ast_log(LOG_DEBUG, "cdr_pgsql: SQL command executed:  %s\n",sqlcmd);
+               ast_debug(3, "cdr_pgsql: SQL command executed:  %s\n",sqlcmd);
                
                /* Test to be sure we're still connected... */
                /* If we're connected, and connection is working, good. */
@@ -147,32 +148,36 @@ static int pgsql_log(struct ast_cdr *cdr)
                                pgerror = PQerrorMessage(conn);
                                ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
                                ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
+                               PQfinish(conn);
+                               conn = NULL;
                                connected = 0;
                                ast_mutex_unlock(&pgsql_lock);
                                return -1;
                        }
                }
                result = PQexec(conn, sqlcmd);
-               if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
-                        pgerror = PQresultErrorMessage(result);
+               if (PQresultStatus(result) != PGRES_COMMAND_OK) {
+                       pgerror = PQresultErrorMessage(result);
                        ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
-                        ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
+                       ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
                        ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
                        PQreset(conn);
                        if (PQstatus(conn) == CONNECTION_OK) {
                                ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
                                connected = 1;
+                               PQclear(result);
                                result = PQexec(conn, sqlcmd);
-                               if ( PQresultStatus(result) != PGRES_COMMAND_OK)
-                               {
+                               if (PQresultStatus(result) != PGRES_COMMAND_OK) {
                                        pgerror = PQresultErrorMessage(result);
                                        ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
                                        ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
                                }
                        }
                        ast_mutex_unlock(&pgsql_lock);
+                       PQclear(result);
                        return -1;
                }
+               PQclear(result);
        }
        ast_mutex_unlock(&pgsql_lock);
        return 0;
@@ -180,20 +185,19 @@ static int pgsql_log(struct ast_cdr *cdr)
 
 static int my_unload_module(void)
 { 
-       if (conn)
-               PQfinish(conn);
+       PQfinish(conn);
        if (pghostname)
-               free(pghostname);
+               ast_free(pghostname);
        if (pgdbname)
-               free(pgdbname);
+               ast_free(pgdbname);
        if (pgdbuser)
-               free(pgdbuser);
+               ast_free(pgdbuser);
        if (pgpassword)
-               free(pgpassword);
+               ast_free(pgpassword);
        if (pgdbport)
-               free(pgdbport);
+               ast_free(pgdbport);
        if (table)
-               free(table);
+               ast_free(table);
        ast_cdr_unregister(name);
        return 0;
 }
@@ -257,20 +261,19 @@ static int process_my_load_module(struct ast_config *cfg)
 
        if (option_debug) {
                if (ast_strlen_zero(pghostname))
-                       ast_log(LOG_DEBUG, "cdr_pgsql: using default unix socket\n");
+                       ast_debug(1, "cdr_pgsql: using default unix socket\n");
                else
-                       ast_log(LOG_DEBUG, "cdr_pgsql: got hostname of %s\n", pghostname);
-               ast_log(LOG_DEBUG, "cdr_pgsql: got port of %s\n", pgdbport);
-               ast_log(LOG_DEBUG, "cdr_pgsql: got user of %s\n", pgdbuser);
-               ast_log(LOG_DEBUG, "cdr_pgsql: got dbname of %s\n", pgdbname);
-               ast_log(LOG_DEBUG, "cdr_pgsql: got password of %s\n", pgpassword);
-               ast_log(LOG_DEBUG, "cdr_pgsql: got sql table name of %s\n", table);
+                       ast_debug(1, "cdr_pgsql: got hostname of %s\n", pghostname);
+                       ast_debug(1, "cdr_pgsql: got port of %s\n", pgdbport);
+                       ast_debug(1, "cdr_pgsql: got user of %s\n", pgdbuser);
+                       ast_debug(1, "cdr_pgsql: got dbname of %s\n", pgdbname);
+                       ast_debug(1, "cdr_pgsql: got password of %s\n", pgpassword);
+                       ast_debug(1, "cdr_pgsql: got sql table name of %s\n", table);
        }
        
        conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
        if (PQstatus(conn) != CONNECTION_BAD) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "Successfully connected to PostgreSQL database.\n");
+               ast_debug(1, "Successfully connected to PostgreSQL database.\n");
                connected = 1;
        } else {
                 pgerror = PQerrorMessage(conn);
@@ -310,8 +313,12 @@ static int unload_module(void)
 
 static int reload(void)
 {
+       int res;
+       ast_mutex_lock(&pgsql_lock);
        my_unload_module();
-       return my_load_module();
+       res = my_load_module();
+       ast_mutex_unlock(&pgsql_lock);
+       return res;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",