Index: chan_sip.c
===================================================================
--- chan_sip.c	(revision 19128)
+++ chan_sip.c	(working copy)
@@ -1443,7 +1443,8 @@
 			((ast_test_flag(cur, FLAG_RESPONSE)) || 
 			 (!strncasecmp(msg, cur->data, strlen(msg)) && (cur->data[strlen(msg)] < 33)))) {
 			if (!resp && (seqno == p->pendinginvite)) {
-				ast_log(LOG_DEBUG, "Acked pending invite %d\n", p->pendinginvite);
+				if (sipdebug)
+					ast_log(LOG_DEBUG, "Acked pending invite %d\n", p->pendinginvite);
 				p->pendinginvite = 0;
 			}
 			/* this is our baby */
@@ -2486,7 +2487,8 @@
 			
 		case AST_CAUSE_NOTDEFINED:
 		default:
-			ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
+			if (option_debug || sipdebug)
+				ast_log(LOG_DEBUG, "AST hangup cause %d (no match found in SIP)\n", cause);
 			return NULL;
 	}
 
@@ -2504,7 +2506,8 @@
 	struct ast_flags locflags = {0};
 
 	if (!p) {
-		ast_log(LOG_DEBUG, "Asked to hangup channel that was not connected\n");
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Asked to hangup channel that was not connected\n");
 		return 0;
 	}
 	if (option_debug && sipdebug)
@@ -3751,7 +3754,8 @@
 	if (!(p->owner->nativeformats & p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
 		const unsigned slen=512;
 		char s1[slen], s2[slen];
-		ast_log(LOG_DEBUG, "Oooh, we need to change our formats since our peer supports only %s and not %s\n", 
+		if (option_debug && sipdebug)
+			ast_log(LOG_DEBUG, "Oooh, we need to change our formats since our peer supports only %s and not %s\n", 
 				ast_getformatname_multiple(s1, slen, p->jointcapability),
 				ast_getformatname_multiple(s2, slen, p->owner->nativeformats));
 		p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability);
@@ -5553,7 +5557,8 @@
 			ast_sched_del(sched, r->timeout);
 		}
 		r->timeout = ast_sched_add(sched, global_reg_timeout * 1000, sip_reg_timeout, r);
-		ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id  #%d \n", r->hostname, r->timeout);
+		if (sipdebug)
+			ast_log(LOG_DEBUG, "Scheduled a registration timeout for %s id  #%d \n", r->hostname, r->timeout);
 	}
 
 	if (strchr(r->username, '@')) {
@@ -6023,7 +6028,7 @@
 
 		if (option_verbose > 2)
 			ast_verbose(VERBOSE_PREFIX_3 "Unregistered SIP '%s'\n", p->name);
-			manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\n", p->name);
+		manager_event(EVENT_FLAG_SYSTEM, "PeerStatus", "Peer: SIP/%s\r\nPeerStatus: Unregistered\r\n", p->name);
 		return PARSE_REGISTER_UPDATE;
 	}
 	ast_copy_string(p->fullcontact, c, sizeof(p->fullcontact));
@@ -6140,7 +6145,8 @@
 
 	/* Once a persistant route is set, don't fool with it */
 	if (p->route && p->route_persistant) {
-		ast_log(LOG_DEBUG, "build_route: Retaining previous route: <%s>\n", p->route->hop);
+		if (sipdebug && option_debug)
+			ast_log(LOG_DEBUG, "build_route: Retaining previous route: <%s>\n", p->route->hop);
 		return;
 	}
 
