Index: include/asterisk/agi.h
===================================================================
--- include/asterisk/agi.h	(revision 82490)
+++ include/asterisk/agi.h	(working copy)
@@ -51,7 +51,7 @@
 	AST_LIST_ENTRY(agi_command) list;
 } agi_command;
 
-int ast_agi_fdprintf(int fd, char *fmt, ...);
+int ast_agi_fdprintf(struct ast_channel *chan, int fd, char *fmt, ...);
 int ast_agi_register(struct ast_module *mod, agi_command *cmd);
 int ast_agi_unregister(struct ast_module *mod, agi_command *cmd);
 void ast_agi_register_multiple(struct ast_module *mod, agi_command *cmd, int len);
Index: res/res_agi.c
===================================================================
--- res/res_agi.c	(revision 82490)
+++ res/res_agi.c	(working copy)
@@ -120,7 +120,7 @@
 AST_THREADSTORAGE(agi_buf);
 #define AGI_BUF_INITSIZE 256
 
-int ast_agi_fdprintf(int fd, char *fmt, ...)
+int ast_agi_fdprintf(struct ast_channel *chan, int fd, char *fmt, ...)
 {
 	int res = 0;
 	va_list ap;
@@ -138,8 +138,13 @@
 		return -1;
 	}
 
-	if (agidebug)
-		ast_verbose("AGI Tx >> %s", buf->str);
+	if (agidebug) {
+		if (chan) {
+			ast_verbose("<%s>AGI Tx >> %s", chan->name, buf->str);
+		} else {
+			ast_verbose("AGI Tx >> %s", buf->str);
+		}
+	}
 
 	return ast_carefulwrite(fd, buf->str, buf->used, 100);
 }
@@ -214,7 +219,7 @@
 		}
 	}
 
