aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/test/r9c_tests_SUITE_data/src/inets/mod_log.erl
blob: 29fa2cfd11f1cbaa622bc1d709c375a3fb786388 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%% 
%%     $Id: mod_log.erl,v 1.1 2008/12/17 09:53:35 mikpe Exp $
%%
-module(mod_log).
-export([do/1,error_log/5,security_log/2,load/2,store/2,remove/1]).

-export([report_error/2]).

-include("httpd.hrl").

-define(VMODULE,"LOG").
-include("httpd_verbosity.hrl").

%% do

do(Info) ->
    AuthUser = auth_user(Info#mod.data),
    Date     = custom_date(),
    log_internal_info(Info,Date,Info#mod.data),
    case httpd_util:key1search(Info#mod.data,status) of
	%% A status code has been generated!
	{StatusCode,PhraseArgs,Reason} ->
	    transfer_log(Info,"-",AuthUser,Date,StatusCode,0),
	    if
		StatusCode >= 400 ->
		    error_log(Info,Date,Reason);
		true ->
		    not_an_error
	    end,
	    {proceed,Info#mod.data};
	%% No status code has been generated!
	undefined ->
	    case httpd_util:key1search(Info#mod.data,response) of
		{already_sent,StatusCode,Size} ->
		    transfer_log(Info,"-",AuthUser,Date,StatusCode,Size),
		    {proceed,Info#mod.data};
		{response,Head,Body} ->
		    Size=httpd_util:key1search(Head,content_length,unknown),
		    Code=httpd_util:key1search(Head,code,unknown),
		    transfer_log(Info,"-",AuthUser,Date,Code,Size),
		    {proceed,Info#mod.data};
		{StatusCode,Response} ->
		    transfer_log(Info,"-",AuthUser,Date,200,
				 httpd_util:flatlength(Response)),
		    {proceed,Info#mod.data};
		undefined ->
		    transfer_log(Info,"-",AuthUser,Date,200,0),
		    {proceed,Info#mod.data}
	    end
    end.

custom_date() ->
    LocalTime=calendar:local_time(),
    UniversalTime=calendar:universal_time(),
    Minutes=round(diff_in_minutes(LocalTime,UniversalTime)),
    {{YYYY,MM,DD},{Hour,Min,Sec}}=LocalTime,
    Date = 
	io_lib:format("~.2.0w/~.3s/~.4w:~.2.0w:~.2.0w:~.2.0w ~c~.2.0w~.2.0w",
		      [DD, httpd_util:month(MM), YYYY, Hour, Min, Sec, 
		       sign(Minutes),
		       abs(Minutes) div 60, abs(Minutes) rem 60]),  
    lists:flatten(Date).

diff_in_minutes(L,U) ->
    (calendar:datetime_to_gregorian_seconds(L) -
     calendar:datetime_to_gregorian_seconds(U))/60.

sign(Minutes) when Minutes > 0 ->
    $+;
sign(Minutes) ->
    $-.

auth_user(Data) ->
    case httpd_util:key1search(Data,remote_user) of
	undefined ->
	    "-";
	RemoteUser ->
	    RemoteUser
    end.

%% log_internal_info

log_internal_info(Info,Date,[]) ->
    ok;
log_internal_info(Info,Date,[{internal_info,Reason}|Rest]) ->
    error_log(Info,Date,Reason),
    log_internal_info(Info,Date,Rest);
log_internal_info(Info,Date,[_|Rest]) ->
    log_internal_info(Info,Date,Rest).

%% transfer_log

transfer_log(Info,RFC931,AuthUser,Date,StatusCode,Bytes) ->
    case httpd_util:lookup(Info#mod.config_db,transfer_log) of
	undefined ->
	    no_transfer_log;
	TransferLog ->
	    {PortNumber,RemoteHost}=(Info#mod.init_data)#init_data.peername,
	    case (catch io:format(TransferLog, "~s ~s ~s [~s] \"~s\" ~w ~w~n",
				  [RemoteHost, RFC931, AuthUser, 
				   Date, Info#mod.request_line,
				   StatusCode, Bytes])) of
		ok ->
		    ok;
		Error ->
		    error_logger:error_report(Error)
	    end
    end.

%% security log

security_log(Info, Reason) ->
    case httpd_util:lookup(Info#mod.config_db, security_log) of
	undefined ->
	    no_security_log;
	SecurityLog ->
	    io:format(SecurityLog,"[~s] ~s~n", [custom_date(), Reason])
    end.

%% error_log

error_log(Info,Date,Reason) ->
    case httpd_util:lookup(Info#mod.config_db, error_log) of
	undefined ->
	    no_error_log;
	ErrorLog ->
	    {PortNumber,RemoteHost}=(Info#mod.init_data)#init_data.peername,
	    io:format(ErrorLog,"[~s] access to ~s failed for ~s, reason: ~p~n",
		      [Date,Info#mod.request_uri,RemoteHost,Reason])
    end.

error_log(SocketType,Socket,ConfigDB,{PortNumber,RemoteHost},Reason) ->
    case httpd_util:lookup(ConfigDB,error_log) of
	undefined ->
	    no_error_log;
	ErrorLog ->
	    Date=custom_date(),
	    io:format(ErrorLog,"[~s] server crash for ~s, reason: ~p~n",
		      [Date,RemoteHost,Reason]),
	    ok
    end.

report_error(ConfigDB,Error) ->
    case httpd_util:lookup(ConfigDB,error_log) of
	undefined ->
	    no_error_log;
	ErrorLog ->
	    Date=custom_date(),
	    io:format(ErrorLog,"[~s] reporting error: ~s~n",[Date,Error]),
	    ok
    end.

%%
%% Configuration
%%

%% load

load([$T,$r,$a,$n,$s,$f,$e,$r,$L,$o,$g,$ |TransferLog],[]) ->
    {ok,[],{transfer_log,httpd_conf:clean(TransferLog)}};
load([$E,$r,$r,$o,$r,$L,$o,$g,$ |ErrorLog],[]) ->
    {ok,[],{error_log,httpd_conf:clean(ErrorLog)}};
load([$S,$e,$c,$u,$r,$i,$t,$y,$L,$o,$g,$ |SecurityLog], []) ->
    {ok, [], {security_log, httpd_conf:clean(SecurityLog)}}.

%% store

store({transfer_log,TransferLog},ConfigList) ->
    case create_log(TransferLog,ConfigList) of
	{ok,TransferLogStream} ->
	    {ok,{transfer_log,TransferLogStream}};
	{error,Reason} ->
	    {error,Reason}
    end;
store({error_log,ErrorLog},ConfigList) ->
    case create_log(ErrorLog,ConfigList) of
	{ok,ErrorLogStream} ->
	    {ok,{error_log,ErrorLogStream}};
	{error,Reason} ->
	    {error,Reason}
    end;
store({security_log, SecurityLog},ConfigList) ->
    case create_log(SecurityLog, ConfigList) of
	{ok, SecurityLogStream} ->
	    {ok, {security_log, SecurityLogStream}};
	{error, Reason} ->
	    {error, Reason}
    end.

create_log(LogFile,ConfigList) ->
    Filename = httpd_conf:clean(LogFile),
    case filename:pathtype(Filename) of
	absolute ->
	    case file:open(Filename, [read,write]) of
		{ok,LogStream} ->
		    file:position(LogStream,{eof,0}),
		    {ok,LogStream};
		{error,_} ->
		    {error,?NICE("Can't create "++Filename)}
	    end;
	volumerelative ->
	    case file:open(Filename, [read,write]) of
		{ok,LogStream} ->
		    file:position(LogStream,{eof,0}),
		    {ok,LogStream};
		{error,_} ->
		    {error,?NICE("Can't create "++Filename)}
	    end;
	relative ->
	    case httpd_util:key1search(ConfigList,server_root) of
		undefined ->
		    {error,
		     ?NICE(Filename++
			   " is an invalid logfile name beacuse ServerRoot is not defined")};
		ServerRoot ->
		    AbsoluteFilename=filename:join(ServerRoot,Filename),
		    case file:open(AbsoluteFilename, [read,write]) of
			{ok,LogStream} ->
			    file:position(LogStream,{eof,0}),
			    {ok,LogStream};
			{error,Reason} ->
			    {error,?NICE("Can't create "++AbsoluteFilename)}
		    end
	    end
    end.

%% remove

remove(ConfigDB) ->
    lists:foreach(fun([Stream]) -> file:close(Stream) end,
		  ets:match(ConfigDB,{transfer_log,'$1'})),
    lists:foreach(fun([Stream]) -> file:close(Stream) end,
		  ets:match(ConfigDB,{error_log,'$1'})),
    lists:foreach(fun([Stream]) -> file:close(Stream) end,
		  ets:match(ConfigDB,{security_log,'$1'})),
    ok.