~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to lib/inets/examples/httpd_load_test/hdlt_logger.erl

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%%
 
2
%% %CopyrightBegin%
 
3
%%
 
4
%% Copyright Ericsson AB 2010. All Rights Reserved.
 
5
%%
 
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/.
 
11
%%
 
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
 
15
%% under the License.
 
16
%%
 
17
%% %CopyrightEnd%
 
18
%%
 
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
%%----------------------------------------------------------------------
 
25
 
 
26
%%
 
27
 
 
28
-module(hdlt_logger).
 
29
 
 
30
-export([
 
31
         start/0,
 
32
         set_level/1, get_level/0, set_name/1,
 
33
         info/2, log/2, debug/2
 
34
        ]).
 
35
 
 
36
-export([logger/1]).
 
37
 
 
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 ").
 
45
 
 
46
 
 
47
start() ->
 
48
    Self = self(), 
 
49
    proc_lib:start(?MODULE, logger, [Self]).
 
50
 
 
51
set_name(Name) when is_list(Name) ->
 
52
    put(?NAME, Name),
 
53
    ok.
 
54
 
 
55
get_level() ->
 
56
    get(?LEVEL).
 
57
 
 
58
set_level(Level) ->
 
59
    case lists:member(Level, [silence, info, log, debug]) of
 
60
        true ->
 
61
            put(?LEVEL, Level),
 
62
            ok;
 
63
        false ->
 
64
            erlang:error({bad_debug_level, Level})
 
65
    end.
 
66
 
 
67
 
 
68
info(F, A) ->
 
69
%%     io:format("info -> " ++ F ++ "~n", A),
 
70
    do_log(info, get(?LEVEL), F, A).
 
71
 
 
72
log(F, A) ->
 
73
%%     io:format("log -> " ++ F ++ "~n", A),
 
74
    do_log(log, get(?LEVEL), F, A).
 
75
 
 
76
debug(F, A) ->
 
77
%%     io:format("debug -> " ++ F ++ "~n", A),
 
78
    do_log(debug, get(?LEVEL), F, A).
 
79
 
 
80
 
 
81
logger(Parent) ->
 
82
    global:register_name(?LOGGER, self()),
 
83
    Ref = erlang:monitor(process, Parent),
 
84
    proc_lib:init_ack(self()),
 
85
    logger_loop(Ref).
 
86
 
 
87
logger_loop(Ref) ->
 
88
    receive
 
89
        {?MSG, F, A} ->
 
90
            io:format(F, A),
 
91
            logger_loop(Ref);
 
92
        {'DOWN', Ref, process, _Object, _Info} ->
 
93
            %% start the stop timer
 
94
            erlang:send_after(timer:seconds(5), self(), stop),
 
95
            logger_loop(undefined);
 
96
        stop ->
 
97
            global:unregister_name(?LOGGER),
 
98
            ok
 
99
    end.
 
100
 
 
101
 
 
102
formated_timestamp() ->
 
103
    {Date, Time}   = erlang:localtime(),
 
104
    {YYYY,MM,DD}   = Date,
 
105
    {Hour,Min,Sec} = Time,
 
106
    FormatDate =
 
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).
 
110
 
 
111
do_log(_, silence, _, _) ->
 
112
    ok;
 
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) ->
 
126
    ok.
 
127
 
 
128
do_log(SEV, F, A) ->
 
129
    Name = 
 
130
        case get(?NAME) of
 
131
            L when is_list(L) ->
 
132
                L;
 
133
            _ ->
 
134
                "UNDEFINED"
 
135
        end,
 
136
    Msg = {?MSG, "~s ~s [~s] " ++ F ++ "~n", 
 
137
           [SEV, Name, formated_timestamp() | A]}, 
 
138
    (catch global:send(?LOGGER, Msg)).