blob: b5d9304c4f68e8f07da65cff4b502a92a7bb9b4e [file] [log] [blame]
Benny Prijono85598d92006-01-07 18:44:25 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono85598d92006-01-07 18:44:25 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
Benny Prijono40f2f642006-01-30 18:40:05 +000020#include <pjsip.h>
Benny Prijono85598d92006-01-07 18:44:25 +000021#include <pjlib.h>
22
23#define THIS_FILE "msg_logger.c"
24
25static pj_bool_t msg_log_enabled;
26
27static pj_bool_t on_rx_msg(pjsip_rx_data *rdata)
28{
29 if (msg_log_enabled) {
Benny Prijonoe93e2872006-06-28 16:46:49 +000030 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s:%s:%d:\n"
31 "%.*s\n"
Benny Prijono85598d92006-01-07 18:44:25 +000032 "--end msg--",
33 rdata->msg_info.len,
34 pjsip_rx_data_get_info(rdata),
Benny Prijonoe93e2872006-06-28 16:46:49 +000035 rdata->tp_info.transport->type_name,
Benny Prijono85598d92006-01-07 18:44:25 +000036 rdata->pkt_info.src_name,
37 rdata->pkt_info.src_port,
Benny Prijonoe93e2872006-06-28 16:46:49 +000038 rdata->msg_info.len,
Benny Prijono85598d92006-01-07 18:44:25 +000039 rdata->msg_info.msg_buf));
40 }
41
42 return PJ_FALSE;
43}
44
45static pj_status_t on_tx_msg(pjsip_tx_data *tdata)
46{
47 if (msg_log_enabled) {
Benny Prijonoe93e2872006-06-28 16:46:49 +000048 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s:%s:%d:\n"
49 "%.*s\n"
Benny Prijono85598d92006-01-07 18:44:25 +000050 "--end msg--",
51 (tdata->buf.cur - tdata->buf.start),
52 pjsip_tx_data_get_info(tdata),
Benny Prijonoe93e2872006-06-28 16:46:49 +000053 tdata->tp_info.transport->type_name,
Benny Prijono85598d92006-01-07 18:44:25 +000054 tdata->tp_info.dst_name,
55 tdata->tp_info.dst_port,
Benny Prijonoe93e2872006-06-28 16:46:49 +000056 (tdata->buf.cur - tdata->buf.start),
Benny Prijono85598d92006-01-07 18:44:25 +000057 tdata->buf.start));
58 }
59 return PJ_SUCCESS;
60}
61
62
63/* Message logger module. */
64static pjsip_module mod_msg_logger =
65{
66 NULL, NULL, /* prev and next */
67 { "mod-msg-logger", 14}, /* Name. */
68 -1, /* Id */
69 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
Benny Prijono85598d92006-01-07 18:44:25 +000070 NULL, /* load() */
71 NULL, /* start() */
72 NULL, /* stop() */
73 NULL, /* unload() */
74 &on_rx_msg, /* on_rx_request() */
75 &on_rx_msg, /* on_rx_response() */
76 &on_tx_msg, /* on_tx_request() */
77 &on_tx_msg, /* on_tx_response() */
78 NULL, /* on_tsx_state() */
79};
80
81int init_msg_logger(void)
82{
83 pj_status_t status;
84
85 if (mod_msg_logger.id != -1)
86 return 0;
87
88 status = pjsip_endpt_register_module(endpt, &mod_msg_logger);
89 if (status != PJ_SUCCESS) {
90 app_perror(" error registering module", status);
91 return -10;
92 }
93
94 return 0;
95}
96
97int msg_logger_set_enabled(pj_bool_t enabled)
98{
99 int val = msg_log_enabled;
100 msg_log_enabled = enabled;
101 return val;
102}
103