@@ -6172,7 +6178,8 @@
 			if ((thishop = ast_malloc(sizeof(*thishop) + len))) {
 				/* ast_calloc is not needed because all fields are initialized in this block */
 				ast_copy_string(thishop->hop, rr, len);
-				ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
+				if (sipdebug && option_debug)
+					ast_log(LOG_DEBUG, "build_route: Record-Route hop: <%s>\n", thishop->hop);
 				/* Link in */
 				if (backwards) {
 					/* Link in at head so they end up in reverse order */
@@ -6200,7 +6207,8 @@
 		/* Can be multiple Contact headers, comma separated values - we just take the first */
 		contact = get_header(req, "Contact");
 		if (!ast_strlen_zero(contact)) {
-			ast_log(LOG_DEBUG, "build_route: Contact hop: %s\n", contact);
+			if (sipdebug && option_debug)
+				ast_log(LOG_DEBUG, "build_route: Contact hop: %s\n", contact);
 			/* Look for <: delimited address */
 			c = strchr(contact, '<');
 			if (c) {
@@ -6407,7 +6415,8 @@
 		if (p->autokillid > -1)
 			sip_cancel_destroy(p);	/* Remove subscription expiry for renewals */
 		sip_scheddestroy(p, 15000);	/* Delete subscription in 15 secs */
-		ast_verbose(VERBOSE_PREFIX_2 "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
+		if (option_debug > 1)
+			ast_verbose(VERBOSE_PREFIX_2 "Extension state: Watcher for hint %s %s. Notify User %s\n", exten, state == AST_EXTENSION_DEACTIVATED ? "deactivated" : "removed", p->username);
 		p->stateid = -1;
 		p->subscribed = NONE;
 		append_history(p, "Subscribestatus", "%s", state == AST_EXTENSION_REMOVED ? "HintRemoved" : "Deactivated");
@@ -6659,7 +6668,8 @@
 		domain_context[0] = '\0';
 		if (!check_sip_domain(p->domain, domain_context, sizeof(domain_context))) {
 			if (!allow_external_domains && (req->method == SIP_INVITE || req->method == SIP_REFER)) {
-				ast_log(LOG_DEBUG, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
+				if (sipdebug && option_debug)
+					ast_log(LOG_DEBUG, "Got SIP %s to non-local domain '%s'; refusing request.\n", sip_methods[req->method].text, p->domain);
 				return -2;
 			}
 		}
@@ -6811,7 +6821,8 @@
 	}
 	if (!ast_strlen_zero(replace_callid)) {	
 		/* This is a supervised transfer */
-		ast_log(LOG_DEBUG,"Assigning Replace-Call-ID Info %s to REPLACE_CALL_ID\n",replace_callid);
+		if (option_debug > 2)
+			ast_log(LOG_DEBUG,"Assigning Replace-Call-ID Info %s to REPLACE_CALL_ID\n",replace_callid);
 		
 		ast_string_field_free(sip_pvt, refer_to);
 		ast_string_field_free(sip_pvt, referred_by);
@@ -6833,10 +6844,12 @@
 	} else if (ast_exists_extension(NULL, sip_pvt->context, refer_to, 1, NULL) || !strcmp(refer_to, ast_parking_ext())) {
 		/* This is an unsupervised transfer (blind transfer) */
 		
-		ast_log(LOG_DEBUG,"Unsupervised transfer to (Refer-To): %s\n", refer_to);
-		if (referred_by)
-			ast_log(LOG_DEBUG,"Transferred by  (Referred-by: ) %s \n", referred_by);
-		ast_log(LOG_DEBUG,"Transfer Contact Info %s (REFER_CONTACT)\n", h_contact);
+		if (option_debug > 2) {
+			ast_log(LOG_DEBUG,"Unsupervised transfer to (Refer-To): %s\n", refer_to);
+			if (referred_by)
+				ast_log(LOG_DEBUG,"Transferred by  (Referred-by: ) %s \n", referred_by);
+			ast_log(LOG_DEBUG,"Transfer Contact Info %s (REFER_CONTACT)\n", h_contact);
+		}
 		ast_string_field_set(sip_pvt, refer_to, refer_to);
 		if (referred_by)
 			ast_string_field_set(sip_pvt, referred_by, referred_by);
@@ -7238,11 +7251,13 @@
 			}
 			usenatroute = ast_test_flag(&p->flags[0], SIP_NAT_ROUTE);
 			if (p->rtp) {
-				ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", usenatroute);
+				if (option_debug)
+					ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", usenatroute);
 				ast_rtp_setnat(p->rtp, usenatroute);
 			}
 			if (p->vrtp) {
-				ast_log(LOG_DEBUG, "Setting NAT on VRTP to %d\n", usenatroute);
+				if (option_debug)
+					ast_log(LOG_DEBUG, "Setting NAT on VRTP to %d\n", usenatroute);
 				ast_rtp_setnat(p->vrtp, usenatroute);
 			}
 			ast_string_field_set(p, peersecret, peer->secret);
@@ -9518,7 +9533,8 @@
 		e = strchr(s, '/');
 		if (e)
 			*e = '\0';
-		ast_log(LOG_DEBUG, "Found promiscuous redirection to 'SIP/%s'\n", s);
+		if (option_debug > 1)
+			ast_log(LOG_DEBUG, "Found promiscuous redirection to 'SIP/%s'\n", s);
 		if (p->owner)
 			ast_string_field_build(p->owner, call_forward, "SIP/%s", s);
 	} else {
@@ -9576,7 +9592,8 @@
 	}
 
 	if (ast_test_flag(&p->flags[0], SIP_ALREADYGONE)) { /* This call is already gone */
-		ast_log(LOG_DEBUG, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
+		if (option_debug > 3)
+			ast_log(LOG_DEBUG, "Got response on call that is already terminated: %s (ignoring)\n", p->callid);
 		return;
 	}
 
@@ -9816,9 +9833,11 @@
 		r->regstate = REG_STATE_REGISTERED;
 		manager_event(EVENT_FLAG_SYSTEM, "Registry", "Channel: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
 		r->regattempts = 0;
-		ast_log(LOG_DEBUG, "Registration successful\n");
+		if (option_debug > 2)
+			ast_log(LOG_DEBUG, "Registration successful\n");
 		if (r->timeout > -1) {
-			ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
+			if (option_debug > 2)
+				ast_log(LOG_DEBUG, "Cancelling timeout %d\n", r->timeout);
 			ast_sched_del(sched, r->timeout);
 		}
 		r->timeout=-1;
@@ -10073,7 +10092,8 @@
 			if (sipmethod == SIP_INVITE)
 				handle_response_invite(p, resp, rest, req, ignore, seqno);
 			else {
-				ast_log(LOG_DEBUG, "Got 491 on %s, unspported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
+				if (option_debug > 2)
+					ast_log(LOG_DEBUG, "Got 491 on %s, unsupported. Call ID %s\n", sip_methods[sipmethod].text, p->callid);
 				ast_set_flag(&p->flags[0], SIP_NEEDDESTROY);	
 			}
 			break;
@@ -10128,7 +10148,8 @@
 					is yet another failure of not having a layer 2 (again, YAY
 					 IETF for thinking ahead).  So we treat this as a call
 					 forward and hope we end up at the right place... */
-					ast_log(LOG_DEBUG, "Hairpin detected, setting up call forward for what it's worth\n");
+					if (option_debug > 2)
+						ast_log(LOG_DEBUG, "Hairpin detected, setting up call forward for what it's worth\n");
 					if (p->owner)
 						ast_string_field_build(p->owner, call_forward,
 								       "Local/%s@%s", p->username, p->context);
@@ -10194,7 +10215,8 @@
 			if (sipmethod == SIP_INVITE) {
 				handle_response_invite(p, resp, rest, req, ignore, seqno);
 			} else if (sipmethod == SIP_CANCEL) {
-				ast_log(LOG_DEBUG, "Got 200 OK on CANCEL\n");
+				if (option_debug && sipdebug)
+					ast_log(LOG_DEBUG, "Got 200 OK on CANCEL\n");
 				/* Wait for 487, then destroy */
 			} else if (sipmethod == SIP_MESSAGE)
 				/* We successfully transmitted a message */
@@ -10690,7 +10712,8 @@
 				}
 			} else {
 				p->jointcapability = p->capability;
-				ast_log(LOG_DEBUG, "Hm....  No sdp for the moment\n");
+				if (option_debug > 3)
+					ast_log(LOG_DEBUG, "Hm....  No sdp for the moment\n");
 			}
 			if (recordhistory) /* This is a response, note what it was for */
 				append_history(p, "%s", "Re-invite received");
@@ -10905,7 +10928,8 @@
 		int nobye = 0;
 		if (!ignore) {
 			if (p->refer_call) {
-				ast_log(LOG_DEBUG,"202 Accepted (supervised)\n");
+				if (option_debug > 2)
+					ast_log(LOG_DEBUG,"202 Accepted (supervised)\n");
 				attempt_transfer(p, p->refer_call);
 				if (p->refer_call->owner)
 					ast_mutex_unlock(&p->refer_call->owner->lock);
@@ -10913,12 +10937,14 @@
 				p->refer_call = NULL;
 				ast_set_flag(&p->flags[0], SIP_GOTREFER);	
 			} else {
-				ast_log(LOG_DEBUG,"202 Accepted (blind)\n");
+				if (option_debug > 2)
+					ast_log(LOG_DEBUG,"202 Accepted (blind)\n");
 				c = p->owner;
 				if (c) {
 					transfer_to = ast_bridged_channel(c);
 					if (transfer_to) {
-						ast_log(LOG_DEBUG, "Got SIP blind transfer, applying to '%s'\n", transfer_to->name);
+						if (option_debug > 2)
+							ast_log(LOG_DEBUG, "Got SIP blind transfer, applying to '%s'\n", transfer_to->name);
 						ast_moh_stop(transfer_to);
 						if (!strcmp(p->refer_to, ast_parking_ext())) {
 							/* Must release c's lock now, because it will not longer
@@ -10935,7 +10961,8 @@
 							ast_async_goto(transfer_to,p->context, p->refer_to,1);
 						}
 					} else {
-						ast_log(LOG_DEBUG, "Got SIP blind transfer but nothing to transfer to.\n");
+						if (option_debug > 2)
+							ast_log(LOG_DEBUG, "Got SIP blind transfer but nothing to transfer to.\n");
 						ast_queue_hangup(p->owner);
 					}
 				}
@@ -11063,7 +11090,8 @@
 			/* For transfers, this could happen, but since we haven't seen it happening, let us just refuse this */
  			transmit_response(p, "403 Forbidden (within dialog)", req);
 			/* Do not destroy session, since we will break the call if we do */
-			ast_log(LOG_DEBUG, "Got a subscription within the context of another call, can't handle that - %s (Method %s)\n", p->callid, sip_methods[p->initreq.method].text);
+			if (option_debug > 1)
+				ast_log(LOG_DEBUG, "Got a subscription within the context of another call, can't handle that - %s (Method %s)\n", p->callid, sip_methods[p->initreq.method].text);
 			return 0;
 		} else if (ast_test_flag(req, SIP_PKT_DEBUG)) {
 			if (resubscribe)
@@ -11526,7 +11554,7 @@
 			ast_log(LOG_WARNING, "Recv error: %s\n", strerror(errno));
 		return 1;
 	}
-	if (res == sizeof(req.data)) {
+	if (option_debug && (res == sizeof(req.data))) {
 		ast_log(LOG_DEBUG, "Received packet exceeds buffer. Data is possibly lost\n");
 	}
 	req.data[res] = '\0';
@@ -11561,7 +11589,8 @@
 		/* Go ahead and lock the owner if it has one -- we may need it */
 		/* becaues this is deadlock-prone, we need to try and unlock if failed */
 		if (p->owner && ast_mutex_trylock(&p->owner->lock)) {
-			ast_log(LOG_DEBUG, "Failed to grab lock, trying again...\n");
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Failed to grab lock, trying again...\n");
 			ast_mutex_unlock(&p->lock);
 			ast_mutex_unlock(&netlock);
 			/* Sleep infintismly short amount of time */
@@ -11574,7 +11603,8 @@
 		nounlock = 0;
 		if (handle_request(p, &req, &sin, &recount, &nounlock) == -1) {
 			/* Request failed */
-			ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
+			if (option_debug)
+				ast_log(LOG_DEBUG, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
 		}
 		
 		if (p->owner && !nounlock)
@@ -12237,7 +12267,8 @@
 	if (ast_strlen_zero(configuration))
 		return authlist;
 
-	ast_log(LOG_DEBUG, "Auth config ::  %s\n", configuration);
+	if (option_debug)
+		ast_log(LOG_DEBUG, "Auth config ::  %s\n", configuration);
 
 	ast_copy_string(authcopy, configuration, sizeof(authcopy));
 	stringp = authcopy;
@@ -12820,7 +12851,8 @@
 			ast_copy_string(global_realm, v->value, sizeof(global_realm));
 		} else if (!strcasecmp(v->name, "useragent")) {
 			ast_copy_string(global_useragent, v->value, sizeof(global_useragent));
-			ast_log(LOG_DEBUG, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Setting SIP channel User-Agent Name to %s\n", global_useragent);
 		} else if (!strcasecmp(v->name, "rtcachefriends")) {
 			ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);	
 		} else if (!strcasecmp(v->name, "rtupdate")) {
@@ -13100,7 +13132,7 @@
 			} else {
 				if (option_verbose > 1) { 
 					ast_verbose(VERBOSE_PREFIX_2 "SIP Listening on %s:%d\n", 
-					ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port));
+						ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr), ntohs(bindaddr.sin_port));
 					ast_verbose(VERBOSE_PREFIX_2 "Using SIP TOS: %s\n", ast_tos2str(global_tos_sip));
 				}
 				if (setsockopt(sipsock, IPPROTO_IP, IP_TOS, &global_tos_sip, sizeof(global_tos_sip))) 