-	if (ast_agi_fdprintf(s, "agi_network: yes\n") < 0) {
+	if (ast_agi_fdprintf(NULL, s, "agi_network: yes\n") < 0) {
 		if (errno != EINTR) {
 			ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno));
 			close(s);
@@ -225,7 +230,7 @@
 	/* If we have a script parameter, relay it to the fastagi server */
 	/* Script parameters take the form of: AGI(agi://my.example.com/?extension=${EXTEN}) */
 	if (!ast_strlen_zero(script))
-		ast_agi_fdprintf(s, "agi_network_script: %s\n", script);
+		ast_agi_fdprintf(NULL, s, "agi_network_script: %s\n", script);
 
 	ast_debug(4, "Wow, connected!\n");
 	fds[0] = s;
@@ -372,38 +377,38 @@
 
 	/* Print initial environment, with agi_request always being the first
 	   thing */
-	ast_agi_fdprintf(fd, "agi_request: %s\n", request);
-	ast_agi_fdprintf(fd, "agi_channel: %s\n", chan->name);
-	ast_agi_fdprintf(fd, "agi_language: %s\n", chan->language);
-	ast_agi_fdprintf(fd, "agi_type: %s\n", chan->tech->type);
-	ast_agi_fdprintf(fd, "agi_uniqueid: %s\n", chan->uniqueid);
+	ast_agi_fdprintf(chan, fd, "agi_request: %s\n", request);
+	ast_agi_fdprintf(chan, fd, "agi_channel: %s\n", chan->name);
+	ast_agi_fdprintf(chan, fd, "agi_language: %s\n", chan->language);
+	ast_agi_fdprintf(chan, fd, "agi_type: %s\n", chan->tech->type);
+	ast_agi_fdprintf(chan, fd, "agi_uniqueid: %s\n", chan->uniqueid);
 
 	/* ANI/DNIS */
-	ast_agi_fdprintf(fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown"));
-	ast_agi_fdprintf(fd, "agi_calleridname: %s\n", S_OR(chan->cid.cid_name, "unknown"));
-	ast_agi_fdprintf(fd, "agi_callingpres: %d\n", chan->cid.cid_pres);
-	ast_agi_fdprintf(fd, "agi_callingani2: %d\n", chan->cid.cid_ani2);
-	ast_agi_fdprintf(fd, "agi_callington: %d\n", chan->cid.cid_ton);
-	ast_agi_fdprintf(fd, "agi_callingtns: %d\n", chan->cid.cid_tns);
-	ast_agi_fdprintf(fd, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown"));
-	ast_agi_fdprintf(fd, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown"));
+	ast_agi_fdprintf(chan, fd, "agi_callerid: %s\n", S_OR(chan->cid.cid_num, "unknown"));
+	ast_agi_fdprintf(chan, fd, "agi_calleridname: %s\n", S_OR(chan->cid.cid_name, "unknown"));
+	ast_agi_fdprintf(chan, fd, "agi_callingpres: %d\n", chan->cid.cid_pres);
+	ast_agi_fdprintf(chan, fd, "agi_callingani2: %d\n", chan->cid.cid_ani2);
+	ast_agi_fdprintf(chan, fd, "agi_callington: %d\n", chan->cid.cid_ton);
+	ast_agi_fdprintf(chan, fd, "agi_callingtns: %d\n", chan->cid.cid_tns);
+	ast_agi_fdprintf(chan, fd, "agi_dnid: %s\n", S_OR(chan->cid.cid_dnid, "unknown"));
+	ast_agi_fdprintf(chan, fd, "agi_rdnis: %s\n", S_OR(chan->cid.cid_rdnis, "unknown"));
 
 	/* Context information */
-	ast_agi_fdprintf(fd, "agi_context: %s\n", chan->context);
-	ast_agi_fdprintf(fd, "agi_extension: %s\n", chan->exten);
-	ast_agi_fdprintf(fd, "agi_priority: %d\n", chan->priority);
-	ast_agi_fdprintf(fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
+	ast_agi_fdprintf(chan, fd, "agi_context: %s\n", chan->context);
+	ast_agi_fdprintf(chan, fd, "agi_extension: %s\n", chan->exten);
+	ast_agi_fdprintf(chan, fd, "agi_priority: %d\n", chan->priority);
+	ast_agi_fdprintf(chan, fd, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0");
 
 	/* User information */
-	ast_agi_fdprintf(fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : "");
+	ast_agi_fdprintf(chan, fd, "agi_accountcode: %s\n", chan->accountcode ? chan->accountcode : "");
 
 	/* Send any parameters to the fastagi server that have been passed via the agi application */
 	/* Agi application paramaters take the form of: AGI(/path/to/example/script|${EXTEN}) */
 	for(count = 1; count < argc; count++)
-		ast_agi_fdprintf(fd, "agi_arg_%d: %s\n", count, argv[count]);
+		ast_agi_fdprintf(chan, fd, "agi_arg_%d: %s\n", count, argv[count]);
 
 	/* End with empty return */
-	ast_agi_fdprintf(fd, "\n");
+	ast_agi_fdprintf(chan, fd, "\n");
 }
 
 static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, char *argv[])
@@ -414,7 +419,7 @@
 	if (chan->_state != AST_STATE_UP)
 		res = ast_answer(chan);
 
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -427,7 +432,7 @@
 	if (sscanf(argv[3], "%d", &to) != 1)
 		return RESULT_SHOWUSAGE;
 	res = ast_waitfordigit_full(chan, to, agi->audio, agi->ctrl);
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -446,7 +451,7 @@
 	   parsing, then here, add a newline at the end of the string
 	   before sending it to ast_sendtext --DUDE */
 	res = ast_sendtext(chan, argv[2]);
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -459,15 +464,15 @@
 
 	res = ast_recvchar(chan,atoi(argv[2]));
 	if (res == 0) {
-		ast_agi_fdprintf(agi->fd, "200 result=%d (timeout)\n", res);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%d (timeout)\n", res);
 		return RESULT_SUCCESS;
 	}
 	if (res > 0) {
-		ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 		return RESULT_SUCCESS;
 	}
 	else {
-		ast_agi_fdprintf(agi->fd, "200 result=%d (hangup)\n", res);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%d (hangup)\n", res);
 		return RESULT_FAILURE;
 	}
 }
@@ -481,10 +486,10 @@
 
 	buf = ast_recvtext(chan,atoi(argv[2]));
 	if (buf) {
-		ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", buf);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=1 (%s)\n", buf);
 		ast_free(buf);
 	} else {	
-		ast_agi_fdprintf(agi->fd, "200 result=-1\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=-1\n");
 	}
 	return RESULT_SUCCESS;
 }
@@ -506,9 +511,9 @@
 		x = 1;
 	res = ast_channel_setoption(chan, AST_OPTION_TDD, &x, sizeof(char), 0);
 	if (res != RESULT_SUCCESS)
-		ast_agi_fdprintf(agi->fd, "200 result=0\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	else
-		ast_agi_fdprintf(agi->fd, "200 result=1\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=1\n");
 	return RESULT_SUCCESS;
 }
 
@@ -522,7 +527,7 @@
 	res = ast_send_image(chan, argv[2]);
 	if (!ast_check_hangup(chan))
 		res = 0;
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -559,7 +564,7 @@
 	
 	res = ast_control_streamfile(chan, argv[3], fwd, rev, stop, pause, NULL, skipms, NULL);
 	
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
@@ -581,7 +586,7 @@
 		return RESULT_SHOWUSAGE;
 
 	if (!(fs = ast_openstream(chan, argv[2], chan->language))) {
-		ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
 		return RESULT_SUCCESS;
 	}
 
@@ -609,7 +614,7 @@
 		/* Stop this command, don't print a result line, as there is a new command */
 		return RESULT_SUCCESS;
 	}
