Index: app_zapras.c
===================================================================
--- app_zapras.c	(revision 69052)
+++ app_zapras.c	(working copy)
@@ -162,8 +162,7 @@
 			if (!res) {
 				/* Check for hangup */
 				if (chan->_softhangup && !signalled) {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Channel '%s' hungup.  Signalling RAS at %d to die...\n", chan->name, pid);
+					ast_debug(1, "Channel '%s' hungup.  Signalling RAS at %d to die...\n", chan->name, pid);
 					kill(pid, SIGTERM);
 					signalled=1;
 				}
Index: app_stack.c
===================================================================
--- app_stack.c	(revision 69052)
+++ app_stack.c	(working copy)
@@ -152,11 +152,11 @@
 	oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
 	AST_LIST_UNLOCK(oldlist);
 
-	if (oldframe)
+	if (oldframe) {
 		gosub_release_frame(chan, oldframe);
-	else if (option_debug)
-		ast_log(LOG_DEBUG, "%s called with an empty gosub stack\n", app_pop);
-
+	} else {
+		ast_debug(1, "%s called with an empty gosub stack\n", app_pop);
+	}
 	return 0;
 }
 
@@ -210,8 +210,7 @@
 	u = ast_module_user_add(chan);
 
 	if (!stack_store) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Channel %s has no datastore, so we're allocating one.\n", chan->name);
+		ast_debug(1, "Channel %s has no datastore, so we're allocating one.\n", chan->name);
 		stack_store = ast_channel_datastore_alloc(&stack_info, NULL);
 		if (!stack_store) {
 			ast_log(LOG_ERROR, "Unable to allocate new datastore.  Gosub will fail.\n");
@@ -259,8 +258,7 @@
 	for (i = 0; i < args2.argc; i++) {
 		snprintf(argname, sizeof(argname), "ARG%d", i + 1);
 		pbx_builtin_pushvar_helper(chan, argname, args2.argval[i]);
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Setting '%s' to '%s'\n", argname, args2.argval[i]);
+		ast_debug(1, "Setting '%s' to '%s'\n", argname, args2.argval[i]);
 	}
 
 	/* And finally, save our return address */
Index: app_chanspy.c
===================================================================
--- app_chanspy.c	(revision 69052)
+++ app_chanspy.c	(working copy)
@@ -378,12 +378,12 @@
 			tmp[0] = res;
 			tmp[1] = '\0';
 			if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
-				ast_log(LOG_DEBUG, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext);
+				ast_debug(1, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext);
 				pbx_builtin_setvar_helper(chan, "SPY_CHANNEL", name);
 				running = -2;
 				break;
-			} else if (option_debug > 1) {
-				ast_log(LOG_DEBUG, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
+			} else {
+				ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
 			}
 		} else if (res >= '0' && res <= '9') {
 			inp[x++] = res;
@@ -508,8 +508,8 @@
 				tmp[1] = '\0';
 				if (!ast_goto_if_exists(chan, exitcontext, tmp, 1))
 					goto exit;
-				else if (option_debug > 1)
-					ast_log(LOG_DEBUG, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
+				else
+					ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
 			}
 		}
 
@@ -524,8 +524,8 @@
 			tmp[1] = '\0';
 			if (!ast_goto_if_exists(chan, exitcontext, tmp, 1))
 				goto exit;
-			else if (option_debug > 1)
-				ast_log(LOG_DEBUG, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
+			else
+				ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
 		}
 				
 		/* reset for the next loop around, unless overridden later */
Index: app_festival.c
===================================================================
--- app_festival.c	(revision 69052)
+++ app_festival.c	(working copy)
@@ -221,8 +221,7 @@
 				break;
 			}
 			if (f->frametype == AST_FRAME_DTMF) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "User pressed a key\n");
+				ast_debug(1, "User pressed a key\n");
 				if (intkeys && strchr(intkeys, f->subclass)) {
 					res = f->subclass;
 					ast_frfree(f);
@@ -252,15 +251,13 @@
 						break;
 					}
 					if (res < needed) { /* last frame */
-						if (option_debug)
-							ast_log(LOG_DEBUG, "Last frame\n");
+						ast_debug(1, "Last frame\n");
 						res=0;
 						ast_frfree(f);
 						break;
 					}
 				} else {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "No more waveform\n");
+					ast_debug(1, "No more waveform\n");
 					res = 0;
 				}
 			}
@@ -380,8 +377,7 @@
 			intstr = AST_DIGIT_ANY;
 	}
 	
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Text passed to festival server : %s\n",(char *)data);
+	ast_debug(1, "Text passed to festival server : %s\n",(char *)data);
 	/* Connect to local festival server */
 	
 	fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -440,21 +436,17 @@
     			if (fdesc!=-1) {
     				writecache=1;
     				strln=strlen((char *)data);
-				if (option_debug)
-					ast_log(LOG_DEBUG,"line length : %d\n",strln);
+				ast_debug(1,"line length : %d\n",strln);
     				write(fdesc,&strln,sizeof(int));
     				write(fdesc,data,strln);
 				seekpos=lseek(fdesc,0,SEEK_CUR);
-				if (option_debug)
-					ast_log(LOG_DEBUG,"Seek position : %d\n",seekpos);
+				ast_debug(1,"Seek position : %d\n",seekpos);
     			}
     		} else {
     			read(fdesc,&strln,sizeof(int));
-			if (option_debug)
-				ast_log(LOG_DEBUG,"Cache file exists, strln=%d, strlen=%d\n",strln,(int)strlen((char *)data));
+			ast_debug(1,"Cache file exists, strln=%d, strlen=%d\n",strln,(int)strlen((char *)data));
     			if (strlen((char *)data)==strln) {
-				if (option_debug)
-					ast_log(LOG_DEBUG,"Size OK\n");
+				ast_debug(1,"Size OK\n");
     				read(fdesc,&bigstring,strln);
 	    			bigstring[strln] = 0;
 				if (strcmp(bigstring,data)==0) { 
@@ -471,11 +463,9 @@
 	if (readcache==1) {
 		close(fd);
 		fd=fdesc;
-		if (option_debug)
-			ast_log(LOG_DEBUG,"Reading from cache...\n");
+		ast_debug(1,"Reading from cache...\n");
 	} else {
-		if (option_debug)
-			ast_log(LOG_DEBUG,"Passing text to festival...\n");
+		ast_debug(1,"Passing text to festival...\n");
 		fs=fdopen(dup(fd),"wb");
 		fprintf(fs,festivalcommand,(char *)data);
 		fflush(fs);
@@ -484,8 +474,7 @@
 	
 	/* Write to cache and then pass it down */
 	if (writecache==1) {
-		if (option_debug)
-			ast_log(LOG_DEBUG,"Writing result to cache...\n");
+		ast_debug(1,"Writing result to cache...\n");
 		while ((strln=read(fd,buffer,16384))!=0) {
 			write(fdesc,buffer,strln);
 		}
@@ -495,8 +484,7 @@
 		lseek(fd,seekpos,SEEK_SET);
 	}
 	
-	if (option_debug)
-		ast_log(LOG_DEBUG,"Passing data to channel...\n");
+	ast_debug(1,"Passing data to channel...\n");
 	
 	/* Read back info from server */
 	/* This assumes only one waveform will come back, also LP is unlikely */
@@ -521,16 +509,14 @@
                }
 		ack[3] = '\0';
 		if (strcmp(ack,"WV\n") == 0) {         /* receive a waveform */
-			if (option_debug)
-				ast_log(LOG_DEBUG,"Festival WV command\n");
+			ast_debug(1,"Festival WV command\n");
 			waveform = socket_receive_file_to_buff(fd,&filesize);
 			res = send_waveform_to_channel(chan,waveform,filesize, intstr);
 			ast_free(waveform);
 			break;
 		}
 		else if (strcmp(ack,"LP\n") == 0) {   /* receive an s-expr */
-			if (option_debug)
-				ast_log(LOG_DEBUG,"Festival LP command\n");
+			ast_debug(1,"Festival LP command\n");
 			waveform = socket_receive_file_to_buff(fd,&filesize);
 			waveform[filesize]='\0';
 			ast_log(LOG_WARNING,"Festival returned LP : %s\n",waveform);
Index: app_alarmreceiver.c
===================================================================
--- app_alarmreceiver.c	(revision 69052)
+++ app_alarmreceiver.c	(working copy)
@@ -251,16 +251,14 @@
 			if(option_verbose >= 4)
 				ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: DTMF Digit Timeout on %s\n", chan->name);
 				
-			if (option_debug)
-				ast_log(LOG_DEBUG,"AlarmReceiver: DTMF timeout on chan %s\n",chan->name);
+			ast_debug(1,"AlarmReceiver: DTMF timeout on chan %s\n",chan->name);
 				
 			res = 1;
 			break;
 		}
 		
 		if ((r = ast_waitfor(chan, -1) < 0)) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Waitfor returned %d\n", r);
+			ast_debug(1, "Waitfor returned %d\n", r);
 			continue;
 		}
 			
@@ -355,8 +353,7 @@
 		if (option_verbose >= 3 )
 		ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: can't write metadata\n");	
 		
-		if (option_debug)
-			ast_log(LOG_DEBUG,"AlarmReceiver: can't write metadata\n");
+		ast_debug(1,"AlarmReceiver: can't write metadata\n");
 	}
 	else
 		res = 0;
@@ -407,8 +404,7 @@
 		if(fd == -1) {
 			if (option_verbose >= 3)
 				ast_verbose(VERBOSE_PREFIX_4 "AlarmReceiver: can't make temporary file\n");	
-			if (option_debug)
-				ast_log(LOG_DEBUG,"AlarmReceiver: can't make temporary file\n");
+			ast_debug(1,"AlarmReceiver: can't make temporary file\n");
 			res = -1;
 		}
 		
@@ -529,8 +525,7 @@
 
 		if (option_verbose >= 2)
 			ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: Received Event %s\n", event);
-		if (option_debug)
-			ast_log(LOG_DEBUG, "AlarmReceiver: Received event: %s\n", event);
+		ast_debug(1, "AlarmReceiver: Received event: %s\n", event);
 		
 		/* Calculate checksum */
 		
@@ -560,8 +555,7 @@
 			database_increment("checksum-errors");
 			if (option_verbose >= 2)
 				ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: Nonzero checksum\n");
-			if (option_debug)
-				ast_log(LOG_DEBUG, "AlarmReceiver: Nonzero checksum\n");
+			ast_debug(1, "AlarmReceiver: Nonzero checksum\n");
 			continue;
 		}
 
@@ -572,8 +566,7 @@
 				database_increment("format-errors");
 				if(option_verbose >= 2)
 					ast_verbose(VERBOSE_PREFIX_2 "AlarmReceiver: Wrong message type\n");
-				if (option_debug)
-					ast_log(LOG_DEBUG, "AlarmReceiver: Wrong message type\n");
+				ast_debug(1, "AlarmReceiver: Wrong message type\n");
 			continue;
 			}
 		}
@@ -715,8 +708,7 @@
 	*/
 	
 	if((!res) && (!ast_strlen_zero(event_app)) && (event_head)){
-		if (option_debug)
-			ast_log(LOG_DEBUG,"Alarmreceiver: executing: %s\n", event_app);
+		ast_debug(1,"Alarmreceiver: executing: %s\n", event_app);
 		ast_safe_system(event_app);
 	}
 
Index: app_talkdetect.c
===================================================================
--- app_talkdetect.c	(revision 69052)
+++ app_talkdetect.c	(working copy)
@@ -103,9 +103,7 @@
 			}
 		}
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n", 
-						tmp, sil, min, max);
+	ast_debug(1, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n", tmp, sil, min, max);
 	if (chan->_state != AST_STATE_UP) {
 		/* Otherwise answer unless we're supposed to send this while on-hook */
 		res = ast_answer(chan);
@@ -164,8 +162,7 @@
 									ms = 0;
 								if ((ms > min) && ((max < 0) || (ms < max))) {
 									char ms_str[10];
-									if (option_debug)
-										ast_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms);
+									ast_debug(1, "Found qualified token of %d ms\n", ms);
 
 									/* Save detected talk time (in milliseconds) */ 
 									sprintf(ms_str, "%d", ms );	
@@ -176,8 +173,7 @@
 									ast_frfree(fr);
 									break;
 								} else {
-									if (option_debug)
-										ast_log(LOG_DEBUG, "Found unqualified token of %d ms\n", ms);
+									ast_debug(1, "Found unqualified token of %d ms\n", ms);
 								}
 								notsilent = 0;
 							}
@@ -185,8 +181,7 @@
 							if (!notsilent) {
 								/* Heard some audio, mark the begining of the token */
 								start = ast_tvnow();
-								if (option_debug)
-									ast_log(LOG_DEBUG, "Start of voice token!\n");
+								ast_debug(1, "Start of voice token!\n");
 								notsilent = 1;
 							}
 						}
Index: app_db.c
===================================================================
--- app_db.c	(revision 69052)
+++ app_db.c	(working copy)
@@ -78,8 +78,7 @@
 		family = strsep(&argv, "/");
 		keytree = strsep(&argv, "\0");
 			if (!family || !keytree) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
+				ast_debug(1, "Ignoring; Syntax error in argument\n");
 				ast_module_user_remove(u);
 				return 0;
 			}
@@ -126,8 +125,7 @@
 		family = strsep(&argv, "/");
 		key = strsep(&argv, "\0");
 		if (!family || !key) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
+			ast_debug(1, "Ignoring; Syntax error in argument\n");
 			ast_module_user_remove(u);
 			return 0;
 		}
@@ -138,8 +136,7 @@
 				ast_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
 		}
 	} else {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
+		ast_debug(1, "Ignoring, no parameters\n");
 	}
 
 	ast_module_user_remove(u);
Index: app_mp3.c
===================================================================
--- app_mp3.c	(revision 69052)
+++ app_mp3.c	(working copy)
@@ -193,8 +193,7 @@
 						break;
 					}
 				} else {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "No more mp3\n");
+					ast_debug(1, "No more mp3\n");
 					res = 0;
 					break;
 				}
