Skip to content

Commit

Permalink
Debug reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
kalta committed Jul 23, 2014
1 parent 9ac5568 commit 48ab0e5
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 57 deletions.
6 changes: 5 additions & 1 deletion include/nksip.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
end).

-define(DO_DEBUG(AppId, CallId, Level, Text, List),
AppId:nkcb_debug(AppId, CallId, {Level, Text, List})).
case AppId:config_debug() of
false -> ok;
_ -> AppId:nkcb_debug(AppId, CallId, {Level, Text, List})
end).


-define(debug(AppId, CallId, Text, List),
?DO_DEBUG(AppId, CallId, debug, Text, List),
Expand Down
55 changes: 25 additions & 30 deletions plugins/src/nksip_debug.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

-export([start/1, stop/1, print/1, print_all/0]).
-export([insert/2, insert/3, find/1, find/2, dump_msgs/0, reset_msgs/0]).
-export([version/0, deps/0, parse_config/1, init/2, terminate/2]).
-export([version/0, deps/0, init/2, terminate/2]).

-include("../include/nksip.hrl").
-include("../include/nksip_call.hrl").
Expand All @@ -52,20 +52,20 @@ deps() ->
[].


%% @doc Parses this plugin specific configuration
-spec parse_config(nksip:optslist()) ->
{ok, nksip:optslist()} | {error, term()}.
% %% @doc Parses this plugin specific configuration
% -spec parse_config(nksip:optslist()) ->
% {ok, nksip:optslist()} | {error, term()}.

parse_config(Opts) ->
case nksip_lib:get_value(nksip_debug, Opts, false) of
Trace when is_boolean(Trace) ->
Cached1 = nksip_lib:get_value(cached_configs, Opts, []),
Cached2 = nksip_lib:store_value(config_nksip_debug, Trace, Cached1),
Opts1 = nksip_lib:store_value(cached_configs, Cached2, Opts),
{ok, Opts1};
_ ->
{error, {invalid_config, nksip_debug}}
end.
% parse_config(Opts) ->
% case nksip_lib:get_value(nksip_debug, Opts, false) of
% Trace when is_boolean(Trace) ->
% Cached1 = nksip_lib:get_value(cached_configs, Opts, []),
% Cached2 = nksip_lib:store_value(config_nksip_debug, Trace, Cached1),
% Opts1 = nksip_lib:store_value(cached_configs, Cached2, Opts),
% {ok, Opts1};
% _ ->
% {error, {invalid_config, nksip_debug}}
% end.


%% @doc Called when the plugin is started
Expand Down Expand Up @@ -113,7 +113,7 @@ start(App) ->
{ok, AppId} ->
Plugins1 = AppId:config_plugins(),
Plugins2 = nksip_lib:store_value(nksip_debug, Plugins1),
case nksip:update(AppId, [{plugins, Plugins2}, {nksip_debug, true}]) of
case nksip:update(AppId, [{plugins, Plugins2}, {debug, true}]) of
{ok, _} -> ok;
{error, Error} -> {error, Error}
end;
Expand All @@ -130,7 +130,7 @@ stop(App) ->
case nksip:find_app_id(App) of
{ok, AppId} ->
Plugins = AppId:config_plugins() -- [nksip_debug],
case nksip:update(App, [{plugins, Plugins}]) of
case nksip:update(App, [{plugins, Plugins}, {debug, false}]) of
{ok, _} -> ok;
{error, Error} -> {error, Error}
end;
Expand All @@ -153,21 +153,16 @@ insert(#sipmsg{app_id=AppId, call_id=CallId}, Info) ->

%% @private
insert(AppId, CallId, Info) ->
case AppId:config_nksip_debug() of
true ->
Time = nksip_lib:l_timestamp(),
Info1 = case Info of
{Type, Str, Fmt} when Type==debug; Type==info; Type==notice;
Type==warning; Type==error ->
{Type, nksip_lib:msg(Str, Fmt)};
_ ->
Info
end,
AppName = AppId:name(),
ets:insert(nksip_debug_msgs, {CallId, Time, AppName, Info1});
Time = nksip_lib:l_timestamp(),
Info1 = case Info of
{Type, Str, Fmt} when Type==debug; Type==info; Type==notice;
Type==warning; Type==error ->
{Type, nksip_lib:msg(Str, Fmt)};
_ ->
ok
end.
Info
end,
AppName = AppId:name(),
ets:insert(nksip_debug_msgs, {CallId, Time, AppName, Info1}).


%% @private
Expand Down
11 changes: 1 addition & 10 deletions plugins/src/nksip_debug_callbacks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

-include("../include/nksip.hrl").

-export([nkcb_connection_sent/2, nkcb_connection_recv/4, nkcb_debug/2, nkcb_debug/3]).
-export([nkcb_connection_sent/2, nkcb_connection_recv/4, nkcb_debug/3]).


%%%%%%%%%%%%%%%% Implemented core plugin callbacks %%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -57,15 +57,6 @@ nkcb_connection_recv(AppId, CallId, Transp, Packet) ->
continue.


%% doc Called at specific debug points
-spec nkcb_debug(nksip:request()|nksip:response(), term()) ->
continue.

nkcb_debug(SipMsg, Info) ->
nksip_debug:insert(SipMsg, Info),
continue.


%% doc Called at specific debug points
-spec nkcb_debug(nksip:app_id(), nksip:call_id(), term()) ->
continue.
Expand Down
1 change: 1 addition & 0 deletions samples/nksip_loadtest/src/nksip_loadtest_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ start_server(Name, Port) ->
{plugins, [nksip_registrar, nksip_stats, nksip_debug]},
{transports, [{udp, {0,0,0,0}, Port}, {tls, {0,0,0,0}, Port+1}]},
{log_level, info},
{debug, true},
no_100
],
case nksip:start(Name, nksip_loadtest_sipapp, [], Opts) of
Expand Down
12 changes: 9 additions & 3 deletions src/nksip_call_uac_transp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
{ok, nksip:request()} | {error, nksip:sipreply()}.

send_request(Req, Opts) ->
#sipmsg{app_id=AppId, class={req, Method}, ruri=RUri, routes=Routes} = Req,
#sipmsg{
app_id = AppId,
call_id = CallId,
class = {req, Method},
ruri = RUri,
routes = Routes
} = Req,
?call_debug("UAC send opts: ~p", [Opts]),
case Routes of
[] ->
Expand Down Expand Up @@ -68,7 +74,7 @@ send_request(Req, Opts) ->
end,
Req1 = Req#sipmsg{ruri=RUri1, routes=Routes1},
MakeReqFun = make_request_fun(Req1, DestUri, Opts),
AppId:nkcb_debug(Req, {uac_out_request, Method}),
AppId:nkcb_debug(AppId, CallId, {uac_out_request, Method}),
Dests = case nksip_lib:get_value(route_flow, Opts) of
{Transp, Pid} ->
[{flow, {Pid, Transp}}, DestUri];
Expand All @@ -79,7 +85,7 @@ send_request(Req, Opts) ->
{ok, SentReq} ->
{ok, SentReq};
error ->
AppId:nkcb_debug(Req, uac_out_request_error),
AppId:nkcb_debug(AppId, CallId, uac_out_request_error),
{error, service_unavailable}
end.

