1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%----------------------------------------------------------------------
%% Purpose: This is a simple logger utility for the HDLT toolkit.
%% It assumesd that the debug level and the "name" of the
%% logging entity has been put in process environment
%% (using the set_level and set_name functions respectively).
%%----------------------------------------------------------------------
%%
-module(hdlt_logger).
-export([
start/0,
set_level/1, get_level/0, set_name/1,
info/2, log/2, debug/2
]).
-export([logger/1]).
-define(LOGGER, ?MODULE).
-define(MSG, hdlt_logger_msg).
-define(LEVEL, hdlt_logger_level).
-define(NAME, hdlt_logger_name).
-define(INFO_STR, "INFO").
-define(LOG_STR, "LOG ").
-define(DEBUG_STR, "DBG ").
start() ->
Self = self(),
proc_lib:start(?MODULE, logger, [Self]).
set_name(Name) when is_list(Name) ->
put(?NAME, Name),
ok.
get_level() ->
get(?LEVEL).
set_level(Level) ->
case lists:member(Level, [silence, info, log, debug]) of
true ->
put(?LEVEL, Level),
ok;
false ->
erlang:error({bad_debug_level, Level})
end.
info(F, A) ->
%% io:format("info -> " ++ F ++ "~n", A),
do_log(info, get(?LEVEL), F, A).
log(F, A) ->
%% io:format("log -> " ++ F ++ "~n", A),
do_log(log, get(?LEVEL), F, A).
debug(F, A) ->
%% io:format("debug -> " ++ F ++ "~n", A),
do_log(debug, get(?LEVEL), F, A).
logger(Parent) ->
global:register_name(?LOGGER, self()),
Ref = erlang:monitor(process, Parent),
proc_lib:init_ack(self()),
logger_loop(Ref).
logger_loop(Ref) ->
receive
{?MSG, F, A} ->
io:format(F, A),
logger_loop(Ref);
{'DOWN', Ref, process, _Object, _Info} ->
%% start the stop timer
erlang:send_after(timer:seconds(5), self(), stop),
logger_loop(undefined);
stop ->
global:unregister_name(?LOGGER),
ok
end.
formated_timestamp() ->
{Date, Time} = erlang:localtime(),
{YYYY,MM,DD} = Date,
{Hour,Min,Sec} = Time,
FormatDate =
io_lib:format("~.4w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w",
[YYYY,MM,DD,Hour,Min,Sec]),
lists:flatten(FormatDate).
do_log(_, silence, _, _) ->
ok;
do_log(info, info, F, A) ->
do_log(?INFO_STR, F, A);
do_log(info, log, F, A) ->
do_log(?INFO_STR, F, A);
do_log(log, log, F, A) ->
do_log(?LOG_STR, F, A);
do_log(info, debug, F, A) ->
do_log(?INFO_STR, F, A);
do_log(log, debug, F, A) ->
do_log(?LOG_STR, F, A);
do_log(debug, debug, F, A) ->
do_log(?DEBUG_STR, F, A);
do_log(_, _, _F, _A) ->
ok.
do_log(SEV, F, A) ->
Name =
case get(?NAME) of
L when is_list(L) ->
L;
_ ->
"UNDEFINED"
end,
Msg = {?MSG, "~s ~s [~s] " ++ F ++ "~n",
[SEV, Name, formated_timestamp() | A]},
(catch global:send(?LOGGER, Msg)).
|