@@ -202,22 +201,19 @@
 			} else {
 				ms = ast_waitfor(chan, ms);
 				if (ms < 0) {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Hangup detected\n");
+					ast_debug(1, "Hangup detected\n");
 					res = -1;
 					break;
 				}
 				if (ms) {
 					f = ast_read(chan);
 					if (!f) {
-						if (option_debug)
-							ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
+						ast_debug(1, "Null frame == hangup() detected\n");
 						res = -1;
 						break;
 					}
 					if (f->frametype == AST_FRAME_DTMF) {
-						if (option_debug)
-							ast_log(LOG_DEBUG, "User pressed a key\n");
+						ast_debug(1, "User pressed a key\n");
 						ast_frfree(f);
 						res = 0;
 						break;
Index: app_rpt.c
===================================================================
--- app_rpt.c	(revision 69052)
+++ app_rpt.c	(working copy)
@@ -2775,10 +2775,8 @@
 		return DC_ERROR;
 
 	ast_copy_string(digitbuf, digits, sizeof(digitbuf));
+	ast_debug(1, "@@@@ ilink param = %s, digitbuf = %s\n", S_OR(param, "(null)"), digitbuf);
 
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ ilink param = %s, digitbuf = %s\n", S_OR(param, "(null)"), digitbuf);
-
 	switch (myatoi(param)) {
 	case 1: /* Link off */
 		if ((digitbuf[0] == '0') && (myrpt->lastlinknode[0]))
@@ -3122,8 +3120,7 @@
 	if (!myrpt->enable)
 		return DC_ERROR;
 
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ Autopatch up\n");
+	ast_debug(1, "@@@@ Autopatch up\n");
 
 	if (!myrpt->callmode) {
 		/* Set defaults */
@@ -3192,8 +3189,7 @@
 	if (!myrpt->enable)
 		return DC_ERROR;
 	
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ Autopatch down\n");
+	ast_debug(1, "@@@@ Autopatch down\n");
 		
 	rpt_mutex_lock(&myrpt->lock);
 	
@@ -3221,8 +3217,7 @@
 	if (!myrpt->enable)
 		return DC_ERROR;
 
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ status param = %s, digitbuf = %s\n", (param)? param : "(null)", digitbuf);
+	ast_debug(1, "@@@@ status param = %s, digitbuf = %s\n", (param)? param : "(null)", digitbuf);
 	
 	switch (myatoi(param)) {
 	case 1: /* System ID */
@@ -3255,8 +3250,7 @@
 	if ((!myrpt->remote) && (!myrpt->enable))
 		return DC_ERROR;
 
-	if (debug) 
-		ast_log(LOG_DEBUG, "@@@@ macro-oni param = %s, digitbuf = %s\n", (param)? param : "(null)", digitbuf);
+	ast_debug(1, "@@@@ macro-oni param = %s, digitbuf = %s\n", (param)? param : "(null)", digitbuf);
 	
 	mychannel = myrpt->remchannel;
 
@@ -3338,8 +3332,7 @@
 		AST_APP_ARG(param);
 	);
 	
-	if (debug)	
-		ast_log(LOG_DEBUG, "@@@@ Digits collected: %s, source: %d\n", digits, command_source);
+	ast_debug(1, "@@@@ Digits collected: %s, source: %d\n", digits, command_source);
 	
 	if (command_source == SOURCE_DPHONE) {
 		if (!myrpt->p.dphone_functions)
@@ -3379,23 +3372,20 @@
 	stringp = ast_strdupa(vp->value);
 	AST_NONSTANDARD_APP_ARGS(args, stringp, ',');
 
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ action: %s, param = %s\n", args.action, S_OR(args.param, "(null)"));
+	ast_debug(1, "@@@@ action: %s, param = %s\n", args.action, S_OR(args.param, "(null)"));
 	/* Look up the action */
 	for (i = 0; i < (sizeof(function_table) / sizeof(struct function_table_tag)); i++) {
 		if (!strncasecmp(args.action, function_table[i].action, strlen(args.action)))
 			break;
 	}
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ table index i = %d\n", i);
+	ast_debug(1, "@@@@ table index i = %d\n", i);
 	if (i == (sizeof(function_table) / sizeof(struct function_table_tag))) {
 		/* Error, action not in table */
 		return DC_ERROR;
 	}
 	if (function_table[i].function == NULL) {
 		/* Error, function undefined */
-		if (debug)
-			ast_log(LOG_DEBUG, "@@@@ NULL for action: %s\n", args.action);
+		ast_debug(1, "@@@@ NULL for action: %s\n", args.action);
 		return DC_ERROR;
 	}
 	functiondigits = digits + strlen(vp->name);
@@ -3872,16 +3862,14 @@
 	int i;
 	struct zt_radio_param prm;
 
-	if (debug) {
-		char *buf = alloca(30 + txbytes * 3);
-		int len;
-		ast_copy_string(buf, "String output was: ", 30 + txbytes * 3);
-		len = strlen(buf);
-		for (i = 0; i < txbytes; i++)
-			len += snprintf(buf + len, 30 + txbytes * 3 - len, "%02X ", (unsigned char) txbuf[i]);
-		strcat(buf + len, "\n");
-		ast_log(LOG_DEBUG, "%s", buf);
-	}
+	char *buf = alloca(30 + txbytes * 3);
+	int len;
+	ast_copy_string(buf, "String output was: ", 30 + txbytes * 3);
+	len = strlen(buf);
+	for (i = 0; i < txbytes; i++)
+		len += snprintf(buf + len, 30 + txbytes * 3 - len, "%02X ", (unsigned char) txbuf[i]);
+	strcat(buf + len, "\n");
+	ast_debug(1, "%s", buf);
 
 	prm.radpar = ZT_RADPAR_REMMODE;
 	if (asciiflag)
@@ -3920,42 +3908,36 @@
 	/* if no decimal, is invalid */
 	
 	if (s == NULL) {
-		if (debug)
-			ast_log(LOG_DEBUG, "@@@@ Frequency needs a decimal\n");
+		ast_debug(1, "@@@@ Frequency needs a decimal\n");
 		return -1;
 	}
 	
 	*s++ = 0;
 	if (strlen(tmp) < 2) {
-		if (debug)
-			ast_log(LOG_DEBUG, "@@@@ Bad MHz digits: %s\n", tmp);
+		ast_debug(1, "@@@@ Bad MHz digits: %s\n", tmp);
 	 	return -1;
 	}
 	 
 	if (strlen(s) < 3) {
-		if (debug)
-			ast_log(LOG_DEBUG, "@@@@ Bad KHz digits: %s\n", s);
+		ast_debug(1, "@@@@ Bad KHz digits: %s\n", s);
 	 	return -1;
 	}
 
 	if ((s[2] != '0') && (s[2] != '5')) {
-		if (debug)
-			ast_log(LOG_DEBUG, "@@@@ KHz must end in 0 or 5: %c\n", s[2]);
+		ast_debug(1, "@@@@ KHz must end in 0 or 5: %c\n", s[2]);
 	 	return -1;
 	}
 	 
 	band = rbi_mhztoband(tmp);
 	if (band == -1) {
-		if (debug)
-			ast_log(LOG_DEBUG, "@@@@ Bad Band: %s\n", tmp);
+		ast_debug(1, "@@@@ Bad Band: %s\n", tmp);
 	 	return -1;
 	}
 	
 	txpl = rbi_pltocode(myrpt->txpl);
 	
 	if (txpl == -1) {
-		if (debug)
-			ast_log(LOG_DEBUG, "@@@@ Bad TX PL: %s\n", myrpt->txpl);
+		ast_debug(1, "@@@@ Bad TX PL: %s\n", myrpt->txpl);
 	 	return -1;
 	}
 
@@ -4148,8 +4130,7 @@
 	int fd, m, d;
 
 	fd = 0;
-	if (debug) 
-		ast_log(LOG_DEBUG, "New frequency: %s\n", newfreq);
+	ast_debug(1, "New frequency: %s\n", newfreq);
 
 	if (split_freq(&m, &d, newfreq))
 		return -1; 
@@ -4274,53 +4255,44 @@
 {
 	int res;
 	
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ lock on\n");
+	ast_debug(1, "@@@@ lock on\n");
 
 	res = simple_command_ft897(myrpt, 0x00);				/* LOCK on */	
 
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ ptt off\n");
+	ast_debug(1, "@@@@ ptt off\n");
 
 	if (!res)
 		res = simple_command_ft897(myrpt, 0x88);		/* PTT off */
 
-	if (debug)
-		ast_log(LOG_DEBUG, "Modulation mode\n");
+	ast_debug(1, "Modulation mode\n");
 
 	if (!res)
 		res = set_mode_ft897(myrpt, myrpt->remmode);		/* Modulation mode */
 
-	if (debug)
-		ast_log(LOG_DEBUG, "Split off\n");
+	ast_debug(1, "Split off\n");
 
 	if (!res)
 		simple_command_ft897(myrpt, 0x82);			/* Split off */
 
-	if (debug)
-		ast_log(LOG_DEBUG, "Frequency\n");
+	ast_debug(1, "Frequency\n");
 
 	if (!res)
 		res = set_freq_ft897(myrpt, myrpt->freq);		/* Frequency */
 	if ((myrpt->remmode == REM_MODE_FM)) {
-		if (debug)
-			ast_log(LOG_DEBUG, "Offset\n");
+		ast_debug(1, "Offset\n");
 		if (!res)
 			res = set_offset_ft897(myrpt, myrpt->offset);	/* Offset if FM */
 		if ((!res)&&(myrpt->rxplon || myrpt->txplon)) {
-			if (debug)
-				ast_log(LOG_DEBUG, "CTCSS tone freqs.\n");
+			ast_debug(1, "CTCSS tone freqs.\n");
 			res = set_ctcss_freq_ft897(myrpt, myrpt->txpl, myrpt->rxpl); /* CTCSS freqs if CTCSS is enabled */
 		}
 		if (!res) {
-			if (debug)
-				ast_log(LOG_DEBUG, "CTCSS mode\n");
+			ast_debug(1, "CTCSS mode\n");
 			res = set_ctcss_mode_ft897(myrpt, myrpt->txplon, myrpt->rxplon); /* CTCSS mode */
 		}
 	}
 	if ((myrpt->remmode == REM_MODE_USB)||(myrpt->remmode == REM_MODE_LSB)) {
-		if (debug)
-			ast_log(LOG_DEBUG, "Clarifier off\n");
+		ast_debug(1, "Clarifier off\n");
 		simple_command_ft897(myrpt, 0x85);			/* Clarifier off if LSB or USB */
 	}
 	return res;
@@ -4342,8 +4314,7 @@
 {
 	int m, d;
 
-	if (debug)
-		ast_log(LOG_DEBUG, "Before bump: %s\n", myrpt->freq);
+	ast_debug(1, "Before bump: %s\n", myrpt->freq);
 
 	if (split_freq(&m, &d, myrpt->freq))
 		return -1;
@@ -4358,16 +4329,13 @@
 	}
 
 	if (check_freq_ft897(m, d, NULL)) {
-		if (debug)
-			ast_log(LOG_DEBUG, "Bump freq invalid\n");
+		ast_debug(1, "Bump freq invalid\n");
 		return -1;
 	}
 
 	snprintf(myrpt->freq, MAXREMSTR, "%d.%05d", m, d);
+	ast_debug(1, "After bump: %s\n", myrpt->freq);
 
-	if (debug)
-		ast_log(LOG_DEBUG, "After bump: %s\n", myrpt->freq);
-
 	return set_freq_ft897(myrpt, myrpt->freq);	
 }
 
@@ -4768,9 +4736,7 @@
 		/* Check frequency for validity and establish a default mode */
 			
 		snprintf(freq, sizeof(freq), "%s.%03d%02d", args.s1, k, ht);
-
-		if (debug)
-			ast_log(LOG_DEBUG, "New frequency: %s\n", freq);		
+		ast_debug(1, "New frequency: %s\n", freq);		
 	
 		split_freq(&mhz, &decimals, freq);
 
@@ -4842,8 +4808,7 @@
 		i = strlen(digitbuf) - 1;
 		if ((j != 1) || (k < 2)|| (l != 1))
 			break; /* Not yet */
-		if (debug)
-			ast_log(LOG_DEBUG, "PL digits entered %s\n", digitbuf);
+		ast_debug(1, "PL digits entered %s\n", digitbuf);
  		
 		ast_copy_string(tmp, digitbuf, sizeof(tmp));
 		/* see if we have at least 1 */
@@ -4879,8 +4844,7 @@
 		i = strlen(digitbuf) - 1;
 		if ((j != 1) || (k < 2)|| (l != 1))
 			break; /* Not yet */
-		if (debug)
-			ast_log(LOG_DEBUG, "PL digits entered %s\n", digitbuf);
+		ast_debug(1, "PL digits entered %s\n", digitbuf);
 
 		ast_copy_string(tmp, digitbuf, sizeof(tmp));
 		/* see if we have at least 1 */
@@ -6287,8 +6251,7 @@
 		if (who == myrpt->rxchannel) { /* if it was a read from rx */
 			f = ast_read(myrpt->rxchannel);
 			if (!f) {
-				if (debug)
-					ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+				ast_debug(1, "@@@@ rpt:Hung Up\n");
 				break;
 			}
 			if (f->frametype == AST_FRAME_VOICE) {
@@ -6347,24 +6310,21 @@
 				continue;
 			} else if (f->frametype == AST_FRAME_CONTROL) {
 				if (f->subclass == AST_CONTROL_HANGUP) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
 				/* if RX key */
 				if (f->subclass == AST_CONTROL_RADIO_KEY) {
 					if ((!lasttx) || (myrpt->p.duplex > 1)) {
-						if (debug == 7)
-							ast_log(LOG_DEBUG, "@@@@ rx key\n");
+						ast_debug(8, "@@@@ rx key\n");
 						myrpt->keyed = 1;
 					}
 				}
 				/* if RX un-key */
 				if (f->subclass == AST_CONTROL_RADIO_UNKEY) {
 					if ((!lasttx) || (myrpt->p.duplex > 1)) {
-						if (debug == 7)
-							ast_log(LOG_DEBUG, "@@@@ rx un-key\n");
+						ast_debug(8, "@@@@ rx un-key\n");
 						if (myrpt->keyed) {
 							rpt_telemetry(myrpt, UNKEY, NULL);
 						}
@@ -6378,8 +6338,7 @@
 		if (who == myrpt->pchannel) { /* if it was a read from pseudo */
 			f = ast_read(myrpt->pchannel);
 			if (!f) {
-				if (debug)
-					ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+				ast_debug(1, "@@@@ rpt:Hung Up\n");
 				break;
 			}
 			if (f->frametype == AST_FRAME_VOICE) {
@@ -6387,8 +6346,7 @@
 			}
 			if (f->frametype == AST_FRAME_CONTROL) {
 				if (f->subclass == AST_CONTROL_HANGUP) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
@@ -6399,14 +6357,12 @@
 		if (who == myrpt->txchannel) { /* if it was a read from tx */
 			f = ast_read(myrpt->txchannel);
 			if (!f) {
-				if (debug)
-					ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+				ast_debug(1, "@@@@ rpt:Hung Up\n");
 				break;
 			}
 			if (f->frametype == AST_FRAME_CONTROL) {
 				if (f->subclass == AST_CONTROL_HANGUP) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
@@ -6514,14 +6470,12 @@
 					}
 					/* if RX key */
 					if (f->subclass == AST_CONTROL_RADIO_KEY) {
-						if (debug == 7 )
-							ast_log(LOG_DEBUG, "@@@@ rx key\n");
+						ast_debug(8, "@@@@ rx key\n");
 						l->lastrx = 1;
 					}
 					/* if RX un-key */
 					if (f->subclass == AST_CONTROL_RADIO_UNKEY) {
-						if (debug == 7)
-							ast_log(LOG_DEBUG, "@@@@ rx un-key\n");
+						ast_debug(8, "@@@@ rx un-key\n");
 						l->lastrx = 0;
 					}
 					if (f->subclass == AST_CONTROL_HANGUP) {
@@ -6577,8 +6531,7 @@
 				rpt_mutex_unlock(&myrpt->lock);
 				f = ast_read(l->pchan);
 				if (!f) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					toexit = 1;
 					rpt_mutex_lock(&myrpt->lock);
 					break;
@@ -6589,8 +6542,7 @@
 				}
 				if (f->frametype == AST_FRAME_CONTROL) {
 					if (f->subclass == AST_CONTROL_HANGUP) {
-						if (debug)
-							ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+						ast_debug(1, "@@@@ rpt:Hung Up\n");
 						ast_frfree(f);
 						toexit = 1;
 						rpt_mutex_lock(&myrpt->lock);
@@ -6609,14 +6561,12 @@
 		if (who == myrpt->txpchannel) { /* if it was a read from remote tx */
 			f = ast_read(myrpt->txpchannel);
 			if (!f) {
-				if (debug)
-					ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+				ast_debug(1, "@@@@ rpt:Hung Up\n");
 				break;
 			}
 			if (f->frametype == AST_FRAME_CONTROL) {
 				if (f->subclass == AST_CONTROL_HANGUP) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
@@ -6645,8 +6595,7 @@
 		ast_free(ll);
 	}
 	rpt_mutex_unlock(&myrpt->lock);
-	if (debug)
-		ast_log(LOG_DEBUG, "@@@@ rpt:Hung up channel\n");
+	ast_debug(1, "@@@@ rpt:Hung up channel\n");
 	myrpt->rpt_thread = AST_PTHREADT_STOP;
 	pthread_exit(NULL); 
 	return NULL;
@@ -7222,8 +7171,7 @@
 		if (who == chan) { /* if it was a read from incoming */
 			f = ast_read(chan);
 			if (!f) {
-				if (debug)
-					ast_log(LOG_DEBUG, "@@@@ link:Hung Up\n");
+				ast_debug(1, "@@@@ link:Hung Up\n");
 				break;
 			}
 			if (f->frametype == AST_FRAME_VOICE) {
@@ -7235,8 +7183,7 @@
 			if (f->frametype == AST_FRAME_DTMF) {
 				myrpt->remchannel = chan; /* Save copy of channel */
 				if (handle_remote_phone_dtmf(myrpt, f->subclass, &keyed, phone_mode) == -1) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
@@ -7244,29 +7191,25 @@
 			if (f->frametype == AST_FRAME_TEXT) {
 				myrpt->remchannel = chan; /* Save copy of channel */
 				if (handle_remote_data(myrpt, f->data) == -1) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
 			}
 			if (f->frametype == AST_FRAME_CONTROL) {
 				if (f->subclass == AST_CONTROL_HANGUP) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
 				/* if RX key */
 				if (f->subclass == AST_CONTROL_RADIO_KEY) {
-					if (debug == 7)
-						ast_log(LOG_DEBUG, "@@@@ rx key\n");
+					ast_debug(8, "@@@@ rx key\n");
 					keyed = 1;
 				}
 				/* if RX un-key */
 				if (f->subclass == AST_CONTROL_RADIO_UNKEY) {
-					if (debug == 7)
-						ast_log(LOG_DEBUG, "@@@@ rx un-key\n");
+					ast_debug(8, "@@@@ rx un-key\n");
 					keyed = 0;
 				}
 			}
@@ -7308,7 +7251,7 @@
 		if (who == myrpt->rxchannel) { /* if it was a read from radio */
 			f = ast_read(myrpt->rxchannel);
 			if (!f) {
-				if (debug) ast_log(LOG_DEBUG, "@@@@ link:Hung Up\n");
+				ast_debug(1, "@@@@ link:Hung Up\n");
 				break;
 			}
 			if (f->frametype == AST_FRAME_VOICE) {
@@ -7317,15 +7260,13 @@
 				 ast_write(chan, f);
 			} else if (f->frametype == AST_FRAME_CONTROL) {
 				if (f->subclass == AST_CONTROL_HANGUP) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
 				/* if RX key */
 				if (f->subclass == AST_CONTROL_RADIO_KEY) {
-					if (debug == 7)
-						ast_log(LOG_DEBUG, "@@@@ remote rx key\n");
+					ast_debug(8, "@@@@ remote rx key\n");
 					if (!myrpt->remotetx) {
 						ast_indicate(chan, AST_CONTROL_RADIO_KEY);
 						myrpt->remoterx = 1;
@@ -7333,8 +7274,7 @@
 				}
 				/* if RX un-key */
 				if (f->subclass == AST_CONTROL_RADIO_UNKEY) {
-					if (debug == 7)
-						ast_log(LOG_DEBUG, "@@@@ remote rx un-key\n");
+					ast_debug(8, "@@@@ remote rx un-key\n");
 					if (!myrpt->remotetx) {
 						ast_indicate(chan, AST_CONTROL_RADIO_UNKEY);
 						myrpt->remoterx = 0;
@@ -7348,14 +7288,12 @@
 			/* do this cuz you have to */
 			f = ast_read(myrpt->txchannel);
 			if (!f) {
-				if (debug)
-					ast_log(LOG_DEBUG, "@@@@ link:Hung Up\n");
+				ast_debug(1, "@@@@ link:Hung Up\n");
 				break;
 			}
 			if (f->frametype == AST_FRAME_CONTROL) {
 				if (f->subclass == AST_CONTROL_HANGUP) {
-					if (debug)
-						ast_log(LOG_DEBUG, "@@@@ rpt:Hung Up\n");
+					ast_debug(1, "@@@@ rpt:Hung Up\n");
 					ast_frfree(f);
 					break;
 				}
Index: app_meetme.c
===================================================================
--- app_meetme.c	(revision 69052)
+++ app_meetme.c	(working copy)
@@ -948,8 +948,7 @@
 	} else 
 		return RESULT_SHOWUSAGE;
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Cmdline: %s\n", cmdline);
+	ast_debug(1, "Cmdline: %s\n", cmdline);
 
 	admin_exec(NULL, cmdline);
 
@@ -1325,7 +1324,7 @@
 	AST_RWLIST_UNLOCK(&sla_stations);
 
 	if (!trunk_ref) {
-		ast_log(LOG_DEBUG, "Trunk not found for event!\n");
+		ast_debug(1, "Trunk not found for event!\n");
 		return;
 	}
 
@@ -1616,8 +1615,7 @@
 	if (ztc.confmode) {
 		/* Whoa, already in a conference...  Retry... */
 		if (!retryzap) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
+			ast_debug(1, "Zap channel is in a conference already, retrying with pseudo\n");
 			retryzap = 1;
 			goto zapretry;
 		}
@@ -1651,8 +1649,7 @@
 		ast_mutex_unlock(&conf->playlock);
 		goto outrun;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Placed channel %s in ZAP conf %d\n", chan->name, conf->zapconf);
+	ast_debug(1, "Placed channel %s in ZAP conf %d\n", chan->name, conf->zapconf);
 
 	if (!sent_event) {
 		manager_event(EVENT_FLAG_CALL, "MeetmeJoin", 
@@ -1891,8 +1888,7 @@
 						close(fd);
 						using_pseudo = 0;
 					}
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
+					ast_debug(1, "Ooh, something swapped out under us, starting over\n");
 					retryzap = strcasecmp(c->tech->type, "Zap");
 					user->zapchannel = !retryzap;
 					goto zapretry;
@@ -1959,13 +1955,13 @@
 					tmp[0] = f->subclass;
 					tmp[1] = '\0';
 					if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
-						if (option_debug)
-							ast_log(LOG_DEBUG, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext);
+						ast_debug(1, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext);
 						ret = 0;
 						ast_frfree(f);
 						break;
-					} else if (option_debug > 1)
-						ast_log(LOG_DEBUG, "Exit by single digit did not work in meetme. Extension %s does not exist in context %s\n", tmp, exitcontext);
+					} else {
+						ast_debug(2, "Exit by single digit did not work in meetme. Extension %s does not exist in context %s\n", tmp, exitcontext);
+					}
 				} else if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#') && (confflags & CONFFLAG_POUNDEXIT)) {
 					ret = 0;
 					ast_frfree(f);
@@ -2136,10 +2132,8 @@
 					default:
 						break;
 					}
-				} else if (option_debug) {
-					ast_log(LOG_DEBUG,
-						"Got unrecognized frame on channel %s, f->frametype=%d,f->subclass=%d\n",
-						chan->name, f->frametype, f->subclass);
+				} else {
+					ast_debug(1, "Got unrecognized frame on channel %s, f->frametype=%d,f->subclass=%d\n", chan->name, f->frametype, f->subclass);
 				}
 				ast_frfree(f);
 			} else if (outfd > -1) {
@@ -2368,8 +2362,7 @@
 	if (!cnf) {
 		if (dynamic) {
 			/* No need to parse meetme.conf */
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Building dynamic conference '%s'\n", confno);
+			ast_debug(1, "Building dynamic conference '%s'\n", confno);
 			if (dynamic_pin) {
 				if (dynamic_pin[0] == 'q') {
 					/* Query the user to enter a PIN */
@@ -2405,7 +2398,7 @@
 				}
 			}
 			if (!var) {
-				ast_log(LOG_DEBUG, "%s isn't a valid conference\n", confno);
+				ast_debug(1, "%s isn't a valid conference\n", confno);
 			}
 			ast_config_destroy(cfg);
 		}
@@ -3480,8 +3473,7 @@
 			ringing_trunk = sla_choose_ringing_trunk(ringing_station->station, &s_trunk_ref, 1);
 			ast_mutex_unlock(&sla.lock);
 			if (!ringing_trunk) {
-				ast_log(LOG_DEBUG, "Found no ringing trunk for station '%s' to answer!\n",
-					ringing_station->station->name);
+				ast_debug(1, "Found no ringing trunk for station '%s' to answer!\n", ringing_station->station->name);
 				break;
 			}
 			/* Track the channel that answered this trunk */
@@ -4256,7 +4248,7 @@
 		ast_cond_destroy(&cond);
 		ast_autoservice_stop(chan);
 		if (!trunk_ref->trunk->chan) {
-			ast_log(LOG_DEBUG, "Trunk didn't get created. chan: %lx\n", (long) trunk_ref->trunk->chan);
+			ast_debug(1, "Trunk didn't get created. chan: %lx\n", (long) trunk_ref->trunk->chan);
 			pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");
 			sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
 			trunk_ref->chan = NULL;
Index: app_test.c
===================================================================
--- app_test.c	(revision 69052)
+++ app_test.c	(working copy)
@@ -112,8 +112,7 @@
 		ast_log(LOG_NOTICE, "No samples were received from the other side!\n");
 		return -1;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
+	ast_debug(1, "%s: Noise: %d, samples: %d, avg: %d\n", who, noise, samples, noise / samples);
 	return (noise / samples);
 }
 
@@ -153,18 +152,15 @@
 	/* Transmit client version */
 	if (!res)
 		res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Transmit client version\n");
+	ast_debug(1, "Transmit client version\n");
 	
 	/* Read server version */
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Read server version\n");
+	ast_debug(1, "Read server version\n");
 	if (!res) 
 		res = ast_app_getdata(chan, NULL, serverver, sizeof(serverver) - 1, 0);
 	if (res > 0)
 		res = 0;
-	if (option_debug)
-		ast_log(LOG_DEBUG, "server version: %s\n", serverver);
+	ast_debug(1, "server version: %s\n", serverver);
 		
 	if (res > 0)
 		res = 0;
@@ -176,8 +172,7 @@
 		res = ast_dtmf_stream(chan, NULL, testid, 0);		
 	if (!res) 
 		res = ast_dtmf_stream(chan, NULL, "#", 0);		
-	if (option_debug)
-		ast_log(LOG_DEBUG, "send test identifier: %s\n", testid);
+	ast_debug(1, "send test identifier: %s\n", testid);
 
 	if ((res >=0) && (!ast_strlen_zero(testid))) {
 		/* Make the directory to hold the test results in case it's not there */
@@ -193,8 +188,7 @@
 			
 			if (!res) {
 				/* Step 1: Wait for "1" */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 2.  Wait DTMF 1\n");
+				ast_debug(1, "TestClient: 2.  Wait DTMF 1\n");
 				res = ast_waitfordigit(chan, 3000);
 				fprintf(f, "WAIT DTMF 1:   %s\n", (res != '1') ? "FAIL" : "PASS");
 				if (res == '1')
@@ -206,8 +200,7 @@
 				res = ast_safe_sleep(chan, 1000);
 			if (!res) {
 				/* Step 2: Send "2" */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 2.  Send DTMF 2\n");
+				ast_debug(1, "TestClient: 2.  Send DTMF 2\n");
 				res = ast_dtmf_stream(chan, NULL, "2", 0);
 				fprintf(f, "SEND DTMF 2:   %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -215,8 +208,7 @@
 			}
 			if (!res) {
 				/* Step 3: Wait one second */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 3.  Wait one second\n");
+				ast_debug(1, "TestClient: 3.  Wait one second\n");
 				res = ast_safe_sleep(chan, 1000);
 				fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -224,8 +216,7 @@
 			}			
 			if (!res) {
 				/* Step 4: Measure noise */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 4.  Measure noise\n");
+				ast_debug(1, "TestClient: 4.  Measure noise\n");
 				res = measurenoise(chan, 5000, "TestClient");
 				fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
 				if (res > 0)
@@ -233,8 +224,7 @@
 			}
 			if (!res) {
 				/* Step 5: Wait for "4" */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 5.  Wait DTMF 4\n");
+				ast_debug(1, "TestClient: 5.  Wait DTMF 4\n");
 				res = ast_waitfordigit(chan, 3000);
 				fprintf(f, "WAIT DTMF 4:   %s\n", (res != '4') ? "FAIL" : "PASS");
 				if (res == '4')
@@ -244,15 +234,13 @@
 			}
 			if (!res) {
 				/* Step 6: Transmit tone noise */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 6.  Transmit tone\n");
+				ast_debug(1, "TestClient: 6.  Transmit tone\n");
 				res = sendnoise(chan, 6000);
 				fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
 			}
 			if (!res || (res == '5')) {
 				/* Step 7: Wait for "5" */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 7.  Wait DTMF 5\n");
+				ast_debug(1, "TestClient: 7.  Wait DTMF 5\n");
 				if (!res)
 					res = ast_waitfordigit(chan, 3000);
 				fprintf(f, "WAIT DTMF 5:   %s\n", (res != '5') ? "FAIL" : "PASS");
@@ -263,8 +251,7 @@
 			}
 			if (!res) {
 				/* Step 8: Wait one second */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 8.  Wait one second\n");
+				ast_debug(1, "TestClient: 8.  Wait one second\n");
 				res = ast_safe_sleep(chan, 1000);
 				fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -272,8 +259,7 @@
 			}
 			if (!res) {
 				/* Step 9: Measure noise */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 6.  Measure tone\n");
+				ast_debug(1, "TestClient: 6.  Measure tone\n");
 				res = measurenoise(chan, 4000, "TestClient");
 				fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
 				if (res > 0)
@@ -281,8 +267,7 @@
 			}
 			if (!res) {
 				/* Step 10: Send "7" */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 7.  Send DTMF 7\n");
+				ast_debug(1, "TestClient: 7.  Send DTMF 7\n");
 				res = ast_dtmf_stream(chan, NULL, "7", 0);
 				fprintf(f, "SEND DTMF 7:   %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -290,8 +275,7 @@
 			}
 			if (!res) {
 				/* Step 11: Wait for "8" */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "TestClient: 11.  Wait DTMF 8\n");
+				ast_debug(1, "TestClient: 11.  Wait DTMF 8\n");
 				res = ast_waitfordigit(chan, 3000);
 				fprintf(f, "WAIT DTMF 8:   %s\n", (res != '8') ? "FAIL" : "PASS");
 				if (res == '8')
@@ -301,11 +285,10 @@
 			}
 			if (option_debug && !res ) {
 				/* Step 12: Hangup! */
-				ast_log(LOG_DEBUG, "TestClient: 12.  Hangup\n");
+				ast_debug(1, "TestClient: 12.  Hangup\n");
 			}
 
-			if (option_debug)
-				ast_log(LOG_DEBUG, "-- TEST COMPLETE--\n");
+			ast_debug(1, "-- TEST COMPLETE--\n");
 			fprintf(f, "-- END TEST--\n");
 			fclose(f);
 			res = -1;
@@ -330,16 +313,15 @@
 	if (chan->_state != AST_STATE_UP)
 		res = ast_answer(chan);
 	/* Read version */
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Read client version\n");
+	ast_debug(1, "Read client version\n");
 	if (!res) 
 		res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);
 	if (res > 0)
 		res = 0;
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "client version: %s\n", testid);
-		ast_log(LOG_DEBUG, "Transmit server version\n");
-	}
+
+	ast_debug(1, "client version: %s\n", testid);
+	ast_debug(1, "Transmit server version\n");
+
 	res = ast_safe_sleep(chan, 1000);
 	if (!res)
 		res = ast_dtmf_stream(chan, NULL, "8378*1#", 0);
@@ -348,8 +330,7 @@
 
 	if (!res) 
 		res = ast_app_getdata(chan, NULL, testid, sizeof(testid) - 1, 0);		
-	if (option_debug) 
-		ast_log(LOG_DEBUG, "read test identifier: %s\n", testid);
+	ast_debug(1, "read test identifier: %s\n", testid);
 	/* Check for sneakyness */
 	if (strchr(testid, '/'))
 		res = -1;
@@ -364,13 +345,11 @@
 			fprintf(f, "SERVERCHAN:    %s\n", chan->name);
 			fprintf(f, "SERVERTEST ID: %s\n", testid);
 			fprintf(f, "ANSWER:        PASS\n");
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Processing Test ID '%s'\n", testid);
+			ast_debug(1, "Processing Test ID '%s'\n", testid);
 			res = ast_safe_sleep(chan, 1000);
 			if (!res) {
 				/* Step 1: Send "1" */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 1.  Send DTMF 1\n");
+				ast_debug(1, "TestServer: 1.  Send DTMF 1\n");
 				res = ast_dtmf_stream(chan, NULL, "1", 0);
 				fprintf(f, "SEND DTMF 1:   %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -378,8 +357,7 @@
 			}
 			if (!res) {
 				/* Step 2: Wait for "2" */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 2.  Wait DTMF 2\n");
+				ast_debug(1, "TestServer: 2.  Wait DTMF 2\n");
 				res = ast_waitfordigit(chan, 3000);
 				fprintf(f, "WAIT DTMF 2:   %s\n", (res != '2') ? "FAIL" : "PASS");
 				if (res == '2')
@@ -389,8 +367,7 @@
 			}
 			if (!res) {
 				/* Step 3: Measure noise */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 3.  Measure noise\n");
+				ast_debug(1, "TestServer: 3.  Measure noise\n");
 				res = measurenoise(chan, 6000, "TestServer");
 				fprintf(f, "MEASURENOISE:  %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
 				if (res > 0)
@@ -398,8 +375,7 @@
 			}
 			if (!res) {
 				/* Step 4: Send "4" */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 4.  Send DTMF 4\n");
+				ast_debug(1, "TestServer: 4.  Send DTMF 4\n");
 				res = ast_dtmf_stream(chan, NULL, "4", 0);
 				fprintf(f, "SEND DTMF 4:   %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -408,8 +384,7 @@
 		
 			if (!res) {
 				/* Step 5: Wait one second */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 5.  Wait one second\n");
+				ast_debug(1, "TestServer: 5.  Wait one second\n");
 				res = ast_safe_sleep(chan, 1000);
 				fprintf(f, "WAIT 1 SEC:    %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -418,8 +393,7 @@
 		
 			if (!res) {
 				/* Step 6: Measure noise */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 6.  Measure tone\n");
+				ast_debug(1, "TestServer: 6.  Measure tone\n");
 				res = measurenoise(chan, 4000, "TestServer");
 				fprintf(f, "MEASURETONE:   %s (%d)\n", (res < 0) ? "FAIL" : "PASS", res);
 				if (res > 0)
@@ -428,8 +402,7 @@
 
 			if (!res) {
 				/* Step 7: Send "5" */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 7.  Send DTMF 5\n");
+				ast_debug(1, "TestServer: 7.  Send DTMF 5\n");
 				res = ast_dtmf_stream(chan, NULL, "5", 0);
 				fprintf(f, "SEND DTMF 5:   %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -438,16 +411,14 @@
 
 			if (!res) {
 				/* Step 8: Transmit tone noise */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 8.  Transmit tone\n");
+				ast_debug(1, "TestServer: 8.  Transmit tone\n");
 				res = sendnoise(chan, 6000);
 				fprintf(f, "SENDTONE:      %s\n", (res < 0) ? "FAIL" : "PASS");
 			}
 		
 			if (!res || (res == '7')) {
 				/* Step 9: Wait for "7" */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 9.  Wait DTMF 7\n");
+				ast_debug(1, "TestServer: 9.  Wait DTMF 7\n");
 				if (!res)
 					res = ast_waitfordigit(chan, 3000);
 				fprintf(f, "WAIT DTMF 7:   %s\n", (res != '7') ? "FAIL" : "PASS");
@@ -460,8 +431,7 @@
 				res = ast_safe_sleep(chan, 1000);
 			if (!res) {
 				/* Step 10: Send "8" */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 10.  Send DTMF 8\n");
+				ast_debug(1, "TestServer: 10.  Send DTMF 8\n");
 				res = ast_dtmf_stream(chan, NULL, "8", 0);
 				fprintf(f, "SEND DTMF 8:   %s\n", (res < 0) ? "FAIL" : "PASS");
 				if (res > 0)
@@ -469,8 +439,7 @@
 			}
 			if (!res) {
 				/* Step 11: Wait for hangup to arrive! */
-				if (option_debug) 
-					ast_log(LOG_DEBUG, "TestServer: 11.  Waiting for hangup\n");
+				ast_debug(1, "TestServer: 11.  Waiting for hangup\n");
 				res = ast_safe_sleep(chan, 10000);
 				fprintf(f, "WAIT HANGUP:   %s\n", (res < 0) ? "PASS" : "FAIL");
 			}
Index: app_ices.c
===================================================================
--- app_ices.c	(revision 69052)
+++ app_ices.c	(working copy)
@@ -170,15 +170,13 @@
 			/* Wait for audio, and stream */
 			ms = ast_waitfor(chan, -1);
 			if (ms < 0) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Hangup detected\n");
+				ast_debug(1, "Hangup detected\n");
 				res = -1;
 				break;
 			}
 			f = ast_read(chan);
 			if (!f) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
+				ast_debug(1, "Null frame == hangup() detected\n");
 				res = -1;
 				break;
 			}
Index: app_channelredirect.c
===================================================================
--- app_channelredirect.c	(revision 69052)
+++ app_channelredirect.c	(working copy)
@@ -105,8 +105,7 @@
 		goto chanquit;
 	}
 
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "Attempting async goto (%s) to %s|%s|%d\n", args.channel, S_OR(context, chan2->context), S_OR(exten, chan2->exten), prio);
+	ast_debug(2, "Attempting async goto (%s) to %s|%s|%d\n", args.channel, S_OR(context, chan2->context), S_OR(exten, chan2->exten), prio);
 
 	if (ast_async_goto_if_exists(chan2, S_OR(context, chan2->context), S_OR(exten, chan2->exten), prio))
 		ast_log(LOG_WARNING, "%s failed for %s\n", app, args.channel);
Index: app_directed_pickup.c
===================================================================
--- app_directed_pickup.c	(revision 69052)
+++ app_directed_pickup.c	(working copy)
@@ -59,8 +59,7 @@
 {
 	int res = 0;
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
+	ast_debug(1, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
 
 	if ((res = ast_answer(chan))) {
 		ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
Index: app_minivm.c
===================================================================
--- app_minivm.c	(revision 69052)
+++ app_minivm.c	(working copy)
@@ -506,8 +506,7 @@
 	}
 
 	while (var) {
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "-_-_- Configuring template option %s = \"%s\" for template %s\n", var->name, var->value, name);
+		ast_debug(3, "-_-_- Configuring template option %s = \"%s\" for template %s\n", var->name, var->value, name);
 		if (!strcasecmp(var->name, "fromaddress")) {
 			ast_copy_string(template->fromaddress, var->value, sizeof(template->fromaddress));
 		} else if (!strcasecmp(var->name, "fromemail")) {
@@ -823,8 +822,7 @@
 		ast_log(LOG_NOTICE, "No username or domain? \n");
 		return NULL;
 	}
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "-_-_-_- Looking for voicemail user %s in domain %s\n", username, domain);
+	ast_debug(3, "-_-_-_- Looking for voicemail user %s in domain %s\n", username, domain);
 
 	AST_LIST_LOCK(&minivm_accounts);
 	AST_LIST_TRAVERSE(&minivm_accounts, cur, list) {
@@ -835,8 +833,7 @@
 	AST_LIST_UNLOCK(&minivm_accounts);
 
 	if (cur) {
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "-_-_- Found account for %s@%s\n", username, domain);
+		ast_debug(3, "-_-_- Found account for %s@%s\n", username, domain);
 		vmu = cur;
 
 	} else
@@ -849,8 +846,7 @@
 		if (vmu) {
 			ast_copy_string(vmu->username, username, sizeof(vmu->username));
 			ast_copy_string(vmu->domain, domain, sizeof(vmu->domain));
-			if (option_debug)
-				ast_log(LOG_DEBUG, "--- Created temporary account\n");
+			ast_debug(1, "--- Created temporary account\n");
 		}
 
 	}
@@ -926,8 +922,7 @@
 		return -1;	
 	}
 
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "-_-_- Sending mail to %s@%s - Using template %s\n", vmu->username, vmu->domain, template->name);
+	ast_debug(3, "-_-_- Sending mail to %s@%s - Using template %s\n", vmu->username, vmu->domain, template->name);
 
 	if (!strcmp(format, "wav49"))
 		format = "WAV";
@@ -940,8 +935,7 @@
 		int tmpfd;
 
 		snprintf(newtmp, sizeof(newtmp), "/tmp/XXXXXX");
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "newtmp: %s\n", newtmp);
+		ast_debug(3, "newtmp: %s\n", newtmp);
 		tmpfd = mkstemp(newtmp);
 		snprintf(tmpcmd, sizeof(tmpcmd), "sox -v %.4f %s.%s %s.%s", vmu->volgain, filename, format, newtmp, format);
 		ast_safe_system(tmpcmd);
@@ -955,8 +949,8 @@
 	/* Create file name */
 	snprintf(fname, sizeof(fname), "%s.%s", finalfilename, format);
 
-	if (option_debug && template->attachment)
-		ast_log(LOG_DEBUG, "-- Attaching file '%s', format '%s', uservm is '%d'\n", finalfilename, format, attach_user_voicemail);
+	if (template->attachment)
+		ast_debug(1, "-- Attaching file '%s', format '%s', uservm is '%d'\n", finalfilename, format, attach_user_voicemail);
 
 	/* Make a temporary file instead of piping directly to sendmail, in case the mail
 	   command hangs */
@@ -967,8 +961,7 @@
 			close(pfd);
 			pfd = -1;
 		}
-		if (option_debug)
-			ast_log(LOG_DEBUG, "-_-_- Opening temp file for e-mail: %s\n", tmp);
+		ast_debug(1, "-_-_- Opening temp file for e-mail: %s\n", tmp);
 	}
 	if (!p) {
 		ast_log(LOG_WARNING, "Unable to open temporary file '%s'\n", tmp);
@@ -1029,8 +1022,7 @@
 	if (ast_strlen_zero(fromaddress)) {
 		fprintf(p, "From: Asterisk PBX <%s>\n", who);
 	} else {
-		if (option_debug > 3)
-			ast_log(LOG_DEBUG, "-_-_- Fromaddress template: %s\n", fromaddress);
+		ast_debug(4, "-_-_- Fromaddress template: %s\n", fromaddress);
 		/* Allocate a buffer big enough for variable substitution */
 		int vmlen = strlen(fromaddress) * 3 + 200;
 
@@ -1046,8 +1038,7 @@
 			return -1;	
 		}
 	} 
-	if (option_debug > 3)
-		ast_log(LOG_DEBUG, "-_-_- Fromstring now: %s\n", ast_strlen_zero(passdata) ? "-default-" : passdata);
+	ast_debug(4, "-_-_- Fromstring now: %s\n", ast_strlen_zero(passdata) ? "-default-" : passdata);
 
 	fprintf(p, "Message-ID: <Asterisk-%d-%s-%d-%s>\n", (unsigned int)rand(), vmu->username, getpid(), who);
 	len_passdata = strlen(vmu->fullname) * 2 + 3;
@@ -1069,11 +1060,12 @@
 			fclose(p);
 			return -1;	
 		}
-		if (option_debug > 3)
-			ast_log(LOG_DEBUG, "-_-_- Subject now: %s\n", passdata);
+
+		ast_debug(4, "-_-_- Subject now: %s\n", passdata);
+
 	} else  {
 		fprintf(p, "Subject: New message in mailbox %s@%s\n", vmu->username, vmu->domain);
-		ast_log(LOG_DEBUG, "-_-_- Using default subject for this email \n");
+		ast_debug(1, "-_-_- Using default subject for this email \n");
 	}
 
 
@@ -1094,8 +1086,7 @@
 		if ((passdata = alloca(vmlen))) {
 			memset(passdata, 0, vmlen);
 			pbx_substitute_variables_helper(ast, template->body, passdata, vmlen);
-			if (option_debug > 2)
-				ast_log(LOG_DEBUG, "Message now: %s\n-----\n", passdata);
+			ast_debug(3, "Message now: %s\n-----\n", passdata);
 			fprintf(p, "%s\n", passdata);
 		} else
 			ast_log(LOG_WARNING, "Cannot allocate workspace for variable substitution\n");
@@ -1105,13 +1096,11 @@
 			"in mailbox %s from %s, on %s so you might\n"
 			"want to check it when you get a chance.  Thanks!\n\n\t\t\t\t--Asterisk\n\n", vmu->fullname, 
 			dur,  vmu->username, (cidname ? cidname : (cidnum ? cidnum : "an unknown caller")), date);
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "Using default message body (no template)\n-----\n");
+		ast_debug(3, "Using default message body (no template)\n-----\n");
 	}
 	/* Eww. We want formats to tell us their own MIME type */
 	if (template->attachment) {
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "-_-_- Attaching file to message: %s\n", fname);
+		ast_debug(3, "-_-_- Attaching file to message: %s\n", fname);
 		char *ctype = "audio/x-";
 		if (!strcasecmp(format, "ogg"))
 			ctype = "application/";
@@ -1128,11 +1117,8 @@
 	fclose(p);
 	snprintf(tmp2, sizeof(tmp2), "( %s < %s ; rm -f %s ) &", global_mailcmd, tmp, tmp);
 	ast_safe_system(tmp2);
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "Sent message to %s with command '%s' - %s\n", vmu->email, global_mailcmd, template->attachment ? "(media attachment)" : "");
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "-_-_- Actual command used: %s\n", tmp2);
-	}
+	ast_debug(1, "Sent message to %s with command '%s' - %s\n", vmu->email, global_mailcmd, template->attachment ? "(media attachment)" : "");
+	ast_debug(3, "-_-_- Actual command used: %s\n", tmp2);
 	if (ast)
 		ast_channel_free(ast);
 	return 0;
@@ -1195,8 +1181,7 @@
 			return -1;
 		}
 	}
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "Creating directory for %s@%s folder %s : %s\n", username, domain, folder, dest);
+	ast_debug(2, "Creating directory for %s@%s folder %s : %s\n", username, domain, folder, dest);
 	return 0;
 }
 
@@ -1208,8 +1193,7 @@
 	int res;
 	char fn[PATH_MAX];
 
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "-_-_- Still preparing to play message ...\n");
+	ast_debug(2, "-_-_- Still preparing to play message ...\n");
 
 	snprintf(fn, sizeof(fn), "%s%s/%s/greet", MVM_SPOOL_DIR, domain, username);
 
@@ -1224,12 +1208,10 @@
 		int numericusername = 1;
 		char *i = username;
 
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "-_-_- No personal prompts. Using default prompt set for language\n");
+		ast_debug(2, "-_-_- No personal prompts. Using default prompt set for language\n");
 		
 		while (*i)  {
-			if (option_debug > 1)
-				ast_log(LOG_DEBUG, "-_-_- Numeric? Checking %c\n", *i);
+			ast_debug(2, "-_-_- Numeric? Checking %c\n", *i);
 			if (!isdigit(*i)) {
 				numericusername = FALSE;
 				break;
@@ -1266,8 +1248,7 @@
 {
 	int res;
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "-_-_- Deleting voicemail file %s\n", file);
+	ast_debug(1, "-_-_- Deleting voicemail file %s\n", file);
 
 	res = unlink(file);	/* Remove the meta data file */
 	res |=  ast_filedelete(file, NULL);	/* remove the media file */
@@ -1418,9 +1399,7 @@
 		vmu->username, vmu->domain,
 		chan->cid.cid_name, chan->cid.cid_num);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Executing: %s\n", arguments);
-
+	ast_debug(1, "Executing: %s\n", arguments);
   	ast_safe_system(arguments);
 }
 
@@ -1454,8 +1433,7 @@
 	if (!ast_strlen_zero(etemplate->locale)) {
 		char *newlocale;
 		ast_copy_string(oldlocale, setlocale(LC_TIME, NULL), sizeof(oldlocale));
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "-_-_- Changing locale from %s to %s\n", oldlocale, etemplate->locale);
+		ast_debug(2, "-_-_- Changing locale from %s to %s\n", oldlocale, etemplate->locale);
 		newlocale = setlocale(LC_TIME, etemplate->locale);
 		if (newlocale == NULL) {
 			ast_log(LOG_WARNING, "-_-_- Changing to new locale did not work. Locale: %s\n", etemplate->locale);
@@ -1466,11 +1444,10 @@
 
 	/* Read counter if available */
 	counter = pbx_builtin_getvar_helper(chan, "MVM_COUNTER");
-	if (option_debug > 1) {
-		if (ast_strlen_zero(counter))
-			ast_log(LOG_DEBUG, "-_-_- MVM_COUNTER not found\n");
-		else
-			ast_log(LOG_DEBUG, "-_-_- MVM_COUNTER found - will use it with value %s\n", counter);
+	if (ast_strlen_zero(counter)) {
+		ast_debug(2, "-_-_- MVM_COUNTER not found\n");
+	} else {
+		ast_debug(2, "-_-_- MVM_COUNTER found - will use it with value %s\n", counter);
 	}
 
 	res = sendmail(etemplate, vmu, cidnum, cidname, filename, messageformat, duration, etemplate->attachment, MVM_MESSAGE_EMAIL, counter);
@@ -1555,8 +1532,7 @@
 	/* If we have no user directory, use generic temporary directory */
 	if (!userdir) {
 		create_dirpath(tmpdir, sizeof(tmpdir), "0000_minivm_temp", "mediafiles", "");
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "Creating temporary directory %s\n", tmpdir);
+		ast_debug(3, "Creating temporary directory %s\n", tmpdir);
 	}
 
 
@@ -1583,8 +1559,7 @@
 
 	/* OEJ XXX Maybe this can be turned into a log file? Hmm. */
 	/* Store information */
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "Open file for metadata: %s\n", tmptxtfile);
+	ast_debug(2, "Open file for metadata: %s\n", tmptxtfile);
 
 	res = play_record_review(chan, NULL, tmptxtfile, global_vmmaxmessage, fmt, 1, vmu, &duration, NULL, options->record_gain);
 
@@ -1635,8 +1610,7 @@
 		} 
 		fclose(txt); /* Close log file */
 		if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) {
-			if (option_debug) 
-				ast_log(LOG_DEBUG, "The recorded media file is gone, so we should remove the .txt file too!\n");
+			ast_debug(1, "The recorded media file is gone, so we should remove the .txt file too!\n");
 			unlink(tmptxtfile);
 			pbx_builtin_setvar_helper(chan, "MINIVM_RECORD_STATUS", "FAILED");
 			if(ast_test_flag(vmu, MVM_ALLOCED))
@@ -1656,8 +1630,8 @@
 //	/* Go ahead and delete audio files from system, they're not needed any more */
 //	if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) {
 //		ast_filedelete(tmptxtfile, NULL);
-//		if (option_debug > 1)
-//			ast_log(LOG_DEBUG, "-_-_- Deleted audio file after notification :: %s \n", tmptxtfile);
+//		 /* Even not being used at the moment, it's better to convert ast_log to ast_debug anyway */
+//		ast_debug(2, "-_-_- Deleted audio file after notification :: %s \n", tmptxtfile);
 //	}
 
 	if (res > 0)
@@ -1873,8 +1847,7 @@
 		ast_module_user_remove(u);
 		return -1;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "-_-_- Trying to find configuration for user %s in domain %s\n", username, domain);
+	ast_debug(1, "-_-_- Trying to find configuration for user %s in domain %s\n", username, domain);
 
 	if (!(vmu = find_account(domain, username, TRUE))) {
 		ast_log(LOG_ERROR, "Could not allocate memory. \n");
@@ -1904,12 +1877,10 @@
 	/* Check for temporary greeting - it overrides busy and unavail */
 	snprintf(tempfile, sizeof(tempfile), "%s%s/%s/temp", MVM_SPOOL_DIR, vmu->domain, username);
 	if (!(res = check_dirpath(dest, sizeof(dest), vmu->domain, username, "temp"))) {
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "Temporary message directory does not exist, using default (%s)\n", tempfile);
+		ast_debug(2, "Temporary message directory does not exist, using default (%s)\n", tempfile);
 		ast_copy_string(prefile, tempfile, sizeof(prefile));
 	}
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "-_-_- Preparing to play message ...\n");
+	ast_debug(2, "-_-_- Preparing to play message ...\n");
 
 	/* Check current or macro-calling context for special extensions */
 	if (ast_test_flag(vmu, MVM_OPERATOR)) {
@@ -1944,13 +1915,11 @@
 		if (ast_streamfile(chan, prefile, chan->language) > -1) 
 			res = ast_waitstream(chan, ecodes);
 	} else {
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "%s doesn't exist, doing what we can\n", prefile);
+		ast_debug(2, "%s doesn't exist, doing what we can\n", prefile);
 		res = invent_message(chan, vmu->domain, username, ast_test_flag(&leave_options, OPT_BUSY_GREETING), ecodes);
 	}
 	if (res < 0) {
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "Hang up during prefile playback\n");
+		ast_debug(2, "Hang up during prefile playback\n");
 		pbx_builtin_setvar_helper(chan, "MINIVM_GREET_STATUS", "FAILED");
 		if(ast_test_flag(vmu, MVM_ALLOCED))
 			free_user(vmu);
@@ -2042,17 +2011,14 @@
 	if (ast_fileexists(filename, NULL, NULL) > 0) {
 		res = vm_delete(filename);
 		if (res) {
-			if (option_debug > 1)
-				ast_log(LOG_DEBUG, "-_-_- Can't delete file: %s\n", filename);
+			ast_debug(2, "-_-_- Can't delete file: %s\n", filename);
 			pbx_builtin_setvar_helper(chan, "MINIVM_DELETE_STATUS", "FAILED");
 		} else {
-			if (option_debug > 1)
-				ast_log(LOG_DEBUG, "-_-_- Deleted voicemail file :: %s \n", filename);
+			ast_debug(2, "-_-_- Deleted voicemail file :: %s \n", filename);
 			pbx_builtin_setvar_helper(chan, "MINIVM_DELETE_STATUS", "SUCCESS");
 		}
 	} else {
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "-_-_- Filename does not exist: %s\n", filename);
+		ast_debug(2, "-_-_- Filename does not exist: %s\n", filename);
 		pbx_builtin_setvar_helper(chan, "MINIVM_DELETE_STATUS", "FAILED");
 	}
 	
@@ -2159,8 +2125,7 @@
 	/* Maybe we should check the result of play_record_review ? */
 	cmd = play_record_review(chan, prompt, filename, global_maxgreet, default_vmformat, 0, vmu, &duration, NULL, FALSE);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Recorded new %s message in %s (duration %d)\n", message, filename, duration);
+	ast_debug(1, "Recorded new %s message in %s (duration %d)\n", message, filename, duration);
 
 	if(ast_test_flag(vmu, MVM_ALLOCED))
 		free_user(vmu);
@@ -2181,8 +2146,7 @@
 	char *username;
 	char accbuf[BUFSIZ];
 
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "Creating %s account for [%s]\n", realtime ? "realtime" : "static", name);
+	ast_debug(3, "Creating %s account for [%s]\n", realtime ? "realtime" : "static", name);
 
 	ast_copy_string(accbuf, name, sizeof(accbuf));
 	username = accbuf;
@@ -2196,8 +2160,7 @@
 		return 0;
 	}
 
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "Creating static account for user %s domain %s\n", username, domain);
+	ast_debug(3, "Creating static account for user %s domain %s\n", username, domain);
 
 	/* Allocate user account */
 	vmu = ast_calloc(1, sizeof(*vmu));
