4
%% Copyright Ericsson AB 2010. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
19
%%----------------------------------------------------------------------
20
%% Purpose: This is a simple logger utility for the HDLT toolkit.
21
%% It assumesd that the debug level and the "name" of the
22
%% logging entity has been put in process environment
23
%% (using the set_level and set_name functions respectively).
24
%%----------------------------------------------------------------------
32
set_level/1, get_level/0, set_name/1,
33
info/2, log/2, debug/2
38
-define(LOGGER, ?MODULE).
39
-define(MSG, hdlt_logger_msg).
40
-define(LEVEL, hdlt_logger_level).
41
-define(NAME, hdlt_logger_name).
42
-define(INFO_STR, "INFO").
43
-define(LOG_STR, "LOG ").
44
-define(DEBUG_STR, "DBG ").
49
proc_lib:start(?MODULE, logger, [Self]).
51
set_name(Name) when is_list(Name) ->
59
case lists:member(Level, [silence, info, log, debug]) of
64
erlang:error({bad_debug_level, Level})
69
%% io:format("info -> " ++ F ++ "~n", A),
70
do_log(info, get(?LEVEL), F, A).
73
%% io:format("log -> " ++ F ++ "~n", A),
74
do_log(log, get(?LEVEL), F, A).
77
%% io:format("debug -> " ++ F ++ "~n", A),
78
do_log(debug, get(?LEVEL), F, A).
82
global:register_name(?LOGGER, self()),
83
Ref = erlang:monitor(process, Parent),
84
proc_lib:init_ack(self()),
92
{'DOWN', Ref, process, _Object, _Info} ->
93
%% start the stop timer
94
erlang:send_after(timer:seconds(5), self(), stop),
95
logger_loop(undefined);
97
global:unregister_name(?LOGGER),
102
formated_timestamp() ->
103
{Date, Time} = erlang:localtime(),
105
{Hour,Min,Sec} = Time,
107
io_lib:format("~.4w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w",
108
[YYYY,MM,DD,Hour,Min,Sec]),
109
lists:flatten(FormatDate).
111
do_log(_, silence, _, _) ->
113
do_log(info, info, F, A) ->
114
do_log(?INFO_STR, F, A);
115
do_log(info, log, F, A) ->
116
do_log(?INFO_STR, F, A);
117
do_log(log, log, F, A) ->
118
do_log(?LOG_STR, F, A);
119
do_log(info, debug, F, A) ->
120
do_log(?INFO_STR, F, A);
121
do_log(log, debug, F, A) ->
122
do_log(?LOG_STR, F, A);
123
do_log(debug, debug, F, A) ->
124
do_log(?DEBUG_STR, F, A);
125
do_log(_, _, _F, _A) ->
136
Msg = {?MSG, "~s ~s [~s] " ++ F ++ "~n",
137
[SEV, Name, formated_timestamp() | A]},
138
(catch global:send(?LOGGER, Msg)).