Expand Down
8 changes: 5 additions & 3 deletions src/nksip_call_uas_transp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
send_response(#sipmsg{class={resp, Code, _Reason}}=Resp, Opts) ->
#sipmsg{
app_id = AppId,
call_id = CallId,
vias = [Via|_],
cseq = {_, Method},
transport = Transp
Expand All @@ -66,7 +67,7 @@ send_response(#sipmsg{class={resp, Code, _Reason}}=Resp, Opts) ->
GlobalId = nksip_config_cache:global_id(),
RouteHash = <<"NkQ", (nksip_lib:hash({GlobalId, AppId, RouteBranch}))/binary>>,
MakeRespFun = make_response_fun(RouteHash, Resp, Opts),
AppId:nkcb_debug(Resp, {send_response, Method, Code}),
AppId:nkcb_debug(AppId, CallId, {send_response, Method, Code}),
Return = nksip_transport:send(AppId, TranspSpec, MakeRespFun, Opts),
AppId:nkcb_transport_uas_sent(Resp),
Return.
Expand All @@ -76,13 +77,14 @@ send_response(#sipmsg{class={resp, Code, _Reason}}=Resp, Opts) ->
-spec resend_response(Resp::nksip:response(), nksip:optslist()) ->
{ok, nksip:response()} | error.

resend_response(#sipmsg{class={resp, Code, _}, app_id=AppId, cseq={_, Method},
resend_response(#sipmsg{class={resp, Code, _},
transport=#transport{}=Transport}=Resp, Opts) ->
#sipmsg{app_id=AppId, cseq={_, Method}, call_id=CallId} = Resp,
#transport{proto=Proto, remote_ip=Ip, remote_port=Port, resource=Res} = Transport,
MakeResp = fun(_) -> Resp end,
TranspSpec = [{current, {Proto, Ip, Port, Res}}],
Return = nksip_transport:send(AppId, TranspSpec, MakeResp, Opts),
AppId:nkcb_debug(Resp, {sent_response, Method, Code}),
AppId:nkcb_debug(AppId, CallId, {sent_response, Method, Code}),
Return;

resend_response(Resp, Opts) ->
Expand Down
10 changes: 1 addition & 9 deletions src/nksip_callbacks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
-export([nkcb_connection_sent/2, nkcb_connection_recv/4]).
-export([nkcb_handle_call/3, nkcb_handle_cast/2, nkcb_handle_info/2,
nkcb_sipapp_updated/1]).
-export([nkcb_debug/2, nkcb_debug/3]).
-export([nkcb_debug/3]).

-type nkcb_common() :: continue | {continue, list()}.

Expand Down Expand Up @@ -319,14 +319,6 @@ nkcb_sipapp_updated(SipAppState) ->
{ok, SipAppState}.


%% doc Called at specific debug points
-spec nkcb_debug(nksip:request()|nksip:response(), term()) ->
ok | nkcb_common().

nkcb_debug(_SipMsg, _Info) ->
ok.


%% doc Called at specific debug points
-spec nkcb_debug(nksip:app_id(), nksip:call_id(), term()) ->
ok | nkcb_common().
Expand Down
6 changes: 5 additions & 1 deletion src/nksip_sipapp_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ default_config() ->
{event_expires, 60}, % (secs) 1 min
{nonce_timeout, 30}, % (secs) 30 secs
{max_calls, 100000}, % Each Call-ID counts as a call
{max_connections, 1024} % Per transport and SipApp
{max_connections, 1024}, % Per transport and SipApp
{debug, false} % Used in nksip_debug plugin
].


Expand Down Expand Up @@ -303,6 +304,8 @@ parse_opts([Term|Rest], Opts) ->
{no_100, _} ->
error;

{debug, Term} -> {update, Term};

{log_level, debug} -> {update, 8};
{log_level, info} -> {update, 7};
{log_level, notice} -> {update, 6};
Expand Down Expand Up @@ -412,6 +415,7 @@ cache_syntax(Opts) ->
{config, Opts},
{config_plugins, nksip_lib:get_value(sorted_plugins, Opts, [])},
{config_log_level, nksip_lib:get_value(log_level, Opts)},
{config_debug, nksip_lib:get_value(debug, Opts)},
{config_max_connections, nksip_lib:get_value(max_connections, Opts)},
{config_max_calls, nksip_lib:get_value(max_calls, Opts)},
{config_timers, #call_timers{
Expand Down

0 comments on commit 48ab0e5

Please sign in to comment.