@@ -2209,12 +2172,10 @@
 
 	populate_defaults(vmu);
 
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "...Configuring account %s\n", name);
+	ast_debug(3, "...Configuring account %s\n", name);
 
 	while (var) {
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "---- Configuring %s = \"%s\" for account %s\n", var->name, var->value, name);
+		ast_debug(3, "---- Configuring %s = \"%s\" for account %s\n", var->name, var->value, name);
 		if (!strcasecmp(var->name, "serveremail")) {
 			ast_copy_string(vmu->serveremail, var->value, sizeof(vmu->serveremail));
 		} else if (!strcasecmp(var->name, "email")) {
@@ -2259,8 +2220,7 @@
 		}
 		var = var->next;
 	}
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "...Linking account %s\n", name);
+	ast_debug(3, "...Linking account %s\n", name);
 	
 	AST_LIST_LOCK(&minivm_accounts);
 	AST_LIST_INSERT_TAIL(&minivm_accounts, vmu, list);
@@ -2268,8 +2228,7 @@
 
 	global_stats.voicemailaccounts++;
 
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "MINIVM :: Created account %s@%s - tz %s etemplate %s %s\n", username, domain, ast_strlen_zero(vmu->zonetag) ? "" : vmu->zonetag, ast_strlen_zero(vmu->etemplate) ? "" : vmu->etemplate, realtime ? "(realtime)" : "");
+	ast_debug(2, "MINIVM :: Created account %s@%s - tz %s etemplate %s %s\n", username, domain, ast_strlen_zero(vmu->zonetag) ? "" : vmu->zonetag, ast_strlen_zero(vmu->etemplate) ? "" : vmu->etemplate, realtime ? "(realtime)" : "");
 	return 0;
 }
 
