|
@ -31,7 +31,7 @@ |
|
|
md/0, md/1, |
|
|
md/0, md/1, |
|
|
rotate_handler/1, rotate_handler/2, rotate_sink/1, rotate_all/0, |
|
|
rotate_handler/1, rotate_handler/2, rotate_sink/1, rotate_all/0, |
|
|
trace/2, trace/3, trace_file/2, trace_file/3, trace_file/4, trace_console/1, trace_console/2, |
|
|
trace/2, trace/3, trace_file/2, trace_file/3, trace_file/4, trace_console/1, trace_console/2, |
|
|
install_trace/2, remove_trace/1, trace_func/3, |
|
|
|
|
|
|
|
|
install_trace/2, install_trace/3, remove_trace/1, trace_state/3, trace_func/3, |
|
|
list_all_sinks/0, clear_all_traces/0, clear_trace_by_destination/1, stop_trace/1, stop_trace/3, status/0, |
|
|
list_all_sinks/0, clear_all_traces/0, clear_trace_by_destination/1, stop_trace/1, stop_trace/3, status/0, |
|
|
get_loglevel/1, get_loglevel/2, set_loglevel/2, set_loglevel/3, set_loglevel/4, get_loglevels/1, |
|
|
get_loglevel/1, get_loglevel/2, set_loglevel/2, set_loglevel/3, set_loglevel/4, get_loglevels/1, |
|
|
update_loglevel_config/1, posix_error/1, set_loghwm/2, set_loghwm/3, set_loghwm/4, |
|
|
update_loglevel_config/1, posix_error/1, set_loghwm/2, set_loghwm/3, set_loghwm/4, |
|
@ -43,12 +43,25 @@ |
|
|
|
|
|
|
|
|
-export_type([log_level/0, log_level_number/0]). |
|
|
-export_type([log_level/0, log_level_number/0]). |
|
|
|
|
|
|
|
|
|
|
|
-record(trace_func_state_v1, { |
|
|
|
|
|
pid :: undefined | pid(), |
|
|
|
|
|
level :: log_level(), |
|
|
|
|
|
count :: infinity | pos_integer(), |
|
|
|
|
|
format_string :: string(), |
|
|
|
|
|
timeout :: infinity | pos_integer(), |
|
|
|
|
|
started = os:timestamp() :: erlang:timestamp() %% use os:timestamp for compatability |
|
|
|
|
|
}). |
|
|
|
|
|
|
|
|
%% API |
|
|
%% API |
|
|
|
|
|
|
|
|
%% @doc installs a lager trace handler into the target process (using sys:install) at the specified level. |
|
|
%% @doc installs a lager trace handler into the target process (using sys:install) at the specified level. |
|
|
-spec install_trace(pid(), log_level()) -> ok. |
|
|
-spec install_trace(pid(), log_level()) -> ok. |
|
|
install_trace(Pid, Level) -> |
|
|
install_trace(Pid, Level) -> |
|
|
sys:install(Pid, {fun ?MODULE:trace_func/3, {Pid, Level}}). |
|
|
|
|
|
|
|
|
install_trace(Pid, Level, []). |
|
|
|
|
|
|
|
|
|
|
|
-spec install_trace(pid(), log_level(), [{count, infinity | pos_integer()} | {format_string, string()} | {timeout, timeout()}]) -> ok. |
|
|
|
|
|
install_trace(Pid, Level, Options) -> |
|
|
|
|
|
sys:install(Pid, {fun ?MODULE:trace_func/3, trace_state(Pid, Level, Options)}). |
|
|
|
|
|
|
|
|
%% @doc remove a previously installed lager trace handler from the target process. |
|
|
%% @doc remove a previously installed lager trace handler from the target process. |
|
|
-spec remove_trace(pid()) -> ok. |
|
|
-spec remove_trace(pid()) -> ok. |
|
@ -677,6 +690,32 @@ rotate_handler(Handler, Sink) -> |
|
|
gen_event:call(Sink, Handler, rotate, ?ROTATE_TIMEOUT). |
|
|
gen_event:call(Sink, Handler, rotate, ?ROTATE_TIMEOUT). |
|
|
|
|
|
|
|
|
%% @private |
|
|
%% @private |
|
|
trace_func({Pid, Level}=FuncState, Event, ProcState) -> |
|
|
|
|
|
lager:log(Level, Pid, "TRACE ~p ~p", [Event, ProcState]), |
|
|
|
|
|
FuncState. |
|
|
|
|
|
|
|
|
trace_func(#trace_func_state_v1{pid=Pid, level=Level, format_string=Fmt}=FuncState, Event, ProcState) -> |
|
|
|
|
|
lager:log(Level, Pid, Fmt, [Event, ProcState]), |
|
|
|
|
|
check_timeout(decrement_count(FuncState)). |
|
|
|
|
|
|
|
|
|
|
|
%% @private |
|
|
|
|
|
trace_state(Pid, Level, Options) -> |
|
|
|
|
|
#trace_func_state_v1{pid=Pid, |
|
|
|
|
|
level=Level, |
|
|
|
|
|
count=proplists:get_value(count, Options, infinity), |
|
|
|
|
|
timeout=proplists:get_value(timeout, Options, infinity), |
|
|
|
|
|
format_string=proplists:get_value(format_string, Options, "TRACE ~p ~p")}. |
|
|
|
|
|
|
|
|
|
|
|
decrement_count(#trace_func_state_v1{count=infinity} = FuncState) -> |
|
|
|
|
|
FuncState; |
|
|
|
|
|
decrement_count(#trace_func_state_v1{count=1}) -> |
|
|
|
|
|
%% hit the counter limit |
|
|
|
|
|
done; |
|
|
|
|
|
decrement_count(#trace_func_state_v1{count=Count} = FuncState) -> |
|
|
|
|
|
FuncState#trace_func_state_v1{count=Count - 1}. |
|
|
|
|
|
|
|
|
|
|
|
check_timeout(#trace_func_state_v1{timeout=infinity} = FuncState) -> |
|
|
|
|
|
FuncState; |
|
|
|
|
|
check_timeout(#trace_func_state_v1{timeout=Timeout, started=Started} = FuncState) -> |
|
|
|
|
|
case (timer:now_diff(os:timestamp(), Started) / 1000) > Timeout of |
|
|
|
|
|
true -> |
|
|
|
|
|
done; |
|
|
|
|
|
false -> |
|
|
|
|
|
FuncState |
|
|
|
|
|
end. |