-	ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -636,7 +641,7 @@
 	}
 
 	if (!(fs = ast_openstream(chan, argv[2], chan->language))) {
-		ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%d endpos=%ld\n", 0, sample_offset);
 		ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]);
 		return RESULT_SUCCESS;
 	}
@@ -674,7 +679,7 @@
 			res=0;
 	}
 
-	ast_agi_fdprintf(agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d endpos=%ld\n", res, sample_offset);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -694,7 +699,7 @@
 	res = ast_say_number_full(chan, num, argv[3], chan->language, argc > 4 ? argv[4] : NULL, agi->audio, agi->ctrl);
 	if (res == 1)
 		return RESULT_SUCCESS;
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -710,7 +715,7 @@
 	res = ast_say_digit_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
 	if (res == 1) /* New command */
 		return RESULT_SUCCESS;
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -724,7 +729,7 @@
 	res = ast_say_character_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
 	if (res == 1) /* New command */
 		return RESULT_SUCCESS;
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -739,7 +744,7 @@
 	res = ast_say_date(chan, num, argv[3], chan->language);
 	if (res == 1)
 		return RESULT_SUCCESS;
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -754,7 +759,7 @@
 	res = ast_say_time(chan, num, argv[3], chan->language);
 	if (res == 1)
 		return RESULT_SUCCESS;
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -788,7 +793,7 @@
 	if (res == 1)
 		return RESULT_SUCCESS;
 
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -802,7 +807,7 @@
 	res = ast_say_phonetic_str_full(chan, argv[2], argv[3], chan->language, agi->audio, agi->ctrl);
 	if (res == 1) /* New command */
 		return RESULT_SUCCESS;
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 	return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE;
 }
 
@@ -825,11 +830,11 @@
 	if (res == 2)			/* New command */
 		return RESULT_SUCCESS;
 	else if (res == 1)
-		ast_agi_fdprintf(agi->fd, "200 result=%s (timeout)\n", data);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%s (timeout)\n", data);
 	else if (res < 0 )