@@ -2363,10 +2322,8 @@
 	fclose(fi);
 	messagebody = ast_calloc(1, strlen(buf + 1));
 	ast_copy_string(messagebody, buf, strlen(buf) + 1);
-	if (option_debug > 3) {
-		ast_log(LOG_DEBUG, "---> Size of allocation %d\n", (int) strlen(buf + 1) );
-		ast_log(LOG_DEBUG, "---> Done reading message template : \n%s\n---- END message template--- \n", messagebody);
-	}
+	ast_debug(4, "---> Size of allocation %d\n", (int) strlen(buf + 1) );
+	ast_debug(4, "---> Done reading message template : \n%s\n---- END message template--- \n", messagebody);
 
 	return messagebody;
 }
@@ -2472,8 +2429,7 @@
 	message_destroy_list();		/* Destroy list of voicemail message templates */
 	timezone_destroy_list();	/* Destroy list of timezones */
 	vmaccounts_destroy_list();	/* Destroy list of voicemail accounts */
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "Destroyed memory objects...\n");
+	ast_debug(2, "Destroyed memory objects...\n");
 
 	/* First, set some default settings */
 	global_externnotify[0] = '\0';
@@ -2501,15 +2457,13 @@
 		return 0;
 	}
 
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "-_-_- Loaded configuration file, now parsing\n");
+	ast_debug(2, "-_-_- Loaded configuration file, now parsing\n");
 
 	/* General settings */
 
 	cat = ast_category_browse(cfg, NULL);
 	while (cat) {
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "-_-_- Found configuration section [%s]\n", cat);
+		ast_debug(3, "-_-_- Found configuration section [%s]\n", cat);
 		if (!strcasecmp(cat, "general")) {
 			/* Nothing right now */
 			error += apply_general_options(ast_variable_browse(cfg, cat));
@@ -2584,8 +2538,8 @@
 		minivmlogfile = fopen(global_logfile, "a");
 		if(!minivmlogfile)
 			ast_log(LOG_ERROR, "Failed to open minivm log file %s : %s\n", global_logfile, strerror(errno));
-		if (option_debug > 2 && minivmlogfile)
-			ast_log(LOG_DEBUG, "-_-_- Opened log file %s \n", global_logfile);
+		if (minivmlogfile)
+			ast_debug(3, "-_-_- Opened log file %s \n", global_logfile);
 	}
 
 	return 0;
@@ -2901,8 +2855,7 @@
 		counterfile = fopen(filename, "r");
 		if (counterfile) {
 			if(fgets(readbuf, sizeof(readbuf), counterfile)) {
-				if (option_debug > 2)
-					ast_log(LOG_DEBUG, "Read this string from counter file: %s\n", readbuf);
+				ast_debug(3, "Read this string from counter file: %s\n", readbuf);
 				old = counter = atoi(readbuf);
 			}
 			fclose(counterfile);
@@ -2911,8 +2864,7 @@
 	switch (operand) {
 	case 0:	/* Read only */
 		ast_unlock_path(directory);
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "MINIVM Counter %s/%s: Value %d\n", directory, countername, counter);
+		ast_debug(2, "MINIVM Counter %s/%s: Value %d\n", directory, countername, counter);
 		return counter;
 		break;
 	case 1: /* Set new value */
@@ -2935,8 +2887,7 @@
 	fprintf(counterfile, "%d\n\n", counter);
 	fclose(counterfile);
 	ast_unlock_path(directory);
-	if (option_debug > 1)
-		ast_log(LOG_DEBUG, "MINIVM Counter %s/%s: Old value %d New value %d\n", directory, countername, old, counter);
+	ast_debug(2, "MINIVM Counter %s/%s: Old value %d New value %d\n", directory, countername, old, counter);
 	return counter;
 }
 
Index: app_macro.c
===================================================================
--- app_macro.c	(revision 69052)
+++ app_macro.c	(working copy)
@@ -220,8 +220,7 @@
 
 	/* If we are to run the macro exclusively, take the mutex */
 	if (exclusive) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Locking macrolock for '%s'\n", fullmacro);
+		ast_debug(1, "Locking macrolock for '%s'\n", fullmacro);
 		ast_autoservice_start(chan);
 		if (ast_context_lockmacro(fullmacro)) {
 			ast_log(LOG_WARNING, "Failed to lock macro '%s' as in-use\n", fullmacro);
@@ -313,8 +312,7 @@
 			if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
 		    	(res == '*') || (res == '#')) {
 				/* Just return result as to the previous application as if it had been dialed */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
+				ast_debug(1, "Oooh, got something to jump out with ('%c')!\n", res);
 				break;
 			}
 			switch(res) {
@@ -322,27 +320,25 @@
 				res = 0;
 				goto out;
 			case AST_PBX_KEEPALIVE:
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited KEEPALIVE in macro %s on '%s'\n", chan->context, chan->exten, chan->priority, macro, chan->name);
-				else if (option_verbose > 1)
+				ast_debug(2, "Spawn extension (%s,%s,%d) exited KEEPALIVE in macro %s on '%s'\n", chan->context, chan->exten, chan->priority, macro, chan->name);
+				if (option_verbose > 1)
 					ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited KEEPALIVE in macro '%s' on '%s'\n", chan->context, chan->exten, chan->priority, macro, chan->name);
 				goto out;
 				break;
 			default:
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s' in macro '%s'\n", chan->context, chan->exten, chan->priority, chan->name, macro);
-				else if (option_verbose > 1)
+				ast_debug(2, "Spawn extension (%s,%s,%d) exited non-zero on '%s' in macro '%s'\n", chan->context, chan->exten, chan->priority, chan->name, macro);
+				if (option_verbose > 1)
 					ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s' in macro '%s'\n", chan->context, chan->exten, chan->priority, chan->name, macro);
 				dead = 1;
 				goto out;
 			}
 		}
 
-		ast_log(LOG_DEBUG, "Executed application: %s\n", runningapp);
+		ast_debug(1, "Executed application: %s\n", runningapp);
 
 		if (!strcasecmp(runningapp, "GOSUB")) {
 			gosub_level++;
-			ast_log(LOG_DEBUG, "Incrementing gosub_level\n");
+			ast_debug(1, "Incrementing gosub_level\n");
 		} else if (!strcasecmp(runningapp, "GOSUBIF")) {
 			char tmp2[1024] = "", *cond, *app, *app2 = tmp2;
 			pbx_substitute_variables_helper(chan, runningdata, tmp2, sizeof(tmp2) - 1);
@@ -351,20 +347,20 @@
 			if (pbx_checkcondition(cond)) {
 				if (!ast_strlen_zero(app)) {
 					gosub_level++;
-					ast_log(LOG_DEBUG, "Incrementing gosub_level\n");
+					ast_debug(1, "Incrementing gosub_level\n");
 				}
 			} else {
 				if (!ast_strlen_zero(app2)) {
 					gosub_level++;
-					ast_log(LOG_DEBUG, "Incrementing gosub_level\n");
+					ast_debug(1, "Incrementing gosub_level\n");
 				}
 			}
 		} else if (!strcasecmp(runningapp, "RETURN")) {
 			gosub_level--;
-			ast_log(LOG_DEBUG, "Decrementing gosub_level\n");
+			ast_debug(1, "Decrementing gosub_level\n");
 		} else if (!strcasecmp(runningapp, "STACKPOP")) {
 			gosub_level--;
-			ast_log(LOG_DEBUG, "Decrementing gosub_level\n");
+			ast_debug(1, "Decrementing gosub_level\n");
 		} else if (!strncasecmp(runningapp, "EXEC", 4)) {
 			/* Must evaluate args to find actual app */
 			char tmp2[1024] = "", *tmp3 = NULL;
@@ -379,17 +375,17 @@
 				tmp3 = tmp2;
 
 			if (tmp3)
-				ast_log(LOG_DEBUG, "Last app: %s\n", tmp3);
+				ast_debug(1, "Last app: %s\n", tmp3);
 
 			if (tmp3 && !strncasecmp(tmp3, "GOSUB", 5)) {
 				gosub_level++;
-				ast_log(LOG_DEBUG, "Incrementing gosub_level\n");
+				ast_debug(1, "Incrementing gosub_level\n");
 			} else if (tmp3 && !strncasecmp(tmp3, "RETURN", 6)) {
 				gosub_level--;
-				ast_log(LOG_DEBUG, "Decrementing gosub_level\n");
+				ast_debug(1, "Decrementing gosub_level\n");
 			} else if (tmp3 && !strncasecmp(tmp3, "STACKPOP", 8)) {
 				gosub_level--;
-				ast_log(LOG_DEBUG, "Decrementing gosub_level\n");
+				ast_debug(1, "Decrementing gosub_level\n");
 			}
 		}
 
@@ -401,9 +397,7 @@
 
 		/* don't stop executing extensions when we're in "h" */
 		if (chan->_softhangup && !inhangup) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Extension %s, macroexten %s, priority %d returned normally even though call was hung up\n",
-					chan->exten, chan->macroexten, chan->priority);
+			ast_debug(1, "Extension %s, macroexten %s, priority %d returned normally even though call was hung up\n", chan->exten, chan->macroexten, chan->priority);
 			goto out;
 		}
 		chan->priority++;
@@ -474,8 +468,7 @@
 
 	/* Unlock the macro */
 	if (exclusive) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Unlocking macrolock for '%s'\n", fullmacro);
+		ast_debug(1, "Unlocking macrolock for '%s'\n", fullmacro);
 		if (ast_context_unlockmacro(fullmacro)) {
 			ast_log(LOG_ERROR, "Failed to unlock macro '%s' - that isn't good\n", fullmacro);
 			res = 0;
Index: app_voicemail.c
===================================================================
--- app_voicemail.c	(revision 69052)
+++ app_voicemail.c	(working copy)
@@ -885,22 +885,18 @@
 	/* check users.conf and update the password stored for the mailbox*/
 	/* if no vmsecret entry exists create one. */
 	if ((cfg = ast_config_load_with_comments("users.conf"))) {
-		if (option_debug > 3)
-			ast_log(LOG_DEBUG, "we are looking for %s\n", vmu->mailbox);
+		ast_debug(4, "we are looking for %s\n", vmu->mailbox);
 		while ((category = ast_category_browse(cfg, category))) {
-			if (option_debug > 3)
-				ast_log(LOG_DEBUG, "users.conf: %s\n", category);
+			ast_debug(4, "users.conf: %s\n", category);
 			if (!strcasecmp(category, vmu->mailbox)) {
 				if (!(tmp = ast_variable_retrieve(cfg, category, "vmsecret"))) {
-					if (option_debug > 3)
-						ast_log(LOG_DEBUG, "looks like we need to make vmsecret!\n");
+					ast_debug(3, "looks like we need to make vmsecret!\n");
 					var = ast_variable_new("vmsecret", newpassword);
 				} 
 				new = alloca(strlen(newpassword)+1);
 				sprintf(new, "%s", newpassword);
 				if (!(cat = ast_category_get(cfg, category))) {
-					if (option_debug > 3)
-						ast_log(LOG_DEBUG, "failed to get category!\n");
+					ast_debug(4, "failed to get category!\n");
 					break;
 				}
 				if (!var)		
@@ -953,8 +949,7 @@
 		ast_log(LOG_WARNING, "msgnum %d, mailbox message %lu is zero.\n",msgnum,messageNum);
 		return;
 	}
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum);
+	ast_debug(3, "deleting msgnum %d, which is mailbox message %lu\n",msgnum,messageNum);
 	/* delete message */
 	sprintf (arg,"%lu",messageNum);
 	mail_setflag (vms->mailstream,arg,"\\DELETED");
@@ -2094,14 +2089,12 @@
 		create_dirpath(tmpdir, sizeof(tmpdir), vmu->context, vmu->mailbox, "tmp");
 		snprintf(newtmp, sizeof(newtmp), "%s/XXXXXX", tmpdir);
 		tmpfd = mkstemp(newtmp);
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "newtmp: %s\n", newtmp);
+		ast_debug(3, "newtmp: %s\n", newtmp);
 		if (vmu->volgain < -.001 || vmu->volgain > .001) {
 			snprintf(tmpcmd, sizeof(tmpcmd), "sox -v %.4f %s.%s %s.%s", vmu->volgain, attach, format, newtmp, format);
 			ast_safe_system(tmpcmd);
 			attach = newtmp;
-			if (option_debug > 2)
-				ast_log(LOG_DEBUG, "VOLGAIN: Stored at: %s.%s - Level: %.4f - Mailbox: %s\n", attach, format, vmu->volgain, mailbox);
+			ast_debug(3, "VOLGAIN: Stored at: %s.%s - Level: %.4f - Mailbox: %s\n", attach, format, vmu->volgain, mailbox);
 		}
 		fprintf(p, "--%s" ENDL, bound);
 		fprintf(p, "Content-Type: %s%s; name=\"msg%04d.%s\"" ENDL, ctype, format, msgnum + 1, format);
@@ -2130,8 +2123,7 @@
 	}
 	if (!strcmp(format, "wav49"))
 		format = "WAV";
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "Attaching file '%s', format '%s', uservm is '%d', global is %d\n", attach, format, attach_user_voicemail, ast_test_flag((&globalflags), VM_ATTACH));
+	ast_debug(3, "Attaching file '%s', format '%s', uservm is '%d', global is %d\n", attach, format, attach_user_voicemail, ast_test_flag((&globalflags), VM_ATTACH));
 	/* Make a temporary file instead of piping directly to sendmail, in case the mail
 	   command hangs */
 	if ((p = vm_mkftemp(tmp)) == NULL) {
@@ -2142,8 +2134,7 @@
 		fclose(p);
 		snprintf(tmp2, sizeof(tmp2), "( %s < %s ; rm -f %s ) &", mailcmd, tmp, tmp);
 		ast_safe_system(tmp2);
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Sent mail to %s with command '%s'\n", vmu->email, mailcmd);
+		ast_debug(1, "Sent mail to %s with command '%s'\n", vmu->email, mailcmd);
 	}
 	return 0;
 }
@@ -2229,8 +2220,7 @@
 	fclose(p);
 	snprintf(tmp2, sizeof(tmp2), "( %s < %s ; rm -f %s ) &", mailcmd, tmp, tmp);
 	ast_safe_system(tmp2);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Sent page to %s with command '%s'\n", pager, mailcmd);
+	ast_debug(1, "Sent page to %s with command '%s'\n", pager, mailcmd);
 	return 0;
 }
 
@@ -2524,8 +2514,7 @@
 
 	if (!strcmp(fmt, "wav49"))
 		fmt = "WAV";
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG, "Storing file '%s', format '%s'\n", fn, fmt);
+	ast_debug(3, "Storing file '%s', format '%s'\n", fn, fmt);
 
 	/* Make a temporary file instead of piping directly to sendmail, in case the mail
 	   command hangs */
@@ -2552,8 +2541,7 @@
 	fclose(p);
 	unlink(tmp);
 	ast_free(buf);
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG, "%s stored\n", fn);
+	ast_debug(3, "%s stored\n", fn);
 	return 0;
 
 }
@@ -2577,8 +2565,7 @@
  	if (oldmsgs)
  		*oldmsgs = 0;
  
-	if(option_debug > 2)
-	 	ast_log (LOG_DEBUG,"Mailbox is set to %s\n",mailbox);
+	 ast_debug(3,"Mailbox is set to %s\n",mailbox);
 
  	/* If no mailbox, return immediately */
  	if (ast_strlen_zero(mailbox))
@@ -2616,7 +2603,7 @@
  	}
  
  	/* We have to get the user before we can open the stream! */
- 	/*ast_log (LOG_DEBUG,"Before find_user, context is %s and mailbox is %s\n",context,mailbox); */
+ 	/*ast_debug(1,"Before find_user, context is %s and mailbox is %s\n",context,mailbox); */
 	if (!(vmu = find_user(NULL, context, mailboxnc))) {
 		ast_log(LOG_ERROR, "Couldn't find mailbox %s in context %s\n", mailboxnc, context);
 		return -1;
@@ -2914,8 +2901,7 @@
 			ast_log(LOG_WARNING, "The switch reported '%s'\n", mwi_msg->cause);
 			ASTOBJ_UNREF(mwi_msg, ast_smdi_mwi_message_destroy);
 		} else {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Successfully executed SMDI MWI change for %s on %s\n", extension, smdi_iface->name);
+			ast_debug(1, "Successfully executed SMDI MWI change for %s on %s\n", extension, smdi_iface->name);
 		}
 	}
 
@@ -2924,8 +2910,7 @@
 			ast_log(LOG_ERROR, "Problem in calculating number of voicemail messages available for extension %s\n", extension);
 		} else {
 			snprintf(arguments, sizeof(arguments), "%s %s %s %d&", externnotify, context, extension, newvoicemails);
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Executing %s\n", arguments);
+			ast_debug(1, "Executing %s\n", arguments);
 			ast_safe_system(arguments);
 		}
 	}
@@ -2986,8 +2971,7 @@
 
 	category = pbx_builtin_getvar_helper(chan, "VM_CATEGORY");
 
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "Before find_user\n");
+	ast_debug(3, "Before find_user\n");
 	if (!(vmu = find_user(&svm, context, ext))) {
 		ast_log(LOG_WARNING, "No entry in voicemail config file for '%s'\n", ext);
 		pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED");
@@ -3052,14 +3036,12 @@
 			if (ast_streamfile(chan, prefile, chan->language) > -1) 
 				res = ast_waitstream(chan, ecodes);
 		} else {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "%s doesn't exist, doing what we can\n", prefile);
+			ast_debug(1, "%s doesn't exist, doing what we can\n", prefile);
 			res = invent_message(chan, vmu->context, ext, ast_test_flag(options, OPT_BUSY_GREETING), ecodes);
 		}
 		DISPOSE(prefile, -1);
 		if (res < 0) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Hang up during prefile playback\n");
+			ast_debug(1, "Hang up during prefile playback\n");
 			free_user(vmu);
 			pbx_builtin_setvar_helper(chan, "VMSTATUS", "FAILED");
 			return -1;
@@ -3128,8 +3110,7 @@
 		/* must open stream for this user to get info! */
 		vms = get_vm_state_by_mailbox(ext,0);
 		if (vms) {
-			if(option_debug > 2)
-				ast_log(LOG_DEBUG, "Using vm_state, interactive set to %d.\n",vms->interactive);
+			ast_debug(3, "Using vm_state, interactive set to %d.\n",vms->interactive);
 			newmsgs = vms->newmessages++;
 			oldmsgs = vms->oldmessages;
 		} else {
@@ -3142,23 +3123,20 @@
 		}
 		/* here is a big difference! We add one to it later */
 		msgnum = newmsgs + oldmsgs;
-		if(option_debug > 2)
-			ast_log(LOG_DEBUG, "Messagecount set to %d\n",msgnum);
+		ast_debug(3, "Messagecount set to %d\n",msgnum);
 		snprintf(fn, sizeof(fn), "%s/imap/msg%s%04d", VM_SPOOL_DIR, vmu->mailbox, msgnum);
 		/* set variable for compatibility */
 		pbx_builtin_setvar_helper(chan, "VM_MESSAGEFILE", "IMAP_STORAGE");
 
 		/* Check if mailbox is full */
 		if (vms->quota_limit && vms->quota_usage >= vms->quota_limit) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "*** QUOTA EXCEEDED!! %u >= %u\n", vms->quota_usage, vms->quota_limit);
+			ast_debug(1, "*** QUOTA EXCEEDED!! %u >= %u\n", vms->quota_usage, vms->quota_limit);
 			ast_play_and_wait(chan, "vm-mailboxfull");
 			return -1;
 		}
 		/* here is a big difference! We add one to it later */
 		msgnum = newmsgs + oldmsgs;
