Index: pjproject/branches/pjproject-0.5-stable/pjsip-apps/src/samples/proxy.h
===================================================================
--- pjproject/branches/pjproject-0.5-stable/pjsip-apps/src/samples/proxy.h	(revision 1121)
+++ pjproject/branches/pjproject-0.5-stable/pjsip-apps/src/samples/proxy.h	(revision 1123)
@@ -54,7 +54,8 @@
     puts("Options:\n"
 	 "\n"
-	 " -p, --port N    Set local listener port to N\n"
-	 " -R, --rr        Perform record routing\n"
-	 " -h, --help      Show this help screen\n"
+	 " --port N       Set local listener port to N\n"
+	 " --rr           Perform record routing\n"
+	 " --log-level N  Set log level to N (default: 4)\n"
+	 " --help         Show this help screen\n"
 	 );
 }
@@ -64,7 +65,8 @@
 {
     struct pj_getopt_option long_opt[] = {
-	{ "port",1, 0, 'p'},
-	{ "rr",  1, 0, 'R'},
-	{ "help",1, 0, 'h'},
+	{ "port",	1, 0, 'p'},
+	{ "rr",		0, 0, 'R'},
+	{ "log-level",	1, 0, 'L'},
+	{ "help",	0, 0, 'h'},
     };
     int c;
@@ -72,5 +74,5 @@
 
     pj_optind = 0;
-    while((c=pj_getopt_long(argc, argv, "pRh", long_opt, &opt_ind))!=-1) {
+    while((c=pj_getopt_long(argc, argv, "", long_opt, &opt_ind))!=-1) {
 	switch (c) {
 	case 'p':
@@ -84,4 +86,8 @@
 	    break;
 
+	case 'L':
+	    pj_log_set_level(atoi(pj_optarg));
+	    break;
+
 	case 'h':
 	    usage();
@@ -89,6 +95,6 @@
 
 	default:
-	    puts("Unknown option ignored");
-	    break;
+	    puts("Unknown option. Run with --help for help.");
+	    return -1;
 	}
     }
@@ -96,4 +102,81 @@
     return PJ_SUCCESS;
 }
+
+
+/*****************************************************************************
+ * This is a very simple PJSIP module, whose sole purpose is to display
+ * incoming and outgoing messages to log. This module will have priority
+ * higher than transport layer, which means:
+ *
+ *  - incoming messages will come to this module first before reaching
+ *    transaction layer.
+ *
+ *  - outgoing messages will come to this module last, after the message
+ *    has been 'printed' to contiguous buffer by transport layer and
+ *    appropriate transport instance has been decided for this message.
+ *
+ */
+
+/* Notification on incoming messages */
+static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
+{
+    PJ_LOG(5,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
+			 "%.*s\n"
+			 "--end msg--",
+			 rdata->msg_info.len,
+			 pjsip_rx_data_get_info(rdata),
+			 rdata->tp_info.transport->type_name,
+			 rdata->pkt_info.src_name,
+			 rdata->pkt_info.src_port,
+			 (int)rdata->msg_info.len,
+			 rdata->msg_info.msg_buf));
+    
+    /* Always return false, otherwise messages will not get processed! */
+    return PJ_FALSE;
+}
+
+/* Notification on outgoing messages */
+static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
+{
+    
+    /* Important note:
+     *	tp_info field is only valid after outgoing messages has passed
+     *	transport layer. So don't try to access tp_info when the module
+     *	has lower priority than transport layer.
+     */
+
+    PJ_LOG(5,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
+			 "%.*s\n"
+			 "--end msg--",
+			 (tdata->buf.cur - tdata->buf.start),
+			 pjsip_tx_data_get_info(tdata),
+			 tdata->tp_info.transport->type_name,
+			 tdata->tp_info.dst_name,
+			 tdata->tp_info.dst_port,
+			 (int)(tdata->buf.cur - tdata->buf.start),
+			 tdata->buf.start));
+
+    /* Always return success, otherwise message will not get sent! */
+    return PJ_SUCCESS;
+}
+
+/* The module instance. */
+static pjsip_module mod_msg_logger = 
+{
+    NULL, NULL,				/* prev, next.		*/
+    { "mod-msg-logger", 14 },		/* Name.		*/
+    -1,					/* Id			*/
+    PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority	        */
+    NULL,				/* load()		*/
+    NULL,				/* start()		*/
+    NULL,				/* stop()		*/
+    NULL,				/* unload()		*/
+    &logging_on_rx_msg,			/* on_rx_request()	*/
+    &logging_on_rx_msg,			/* on_rx_response()	*/
+    &logging_on_tx_msg,			/* on_tx_request.	*/
+    &logging_on_tx_msg,			/* on_tx_response()	*/
+    NULL,				/* on_tsx_state()	*/
+
+};
 
 
@@ -142,4 +225,7 @@
     global.pool = pj_pool_create(&global.cp.factory, "proxyapp", 
 				 4000, 4000, NULL);
+
+    /* Register the logger module */
+    pjsip_endpt_register_module(global.endpt, &mod_msg_logger);
 
     return PJ_SUCCESS;
@@ -413,9 +499,11 @@
 
     /* We're not interested to receive request destined to us, so
-     * respond with 404/Not Found.
-     */
-    pjsip_endpt_respond_stateless(global.endpt, rdata,
-				  PJSIP_SC_NOT_FOUND, NULL,
-				  NULL, NULL);
+     * respond with 404/Not Found (only if request is not ACK!).
+     */
+    if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
+	pjsip_endpt_respond_stateless(global.endpt, rdata,
+				      PJSIP_SC_NOT_FOUND, NULL,
+				      NULL, NULL);
+    }
 
     /* Delete the request since we're not forwarding it */