-		ast_agi_fdprintf(agi->fd, "200 result=-1\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=-1\n");
 	else
-		ast_agi_fdprintf(agi->fd, "200 result=%s\n", data);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%s\n", data);
 	return RESULT_SUCCESS;
 }
 
@@ -839,7 +844,7 @@
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
 	ast_copy_string(chan->context, argv[2], sizeof(chan->context));
-	ast_agi_fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 	
@@ -848,7 +853,7 @@
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
 	ast_copy_string(chan->exten, argv[2], sizeof(chan->exten));
-	ast_agi_fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 
@@ -865,7 +870,7 @@
 	}
 
 	ast_explicit_goto(chan, NULL, NULL, pri);
-	ast_agi_fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 		
@@ -941,12 +946,12 @@
 	if (!res)
 		res = ast_waitstream(chan, argv[4]);
 	if (res) {
-		ast_agi_fdprintf(agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset);
 	} else {
 		fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY | (sample_offset ? O_APPEND : 0), 0, AST_FILE_MODE);
 		if (!fs) {
 			res = -1;
-			ast_agi_fdprintf(agi->fd, "200 result=%d (writefile)\n", res);
+			ast_agi_fdprintf(chan, agi->fd, "200 result=%d (writefile)\n", res);
 			if (sildet)
 				ast_dsp_free(sildet);
 			return RESULT_FAILURE;
@@ -966,14 +971,14 @@
 			res = ast_waitfor(chan, -1);
 			if (res < 0) {
 				ast_closestream(fs);
-				ast_agi_fdprintf(agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset);
+				ast_agi_fdprintf(chan, agi->fd, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset);
 				if (sildet)
 					ast_dsp_free(sildet);
 				return RESULT_FAILURE;
 			}
 			f = ast_read(chan);
 			if (!f) {
-				ast_agi_fdprintf(agi->fd, "200 result=%d (hangup) endpos=%ld\n", 0, sample_offset);
+				ast_agi_fdprintf(chan, agi->fd, "200 result=%d (hangup) endpos=%ld\n", 0, sample_offset);
 				ast_closestream(fs);
 				if (sildet)
 					ast_dsp_free(sildet);
@@ -988,7 +993,7 @@
 					ast_stream_rewind(fs, 200);
 					ast_truncstream(fs);
 					sample_offset = ast_tellstream(fs);
-					ast_agi_fdprintf(agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset);
+					ast_agi_fdprintf(chan, agi->fd, "200 result=%d (dtmf) endpos=%ld\n", f->subclass, sample_offset);
 					ast_closestream(fs);
 					ast_frfree(f);
 					if (sildet)
@@ -1033,7 +1038,7 @@
 				ast_truncstream(fs);
 				sample_offset = ast_tellstream(fs);
 		}		
-		ast_agi_fdprintf(agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset);
 		ast_closestream(fs);
 	}
 
@@ -1060,7 +1065,7 @@
 		chan->whentohangup = time(NULL) + timeout;
 	else
 		chan->whentohangup = 0;
-	ast_agi_fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1071,7 +1076,7 @@
 	if (argc == 1) {
 		/* no argument: hangup the current channel */
 		ast_softhangup(chan,AST_SOFTHANGUP_EXPLICIT);
-		ast_agi_fdprintf(agi->fd, "200 result=1\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=1\n");
 		return RESULT_SUCCESS;
 	} else if (argc == 2) {
 		/* one argument: look for info on the specified channel */
@@ -1079,12 +1084,12 @@
 		if (c) {
 			/* we have a matching channel */
 			ast_softhangup(c,AST_SOFTHANGUP_EXPLICIT);
-			ast_agi_fdprintf(agi->fd, "200 result=1\n");
+			ast_agi_fdprintf(chan, agi->fd, "200 result=1\n");
 			ast_channel_unlock(c);
 			return RESULT_SUCCESS;
 		}
 		/* if we get this far no channel name matched the argument given */
-		ast_agi_fdprintf(agi->fd, "200 result=-1\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=-1\n");
 		return RESULT_SUCCESS;
 	} else {
 		return RESULT_SHOWUSAGE;
@@ -1107,7 +1112,7 @@
 		ast_log(LOG_WARNING, "Could not find application (%s)\n", argv[1]);
 		res = -2;
 	}