-		if(option_debug > 2)
-			ast_log(LOG_DEBUG, "Messagecount set to %d\n",msgnum);
+		ast_debug(3, "Messagecount set to %d\n",msgnum);
 
 #else
 		if (count_messages(vmu, dir) >= vmu->maxmsg) {
@@ -3251,8 +3229,7 @@
 					ast_filedelete(tmptxtfile, NULL);
 					unlink(tmptxtfile);
 				} else if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) {
-					if (option_debug) 
-						ast_log(LOG_DEBUG, "The recorded media file is gone, so we should remove the .txt file too!\n");
+					ast_debug(1, "The recorded media file is gone, so we should remove the .txt file too!\n");
 					unlink(tmptxtfile);
 					ast_unlock_path(dir);
 					if (ast_check_realtime("voicemail_data")) {
@@ -3375,8 +3352,7 @@
 	/* get the real IMAP message number for this message */
 	sprintf(sequence,"%ld",vms->msgArray[msg]);
 	imap_mailbox_name(dbox, vms, box, 1);
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "Copying sequence %s to mailbox %s\n",sequence,dbox);
+	ast_debug(3, "Copying sequence %s to mailbox %s\n",sequence,dbox);
 	res = mail_copy(vms->mailstream, sequence, dbox);
 	if (res == 1) return 0;
 	return 1;
@@ -3525,8 +3501,7 @@
 	bytes += ast_adsi_voice_mode(buf + bytes, 0);
 	ast_adsi_transmit_message(chan, buf, bytes, ADSI_MSG_DOWNLOAD);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Done downloading scripts...\n");
+	ast_debug(1, "Done downloading scripts...\n");
 
 #ifdef DISPLAY
 	/* Add last dot */
@@ -3534,8 +3509,7 @@
 	bytes += ast_adsi_display(buf + bytes, ADSI_COMM_PAGE, 4, ADSI_JUST_CENT, 0, "   ......", "");
 	bytes += ast_adsi_set_line(buf + bytes, ADSI_COMM_PAGE, 1);
 #endif
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Restarting session...\n");
+	ast_debug(1, "Restarting session...\n");
 
 	bytes = 0;
 	/* Load the session now */
@@ -4458,15 +4432,13 @@
 		return res;
 
 	/* Strip off caller ID number from name */
-	if (option_debug)
-		ast_log(LOG_DEBUG, "VM-CID: composite caller ID received: %s, context: %s\n", cid, context);
+	ast_debug(1, "VM-CID: composite caller ID received: %s, context: %s\n", cid, context);
 	ast_callerid_parse(cid, &name, &callerid);
 	if ((!ast_strlen_zero(callerid)) && strcmp(callerid, "Unknown")) {
 		/* Check for internal contexts and only */
 		/* say extension when the call didn't come from an internal context in the list */
 		for (i = 0 ; i < MAX_NUM_CID_CONTEXTS ; i++){
-			if (option_debug)
-				ast_log(LOG_DEBUG, "VM-CID: comparing internalcontext: %s\n", cidinternalcontexts[i]);
+			ast_debug(1, "VM-CID: comparing internalcontext: %s\n", cidinternalcontexts[i]);
 			if ((strcmp(cidinternalcontexts[i], context) == 0))
 				break;
 		}
@@ -4494,8 +4466,7 @@
 		}
 
 		else if (!res) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "VM-CID: Numeric caller id: (%s)\n", callerid);
+			ast_debug(1, "VM-CID: Numeric caller id: (%s)\n", callerid);
 			/* BB: Since this is all nicely figured out, why not say "from phone number" in this case" */
 			if (!callback)
 				res = wait_file2(chan, vms, "vm-from-phonenumber");
@@ -4503,8 +4474,7 @@
 		}
 	} else {
 		/* Number unknown */
-		if (option_debug)
-			ast_log(LOG_DEBUG, "VM-CID: From an unknown number\n");
+		ast_debug(1, "VM-CID: From an unknown number\n");
 		/* Say "from an unknown caller" as one phrase - it is already recorded by "the voice" anyhow */
 		res = wait_file2(chan, vms, "vm-unknown-caller");
 	}
@@ -4524,8 +4494,7 @@
 	durations=atoi(duration);
 	durationm=(durations / 60);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "VM-Duration: duration is: %d seconds converted to: %d minutes\n", durations, durationm);
+	ast_debug(1, "VM-Duration: duration is: %d seconds converted to: %d minutes\n", durations, durationm);
 
 	if ((!res) && (durationm >= minduration)) {
 		res = wait_file2(chan, vms, "vm-duration");
@@ -4879,10 +4848,8 @@
 	char dbox[256];
 
 	ast_copy_string(vms->imapuser,vmu->imapuser, sizeof(vms->imapuser));
+	ast_debug(3,"Before init_mailstream, user is %s\n",vmu->imapuser);
 
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG,"Before init_mailstream, user is %s\n",vmu->imapuser);
-
 	if ((ret = init_mailstream(vms, box)) || !vms->mailstream) {
 		ast_log (LOG_ERROR,"Could not initialize mailstream\n");
 		return -1;
@@ -4910,8 +4877,7 @@
 		pgm->unseen = 0;
 	}
 
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG,"Before mail_search_full, user is %s\n",vmu->imapuser);
+	ast_debug(3,"Before mail_search_full, user is %s\n",vmu->imapuser);
 
 	vms->vmArrayIndex = 0;
 	mail_search_full (vms->mailstream, NULL, pgm, NIL);
@@ -5028,8 +4994,7 @@
 	if (vms->deleted) {
 		for (x=0;x < vmu->maxmsg;x++) { 
 			if (vms->deleted[x]) { 
-				if(option_debug > 2)
-					ast_log(LOG_DEBUG,"IMAP delete of %d\n",x);
+				ast_debug(3,"IMAP delete of %d\n",x);
 				IMAP_DELETE(vms->curdir, x, vms->fn, vms);
 			}
 		}
@@ -5973,8 +5938,7 @@
 	if ((pwdchange & PWDCHANGE_EXTERNAL) && !ast_strlen_zero(ext_pass_cmd))
 		vm_change_password_shell(vmu, newpassword);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG,"User %s set password to %s of length %d\n",vms->username,newpassword,(int)strlen(newpassword));
+	ast_debug(1,"User %s set password to %s of length %d\n",vms->username,newpassword,(int)strlen(newpassword));
 	cmd = ast_play_and_wait(chan, vm_passchanged);
 
 	/* If forcename is set, have the user record their name */	
@@ -6108,8 +6072,7 @@
 			if ((pwdchange & PWDCHANGE_EXTERNAL) && !ast_strlen_zero(ext_pass_cmd))
 				vm_change_password_shell(vmu, newpassword);
 
-			if (option_debug)
-				ast_log(LOG_DEBUG,"User %s set password to %s of length %d\n",vms->username,newpassword,(int)strlen(newpassword));
+			ast_debug(1,"User %s set password to %s of length %d\n",vms->username,newpassword,(int)strlen(newpassword));
 			cmd = ast_play_and_wait(chan, vm_passchanged);
 			break;
 		case '*': 
@@ -6368,8 +6331,7 @@
 			ast_copy_string(mailbox, fullusername, mailbox_size);
 		}
 
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Before find user for mailbox %s\n",mailbox);
+		ast_debug(1, "Before find user for mailbox %s\n",mailbox);
 		vmu = find_user(&vmus, context, mailbox);
 		if (vmu && (vmu->password[0] == '\0' || (vmu->password[0] == '-' && vmu->password[1] == '\0'))) {
 			/* saved password is blank, so don't bother asking */
@@ -6461,8 +6423,7 @@
 	memset(&vmus, 0, sizeof(vmus));
 
 	if (chan->_state != AST_STATE_UP) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Before ast_answer\n");
+		ast_debug(1, "Before ast_answer\n");
 		ast_answer(chan);
 	}
 
@@ -6544,8 +6505,7 @@
 	if (!valid)
 		res = vm_authenticate(chan, vms.username, sizeof(vms.username), &vmus, context, prefixstr, skipuser, maxlogins, 0);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "After vm_authenticate\n");
+	ast_debug(1, "After vm_authenticate\n");
 	if (!res) {
 		valid = 1;
 		if (!skipuser)
@@ -6580,21 +6540,18 @@
 	create_dirpath(vms.curdir, sizeof(vms.curdir), vmu->context, vms.username, "");
 #endif
 	/* Retrieve old and new message counts */
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Before open_mailbox\n");
+	ast_debug(1, "Before open_mailbox\n");
 	res = open_mailbox(&vms, vmu, 1);
 	if (res == ERROR_LOCK_PATH)
 		goto out;
 	vms.oldmessages = vms.lastmsg + 1;
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Number of old messages: %d\n",vms.oldmessages);
+	ast_debug(1, "Number of old messages: %d\n",vms.oldmessages);
 	/* Start in INBOX */
 	res = open_mailbox(&vms, vmu, 0);
 	if (res == ERROR_LOCK_PATH)
 		goto out;
 	vms.newmessages = vms.lastmsg + 1;
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Number of new messages: %d\n",vms.newmessages);
+	ast_debug(1, "Number of new messages: %d\n",vms.newmessages);
 		
 	/* Select proper mailbox FIRST!! */
 	if (play_auto) {
@@ -6638,11 +6595,9 @@
 		}
 	}
 #ifdef IMAP_STORAGE
-		if(option_debug > 2)
-			ast_log(LOG_DEBUG, "Checking quotas: comparing %u to %u\n",vms.quota_usage,vms.quota_limit);
+		ast_debug(3, "Checking quotas: comparing %u to %u\n",vms.quota_usage,vms.quota_limit);
 		if (vms.quota_limit && vms.quota_usage >= vms.quota_limit) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "*** QUOTA EXCEEDED!!\n");
+			ast_debug(1, "*** QUOTA EXCEEDED!!\n");
 			cmd = ast_play_and_wait(chan, "vm-mailboxfull");
 		}
 #endif
@@ -6948,8 +6903,7 @@
 	}
 #ifdef IMAP_STORAGE
 	/* expunge message - use UID Expunge if supported on IMAP server*/
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG, "*** Checking if we can expunge, deleted set to %d, expungeonhangup set to %d\n",deleted,expungeonhangup);
+	ast_debug(3, "*** Checking if we can expunge, deleted set to %d, expungeonhangup set to %d\n",deleted,expungeonhangup);
 	if (vmu && deleted == 1 && expungeonhangup == 1) {
 #ifdef HAVE_IMAP_TK2006
 		if (LEVELUIDPLUS (vms.mailstream)) {
@@ -7734,28 +7688,24 @@
 		/* External voicemail notify application */
 		if ((val = ast_variable_retrieve(cfg, "general", "externnotify"))) {
 			ast_copy_string(externnotify, val, sizeof(externnotify));
-			if (option_debug)
-				ast_log(LOG_DEBUG, "found externnotify: %s\n", externnotify);
+			ast_debug(1, "found externnotify: %s\n", externnotify);
 		} else {
 			externnotify[0] = '\0';
 		}
 
 		/* SMDI voicemail notification */
 		if ((s = ast_variable_retrieve(cfg, "general", "smdienable")) && ast_true(s)) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Enabled SMDI voicemail notification\n");
+			ast_debug(1, "Enabled SMDI voicemail notification\n");
 			if ((val = ast_variable_retrieve(cfg, "general", "smdiport"))) {
 				smdi_iface = ast_smdi_interface_find(val);
 			} else {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "No SMDI interface set, trying default (/dev/ttyS0)\n");
+				ast_debug(1, "No SMDI interface set, trying default (/dev/ttyS0)\n");
 				smdi_iface = ast_smdi_interface_find("/dev/ttyS0");
 			}
 			if (!smdi_iface) {
 				ast_log(LOG_ERROR, "No valid SMDI interface specfied, disabling SMDI voicemail notification\n");
 			} else {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Using SMDI port %s\n", smdi_iface->name);
+				ast_debug(1, "Using SMDI port %s\n", smdi_iface->name);
 			}
 		}
 
@@ -7854,8 +7804,7 @@
 		ast_set2_flag((&globalflags), ast_true(val), VM_FORCEGREET);
 
 		if ((s = ast_variable_retrieve(cfg, "general", "cidinternalcontexts"))){
-			if (option_debug)
-				ast_log(LOG_DEBUG,"VM_CID Internal context string: %s\n",s);
+			ast_debug(1,"VM_CID Internal context string: %s\n",s);
 			stringp = ast_strdupa(s);
 			for (x = 0 ; x < MAX_NUM_CID_CONTEXTS ; x++){
 				if (!ast_strlen_zero(stringp)) {
@@ -7863,62 +7812,53 @@
 					while ((*q == ' ')||(*q == '\t')) /* Eat white space between contexts */
 						q++;
 					ast_copy_string(cidinternalcontexts[x], q, sizeof(cidinternalcontexts[x]));
-					if (option_debug)
-						ast_log(LOG_DEBUG,"VM_CID Internal context %d: %s\n", x, cidinternalcontexts[x]);
+					ast_debug(1,"VM_CID Internal context %d: %s\n", x, cidinternalcontexts[x]);
 				} else {
 					cidinternalcontexts[x][0] = '\0';
 				}
 			}
 		}
 		if (!(val = ast_variable_retrieve(cfg, "general", "review"))){
-			if (option_debug)
-				ast_log(LOG_DEBUG,"VM Review Option disabled globally\n");
+			ast_debug(1,"VM Review Option disabled globally\n");
 			val = "no";
 		}
 		ast_set2_flag((&globalflags), ast_true(val), VM_REVIEW);	
 
 		/*Temporary greeting reminder */
 		if (!(val = ast_variable_retrieve(cfg, "general", "tempgreetwarn"))) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "VM Temporary Greeting Reminder Option disabled globally\n");
+			ast_debug(1, "VM Temporary Greeting Reminder Option disabled globally\n");
 			val = "no";
 		} else {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "VM Temporary Greeting Reminder Option enabled globally\n");
+			ast_debug(1, "VM Temporary Greeting Reminder Option enabled globally\n");
 		}
 		ast_set2_flag((&globalflags), ast_true(val), VM_TEMPGREETWARN);
 
 		if (!(val = ast_variable_retrieve(cfg, "general", "operator"))){
-			if (option_debug)
-				ast_log(LOG_DEBUG,"VM Operator break disabled globally\n");
+			ast_debug(1,"VM Operator break disabled globally\n");
 			val = "no";
 		}
 		ast_set2_flag((&globalflags), ast_true(val), VM_OPERATOR);	
 
 		if (!(val = ast_variable_retrieve(cfg, "general", "saycid"))) {
-			if (option_debug)
-				ast_log(LOG_DEBUG,"VM CID Info before msg disabled globally\n");
+			ast_debug(1,"VM CID Info before msg disabled globally\n");
 			val = "no";
 		} 
 		ast_set2_flag((&globalflags), ast_true(val), VM_SAYCID);	
 
 		if (!(val = ast_variable_retrieve(cfg,"general", "sendvoicemail"))){
-			if (option_debug)
-				ast_log(LOG_DEBUG,"Send Voicemail msg disabled globally\n");
+			ast_debug(1,"Send Voicemail msg disabled globally\n");
 			val = "no";
 		}
 		ast_set2_flag((&globalflags), ast_true(val), VM_SVMAIL);
 	
 		if (!(val = ast_variable_retrieve(cfg, "general", "envelope"))) {
-			if (option_debug)
-				ast_log(LOG_DEBUG,"ENVELOPE before msg enabled globally\n");
+			ast_debug(1,"ENVELOPE before msg enabled globally\n");
 			val = "yes";
 		}
 		ast_set2_flag((&globalflags), ast_true(val), VM_ENVELOPE);	
 
 		if (!(val = ast_variable_retrieve(cfg, "general", "sayduration"))) {
-			if (option_debug)
-				ast_log(LOG_DEBUG,"Duration info before msg enabled globally\n");
+			ast_debug(1,"Duration info before msg enabled globally\n");
 			val = "yes";
 		}
 		ast_set2_flag((&globalflags), ast_true(val), VM_SAYDURATION);	
@@ -7933,32 +7873,28 @@
 		}
 
 		if (!(val = ast_variable_retrieve(cfg, "general", "nextaftercmd"))) {
-			if (option_debug)
-				ast_log(LOG_DEBUG,"We are not going to skip to the next msg after save/delete\n");
+			ast_debug(1,"We are not going to skip to the next msg after save/delete\n");
 			val = "no";
 		}
 		ast_set2_flag((&globalflags), ast_true(val), VM_SKIPAFTERCMD);
 
 		if ((val = ast_variable_retrieve(cfg, "general", "dialout"))) {
 			ast_copy_string(dialcontext, val, sizeof(dialcontext));
-			if (option_debug)
-				ast_log(LOG_DEBUG, "found dialout context: %s\n", dialcontext);
+			ast_debug(1, "found dialout context: %s\n", dialcontext);
 		} else {
 			dialcontext[0] = '\0';	
 		}
 		
 		if ((val = ast_variable_retrieve(cfg, "general", "callback"))) {
 			ast_copy_string(callcontext, val, sizeof(callcontext));
-			if (option_debug)
-				ast_log(LOG_DEBUG, "found callback context: %s\n", callcontext);
+			ast_debug(1, "found callback context: %s\n", callcontext);
 		} else {
 			callcontext[0] = '\0';
 		}
 
 		if ((val = ast_variable_retrieve(cfg, "general", "exitcontext"))) {
 			ast_copy_string(exitcontext, val, sizeof(exitcontext));
-			if (option_debug)
-				ast_log(LOG_DEBUG, "found operator context: %s\n", exitcontext);
+			ast_debug(1, "found operator context: %s\n", exitcontext);
 		} else {
 			exitcontext[0] = '\0';
 		}
@@ -8295,8 +8231,7 @@
 	/* START HERE */
 
 	/* get the message info!! */
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG, "Before mail_fetchheaders, curmsg is: %d, imap messages is %lu\n", vms->curmsg, vms->msgArray[vms->curmsg]);
+	ast_debug(3, "Before mail_fetchheaders, curmsg is: %d, imap messages is %lu\n", vms->curmsg, vms->msgArray[vms->curmsg]);
 
 	if (!vms->msgArray[vms->curmsg]) {
 		ast_log(LOG_WARNING, "Trying to access unknown message\n");
@@ -8744,8 +8679,7 @@
 		return;
 	}
 	
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG, "saving mailbox message number %lu as message %d. Interactive set to %d\n", number, vms->vmArrayIndex, vms->interactive);
+	ast_debug(3, "saving mailbox message number %lu as message %d. Interactive set to %d\n", number, vms->vmArrayIndex, vms->interactive);
 
 	vms->msgArray[vms->vmArrayIndex++] = number;
 }
@@ -8984,33 +8918,30 @@
 		delimiter = delim;
 		ast_mutex_unlock(&delimiter_lock);
 	}
-	if (option_debug > 4) {
-		ast_log(LOG_DEBUG, "Delimiter set to %c and mailbox %s\n",delim, mailbox);
-		if (attributes & LATT_NOINFERIORS)
-			ast_log(LOG_DEBUG, "no inferiors\n");
-		if (attributes & LATT_NOSELECT)
-			ast_log(LOG_DEBUG, "no select\n");
-		if (attributes & LATT_MARKED)
-			ast_log(LOG_DEBUG, "marked\n");
-		if (attributes & LATT_UNMARKED)
-			ast_log(LOG_DEBUG, "unmarked\n");
-	}
+
+	ast_debug(5, "Delimiter set to %c and mailbox %s\n",delim, mailbox);
+	if (attributes & LATT_NOINFERIORS)
+		ast_debug(5, "no inferiors\n");
+	if (attributes & LATT_NOSELECT)
+		ast_debug(5, "no select\n");
+	if (attributes & LATT_MARKED)
+		ast_debug(5, "marked\n");
+	if (attributes & LATT_UNMARKED)
+		ast_debug(5, "unmarked\n");
 }
 
 
 void mm_lsub(MAILSTREAM * stream, int delimiter, char *mailbox, long attributes)
 {
-	if (option_debug > 4) {
-		ast_log(LOG_DEBUG, "Delimiter set to %c and mailbox %s\n",delimiter, mailbox);
-		if (attributes & LATT_NOINFERIORS)
-			ast_log(LOG_DEBUG, "no inferiors\n");
-		if (attributes & LATT_NOSELECT)
-			ast_log(LOG_DEBUG, "no select\n");
-		if (attributes & LATT_MARKED)
-			ast_log(LOG_DEBUG, "marked\n");
-		if (attributes & LATT_UNMARKED)
-			ast_log(LOG_DEBUG, "unmarked\n");
-	}
+	ast_debug(5, "Delimiter set to %c and mailbox %s\n",delimiter, mailbox);
+	if (attributes & LATT_NOINFERIORS)
+		ast_debug(5, "no inferiors\n");
+	if (attributes & LATT_NOSELECT)
+		ast_debug(5, "no select\n");
+	if (attributes & LATT_MARKED)
+		ast_debug(5, "marked\n");
+	if (attributes & LATT_UNMARKED)
+		ast_debug(5, "unmarked\n");
 }
 
 
@@ -9035,8 +8966,7 @@
 {
 	switch ((short) errflg) {
 		case NIL:
-			if(option_debug)
-				ast_log(LOG_DEBUG,"IMAP Info: %s\n", string);
+			ast_debug(1,"IMAP Info: %s\n", string);
 			break;
 		case PARSE:
 		case WARN:
@@ -9059,8 +8989,7 @@
 {
 	struct ast_vm_user *vmu;
 
-	if(option_debug > 3)
-		ast_log(LOG_DEBUG, "Entering callback mm_login\n");
+	ast_debug(4, "Entering callback mm_login\n");
 
 	ast_copy_string(user, mb->user, MAILTMPLEN);
 
@@ -9125,8 +9054,7 @@
 		return;
 	}
 
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "User %s usage is %lu, limit is %lu\n", user, usage, limit);
+	ast_debug(3, "User %s usage is %lu, limit is %lu\n", user, usage, limit);
 
 	vms->quota_usage = usage;
 	vms->quota_limit = limit;
@@ -9186,13 +9114,11 @@
 
 	AST_LIST_TRAVERSE(&vmstates, vlist, list) {
 		if (!vlist->vms) {
-			if (option_debug > 2)
-				ast_log(LOG_DEBUG, "error: vms is NULL for %s\n", user);
+			ast_debug(3, "error: vms is NULL for %s\n", user);
 			continue;
 		}
 		if (!vlist->vms->imapuser) {
-			if (option_debug > 2)
-				ast_log(LOG_DEBUG, "error: imapuser is NULL for %s\n", user);
+			ast_debug(3, "error: imapuser is NULL for %s\n", user);
 			continue;
 		}
 
@@ -9202,8 +9128,7 @@
 			return vlist->vms;
 	}
 
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG, "%s not found in vmstates\n", user);
+	ast_debug(3, "%s not found in vmstates\n", user);
 
 	return NULL;
 }
@@ -9214,28 +9139,23 @@
 
 	AST_LIST_TRAVERSE(&vmstates, vlist, list) {
 		if (!vlist->vms) {
-			if (option_debug > 2)
-				ast_log(LOG_DEBUG, "error: vms is NULL for %s\n", mailbox);
+			ast_debug(3, "error: vms is NULL for %s\n", mailbox);
 			continue;
 		}
 		if (!vlist->vms->username) {
-			if (option_debug > 2)
-				ast_log(LOG_DEBUG, "error: username is NULL for %s\n", mailbox);
+			ast_debug(3, "error: username is NULL for %s\n", mailbox);
 			continue;
 		}
 
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "comparing mailbox %s (i=%d) to vmstate mailbox %s (i=%d)\n", mailbox, interactive, vlist->vms->username, vlist->vms->interactive);
+		ast_debug(3, "comparing mailbox %s (i=%d) to vmstate mailbox %s (i=%d)\n", mailbox, interactive, vlist->vms->username, vlist->vms->interactive);
 		
 		if (!strcmp(vlist->vms->username,mailbox) && vlist->vms->interactive == interactive) {
-			if(option_debug > 2)
-				ast_log(LOG_DEBUG, "Found it!\n");
+			ast_debug(3, "Found it!\n");
 			return vlist->vms;
 		}
 	}
 
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "%s not found in vmstates\n", mailbox);
+	ast_debug(3, "%s not found in vmstates\n", mailbox);
 
 	return NULL;
 }
