make hexdump return a 'char *' rather than printing by itself

diff --git a/src/debug.c b/src/debug.c
index c071df0..735fce3 100644
--- a/src/debug.c
+++ b/src/debug.c
@@ -127,12 +127,22 @@
 	fflush(outfd);
 }
 
-void hexdump(unsigned char *buf, int len)
+static char hexd_buff[4096];
+
+char *hexdump(unsigned char *buf, int len)
 {
 	int i;
+	char *cur = hexd_buff;
+
+	hexd_buff[0] = 0;
 	for (i = 0; i < len; i++) {
-		fprintf(stdout, "%02x ", buf[i]);
+		int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
+		int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
+		if (rc <= 0)
+			break;
+		cur += rc;
 	}
-	fprintf(stdout, "\n");
+	hexd_buff[sizeof(hexd_buff)-1] = 0;
+	return hexd_buff;
 }
 
diff --git a/src/input/ipaccess.c b/src/input/ipaccess.c
index 98f47fb..6c83d6d 100644
--- a/src/input/ipaccess.c
+++ b/src/input/ipaccess.c
@@ -124,10 +124,7 @@
 	if (hh->proto == IPAC_PROTO_IPACCESS)
 		return ipaccess_rcvmsg(msg, bfd->fd);
 
-	if (debug_mask & DMI) { 
-		fprintf(stdout, "RX %u: ", ts_nr);
-		hexdump(msgb_l2(msg), ret);
-	}
+	DEBUGP(DMI, "RX %u: %s\n", ts_nr, hexdump(msgb_l2(msg), ret));
 
 	link = e1inp_lookup_sign_link(e1i_ts, 0, hh->proto);
 	if (!link) {
@@ -197,10 +194,7 @@
 		return -EINVAL;
 	}
 
-	if (debug_mask & DMI) {
-		fprintf(stdout, "TX %u: ", ts_nr);
-		hexdump(l2_data, hh->len);
-	}
+	DEBUGP(DMI, "TX %u: %s\n", ts_nr, hexdump(l2_data, hh->len));
 
 	ret = send(bfd->fd, msg->data, msg->len, 0);
 	msgb_free(msg);
diff --git a/src/input/misdn.c b/src/input/misdn.c
index 8acf4c9..245df50 100644
--- a/src/input/misdn.c
+++ b/src/input/misdn.c
@@ -148,10 +148,7 @@
 		break;
 	case DL_DATA_IND:
 		msg->l2h = msg->data + MISDN_HEADER_LEN;
-		if (debug_mask & DMI) { 
-			fprintf(stdout, "RX: ");
-			hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
-		}
+		DEBUGP(DMI, "RX: %s\n", hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
 		ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi);
 		break;
 	default:
@@ -185,10 +182,8 @@
 	hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
 	hh->prim = DL_DATA_REQ;
 
-	if (debug_mask & DMI) {
-		fprintf(stdout, "TX TEI(%d): ", sign_link->tei);
-		hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
-	}
+	DEBUGP(DMI, "TX TEI(%d): %s\n", sign_link->tei,
+		hexdump(l2_data, msg->len - MISDN_HEADER_LEN));
 
 	/* construct the sockaddr */
 	sa.family = AF_ISDN;
@@ -223,10 +218,8 @@
 
 	subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
 
-	if (debug_mask & DMIB) {
-		fprintf(stdout, "BCHAN TX: ");
-		hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN);
-	}
+	DEBUGP(DMIB, "BCHAN TX: %s\n",
+		hexdump(tx_buf+sizeof(*hh), BCHAN_TX_GRAN));
 
 	ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0);
 	if (ret < sizeof(*hh) + BCHAN_TX_GRAN)
@@ -266,10 +259,8 @@
 	switch (hh->prim) {
 	case PH_DATA_IND:
 		msg->l2h = msg->data + MISDN_HEADER_LEN;
-		if (debug_mask & DMIB) {
-			fprintf(stdout, "BCHAN RX: ");
-			hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
-		}
+		DEBUGP(DMIB, "BCHAN RX: %s\n",
+			hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN));
 		ret = e1inp_rx_ts(e1i_ts, msg, 0, 0);
 		break;
 	case PH_ACTIVATE_IND:
diff --git a/src/rs232.c b/src/rs232.c
index d0825d9..24577ea 100644
--- a/src/rs232.c
+++ b/src/rs232.c
@@ -100,10 +100,7 @@
 		return 0;
 	}
 
-	if (debug_mask & DMI) {
-		fprintf(stdout, "RS232 TX: ");
-		hexdump(msg->data, msg->len);
-	}
+	DEBUGP(DMI, "RS232 TX: %s", hexdump(msg->data, msg->len));
 
 	/* send over serial line */
 	written = write(bfd->fd, msg->data, msg->len);
@@ -175,10 +172,7 @@
 			if (msg->len > LAPD_HDR_LEN)
 				msg->l2h = msg->data + LAPD_HDR_LEN;
 
-			if (debug_mask & DMI) {
-				fprintf(stdout, "RS232 RX: ");
-				hexdump(msg->data, msg->len);
-			}
+			DEBUGP(DMI, "RS232 RX: %s\n", hexdump(msg->data, msg->len));
 			rc = handle_serial_msg(msg);
 		}
 	}