-	ast_agi_fdprintf(agi->fd, "200 result=%d\n", res);
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", res);
 
 	/* Even though this is wrong, users are depending upon this result. */
 	return res;
@@ -1130,7 +1135,7 @@
 		ast_set_callerid(chan, l, n, NULL);
 	}
 
-	ast_agi_fdprintf(agi->fd, "200 result=1\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=1\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1139,18 +1144,18 @@
 	struct ast_channel *c;
 	if (argc == 2) {
 		/* no argument: supply info on the current channel */
-		ast_agi_fdprintf(agi->fd, "200 result=%d\n", chan->_state);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", chan->_state);
 		return RESULT_SUCCESS;
 	} else if (argc == 3) {
 		/* one argument: look for info on the specified channel */
 		c = ast_get_channel_by_name_locked(argv[2]);
 		if (c) {
-			ast_agi_fdprintf(agi->fd, "200 result=%d\n", c->_state);
+			ast_agi_fdprintf(chan, agi->fd, "200 result=%d\n", c->_state);
 			ast_channel_unlock(c);
 			return RESULT_SUCCESS;
 		}
 		/* if we get this far no channel name matched the argument given */
-		ast_agi_fdprintf(agi->fd, "200 result=-1\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=-1\n");
 		return RESULT_SUCCESS;
 	} else {
 		return RESULT_SHOWUSAGE;
@@ -1162,7 +1167,7 @@
 	if (argv[3])
 		pbx_builtin_setvar_helper(chan, argv[2], argv[3]);
 
-	ast_agi_fdprintf(agi->fd, "200 result=1\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=1\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1182,9 +1187,9 @@
 	}
 
 	if (ret)
-		ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", ret);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=1 (%s)\n", ret);
 	else
-		ast_agi_fdprintf(agi->fd, "200 result=0\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 
 	return RESULT_SUCCESS;
 }
@@ -1203,9 +1208,9 @@
 	}
 	if (chan2) {
 		pbx_substitute_variables_helper(chan2, argv[3], tmp, sizeof(tmp) - 1);
-		ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=1 (%s)\n", tmp);
 	} else {
-		ast_agi_fdprintf(agi->fd, "200 result=0\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	}
 	if (chan2 && (chan2 != chan))
 		ast_channel_unlock(chan2);
@@ -1242,7 +1247,7 @@
 	if (level <= option_verbose)
 		ast_verbose("%s %s: %s\n", prefix, chan->data, argv[1]);
 	
-	ast_agi_fdprintf(agi->fd, "200 result=1\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=1\n");
 	
 	return RESULT_SUCCESS;
 }
@@ -1256,9 +1261,9 @@
 		return RESULT_SHOWUSAGE;
 	res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
 	if (res) 
-		ast_agi_fdprintf(agi->fd, "200 result=0\n");
+		ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	else
-		ast_agi_fdprintf(agi->fd, "200 result=1 (%s)\n", tmp);
+		ast_agi_fdprintf(chan, agi->fd, "200 result=1 (%s)\n", tmp);
 
 	return RESULT_SUCCESS;
 }
@@ -1270,7 +1275,7 @@
 	if (argc != 5)
 		return RESULT_SHOWUSAGE;
 	res = ast_db_put(argv[2], argv[3], argv[4]);
-	ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%c\n", res ? '0' : '1');
 	return RESULT_SUCCESS;
 }
 
@@ -1281,7 +1286,7 @@
 	if (argc != 4)
 		return RESULT_SHOWUSAGE;
 	res = ast_db_del(argv[2], argv[3]);