@@ -9251,17 +9171,14 @@
 	if (vms->interactive == 1) {
 		altvms = get_vm_state_by_mailbox(vms->username,0);
 		if (altvms) {
-			if(option_debug > 2)
-				ast_log(LOG_DEBUG, "Duplicate mailbox %s, copying message info...\n",vms->username);
+			ast_debug(3, "Duplicate mailbox %s, copying message info...\n",vms->username);
 			vms->newmessages = altvms->newmessages;
 			vms->oldmessages = altvms->oldmessages;
-			if(option_debug > 2)
-				ast_log(LOG_DEBUG, "check_msgArray before memcpy\n");
+			ast_debug(3, "check_msgArray before memcpy\n");
 			check_msgArray(vms);
 			/* memcpy(vms->msgArray, altvms->msgArray, sizeof(long)*256); */
 			copy_msgArray(vms, altvms);
-			if(option_debug > 2)
-				ast_log(LOG_DEBUG, "check_msgArray after memcpy\n");
+			ast_debug(3, "check_msgArray after memcpy\n");
 			check_msgArray(vms);
 			vms->vmArrayIndex = altvms->vmArrayIndex;
 			vms->lastmsg = altvms->lastmsg;
@@ -9279,8 +9196,7 @@
 	
 	v->vms = vms;
 
-	if(option_debug > 2)
-		ast_log(LOG_DEBUG, "Inserting vm_state for user:%s, mailbox %s\n",vms->imapuser,vms->username);
+	ast_debug(3, "Inserting vm_state for user:%s, mailbox %s\n",vms->imapuser,vms->username);
 
 	AST_LIST_LOCK(&vmstates);
 	AST_LIST_INSERT_TAIL(&vmstates, v, list);
@@ -9295,15 +9211,13 @@
 	/* If interactive, we should copy pertinent info
 	   back to the persistent state (to make update immediate) */
 	if (vms->interactive == 1 && (altvms = vms->persist_vms)) {
-		if(option_debug > 2)
-			ast_log(LOG_DEBUG, "Duplicate mailbox %s, copying message info...\n", vms->username);
+		ast_debug(3, "Duplicate mailbox %s, copying message info...\n", vms->username);
 		altvms->newmessages = vms->newmessages;
 		altvms->oldmessages = vms->oldmessages;
 		altvms->updated = 2;
 	}
 	
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "Removing vm_state for user:%s, mailbox %s\n", vms->imapuser, vms->username);
+	ast_debug(3, "Removing vm_state for user:%s, mailbox %s\n", vms->imapuser, vms->username);
 	
 	AST_LIST_LOCK(&vmstates);
 	AST_LIST_TRAVERSE_SAFE_BEGIN(&vmstates, vc, list) {
@@ -9333,8 +9247,7 @@
 		return;
 	}
 
-	if (option_debug > 2)
-		ast_log(LOG_DEBUG, "User %s mailbox set for update.\n", user);
+	ast_debug(3, "User %s mailbox set for update.\n", user);
        
 	vms->updated = 2; /* Set updated flag since mailbox changed */
 }
Index: app_dial.c
===================================================================
--- app_dial.c	(revision 69052)
+++ app_dial.c	(working copy)
@@ -711,8 +711,7 @@
 					}
 					break;
 				default:
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
+					ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
 				}
 			} else if (single) {
 				/* XXX are we sure the logic is correct ? or we should just switch on f->frametype ? */
@@ -1417,8 +1416,7 @@
 		/* check the results of ast_call */
 		if (res) {
 			/* Again, keep going even if there's an error */
-			if (option_debug)
-				ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
+			ast_debug(1, "ast call on peer returned %d\n", res);
 			if (option_verbose > 2)
 				ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", numsubst);
 			ast_hangup(tc);
@@ -1510,8 +1508,7 @@
 			number = numsubst;
 		pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", number);
  		if (!ast_strlen_zero(args.url) && ast_channel_supports_html(peer) ) {
-			if (option_debug)
- 				ast_log(LOG_DEBUG, "app_dial: sendurl=%s.\n", args.url);
+ 			ast_debug(1, "app_dial: sendurl=%s.\n", args.url);
  			ast_channel_sendurl( peer, args.url );
  		}
 		if ( (ast_test_flag(&opts, OPT_PRIVACY) || ast_test_flag(&opts, OPT_SCREENING)) && pa.privdb_val == AST_PRIVACY_UNKNOWN) {
@@ -1569,8 +1566,7 @@
 			if (theapp && !res) {	/* XXX why check res here ? */
 				replace_macro_delimiter(opt_args[OPT_ARG_CALLEE_MACRO]);
 				res = pbx_exec(peer, theapp, opt_args[OPT_ARG_CALLEE_MACRO]);
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Macro exited with status %d\n", res);
+				ast_debug(1, "Macro exited with status %d\n", res);
 				res = 0;
 			} else {
 				ast_log(LOG_ERROR, "Could not find application Macro\n");
@@ -1706,8 +1702,7 @@
 	hanguptree(outgoing, NULL);
 	pbx_builtin_setvar_helper(chan, "DIALSTATUS", pa.status);
 	senddialendevent(chan, pa.status);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Exiting with DIALSTATUS=%s.\n", pa.status);
+	ast_debug(1, "Exiting with DIALSTATUS=%s.\n", pa.status);
 	
 	if ((ast_test_flag(peerflags, OPT_GO_ON)) && (!chan->_softhangup) && (res != AST_PBX_KEEPALIVE)) {
 		if (calldurationlimit)
Index: app_nbscat.c
===================================================================
--- app_nbscat.c	(revision 69052)
+++ app_nbscat.c	(working copy)
@@ -175,8 +175,7 @@
 						break;
 					}
 				} else {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "No more mp3\n");
+					ast_debug(1, "No more mp3\n");
 					res = 0;
 					break;
 				}
@@ -184,22 +183,19 @@
 			} else {
 				ms = ast_waitfor(chan, ms);
 				if (ms < 0) {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Hangup detected\n");
+					ast_debug(1, "Hangup detected\n");
 					res = -1;
 					break;
 				}
 				if (ms) {
 					f = ast_read(chan);
 					if (!f) {
-						if (option_debug)
-							ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
+						ast_debug(1, "Null frame == hangup() detected\n");
 						res = -1;
 						break;
 					}
 					if (f->frametype == AST_FRAME_DTMF) {
-						if (option_debug)
-							ast_log(LOG_DEBUG, "User pressed a key\n");
+						ast_debug(1, "User pressed a key\n");
 						ast_frfree(f);
 						res = 0;
 						break;
Index: app_waitforsilence.c
===================================================================
--- app_waitforsilence.c	(revision 69052)
+++ app_waitforsilence.c	(working copy)
@@ -135,15 +135,13 @@
 			/* Ended happily with silence */
 			res = 1;
 			pbx_builtin_setvar_helper(chan, "WAITSTATUS", "SILENCE");
-			if (option_debug)
-				ast_log(LOG_DEBUG, "WAITSTATUS was set to SILENCE\n");
+			ast_debug(1, "WAITSTATUS was set to SILENCE\n");
 			break;
 		}
 
 		if ( timeout && (difftime(time(&now),waitstart) >= timeout) ) {
 			pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
-			if (option_debug)
-				ast_log(LOG_DEBUG, "WAITSTATUS was set to TIMEOUT\n");
+			ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
 			res = 0;
 			break;
 		}
Index: app_disa.c
===================================================================
--- app_disa.c	(revision 69052)
+++ app_disa.c	(working copy)
@@ -164,10 +164,8 @@
 		return -1;
 	}
 	
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "Digittimeout: %d\n", digittimeout);
-		ast_log(LOG_DEBUG, "Responsetimeout: %d\n", firstdigittimeout);
-	}
+	ast_debug(1, "Digittimeout: %d\n", digittimeout);
+	ast_debug(1, "Responsetimeout: %d\n", firstdigittimeout);
 
 	tmp = ast_strdupa(data);
 
@@ -180,8 +178,7 @@
 	if (!ast_strlen_zero(args.options))
 		ast_app_parse_options(app_opts, &flags, NULL, args.options);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Mailbox: %s\n",args.mailbox);
+	ast_debug(1, "Mailbox: %s\n",args.mailbox);
 
 	special_noanswer = 0;
 	if (ast_test_flag(&flags, NOANSWER_FLAG)) {
@@ -196,13 +193,11 @@
 	acctcode[0] = 0;
 	/* can we access DISA without password? */ 
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Context: %s\n",args.context);
+	ast_debug(1, "Context: %s\n",args.context);
 
 	if (!strcasecmp(args.passcode, "no-password")) {
 		k |= 1; /* We have the password */
-		if (option_debug)
-			ast_log(LOG_DEBUG, "DISA no-password login success\n");
+		ast_debug(1, "DISA no-password login success\n");
 	}
 	lastdigittime = ast_tvnow();
 
@@ -212,14 +207,12 @@
 		  /* if outa time, give em reorder */
 		if (ast_tvdiff_ms(ast_tvnow(), lastdigittime) > 
 		    ((k&2) ? digittimeout : firstdigittimeout)) {
-			if (option_debug)
-				ast_log(LOG_DEBUG,"DISA %s entry timeout on chan %s\n",
-					((k&1) ? "extension" : "password"),chan->name);
+			ast_debug(1,"DISA %s entry timeout on chan %s\n",
+				((k&1) ? "extension" : "password"),chan->name);
 			break;
 		}
 		if ((res = ast_waitfor(chan, -1) < 0)) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Waitfor returned %d\n", res);
+			ast_debug(1, "Waitfor returned %d\n", res);
 			continue;
 		}
 			
@@ -280,8 +273,7 @@
 
 							AST_STANDARD_APP_ARGS(args, pwline);
 			
-							if (option_debug)
-								ast_log(LOG_DEBUG, "Mailbox: %s\n",args.mailbox);
+							ast_debug(1, "Mailbox: %s\n",args.mailbox);
 
 							/* password must be in valid format (numeric) */
 							if (sscanf(args.passcode,"%d", &j) < 1)
@@ -304,8 +296,7 @@
 
 					}
 					 /* password good, set to dial state */
-					if (option_debug)
-						ast_log(LOG_DEBUG,"DISA on chan %s password is good\n",chan->name);
+					ast_debug(1,"DISA on chan %s password is good\n",chan->name);
 					play_dialtone(chan, args.mailbox);
 
 					k|=1; /* In number mode */
@@ -313,8 +304,7 @@
 					exten[sizeof(acctcode)] = 0;
 					ast_copy_string(acctcode, exten, sizeof(acctcode));
 					exten[0] = 0;
-					if (option_debug)
-						ast_log(LOG_DEBUG,"Successful DISA log-in on chan %s\n", chan->name);
+					ast_debug(1,"Successful DISA log-in on chan %s\n", chan->name);
 					continue;
 				}
 			}
Index: app_osplookup.c
===================================================================
--- app_osplookup.c	(revision 69052)
+++ app_osplookup.c	(working copy)
@@ -223,16 +223,14 @@
 			} else {
 				snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s", ast_config_AST_KEY_DIR, v->value);
 			}
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: privatekey '%s'\n", p->privatekey);
+			ast_debug(1, "OSP: privatekey '%s'\n", p->privatekey);
 		} else if (!strcasecmp(v->name, "localcert")) {
 			if (v->value[0] == '/') {
 				ast_copy_string(p->localcert, v->value, sizeof(p->localcert));
 			} else {
 				snprintf(p->localcert, sizeof(p->localcert), "%s/%s", ast_config_AST_KEY_DIR, v->value);
 			}
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: localcert '%s'\n", p->localcert);
+			ast_debug(1, "OSP: localcert '%s'\n", p->localcert);
 		} else if (!strcasecmp(v->name, "cacert")) {
 			if (p->cacount < OSP_MAX_CERTS) {
 				if (v->value[0] == '/') {
@@ -240,8 +238,7 @@
 				} else {
 					snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s", ast_config_AST_KEY_DIR, v->value);
 				}
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
+				ast_debug(1, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
 				p->cacount++;
 			} else {
 				ast_log(LOG_WARNING, "OSP: Too many CA Certificates at line %d\n", v->lineno);
@@ -249,8 +246,7 @@
 		} else if (!strcasecmp(v->name, "servicepoint")) {
 			if (p->spcount < OSP_MAX_SRVS) {
 				ast_copy_string(p->srvpoints[p->spcount], v->value, sizeof(p->srvpoints[0]));
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: servicepoint[%d]: '%s'\n", p->spcount, p->srvpoints[p->spcount]);
+				ast_debug(1, "OSP: servicepoint[%d]: '%s'\n", p->spcount, p->srvpoints[p->spcount]);
 				p->spcount++;
 			} else {
 				ast_log(LOG_WARNING, "OSP: Too many Service Points at line %d\n", v->lineno);
@@ -258,8 +254,7 @@
 		} else if (!strcasecmp(v->name, "maxconnections")) {
 			if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_MAXCONNECTIONS) && (t <= OSP_MAX_MAXCONNECTIONS)) {
 				p->maxconnections = t;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: maxconnections '%d'\n", t);
+				ast_debug(1, "OSP: maxconnections '%d'\n", t);
 			} else {
 				ast_log(LOG_WARNING, "OSP: maxconnections should be an integer from %d to %d, not '%s' at line %d\n",
 					OSP_MIN_MAXCONNECTIONS, OSP_MAX_MAXCONNECTIONS, v->value, v->lineno);
@@ -267,8 +262,7 @@
 		} else if (!strcasecmp(v->name, "retrydelay")) {
 			if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_RETRYDELAY) && (t <= OSP_MAX_RETRYDELAY)) {
 				p->retrydelay = t;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: retrydelay '%d'\n", t);
+				ast_debug(1, "OSP: retrydelay '%d'\n", t);
 			} else {
 				ast_log(LOG_WARNING, "OSP: retrydelay should be an integer from %d to %d, not '%s' at line %d\n",
 					OSP_MIN_RETRYDELAY, OSP_MAX_RETRYDELAY, v->value, v->lineno);
@@ -276,8 +270,7 @@
 		} else if (!strcasecmp(v->name, "retrylimit")) {
 			if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_RETRYLIMIT) && (t <= OSP_MAX_RETRYLIMIT)) {
 				p->retrylimit = t;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: retrylimit '%d'\n", t);
+				ast_debug(1, "OSP: retrylimit '%d'\n", t);
 			} else {
 				ast_log(LOG_WARNING, "OSP: retrylimit should be an integer from %d to %d, not '%s' at line %d\n",
 					OSP_MIN_RETRYLIMIT, OSP_MAX_RETRYLIMIT, v->value, v->lineno);
@@ -285,21 +278,18 @@
 		} else if (!strcasecmp(v->name, "timeout")) {
 			if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_TIMEOUT) && (t <= OSP_MAX_TIMEOUT)) {
 				p->timeout = t;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: timeout '%d'\n", t);
+				ast_debug(1, "OSP: timeout '%d'\n", t);
 			} else {
 				ast_log(LOG_WARNING, "OSP: timeout should be an integer from %d to %d, not '%s' at line %d\n",
 					OSP_MIN_TIMEOUT, OSP_MAX_TIMEOUT, v->value, v->lineno);
 			}
 		} else if (!strcasecmp(v->name, "source")) {
 			ast_copy_string(p->source, v->value, sizeof(p->source));
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: source '%s'\n", p->source);
+			ast_debug(1, "OSP: source '%s'\n", p->source);
 		} else if (!strcasecmp(v->name, "authpolicy")) {
 			if ((sscanf(v->value, "%d", &t) == 1) && ((t == OSP_AUTH_NO) || (t == OSP_AUTH_YES) || (t == OSP_AUTH_EXCLUSIVE))) {
 				p->authpolicy = t;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: authpolicy '%d'\n", t);
+				ast_debug(1, "OSP: authpolicy '%d'\n", t);
 			} else {
 				ast_log(LOG_WARNING, "OSP: authpolicy should be %d, %d or %d, not '%s' at line %d\n",
 					OSP_AUTH_NO, OSP_AUTH_YES, OSP_AUTH_EXCLUSIVE, v->value, v->lineno);
@@ -307,16 +297,13 @@
 		} else if (!strcasecmp(v->name, "defaultprotocol")) {
 			if (!strcasecmp(v->value, OSP_PROT_SIP)) {
 				p->defaultprotocol = OSP_PROT_SIP;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
+				ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
 			} else if (!strcasecmp(v->value, OSP_PROT_H323)) {
 				p->defaultprotocol = OSP_PROT_H323;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
+				ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
 			} else if (!strcasecmp(v->value, OSP_PROT_IAX)) {
 				p->defaultprotocol = OSP_PROT_IAX;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
+				ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
 			} else {
 				ast_log(LOG_WARNING, "OSP: default protocol should be %s, %s, %s, or %s not '%s' at line %d\n",
 					OSP_PROT_SIP, OSP_PROT_H323, OSP_PROT_IAX, OSP_PROT_OTHER, v->value, v->lineno);
@@ -344,8 +331,7 @@
 
 	if (p->cacount < 1) {
 		snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s-cacert.pem", ast_config_AST_KEY_DIR, provider);
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
+		ast_debug(1, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
 		p->cacount++;
 	}
 	for (i = 0; i < p->cacount; i++) {
@@ -397,8 +383,7 @@
 		ast_free(p);
 		res = -1;
 	} else {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: provider '%s'\n", provider);
+		ast_debug(1, "OSP: provider '%s'\n", provider);
 		ast_mutex_lock(&osplock);
 		p->next = ospproviders;
 		ospproviders = p;
@@ -439,8 +424,7 @@
 	while(p) {
 		if (!strcasecmp(p->name, name)) {
 			*provider = p;
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: find provider '%s'\n", name);
+			ast_debug(1, "OSP: find provider '%s'\n", name);
 			res = 1;
 			break;
 		}
@@ -475,16 +459,13 @@
 		if (!strcasecmp(p->name, provider)) {
 			error = OSPPTransactionNew(p->handle, transaction);
 			if (error == OSPC_ERR_NO_ERROR) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: transaction '%d'\n", *transaction);
+				ast_debug(1, "OSP: transaction '%d'\n", *transaction);
 				ast_copy_string(source, p->source, sourcesize);
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: source '%s'\n", source);
+				ast_debug(1, "OSP: source '%s'\n", source);
 				res = 1;
 			} else {
 				*transaction = OSP_INVALID_HANDLE;
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: Unable to create transaction handle, error '%d'\n", error);
+				ast_debug(1, "OSP: Unable to create transaction handle, error '%d'\n", error);
 				res = -1;
 			}
 			break;
@@ -568,16 +549,13 @@
 				NULL,
 				osp_tokenformat);
 	if (error != OSPC_ERR_NO_ERROR) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unable to validate inbound token\n");
+		ast_debug(1, "OSP: Unable to validate inbound token\n");
 		res = -1;
 	} else if (authorised) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Authorised\n");
+		ast_debug(1, "OSP: Authorised\n");
 		res = 1;
 	} else {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unauthorised\n");
+		ast_debug(1, "OSP: Unauthorised\n");
 		res = 0;
 	}
 
@@ -631,15 +609,13 @@
 	int error;
 
 	if (strlen(destination) <= 2) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Wrong destination format '%s'\n", destination);
+		ast_debug(1, "OSP: Wrong destination format '%s'\n", destination);
 		*reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
 		return -1;
 	}
 
 	if ((error = OSPPTransactionIsDestOSPEnabled(result->outhandle, &enabled)) != OSPC_ERR_NO_ERROR) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unable to get destination OSP version, error '%d'\n", error);
+		ast_debug(1, "OSP: Unable to get destination OSP version, error '%d'\n", error);
 		*reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
 		return -1;
 	}
@@ -651,14 +627,12 @@
 	}
 
 	if ((error = OSPPTransactionGetDestNetworkId(result->outhandle, result->networkid)) != OSPC_ERR_NO_ERROR) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unable to get destination network ID, error '%d'\n", error);
+		ast_debug(1, "OSP: Unable to get destination network ID, error '%d'\n", error);
 		result->networkid[0] = '\0';
 	}
 
 	if ((error = OSPPTransactionGetDestProtocol(result->outhandle, &protocol)) != OSPC_ERR_NO_ERROR) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unable to get destination protocol, error '%d'\n", error);
+		ast_debug(1, "OSP: Unable to get destination protocol, error '%d'\n", error);
 		*reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
 		result->token[0] = '\0';
 		result->networkid[0] = '\0';
@@ -670,24 +644,21 @@
 	destination[strlen(destination) - 1] = '\0';
 	switch(protocol) {
 		case OSPE_DEST_PROT_H323_SETUP:
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_H323);
+			ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_H323);
 			ast_copy_string(result->tech, OSP_TECH_H323, sizeof(result->tech));
 			ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
 			ast_copy_string(result->called, called, sizeof(result->called));
 			ast_copy_string(result->calling, calling, sizeof(result->calling));
 			break;
 		case OSPE_DEST_PROT_SIP:
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_SIP);
+			ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_SIP);
 			ast_copy_string(result->tech, OSP_TECH_SIP, sizeof(result->tech));
 			ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
 			ast_copy_string(result->called, called, sizeof(result->called));
 			ast_copy_string(result->calling, calling, sizeof(result->calling));
 			break;
 		case OSPE_DEST_PROT_IAX:
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_IAX);
+			ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_IAX);
 			ast_copy_string(result->tech, OSP_TECH_IAX, sizeof(result->tech));
 			ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
 			ast_copy_string(result->called, called, sizeof(result->called));
@@ -695,10 +666,9 @@
 			break;
 		case OSPE_DEST_PROT_UNDEFINED:
 		case OSPE_DEST_PROT_UNKNOWN:
-			if (option_debug) {
-				ast_log(LOG_DEBUG, "OSP: unknown/undefined protocol '%d'\n", protocol);
-				ast_log(LOG_DEBUG, "OSP: use default protocol '%s'\n", provider->defaultprotocol);
-			}
+			ast_debug(1, "OSP: unknown/undefined protocol '%d'\n", protocol);
+			ast_debug(1, "OSP: use default protocol '%s'\n", provider->defaultprotocol);
+
 			ast_copy_string(result->tech, provider->defaultprotocol, sizeof(result->tech));
 			ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
 			ast_copy_string(result->called, called, sizeof(result->called));
@@ -756,8 +726,7 @@
 	*timelimit = OSP_DEF_TIMELIMIT;
 
 	if ((res = osp_get_provider(provider, &p)) <= 0) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+		ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
 		return res;
 	}
 
@@ -769,8 +738,7 @@
 			if (ast_strlen_zero(token)) {
 				res = 0;
 			} else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
+				ast_debug(1, "OSP: Unable to generate transaction handle\n");
 				*transaction = OSP_INVALID_HANDLE;
 				res = 0;
 			} else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
@@ -782,8 +750,7 @@
 			if (ast_strlen_zero(token)) {
 				res = 1;
 			} else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
+				ast_debug(1, "OSP: Unable to generate transaction handle\n");
 				*transaction = OSP_INVALID_HANDLE;
 				res = 0;
 			} else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
@@ -928,14 +895,12 @@
 	result->outtimelimit = OSP_DEF_TIMELIMIT;
 
 	if ((res = osp_get_provider(provider, &p)) <= 0) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+		ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
 		return res;
 	}
 
 	if ((res = osp_create_transaction(provider, &result->outhandle, sizeof(source), source)) <= 0) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
+		ast_debug(1, "OSP: Unable to generate transaction handle\n");
 		result->outhandle = OSP_INVALID_HANDLE;
 		if (result->inhandle != OSP_INVALID_HANDLE) {
 			OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
@@ -980,8 +945,7 @@
 	}
 
 	if (error != OSPC_ERR_NO_ERROR) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unable to request authorization\n");
+		ast_debug(1, "OSP: Unable to request authorization\n");
 		result->numresults = 0;
 		if (result->inhandle != OSP_INVALID_HANDLE) {
 			OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
@@ -990,8 +954,7 @@
 	}
 
 	if (!result->numresults) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: No more destination\n");
