Index: cdr_addon_mysql.c
===================================================================
--- cdr_addon_mysql.c	(revision 350)
+++ cdr_addon_mysql.c	(working copy)
@@ -14,6 +14,13 @@
  * Added an automatic reconnect as to not lose a cdr record
  * Cleaned up the original code to match the coding guidelines
  *
+ * Modified June 5, 2007
+ * Eric Caron <eric.caron@gmail.com>
+ * Simplified if/else logic statements
+ * Setup uniqueid to be configurable post-compilation (using ast_true())
+ * Changed LOG_WARNING to LOG_NOTICE when using default host, database, user, password and/or socket
+ * Made logging of configuration options consistent (not case based)
+ *
  * This program is free software, distributed under the terms of
  * the GNU General Public License.
  *
@@ -60,6 +67,7 @@
 static int totalrecords = 0;
 static int userfield = 0;
 static unsigned int timeout = 0;
+static int loguniqueid = 0;
 
 AST_MUTEX_DEFINE_STATIC(mysql_lock);
 
@@ -121,9 +129,7 @@
 	char sqlcmd[2048], timestr[128];
 	char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
 	int retries = 5;
-#ifdef MYSQL_LOGUNIQUEID
 	char *uniqueid = NULL;
-#endif
 
 	ast_mutex_lock(&mysql_lock);
 
@@ -189,19 +195,13 @@
 		mysql_escape_string(lastapp, cdr->lastapp, strlen(cdr->lastapp));
 	if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
 		mysql_escape_string(lastdata, cdr->lastdata, strlen(cdr->lastdata));
-#ifdef MYSQL_LOGUNIQUEID
-	if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
+	if (loguniqueid && ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL))
 		mysql_escape_string(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
-#endif
 	if (userfield && ((userfielddata = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL))
 		mysql_escape_string(userfielddata, cdr->userfield, strlen(cdr->userfield));
 
 	/* Check for all alloca failures above at once */
-#ifdef MYSQL_LOGUNIQUEID
-	if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
-#else
-	if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata)) {
-#endif
+	if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (loguniqueid && !uniqueid)) {
 		ast_log(LOG_ERROR, "cdr_mysql:  Out of memory error (insert fails)\n");
 		ast_mutex_unlock(&mysql_lock);
 		return -1;
@@ -210,17 +210,17 @@
 	ast_log(LOG_DEBUG, "cdr_mysql: inserting a CDR record.\n");
 
 	if (userfield && userfielddata) {
-#ifdef MYSQL_LOGUNIQUEID
-		sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')", dbtable, 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, userfielddata);
-#else
-		sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, userfielddata);
-#endif
+		if (loguniqueid) {
+			sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')", dbtable, 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, userfielddata);
+		} else {
+			sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode, userfielddata);
+		}
 	} else {
-#ifdef MYSQL_LOGUNIQUEID
-		sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, 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);
-#else
-		sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode);
-#endif
+		if (loguniqueid) {
+			sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s')", dbtable, 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);
+		} else {
+			sprintf(sqlcmd, "INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s')", dbtable, timestr, clid, cdr->src, cdr->dst, dcontext, channel, dstchannel, lastapp, lastdata, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), cdr->amaflags, cdr->accountcode);
+		}
 	}
 	
 	ast_log(LOG_DEBUG, "cdr_mysql: SQL command as follows: %s\n", sqlcmd);
@@ -277,6 +277,7 @@
 		password = NULL;
 		password_alloc = 0;
 	}
+	loguniqueid = 0;
 	dbport = 0;
 	ast_cdr_unregister(name);
 	return 0;
@@ -312,7 +313,7 @@
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING, "MySQL server hostname not specified.  Assuming localhost\n");
+		ast_log(LOG_NOTICE, "MySQL server hostname not specified.  Assuming localhost\n");
 		hostname = "localhost";
 	}
 
@@ -327,7 +328,7 @@
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING, "MySQL database not specified.  Assuming asteriskcdrdb\n");
+		ast_log(LOG_NOTICE, "MySQL database not specified.  Assuming asteriskcdrdb\n");
 		dbname = "asteriskcdrdb";
 	}
 
@@ -342,7 +343,7 @@
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING, "MySQL database user not specified.  Assuming root\n");
+		ast_log(LOG_NOTICE, "MySQL database user not specified.  Assuming root\n");
 		dbuser = "root";
 	}
 
@@ -357,7 +358,7 @@
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING, "MySQL database sock file not specified.  Using default\n");
+		ast_log(LOG_NOTICE, "MySQL database sock file not specified.  Using default\n");
 		dbsock = NULL;
 	}
 
@@ -387,41 +388,40 @@
 			return -1;
 		}
 	} else {
-		ast_log(LOG_WARNING, "MySQL database password not specified.  Assuming blank\n");
+		ast_log(LOG_NOTICE, "MySQL database password not specified.  Assuming blank\n");
 		password = "";
 	}
 
 	tmp = ast_variable_retrieve(cfg, "global", "port");
-	if (tmp) {
-		if (sscanf(tmp, "%d", &dbport) < 1) {
-			ast_log(LOG_WARNING, "Invalid MySQL port number.  Using default\n");
-			dbport = 0;
-		}
+	if ((tmp != NULL) && (sscanf(tmp, "%d", &dbport) < 1)) {
+		ast_log(LOG_WARNING, "Invalid MySQL port number.  Using default\n");
+		dbport = 0;
 	}
 
 	tmp = ast_variable_retrieve(cfg, "global", "timeout");
-	if (tmp) {
-		if (sscanf(tmp,"%d", &timeout) < 1) {
-			ast_log(LOG_WARNING, "Invalid MySQL timeout number.  Using default\n");
-			timeout = 0;
-		}
+	if ((tmp != NULL) && (sscanf(tmp,"%d", &timeout) < 1)) {
+		ast_log(LOG_WARNING, "Invalid MySQL timeout number.  Using default\n");
+		timeout = 0;
 	}
 	
 	tmp = ast_variable_retrieve(cfg, "global", "userfield");
-	if (tmp) {
-		if (sscanf(tmp, "%d", &userfield) < 1) {
-			ast_log(LOG_WARNING, "Invalid MySQL configuration file\n");
-			userfield = 0;
-		}
+	if ((tmp != NULL) && (sscanf(tmp, "%d", &userfield) < 1)) {
+		ast_log(LOG_WARNING, "Invalid MySQL configuration file\n");
+		userfield = 0;
 	}
 	
+	tmp = ast_variable_retrieve(cfg, "global", "loguniqueid");
+	if ((tmp != NULL) && (ast_true(tmp))) {
+		loguniqueid = 1;
+	}
+	
 	ast_config_destroy(cfg);
 
 	ast_log(LOG_DEBUG, "cdr_mysql: got hostname of %s\n", hostname);
 	ast_log(LOG_DEBUG, "cdr_mysql: got port of %d\n", dbport);
 	ast_log(LOG_DEBUG, "cdr_mysql: got a timeout of %d\n", timeout);
-	if (dbsock)
-		ast_log(LOG_DEBUG, "cdr_mysql: got sock file of %s\n", dbsock);
+	ast_log(LOG_DEBUG, "cdr_mysql: got sock file of %s\n", dbsock);
+	ast_log(LOG_DEBUG, "cdr_mysql: got loguniqueid of %d\n", loguniqueid);
 	ast_log(LOG_DEBUG, "cdr_mysql: got user of %s\n", dbuser);
 	ast_log(LOG_DEBUG, "cdr_mysql: got dbname of %s\n", dbname);
 	ast_log(LOG_DEBUG, "cdr_mysql: got password of %s\n", password);