-	ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%c\n", res ? '0' : '1');
 	return RESULT_SUCCESS;
 }
 
@@ -1296,7 +1301,7 @@
 	else
 		res = ast_db_deltree(argv[2], NULL);
 
-	ast_agi_fdprintf(agi->fd, "200 result=%c\n", res ? '0' : '1');
+	ast_agi_fdprintf(chan, agi->fd, "200 result=%c\n", res ? '0' : '1');
 	return RESULT_SUCCESS;
 }
 
@@ -1328,7 +1333,7 @@
 
 static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, char *argv[])
 {
-	ast_agi_fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1338,7 +1343,7 @@
 		ast_moh_start(chan, argc > 3 ? argv[3] : NULL, NULL);
 	else if (!strncasecmp(argv[2], "off", 3))
 		ast_moh_stop(chan);
-	ast_agi_fdprintf(agi->fd, "200 result=0\n");
+	ast_agi_fdprintf(chan, agi->fd, "200 result=0\n");
 	return RESULT_SUCCESS;
 }
 
@@ -1838,9 +1843,9 @@
 			ast_module_unref(c->mod);
 		switch(res) {
 		case RESULT_SHOWUSAGE:
-			ast_agi_fdprintf(agi->fd, "520-Invalid command syntax.  Proper usage follows:\n");
-			ast_agi_fdprintf(agi->fd, c->usage);
-			ast_agi_fdprintf(agi->fd, "520 End of proper usage.\n");
+			ast_agi_fdprintf(chan, agi->fd, "520-Invalid command syntax.  Proper usage follows:\n");
+			ast_agi_fdprintf(chan, agi->fd, c->usage);
+			ast_agi_fdprintf(chan, agi->fd, "520 End of proper usage.\n");
 			break;
 		case AST_PBX_KEEPALIVE:
 			/* We've been asked to keep alive, so do so */
@@ -1852,9 +1857,9 @@
 			return -1;
 		}
 	} else if ((c = find_command(argv, 0))) {
-		ast_agi_fdprintf(agi->fd, "511 Command Not Permitted on a dead channel\n");
+		ast_agi_fdprintf(chan, agi->fd, "511 Command Not Permitted on a dead channel\n");
 	} else {
-		ast_agi_fdprintf(agi->fd, "510 Invalid or unknown command\n");
+		ast_agi_fdprintf(chan, agi->fd, "510 Invalid or unknown command\n");
 	}
 	return 0;
 }
@@ -1912,7 +1917,7 @@
 				/* Program terminated */
 				if (returnstatus && returnstatus != AST_PBX_KEEPALIVE)
 					returnstatus = -1;
-				ast_verb(3, "AGI Script %s completed, returning %d\n", request, returnstatus);
+				ast_verb(3, "<%s>AGI Script %s completed, returning %d\n", chan->name, request, returnstatus);
 				if (pid > 0)
 					waitpid(pid, status, 0);
 				/* No need to kill the pid anymore, since they closed us */
@@ -1924,7 +1929,7 @@
 			if (*buf && buf[strlen(buf) - 1] == '\n')
 				buf[strlen(buf) - 1] = 0;
 			if (agidebug)
-				ast_verbose("AGI Rx << %s\n", buf);
+				ast_verbose("<%s>AGI Rx << %s\n", chan->name, buf);
 			returnstatus |= agi_handle_command(chan, agi, buf, dead);
 			/* If the handle_command returns -1, we need to stop */
 			if ((returnstatus < 0) || (returnstatus == AST_PBX_KEEPALIVE)) {
@@ -2183,7 +2188,7 @@
 
 static struct ast_cli_entry cli_agi[] = {
 	{ { "agi", "debug", NULL },
-	agi_do_debug, "Enable AGI debugging",
+	agi_do_debug, "Enable AGI debugging for all channels",
 	debug_usage },
 
 	{ { "agi", "debug", "off", NULL },