+		ast_debug(1, "OSP: No more destination\n");
 		if (result->inhandle != OSP_INVALID_HANDLE) {
 			OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
 		}
@@ -1019,8 +982,7 @@
 				&tokenlen,
 				token);
 	if (error != OSPC_ERR_NO_ERROR) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unable to get first route\n");
+		ast_debug(1, "OSP: Unable to get first route\n");
 		result->numresults = 0;
 		result->outtimelimit = OSP_DEF_TIMELIMIT;
 		if (result->inhandle != OSP_INVALID_HANDLE) {
@@ -1031,21 +993,18 @@
 
 	result->numresults--;
 	result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-		ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-		ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-		ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-		ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-	}
+	ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
+	ast_debug(1, "OSP: called '%s'\n", callednum);
+	ast_debug(1, "OSP: calling '%s'\n", callingnum);
+	ast_debug(1, "OSP: destination '%s'\n", destination);
+	ast_debug(1, "OSP: token size '%d'\n", tokenlen);
 
 	if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
 		return 1;
 	}
 
 	if (!result->numresults) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: No more destination\n");
+		ast_debug(1, "OSP: No more destination\n");
 		result->outtimelimit = OSP_DEF_TIMELIMIT;
 		OSPPTransactionRecordFailure(result->outhandle, reason);
 		if (result->inhandle != OSP_INVALID_HANDLE) {
@@ -1079,18 +1038,16 @@
 		if (error == OSPC_ERR_NO_ERROR) {
 			result->numresults--;
 			result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-			if (option_debug) {
-				ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-				ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-				ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-				ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-				ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-			}
+			ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
+			ast_debug(1, "OSP: called '%s'\n", callednum);
+			ast_debug(1, "OSP: calling '%s'\n", callingnum);
+			ast_debug(1, "OSP: destination '%s'\n", destination);
+			ast_debug(1, "OSP: token size '%d'\n", tokenlen);
+
 			if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
 				break;
 			} else if (!result->numresults) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: No more destination\n");
+				ast_debug(1, "OSP: No more destination\n");
 				OSPPTransactionRecordFailure(result->outhandle, reason);
 				if (result->inhandle != OSP_INVALID_HANDLE) {
 					OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
@@ -1099,8 +1056,7 @@
 				break;
 			}
 		} else {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: Unable to get route, error '%d'\n", error);
+			ast_debug(1, "OSP: Unable to get route, error '%d'\n", error);
 			result->numresults = 0;
 			result->outtimelimit = OSP_DEF_TIMELIMIT;
 			if (result->inhandle != OSP_INVALID_HANDLE) {
@@ -1144,14 +1100,12 @@
 	result->outtimelimit = OSP_DEF_TIMELIMIT;
 
 	if ((res = osp_get_provider(provider, &p)) <= 0) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+		ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
 		return res;
 	}
 
 	if (result->outhandle == OSP_INVALID_HANDLE) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Transaction handle undefined\n");
+		ast_debug(1, "OSP: Transaction handle undefined\n");
 		result->numresults = 0;
 		if (result->inhandle != OSP_INVALID_HANDLE) {
 			OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
@@ -1162,8 +1116,7 @@
 	reason = asterisk2osp(cause);
 
 	if (!result->numresults) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: No more destination\n");
+		ast_debug(1, "OSP: No more destination\n");
 		OSPPTransactionRecordFailure(result->outhandle, reason);
 		if (result->inhandle != OSP_INVALID_HANDLE) {
 			OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
@@ -1196,19 +1149,17 @@
 		if (error == OSPC_ERR_NO_ERROR) {
 			result->numresults--;
 			result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-			if (option_debug) {
-				ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-				ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-				ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-				ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-				ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-			}
+			ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
+			ast_debug(1, "OSP: called '%s'\n", callednum);
+			ast_debug(1, "OSP: calling '%s'\n", callingnum);
+			ast_debug(1, "OSP: destination '%s'\n", destination);
+			ast_debug(1, "OSP: token size '%d'\n", tokenlen);
+
 			if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
 				res = 1;
 				break;
 			} else if (!result->numresults) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "OSP: No more destination\n");
+				ast_debug(1, "OSP: No more destination\n");
 				OSPPTransactionRecordFailure(result->outhandle, reason);
 				if (result->inhandle != OSP_INVALID_HANDLE) {
 					OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
@@ -1217,8 +1168,7 @@
 				break;
 			}
 		} else {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSP: Unable to get route, error '%d'\n", error);
+			ast_debug(1, "OSP: Unable to get route, error '%d'\n", error);
 			result->token[0] = '\0';
 			result->numresults = 0;
 			result->outtimelimit = OSP_DEF_TIMELIMIT;
@@ -1288,12 +1238,10 @@
 				&dummy,
 				NULL);
 	if (error == OSPC_ERR_NO_ERROR) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Usage reported\n");
+		ast_debug(1, "OSP: Usage reported\n");
 		res = 1;
 	} else {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: Unable to report usage, error '%d'\n", error);
+		ast_debug(1, "OSP: Unable to report usage, error '%d'\n", error);
 		res = -1;
 	}
 	OSPPTransactionDelete(handle);
@@ -1344,11 +1292,8 @@
 	if (!ast_strlen_zero(args.provider)) {
 		provider = args.provider;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPAuth: provider '%s'\n", provider);
+	ast_debug(1, "OSPAuth: provider '%s'\n", provider);
 
-	}
-
 	headp = &chan->varshead;
 	AST_LIST_TRAVERSE(headp, current, entries) {
 		if (!strcasecmp(ast_var_name(current), "OSPPEERIP")) {
@@ -1358,10 +1303,8 @@
 		}
 	}
 
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "OSPAuth: source '%s'\n", source);
-		ast_log(LOG_DEBUG, "OSPAuth: token size '%zd'\n", strlen(token));
-	}
+	ast_debug(1, "OSPAuth: source '%s'\n", source);
+	ast_debug(1, "OSPAuth: token size '%zd'\n", strlen(token));
 
 	if ((res = osp_auth(provider, &handle, source, chan->cid.cid_num, chan->exten, token, &timelimit)) > 0) {
 		status = AST_OSP_SUCCESS;
@@ -1376,15 +1319,12 @@
 
 	snprintf(buffer, sizeof(buffer), "%d", handle);
 	pbx_builtin_setvar_helper(chan, "OSPINHANDLE", buffer);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPAuth: OSPINHANDLE '%s'\n", buffer);
+	ast_debug(1, "OSPAuth: OSPINHANDLE '%s'\n", buffer);
 	snprintf(buffer, sizeof(buffer), "%d", timelimit);
 	pbx_builtin_setvar_helper(chan, "OSPINTIMELIMIT", buffer);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPAuth: OSPINTIMELIMIT '%s'\n", buffer);
+	ast_debug(1, "OSPAuth: OSPINTIMELIMIT '%s'\n", buffer);
 	pbx_builtin_setvar_helper(chan, "OSPAUTHSTATUS", status);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPAuth: %s\n", status);
+	ast_debug(1, "OSPAuth: %s\n", status);
 
 	if(res <= 0) {
 		res = -1;
@@ -1441,14 +1381,12 @@
 
 	AST_STANDARD_APP_ARGS(args, tmp);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: exten '%s'\n", args.exten);
+	ast_debug(1, "OSPLookup: exten '%s'\n", args.exten);
 
 	if (!ast_strlen_zero(args.provider)) {
 		provider = args.provider;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPlookup: provider '%s'\n", provider);
+	ast_debug(1, "OSPlookup: provider '%s'\n", provider);
 
 	if (args.options) {
 		if (strchr(args.options, 'h')) {
@@ -1461,9 +1399,7 @@
 			callidtypes |= OSP_CALLID_IAX;
 		}
 	}
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "OSPLookup: call id types '%d'\n", callidtypes);
-	}
+	ast_debug(1, "OSPLookup: call id types '%d'\n", callidtypes);
 
 	result.inhandle = OSP_INVALID_HANDLE;
 	result.intimelimit = OSP_DEF_TIMELIMIT;
@@ -1484,12 +1420,10 @@
 			srcdev = ast_var_value(current);
 		}
 	}
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "OSPLookup: OSPINHANDLE '%d'\n", result.inhandle);
-		ast_log(LOG_DEBUG, "OSPLookup: OSPINTIMELIMIT '%d'\n", result.intimelimit);
-		ast_log(LOG_DEBUG, "OSPLookup: OSPINNETWORKID '%s'\n", netid);
-		ast_log(LOG_DEBUG, "OSPLookup: source device '%s'\n", srcdev);
-	}
+	ast_debug(1, "OSPLookup: OSPINHANDLE '%d'\n", result.inhandle);
+	ast_debug(1, "OSPLookup: OSPINTIMELIMIT '%d'\n", result.intimelimit);
+	ast_debug(1, "OSPLookup: OSPINNETWORKID '%s'\n", netid);
+	ast_debug(1, "OSPLookup: source device '%s'\n", srcdev);
 
 	if ((cres = ast_autoservice_start(chan)) < 0) {
 		ast_module_user_remove(u);
@@ -1518,38 +1452,28 @@
 
 	snprintf(buffer, sizeof(buffer), "%d", result.outhandle);
 	pbx_builtin_setvar_helper(chan, "OSPOUTHANDLE", buffer);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPOUTHANDLE '%s'\n", buffer);
+	ast_debug(1, "OSPLookup: OSPOUTHANDLE '%s'\n", buffer);
 	pbx_builtin_setvar_helper(chan, "OSPTECH", result.tech);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPTECH '%s'\n", result.tech);
+	ast_debug(1, "OSPLookup: OSPTECH '%s'\n", result.tech);
 	pbx_builtin_setvar_helper(chan, "OSPDEST", result.dest);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPDEST '%s'\n", result.dest);
+	ast_debug(1, "OSPLookup: OSPDEST '%s'\n", result.dest);
 	pbx_builtin_setvar_helper(chan, "OSPCALLED", result.called);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPCALLED '%s'\n", result.called);
+	ast_debug(1, "OSPLookup: OSPCALLED '%s'\n", result.called);
 	pbx_builtin_setvar_helper(chan, "OSPCALLING", result.calling);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPCALLING '%s'\n", result.calling);
+	ast_debug(1, "OSPLookup: OSPCALLING '%s'\n", result.calling);
 	pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", result.token);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
+	ast_debug(1, "OSPLookup: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
 	snprintf(buffer, sizeof(buffer), "%d", result.numresults);
 	pbx_builtin_setvar_helper(chan, "OSPRESULTS", buffer);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPRESULTS '%s'\n", buffer);
+	ast_debug(1, "OSPLookup: OSPRESULTS '%s'\n", buffer);
 	snprintf(buffer, sizeof(buffer), "%d", result.outtimelimit);
 	pbx_builtin_setvar_helper(chan, "OSPOUTTIMELIMIT", buffer);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPOUTTIMELIMIT '%s'\n", buffer);
+	ast_debug(1, "OSPLookup: OSPOUTTIMELIMIT '%s'\n", buffer);
 	snprintf(buffer, sizeof(buffer), "%d", callidtypes);
 	pbx_builtin_setvar_helper(chan, "OSPOUTCALLIDTYPES", buffer);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: OSPOUTCALLIDTYPES '%s'\n", buffer);
+	ast_debug(1, "OSPLookup: OSPOUTCALLIDTYPES '%s'\n", buffer);
 	pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", status);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPLookup: %s\n", status);
+	ast_debug(1, "OSPLookup: %s\n", status);
 
 	if (!strcasecmp(result.tech, OSP_TECH_H323)) {
 		if ((callidtypes & OSP_CALLID_H323) && (result.outcallid.len != 0)) {
@@ -1566,8 +1490,7 @@
 		if (!ast_strlen_zero(result.token)) {
 			snprintf(buffer, sizeof(buffer), "%s%s", OSP_SIP_HEADER, result.token);
 			pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
+			ast_debug(1, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
 		}
 	} else if (!strcasecmp(result.tech, OSP_TECH_IAX)) {
 		snprintf(buffer, sizeof(buffer), "%s/%s/%s", result.tech, result.dest, result.called);
@@ -1636,14 +1559,12 @@
 	if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%d", &cause) != 1) {
 		cause = 0;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: cause '%d'\n", cause);
+	ast_debug(1, "OSPNext: cause '%d'\n", cause);
 
 	if (!ast_strlen_zero(args.provider)) {
 		provider = args.provider;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPlookup: provider '%s'\n", provider);
+	ast_debug(1, "OSPlookup: provider '%s'\n", provider);
 
 	result.inhandle = OSP_INVALID_HANDLE;
 	result.outhandle = OSP_INVALID_HANDLE;
@@ -1674,13 +1595,11 @@
 			}
 		}
 	}
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "OSPNext: OSPINHANDLE '%d'\n", result.inhandle);
-		ast_log(LOG_DEBUG, "OSPNext: OSPOUTHANDLE '%d'\n", result.outhandle);
-		ast_log(LOG_DEBUG, "OSPNext: OSPINTIMELIMIT '%d'\n", result.intimelimit);
-		ast_log(LOG_DEBUG, "OSPNext: OSPOUTCALLIDTYPES '%d'\n", callidtypes);
-		ast_log(LOG_DEBUG, "OSPNext: OSPRESULTS '%d'\n", result.numresults);
-	}
+	ast_debug(1, "OSPNext: OSPINHANDLE '%d'\n", result.inhandle);
+	ast_debug(1, "OSPNext: OSPOUTHANDLE '%d'\n", result.outhandle);
+	ast_debug(1, "OSPNext: OSPINTIMELIMIT '%d'\n", result.intimelimit);
+	ast_debug(1, "OSPNext: OSPOUTCALLIDTYPES '%d'\n", callidtypes);
+	ast_debug(1, "OSPNext: OSPRESULTS '%d'\n", result.numresults);
 
 	if ((res = osp_next(provider, cause, &result)) > 0) {
 		status = AST_OSP_SUCCESS;
@@ -1703,31 +1622,23 @@
 	}
 
 	pbx_builtin_setvar_helper(chan, "OSPTECH", result.tech);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: OSPTECH '%s'\n", result.tech);
+	ast_debug(1, "OSPNext: OSPTECH '%s'\n", result.tech);
 	pbx_builtin_setvar_helper(chan, "OSPDEST", result.dest);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: OSPDEST '%s'\n", result.dest);
+	ast_debug(1, "OSPNext: OSPDEST '%s'\n", result.dest);
 	pbx_builtin_setvar_helper(chan, "OSPCALLED", result.called);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: OSPCALLED'%s'\n", result.called);
+	ast_debug(1, "OSPNext: OSPCALLED'%s'\n", result.called);
 	pbx_builtin_setvar_helper(chan, "OSPCALLING", result.calling);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: OSPCALLING '%s'\n", result.calling);
+	ast_debug(1, "OSPNext: OSPCALLING '%s'\n", result.calling);
 	pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", result.token);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
+	ast_debug(1, "OSPNext: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
 	snprintf(buffer, sizeof(buffer), "%d", result.numresults);
 	pbx_builtin_setvar_helper(chan, "OSPRESULTS", buffer);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: OSPRESULTS '%s'\n", buffer);
+	ast_debug(1, "OSPNext: OSPRESULTS '%s'\n", buffer);
 	snprintf(buffer, sizeof(buffer), "%d", result.outtimelimit);
 	pbx_builtin_setvar_helper(chan, "OSPOUTTIMELIMIT", buffer);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: OSPOUTTIMELIMIT '%s'\n", buffer);
+	ast_debug(1, "OSPNext: OSPOUTTIMELIMIT '%s'\n", buffer);
 	pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", status);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPNext: %s\n", status);
+	ast_debug(1, "OSPNext: %s\n", status);
 
 	if (!strcasecmp(result.tech, OSP_TECH_H323)) {
 		if ((callidtypes & OSP_CALLID_H323) && (result.outcallid.len != 0)) {
@@ -1744,8 +1655,7 @@
 		if (!ast_strlen_zero(result.token)) {
 			snprintf(buffer, sizeof(buffer), "%s%s", OSP_SIP_HEADER, result.token);
 			pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
-			if (option_debug)
-				ast_log(LOG_DEBUG, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
+			ast_debug(1, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
 		}
 	} else if (!strcasecmp(result.tech, OSP_TECH_IAX)) {
 		snprintf(buffer, sizeof(buffer), "%s/%s/%s", result.tech, result.dest, result.called);
@@ -1822,17 +1732,14 @@
 			}
 		}
 	}
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "OSPFinish: OSPINHANDLE '%d'\n", inhandle);
-		ast_log(LOG_DEBUG, "OSPFinish: OSPOUTHANDLE '%d'\n", outhandle);
-		ast_log(LOG_DEBUG, "OSPFinish: recorded '%d'\n", recorded);
-	}
+	ast_debug(1, "OSPFinish: OSPINHANDLE '%d'\n", inhandle);
+	ast_debug(1, "OSPFinish: OSPOUTHANDLE '%d'\n", outhandle);
+	ast_debug(1, "OSPFinish: recorded '%d'\n", recorded);
 
 	if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%d", &cause) != 1) {
 		cause = 0;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSPFinish: cause '%d'\n", cause);
+	ast_debug(1, "OSPFinish: cause '%d'\n", cause);
 
 	if (chan->cdr) {
 		start = chan->cdr->start.tv_sec;
@@ -1847,17 +1754,14 @@
 		connect = 0;
 		end = 0;
 	}
-	if (option_debug) {
-		ast_log(LOG_DEBUG, "OSPFinish: start '%ld'\n", start);
-		ast_log(LOG_DEBUG, "OSPFinish: connect '%ld'\n", connect);
-		ast_log(LOG_DEBUG, "OSPFinish: end '%ld'\n", end);
-	}
+	ast_debug(1, "OSPFinish: start '%ld'\n", start);
+	ast_debug(1, "OSPFinish: connect '%ld'\n", connect);
+	ast_debug(1, "OSPFinish: end '%ld'\n", end);
 
 	release = chan->_softhangup ? 0 : 1;
 
 	if (osp_finish(outhandle, recorded, cause, start, connect, end, release) <= 0) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSPFinish: Unable to report usage for outbound call\n");
+		ast_debug(1, "OSPFinish: Unable to report usage for outbound call\n");
 	}
 	switch (cause) {
 		case AST_CAUSE_NORMAL_CLEARING:
@@ -1867,8 +1771,7 @@
 			break;
 	}
 	if (osp_finish(inhandle, recorded, cause, start, connect, end, release) <= 0) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSPFinish: Unable to report usage for inbound call\n");
+		ast_debug(1, "OSPFinish: Unable to report usage for inbound call\n");
 	}
 	snprintf(buffer, sizeof(buffer), "%d", OSP_INVALID_HANDLE);
 	pbx_builtin_setvar_helper(chan, "OSPOUTHANDLE", buffer);
@@ -1916,8 +1819,7 @@
 		} else {
 			OSPPInit(0);
 		}
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: osp_hardware '%d'\n", osp_hardware);
+		ast_debug(1, "OSP: osp_hardware '%d'\n", osp_hardware);
 
 		t = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "tokenformat");
 		if (t) {
@@ -1930,8 +1832,7 @@
 					TOKEN_ALGO_SIGNED, TOKEN_ALGO_UNSIGNED, TOKEN_ALGO_BOTH, t);
 			}
 		}
-		if (option_debug)
-			ast_log(LOG_DEBUG, "OSP: osp_tokenformat '%d'\n", osp_tokenformat);
+		ast_debug(1, "OSP: osp_tokenformat '%d'\n", osp_tokenformat);
 
 		t = ast_category_browse(cfg, NULL);
 		while(t) {
@@ -1948,8 +1849,7 @@
 		ast_log(LOG_WARNING, "OSP: Unable to find configuration. OSP support disabled\n");
 		return 0;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "OSP: osp_initialized '%d'\n", osp_initialized);
+	ast_debug(1, "OSP: osp_initialized '%d'\n", osp_initialized);
 
 	return 1;
 }
Index: app_zapbarge.c
===================================================================
--- app_zapbarge.c	(revision 69052)
+++ app_zapbarge.c	(working copy)
@@ -171,8 +171,7 @@
 	if (ztc.confmode) {
 		/* Whoa, already in a conference...  Retry... */
 		if (!retryzap) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
+			ast_debug(1, "Zap channel is in a conference already, retrying with pseudo\n");
 			retryzap = 1;
 			goto zapretry;
 		}
@@ -188,8 +187,7 @@
 		close(fd);
 		goto outrun;
 	}
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
+	ast_debug(1, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
 
 	for(;;) {
 		outfd = -1;
@@ -201,8 +199,7 @@
 					/* Kill old pseudo */
 					close(fd);
 				}
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
+				ast_debug(1, "Ooh, something swapped out under us, starting over\n");
 				retryzap = 0;
 				goto zapretry;
 			}
Index: app_zapscan.c
===================================================================
--- app_zapscan.c	(revision 69052)
+++ app_zapscan.c	(working copy)
@@ -179,8 +179,7 @@
         if (ztc.confmode) {
 			/* Whoa, already in a conference...  Retry... */
 			if (!retryzap) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
+				ast_debug(1, "Zap channel is in a conference already, retrying with pseudo\n");
 				retryzap = 1;
 				goto zapretry;
 			}
@@ -196,8 +195,7 @@
                 close(fd);
                 goto outrun;
         }
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
+	ast_debug(1, "Placed channel %s in ZAP channel %d monitor\n", chan->name, confno);
 		
         for(;;) {
 			outfd = -1;
@@ -209,8 +207,7 @@
 						/* Kill old pseudo */
 						close(fd);
 					}
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
+					ast_debug(1, "Ooh, something swapped out under us, starting over\n");
 					retryzap = 0;
                                 goto zapretry;
 				}
Index: app_amd.c
===================================================================
--- app_amd.c	(revision 69052)
+++ app_amd.c	(working copy)
@@ -159,8 +159,9 @@
 			maximumNumberOfWords = atoi(args.argMaximumNumberOfWords);
 		if (!ast_strlen_zero(args.argSilenceThreshold))
 			silenceThreshold = atoi(args.argSilenceThreshold);
-	} else if (option_debug)
-		ast_log(LOG_DEBUG, "AMD using the default parameters.\n");
+	} else {
+		ast_debug(1, "AMD using the default parameters.\n");
+	}
 
 	/* Now we're ready to roll! */
 	if (option_verbose > 2)
@@ -195,8 +196,7 @@
 		if (!(f = ast_read(chan))) {
 			if (option_verbose > 2)
 				ast_verbose(VERBOSE_PREFIX_3 "AMD: HANGUP\n");
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Got hangup\n");
+			ast_debug(1, "Got hangup\n");
 			strcpy(amdStatus, "HANGUP");
 			break;
 		}
Index: app_record.c
===================================================================
--- app_record.c	(revision 69052)
+++ app_record.c	(working copy)
@@ -336,8 +336,7 @@
 		ast_frfree(f);
 	}
 	if (!f) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Got hangup\n");
+		ast_debug(1, "Got hangup\n");
 		res = -1;
 	}
 			
Index: app_queue.c
===================================================================
--- app_queue.c	(revision 69052)
+++ app_queue.c	(working copy)
@@ -559,13 +559,11 @@
 	AST_LIST_UNLOCK(&interfaces);
 
 	if (!curint) {
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "Device '%s/%s' changed to state '%d' (%s) but we don't care because they're not a member of any queue.\n", technology, loc, sc->state, devstate2str(sc->state));
+		ast_debug(3, "Device '%s/%s' changed to state '%d' (%s) but we don't care because they're not a member of any queue.\n", technology, loc, sc->state, devstate2str(sc->state));
 		return NULL;
 	}
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Device '%s/%s' changed to state '%d' (%s)\n", technology, loc, sc->state, devstate2str(sc->state));
+	ast_debug(1, "Device '%s/%s' changed to state '%d' (%s)\n", technology, loc, sc->state, devstate2str(sc->state));
 	AST_LIST_LOCK(&queues);
 	AST_LIST_TRAVERSE(&queues, q, list) {
 		ast_mutex_lock(&q->lock);
@@ -772,8 +770,7 @@
 		return 0;
 	}
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Adding %s to the list of interfaces that make up all of our queue members.\n", interface);
+	ast_debug(1, "Adding %s to the list of interfaces that make up all of our queue members.\n", interface);
 	
 	if ((curint = ast_calloc(1, sizeof(*curint)))) {
 		ast_copy_string(curint->interface, interface, sizeof(curint->interface));
@@ -814,8 +811,7 @@
 	AST_LIST_TRAVERSE_SAFE_BEGIN(&interfaces, curint, list) {
 		if (!strcasecmp(curint->interface, interface)) {
 			if (!interface_exists_global(interface)) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Removing %s from the list of interfaces that make up all of our queue members.\n", interface);
+				ast_debug(1, "Removing %s from the list of interfaces that make up all of our queue members.\n", interface);
 				AST_LIST_REMOVE_CURRENT(&interfaces, list);
 				ast_free(curint);
 			}
@@ -894,7 +890,7 @@
 		q->announcefrequency = atoi(val);
 	} else if (!strcasecmp(param, "min-announce-frequency")) {
 		q->minannouncefrequency = atoi(val);
-		ast_log(LOG_DEBUG, "%s=%s for queue '%s'\n", param, val, q->name);
+		ast_debug(1, "%s=%s for queue '%s'\n", param, val, q->name);
 	} else if (!strcasecmp(param, "announce-round-seconds")) {
 		q->roundingseconds = atoi(val);
 		/* Rounding to any other values just doesn't make sense... */
@@ -1116,8 +1112,7 @@
 			/*! \note Hmm, can't seem to distinguish a DB failure from a not
 			   found condition... So we might delete an in-core queue
 			   in case of DB failure. */
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Queue %s not found in realtime.\n", queuename);
+			ast_debug(1, "Queue %s not found in realtime.\n", queuename);
 
 			q->dead = 1;
 			/* Delete if unused (else will be deleted when last caller leaves). */
@@ -1299,8 +1294,7 @@
 			S_OR(qe->chan->cid.cid_num, "unknown"), /* XXX somewhere else it is <unknown> */
 			S_OR(qe->chan->cid.cid_name, "unknown"),
 			q->name, qe->pos, q->count, qe->chan->uniqueid );
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
+		ast_debug(1, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
 	}
 	ast_mutex_unlock(&q->lock);
 	AST_LIST_UNLOCK(&queues);
@@ -1497,8 +1491,7 @@
 			manager_event(EVENT_FLAG_CALL, "Leave",
 				"Channel: %s\r\nQueue: %s\r\nCount: %d\r\nUniqueid: %s\r\n",
 				qe->chan->name, q->name,  q->count, qe->chan->uniqueid);
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Queue '%s' Leave, Channel '%s'\n", q->name, qe->chan->name );
+			ast_debug(1, "Queue '%s' Leave, Channel '%s'\n", q->name, qe->chan->name );
 			/* Take us out of the queue */
 			if (prev)
 				prev->next = cur->next;
@@ -1599,11 +1592,9 @@
 				if (strcmp(mem->interface, member->interface))
 					continue;
 
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Found matching member %s in queue '%s'\n", mem->interface, q->name);
+				ast_debug(1, "Found matching member %s in queue '%s'\n", mem->interface, q->name);
 				if (q->weight > rq->weight) {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Queue '%s' (weight %d, calls %d) is preferred over '%s' (weight %d, calls %d)\n", q->name, q->weight, q->count, rq->name, rq->weight, rq->count);
+					ast_debug(1, "Queue '%s' (weight %d, calls %d) is preferred over '%s' (weight %d, calls %d)\n", q->name, q->weight, q->count, rq->name, rq->weight, rq->count);
 					found = 1;
 					break;
 				}
@@ -1670,8 +1661,7 @@
 
 	/* on entry here, we know that tmp->chan == NULL */
 	if (qe->parent->wrapuptime && (time(NULL) - tmp->lastcall < qe->parent->wrapuptime)) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Wrapuptime not yet expired for %s\n", tmp->interface);
+		ast_debug(1, "Wrapuptime not yet expired for %s\n", tmp->interface);
 		if (qe->chan->cdr)
 			ast_cdr_busy(qe->chan->cdr);
 		tmp->stillgoing = 0;
@@ -1680,8 +1670,7 @@
 	}
 
 	if (!qe->parent->ringinuse && (tmp->member->status != AST_DEVICE_NOT_INUSE) && (tmp->member->status != AST_DEVICE_UNKNOWN)) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "%s in use, can't receive call\n", tmp->interface);
+		ast_debug(1, "%s in use, can't receive call\n", tmp->interface);
 		if (qe->chan->cdr)
 			ast_cdr_busy(qe->chan->cdr);
 		tmp->stillgoing = 0;
@@ -1689,16 +1678,14 @@
 	}
 
 	if (tmp->member->paused) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "%s paused, can't receive call\n", tmp->interface);
+		ast_debug(1, "%s paused, can't receive call\n", tmp->interface);
 		if (qe->chan->cdr)
 			ast_cdr_busy(qe->chan->cdr);
 		tmp->stillgoing = 0;
 		return 0;
 	}
 	if (use_weight && compare_weight(qe->parent,tmp->member)) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Priority queue delaying call to %s:%s\n", qe->parent->name, tmp->interface);
+		ast_debug(1, "Priority queue delaying call to %s:%s\n", qe->parent->name, tmp->interface);
 		if (qe->chan->cdr)
 			ast_cdr_busy(qe->chan->cdr);
 		tmp->stillgoing = 0;
@@ -1751,8 +1738,7 @@
 	/* Place the call, but don't wait on the answer */
 	if ((res = ast_call(tmp->chan, location, 0))) {
 		/* Again, keep going even if there's an error */
-		if (option_debug)
-			ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
+		ast_debug(1, "ast call on peer returned %d\n", res);
 		if (option_verbose > 2)
 			ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", tmp->interface);
 		do_hang(tmp);
@@ -1805,8 +1791,7 @@
 	while (ret == 0) {
 		struct callattempt *best = find_best(outgoing);
 		if (!best) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Nobody left to try ringing in queue\n");
+			ast_debug(1, "Nobody left to try ringing in queue\n");
 			break;
 		}
 		if (qe->parent->strategy == QUEUE_STRATEGY_RINGALL) {
@@ -1814,15 +1799,13 @@
 			/* Ring everyone who shares this best metric (for ringall) */
 			for (cur = outgoing; cur; cur = cur->q_next) {
 				if (cur->stillgoing && !cur->chan && cur->metric <= best->metric) {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "(Parallel) Trying '%s' with metric %d\n", cur->interface, cur->metric);
+					ast_debug(1, "(Parallel) Trying '%s' with metric %d\n", cur->interface, cur->metric);
 					ring_entry(qe, cur, busies);
 				}
 			}
 		} else {
 			/* Ring just the best channel */
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Trying '%s' with metric %d\n", best->interface, best->metric);
+			ast_debug(1, "Trying '%s' with metric %d\n", best->interface, best->metric);
 			ring_entry(qe, best, busies);
 		}
 		if (best->chan) /* break out with result = 1 */
@@ -1838,8 +1821,7 @@
 
 	if (best) {
 		/* Ring just the best channel */
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Next is '%s' with metric %d\n", best->interface, best->metric);
+		ast_debug(1, "Next is '%s' with metric %d\n", best->interface, best->metric);
 		qe->parent->rrpos = best->metric % 1000;
 	} else {
 		/* Just increment rrpos */
@@ -1997,8 +1979,7 @@
 		}
 		if (pos == 1 /* not found */) {
 			if (numlines == (numbusies + numnochan)) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Everyone is busy at this time\n");
+				ast_debug(1, "Everyone is busy at this time\n");
 			} else {
 				ast_log(LOG_NOTICE, "No one is answering queue '%s' (%d/%d/%d)\n", queue, numlines, numbusies, numnochan);
 			}
@@ -2139,8 +2120,7 @@
 							/* Ignore going off hook */
 							break;
 						default:
-							if (option_debug)
-								ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
+							ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
 						}
 					}
 					ast_frfree(f);
@@ -2202,12 +2182,10 @@
 		ch = qe->parent->head;
 		/* If we are now at the top of the head, break out */
 		if (ch == qe) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
+			ast_debug(1, "It's our turn (%s).\n", qe->chan->name);
 			res = 1;
 		} else {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "It's not our turn (%s).\n", qe->chan->name);
+			ast_debug(1, "It's not our turn (%s).\n", qe->chan->name);
 			res = 0;
 		}	
 
@@ -2218,8 +2196,7 @@
 		ch = qe->parent->head;
 	
 		if (qe->parent->strategy == QUEUE_STRATEGY_RINGALL) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Even though there are %d available members, the strategy is ringall so only the head call is allowed in\n", avl);
+			ast_debug(1, "Even though there are %d available members, the strategy is ringall so only the head call is allowed in\n", avl);
 			avl = 1;
 		} else {
 			for (cur = qe->parent->members; cur; cur = cur->next) {
@@ -2232,8 +2209,7 @@
 			}
 		}
 
-		if (option_debug)
-			ast_log(LOG_DEBUG, "There are %d available members.\n", avl);
+		ast_debug(1, "There are %d available members.\n", avl);
 	
 		while ((idx < avl) && (ch) && (ch != qe)) {
 			idx++;
@@ -2242,12 +2218,10 @@
 	
 		/* If the queue entry is within avl [the number of available members] calls from the top ... */
 		if (ch && idx < avl) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
+			ast_debug(1, "It's our turn (%s).\n", qe->chan->name);
 			res = 1;
 		} else {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "It's not our turn (%s).\n", qe->chan->name);
+			ast_debug(1, "It's not our turn (%s).\n", qe->chan->name);
 			res = 0;
 		}
 		
@@ -2497,8 +2471,7 @@
 	if (use_weight)
 		AST_LIST_LOCK(&queues);
 	ast_mutex_lock(&qe->parent->lock);
-	if (option_debug)
-		ast_log(LOG_DEBUG, "%s is trying to call a queue member.\n",
+	ast_debug(1, "%s is trying to call a queue member.\n",
 							qe->chan->name);
 	ast_copy_string(queuename, qe->parent->name, sizeof(queuename));
 	cur = qe->parent->members;
@@ -2558,8 +2531,7 @@
 		} else {
 			res = digit;
 		}
-		if (option_debug)
-			ast_log(LOG_DEBUG, "%s: Nobody answered.\n", qe->chan->name);
+		ast_debug(1, "%s: Nobody answered.\n", qe->chan->name);
 	} else { /* peer is valid */
 		/* Ah ha!  Someone answered within the desired timeframe.  Of course after this
 		   we will always return with -1 so that it is hung up properly after the
@@ -2646,8 +2618,7 @@
 		/* Begin Monitoring */
 		if (qe->parent->monfmt && *qe->parent->monfmt) {
 			if (!qe->parent->montype) {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Starting Monitor as requested.\n");
+				ast_debug(1, "Starting Monitor as requested.\n");
 				monitorfilename = pbx_builtin_getvar_helper(qe->chan, "MONITOR_FILENAME");
 				if (pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC") || pbx_builtin_getvar_helper(qe->chan, "MONITOR_EXEC_ARGS"))
 					which = qe->chan;
@@ -2665,8 +2636,7 @@
 				if (qe->parent->monjoin)
 					ast_monitor_setjoinfiles(which, 1);
 			} else {
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Starting MixMonitor as requested.\n");
+				ast_debug(1, "Starting MixMonitor as requested.\n");
 				monitorfilename = pbx_builtin_getvar_helper(qe->chan, "MONITOR_FILENAME");
 				if (!monitorfilename) {
 					if (qe->chan->cdr)
@@ -2723,8 +2693,7 @@
 					else
 						snprintf(mixmonargs, sizeof(mixmonargs)-1, "%s|b%s", tmpid2, monitor_options);
 						
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Arguments being passed to MixMonitor: %s\n", mixmonargs);
+					ast_debug(1, "Arguments being passed to MixMonitor: %s\n", mixmonargs);
 
 					ret = pbx_exec(qe->chan, mixmonapp, mixmonargs);
 
@@ -2736,8 +2705,7 @@
 		/* Drop out of the queue at this point, to prepare for next caller */
 		leave_queue(qe);			
 		if (!ast_strlen_zero(url) && ast_channel_supports_html(peer)) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "app_queue: sendurl=%s.\n", url);
+			ast_debug(1, "app_queue: sendurl=%s.\n", url);
 			ast_channel_sendurl(peer, url);
 		}
 		
@@ -2772,8 +2740,7 @@
 		}
 
 		if (!ast_strlen_zero(macroexec)) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "app_queue: macro=%s.\n", macroexec);
+			ast_debug(1, "app_queue: macro=%s.\n", macroexec);
 			
 			res = ast_autoservice_start(qe->chan);
 			if (res) {
@@ -2785,8 +2752,7 @@
 			
 			if (app) {
 				res = pbx_exec(qe->chan, app, macroexec);
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Macro exited with status %d\n", res);
+				ast_debug(1, "Macro exited with status %d\n", res);
 				res = 0;
 			} else {
 				ast_log(LOG_ERROR, "Could not find application Macro\n");
@@ -2800,8 +2766,7 @@
 		}
 
 		if (!ast_strlen_zero(agi)) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "app_queue: agi=%s.\n", agi);
+			ast_debug(1, "app_queue: agi=%s.\n", agi);
 			app = pbx_findapp("agi");
 			if (app) {
 				agiexec = ast_strdupa(agi);
@@ -3041,8 +3006,7 @@
 			if ((mem = interface_exists(q, interface))) {
 				found++;
 				if (mem->paused == paused) {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "%spausing already-%spaused queue member %s:%s\n", (paused ? "" : "un"), (paused ? "" : "un"), q->name, interface);
+					ast_debug(1, "%spausing already-%spaused queue member %s:%s\n", (paused ? "" : "un"), (paused ? "" : "un"), q->name, interface);
 				}
 				mem->paused = paused;
 
@@ -3145,8 +3109,7 @@
 			if (ast_strlen_zero(membername))
 				membername = interface;
 
-			if (option_debug)
-				ast_log(LOG_DEBUG, "Reload Members: Queue: %s  Member: %s  Name: %s  Penalty: %d  Paused: %d\n", queue_name, interface, membername, penalty, paused);
+			ast_debug(1, "Reload Members: Queue: %s  Member: %s  Name: %s  Penalty: %d  Paused: %d\n", queue_name, interface, membername, penalty, paused);
 			
 			if (add_to_queue(queue_name, interface, membername, penalty, paused, 0) == RES_OUTOFMEMORY) {
 				ast_log(LOG_ERROR, "Out of Memory when reloading persistent queue member\n");
@@ -3289,8 +3252,7 @@
 		res = 0;
 		break;
 	case RES_EXISTS:
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Unable to remove interface '%s' from queue '%s': Not there\n", args.interface, args.queuename);
+		ast_debug(1, "Unable to remove interface '%s' from queue '%s': Not there\n", args.interface, args.queuename);
 		pbx_builtin_setvar_helper(chan, "RQMSTATUS", "NOTINQUEUE");
 		res = 0;
 		break;
@@ -3468,26 +3430,21 @@
 	user_priority = pbx_builtin_getvar_helper(chan, "QUEUE_PRIO");
 	if (user_priority) {
 		if (sscanf(user_priority, "%d", &prio) == 1) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "%s: Got priority %d from ${QUEUE_PRIO}.\n",
-					chan->name, prio);
+			ast_debug(1, "%s: Got priority %d from ${QUEUE_PRIO}.\n", chan->name, prio);
 		} else {
 			ast_log(LOG_WARNING, "${QUEUE_PRIO}: Invalid value (%s), channel %s.\n",
 				user_priority, chan->name);
 			prio = 0;
 		}
 	} else {
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "NO QUEUE_PRIO variable found. Using default.\n");
+		ast_debug(3, "NO QUEUE_PRIO variable found. Using default.\n");
 		prio = 0;
 	}
 
 	/* Get the maximum penalty from the variable ${QUEUE_MAX_PENALTY} */
 	if ((max_penalty_str = pbx_builtin_getvar_helper(chan, "QUEUE_MAX_PENALTY"))) {
 		if (sscanf(max_penalty_str, "%d", &max_penalty) == 1) {
-			if (option_debug)
-				ast_log(LOG_DEBUG, "%s: Got max penalty %d from ${QUEUE_MAX_PENALTY}.\n",
-					chan->name, max_penalty);
+			ast_debug(1, "%s: Got max penalty %d from ${QUEUE_MAX_PENALTY}.\n", chan->name, max_penalty);
 		} else {
 			ast_log(LOG_WARNING, "${QUEUE_MAX_PENALTY}: Invalid value (%s), channel %s.\n",
 				max_penalty_str, chan->name);
@@ -3503,9 +3460,8 @@
 	if (args.options && (strchr(args.options, 'c')))
 		qcontinue = 1;
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "queue: %s, options: %s, url: %s, announce: %s, expires: %ld, priority: %d\n",
-			args.queuename, args.options, args.url, args.announceoverride, (long)qe.expire, prio);
+	ast_debug(1, "queue: %s, options: %s, url: %s, announce: %s, expires: %ld, priority: %d\n",
+		args.queuename, args.options, args.url, args.announceoverride, (long)qe.expire, prio);
 
 	qe.chan = chan;
 	qe.prio = prio;
@@ -3673,9 +3629,7 @@
 				 * of the queue, go and check for our turn again.
 				 */
 				if (!is_our_turn(&qe)) {
-					if (option_debug)
-						ast_log(LOG_DEBUG, "Darn priorities, going back in queue (%s)!\n",
-							qe.chan->name);
+					ast_debug(1, "Darn priorities, going back in queue (%s)!\n", qe.chan->name);
 					goto check_turns;
 				}
 			}
@@ -4093,7 +4047,7 @@
 			if (!q->count)
 				destroy_queue(q);
 			else
-				ast_log(LOG_DEBUG, "XXX Leaking a little memory :( XXX\n");
+				ast_debug(1, "XXX Leaking a little memory :( XXX\n");
 		} else {
 			ast_mutex_lock(&q->lock);
 			for (cur = q->members; cur; cur = cur->next)
Index: app_followme.c
===================================================================
--- app_followme.c	(revision 69052)
+++ app_followme.c	(working copy)
@@ -269,8 +269,7 @@
 		*tmp = '\0';
 	ast_copy_string(cur->number, number, sizeof(cur->number));
 	cur->order = numorder;
-	if (option_debug)
-		ast_log(LOG_DEBUG, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
+	ast_debug(1, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
 
 	return cur;
 }
@@ -356,8 +355,7 @@
 			if (!strcasecmp(f->name, cat))
 				break;
 		}
-		if (option_debug)
-			ast_log(LOG_DEBUG, "New profile %s.\n", cat);
+		ast_debug(1, "New profile %s.\n", cat);
 		if (!f) {
 			/* Make one then */
 			f = alloc_profile(cat);
@@ -406,8 +404,7 @@
 					AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
 				} else {
 					profile_set_param(f, var->name, var->value, var->lineno, 1);
-					if (option_debug > 1)
-						ast_log(LOG_DEBUG, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
+					ast_debug(2, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
 				}
 				var = var->next;
 			} /* End while(var) loop */
@@ -688,8 +685,7 @@
 								ast_verbose( VERBOSE_PREFIX_3 "%s stopped sounds\n", winner->name);
 							break;
 						default:
-							if (option_debug)
-								ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
+							ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
 							break;
 						}
 					} 
@@ -697,24 +693,19 @@
 						if (winner->stream)
 							ast_stopstream(winner);
 						tmpuser->digts = 0;
-						if (option_debug)
-							ast_log(LOG_DEBUG, "DTMF received: %c\n",(char) f->subclass);
+						ast_debug(1, "DTMF received: %c\n",(char) f->subclass);
 						tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
 						tmpuser->ynidx++;
-						if (option_debug)
-							ast_log(LOG_DEBUG, "DTMF string: %s\n", tmpuser->yn);
+						ast_debug(1, "DTMF string: %s\n", tmpuser->yn);
 						if (tmpuser->ynidx >= ynlongest) {
-							if (option_debug)
-								ast_log(LOG_DEBUG, "reached longest possible match - doing evals\n");
+							ast_debug(1, "reached longest possible match - doing evals\n");
 							if (!strcmp(tmpuser->yn, tpargs->takecall)) {
-								if (option_debug)
-									ast_log(LOG_DEBUG, "Match to take the call!\n");
+								ast_debug(1, "Match to take the call!\n");
 								ast_frfree(f);
 								return tmpuser->ochan;	
 							}
 							if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
-								if (option_debug)
-									ast_log(LOG_DEBUG, "Next in dial plan step requested.\n");
+								ast_debug(1, "Next in dial plan step requested.\n");
 								*status = 1;
 								ast_frfree(f);
 								return NULL;
@@ -726,8 +717,7 @@
 					ast_frfree(f);
 				} else {
 					if (winner) {
-						if (option_debug)
-							ast_log(LOG_DEBUG, "we didn't get a frame. hanging up. dg is %d\n",dg);					      
+						ast_debug(1, "we didn't get a frame. hanging up. dg is %d\n",dg);					      
 						if (!dg) {
 							clear_calling_tree(findme_user_list);
 							return NULL;
@@ -735,8 +725,7 @@
 							tmpuser->state = -1;
 						 	ast_hangup(winner);  
 							livechannels--;
-							if (option_debug)
-								ast_log(LOG_DEBUG, "live channels left %d\n", livechannels);
+							ast_debug(1, "live channels left %d\n", livechannels);
 							if (!livechannels) {
 								if (option_verbose > 2)
 									ast_verbose(VERBOSE_PREFIX_3 "no live channels left. exiting.\n");
@@ -747,8 +736,7 @@
 				}					
 				
 			} else
-				if (option_debug)
-					ast_log(LOG_DEBUG, "timed out waiting for action\n");
+				ast_debug(1, "timed out waiting for action\n");
 		}
 		
 	} else {
@@ -793,13 +781,11 @@
 
 	while (nm) {
 
-		if (option_debug > 1)	
-			ast_log(LOG_DEBUG, "Number %s timeout %ld\n", nm->number,nm->timeout);
+		ast_debug(2, "Number %s timeout %ld\n", nm->number,nm->timeout);
 		time(&start_time);
 
 		number = ast_strdupa(nm->number);
-		if (option_debug > 2)
-			ast_log(LOG_DEBUG, "examining %s\n", number);
+		ast_debug(3, "examining %s\n", number);
 		do {
 			rest = strchr(number, '&');
 			if (rest) {
@@ -948,8 +934,7 @@
 	}
 	AST_LIST_UNLOCK(&followmes);
 
-	if (option_debug)
-		ast_log(LOG_DEBUG, "New profile %s.\n", args.followmeid);
+	ast_debug(1, "New profile %s.\n", args.followmeid);
 	if (!f) { 
 		ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.\n", args.followmeid);
 		res = 0;
