aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/test/r9c_tests_SUITE_data/src/inets/mod_disk_log.erl
blob: c5d110ee4b71b093f90e28f9d0db57d84a7fb29e (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
%% ``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_disk_log.erl,v 1.1 2008/12/17 09:53:35 mikpe Exp $
%%
-module(mod_disk_log).
-export([do/1,error_log/5,security_log/2,load/2,store/2,remove/1]).

-export([report_error/2]).

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

-include("httpd.hrl").

%% do

do(Info) ->
    AuthUser  = auth_user(Info#mod.data),
    Date      = custom_date(),
    log_internal_info(Info,Date,Info#mod.data),
    LogFormat = get_log_format(Info#mod.config_db),
    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, LogFormat),
	    if
		StatusCode >= 400 ->
		    error_log(Info, Date, Reason, LogFormat);
		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, LogFormat),
		    {proceed,Info#mod.data};

		{response, Head, Body} ->
		    Size = httpd_util:key1search(Head, content_length, 0),
		    Code = httpd_util:key1search(Head, code, 200),
		    transfer_log(Info, "-", AuthUser, Date, Code, 
				 Size, LogFormat),
		    {proceed,Info#mod.data};	
		
		{StatusCode,Response} ->
		    transfer_log(Info, "-", AuthUser, Date, 200,
				 httpd_util:flatlength(Response), LogFormat),
		    {proceed,Info#mod.data};
		undefined ->
		    transfer_log(Info, "-", AuthUser, Date, 200,
				 0, LogFormat),
		    {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]) ->
    Format = get_log_format(Info#mod.config_db),
    error_log(Info,Date,Reason,Format),
    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,Format) ->
    case httpd_util:lookup(Info#mod.config_db,transfer_disk_log) of
	undefined ->
	    no_transfer_log;
	TransferDiskLog ->
	    {PortNumber,RemoteHost}=(Info#mod.init_data)#init_data.peername,
	    Entry = io_lib:format("~s ~s ~s [~s] \"~s\" ~w ~w~n",
				  [RemoteHost,RFC931,AuthUser,Date,
				   Info#mod.request_line,StatusCode,Bytes]),
	    write(TransferDiskLog, Entry, Format)
    end.


%% error_log

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

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


%% security_log

security_log(ConfigDB, Event) ->
    Format = get_log_format(ConfigDB),
    case httpd_util:lookup(ConfigDB,security_disk_log) of
	undefined ->
	    no_error_log;
	DiskLog ->
	    Date  = custom_date(),
	    Entry = io_lib:format("[~s] ~s ~n", [Date, Event]),
	    write(DiskLog, Entry, Format),
	    ok
    end.

report_error(ConfigDB, Error) ->
    Format = get_log_format(ConfigDB),
    case httpd_util:lookup(ConfigDB, error_disk_log) of
	undefined ->
	    no_error_log;
	ErrorDiskLog ->
	    Date  = custom_date(),
	    Entry = io_lib:format("[~s] reporting error: ~s",[Date,Error]),
	    write(ErrorDiskLog, Entry, Format),
	    ok
    end.

%%----------------------------------------------------------------------
%% Get the current format of the disklog
%%----------------------------------------------------------------------
get_log_format(ConfigDB)->
    httpd_util:lookup(ConfigDB,disk_log_format,external).


%%
%% Configuration
%%

%% load

load([$T,$r,$a,$n,$s,$f,$e,$r,$D,$i,$s,$k,$L,$o,$g,$S,$i,$z,$e,$ |
      TransferDiskLogSize],[]) ->
    case regexp:split(TransferDiskLogSize," ") of
	{ok,[MaxBytes,MaxFiles]} ->
	    case httpd_conf:make_integer(MaxBytes) of
		{ok,MaxBytesInteger} ->
		    case httpd_conf:make_integer(MaxFiles) of
			{ok,MaxFilesInteger} ->
			    {ok,[],{transfer_disk_log_size,
				    {MaxBytesInteger,MaxFilesInteger}}};
			{error,_} ->
			    {error,
			     ?NICE(httpd_conf:clean(TransferDiskLogSize)++
				   " is an invalid TransferDiskLogSize")}
		    end;
		{error,_} ->
		    {error,?NICE(httpd_conf:clean(TransferDiskLogSize)++
				 " is an invalid TransferDiskLogSize")}
	    end
    end;
load([$T,$r,$a,$n,$s,$f,$e,$r,$D,$i,$s,$k,$L,$o,$g,$ |TransferDiskLog],[]) ->
    {ok,[],{transfer_disk_log,httpd_conf:clean(TransferDiskLog)}};

load([$E,$r,$r,$o,$r,$D,$i,$s,$k,$L,$o,$g,$S,$i,$z,$e,$ | ErrorDiskLogSize],[]) ->
    case regexp:split(ErrorDiskLogSize," ") of
	{ok,[MaxBytes,MaxFiles]} ->
	    case httpd_conf:make_integer(MaxBytes) of
		{ok,MaxBytesInteger} ->
		    case httpd_conf:make_integer(MaxFiles) of
			{ok,MaxFilesInteger} ->
			    {ok,[],{error_disk_log_size,
				    {MaxBytesInteger,MaxFilesInteger}}};
			{error,_} ->
			    {error,?NICE(httpd_conf:clean(ErrorDiskLogSize)++
					 " is an invalid ErrorDiskLogSize")}
		    end;
		{error,_} ->
		    {error,?NICE(httpd_conf:clean(ErrorDiskLogSize)++
				 " is an invalid ErrorDiskLogSize")}
	    end
    end;
load([$E,$r,$r,$o,$r,$D,$i,$s,$k,$L,$o,$g,$ |ErrorDiskLog],[]) ->
    {ok, [], {error_disk_log, httpd_conf:clean(ErrorDiskLog)}};

load([$S,$e,$c,$u,$r,$i,$t,$y,$D,$i,$s,$k,$L,$o,$g,$S,$i,$z,$e,$ |SecurityDiskLogSize],[]) ->
    case regexp:split(SecurityDiskLogSize, " ") of
	{ok, [MaxBytes, MaxFiles]} ->
	    case httpd_conf:make_integer(MaxBytes) of
		{ok, MaxBytesInteger} ->
		    case httpd_conf:make_integer(MaxFiles) of
			{ok, MaxFilesInteger} ->
			    {ok, [], {security_disk_log_size,
				      {MaxBytesInteger, MaxFilesInteger}}};
			{error,_} ->
			    {error, ?NICE(httpd_conf:clean(SecurityDiskLogSize)++
					  " is an invalid SecurityDiskLogSize")}
		    end;
		{error, _} ->
		    {error, ?NICE(httpd_conf:clean(SecurityDiskLogSize)++
				  " is an invalid SecurityDiskLogSize")}
	    end
    end;
load([$S,$e,$c,$u,$r,$i,$t,$y,$D,$i,$s,$k,$L,$o,$g,$ |SecurityDiskLog],[]) ->
    {ok, [], {security_disk_log, httpd_conf:clean(SecurityDiskLog)}};

load([$D,$i,$s,$k,$L,$o,$g,$F,$o,$r,$m,$a,$t,$ |Format],[]) ->
    case httpd_conf:clean(Format) of
	"internal" ->
	    {ok, [], {disk_log_format,internal}};
	"external" ->
	    {ok, [], {disk_log_format,external}};
	_Default ->
	    {ok, [], {disk_log_format,external}}
    end.

%% store

store({transfer_disk_log,TransferDiskLog},ConfigList) ->
    case create_disk_log(TransferDiskLog, transfer_disk_log_size, ConfigList) of
	{ok,TransferDB} ->
	    {ok,{transfer_disk_log,TransferDB}};
	{error,Reason} ->
	    {error,Reason}
    end;
store({security_disk_log,SecurityDiskLog},ConfigList) ->
    case create_disk_log(SecurityDiskLog, security_disk_log_size, ConfigList) of
	{ok,SecurityDB} ->
	    {ok,{security_disk_log,SecurityDB}};
	{error,Reason} ->
	    {error,Reason}
    end;
store({error_disk_log,ErrorDiskLog},ConfigList) ->
    case create_disk_log(ErrorDiskLog, error_disk_log_size, ConfigList) of
	{ok,ErrorDB} ->
	    {ok,{error_disk_log,ErrorDB}};
	{error,Reason} ->
	    {error,Reason}
    end.


%%----------------------------------------------------------------------
%% Open or creates the disklogs 
%%----------------------------------------------------------------------
log_size(ConfigList, Tag) ->
    httpd_util:key1search(ConfigList, Tag, {500*1024,8}).

create_disk_log(LogFile, SizeTag, ConfigList) ->
    Filename = httpd_conf:clean(LogFile),
    {MaxBytes, MaxFiles} = log_size(ConfigList, SizeTag),
    case filename:pathtype(Filename) of
	absolute ->
	    create_disk_log(Filename, MaxBytes, MaxFiles, ConfigList);
	volumerelative ->
	    create_disk_log(Filename, MaxBytes, MaxFiles, ConfigList);
	relative ->
	    case httpd_util:key1search(ConfigList,server_root) of
		undefined ->
		    {error,
		     ?NICE(Filename++
			   " is an invalid ErrorLog beacuse ServerRoot is not defined")};
		ServerRoot ->
		    AbsoluteFilename = filename:join(ServerRoot,Filename),
		    create_disk_log(AbsoluteFilename, MaxBytes, MaxFiles,
				     ConfigList)
	    end
    end.

create_disk_log(Filename, MaxBytes, MaxFiles, ConfigList) ->
    Format = httpd_util:key1search(ConfigList, disk_log_format, external),
    open(Filename, MaxBytes, MaxFiles, Format).
    


%% remove
remove(ConfigDB) ->
    lists:foreach(fun([DiskLog]) -> close(DiskLog) end,
		  ets:match(ConfigDB,{transfer_disk_log,'$1'})),
    lists:foreach(fun([DiskLog]) -> close(DiskLog) end,
		  ets:match(ConfigDB,{error_disk_log,'$1'})),
    ok.


%% 
%% Some disk_log wrapper functions:
%% 

%%----------------------------------------------------------------------
%% Function:    open/4
%% Description: Open a disk log file.
%% Control which format the disk log will be in. The external file 
%% format is used as default since that format was used by older 
%% implementations of inets.
%%
%% When the internal disk log format is used, we will do some extra 
%% controls. If the files are valid, try to repair them and if 
%% thats not possible, truncate.
%%----------------------------------------------------------------------

open(Filename, MaxBytes, MaxFiles, internal) ->
    Opts = [{format, internal}, {repair, truncate}],
    open1(Filename, MaxBytes, MaxFiles, Opts);
open(Filename, MaxBytes, MaxFiles, _) ->
    Opts = [{format, external}],
    open1(Filename, MaxBytes, MaxFiles, Opts).

open1(Filename, MaxBytes, MaxFiles, Opts0) ->
    Opts1 = [{name, Filename}, {file, Filename}, {type, wrap}] ++ Opts0,
    case open2(Opts1, {MaxBytes, MaxFiles}) of
        {ok, LogDB} ->
            {ok, LogDB};
        {error, Reason} ->
            ?vlog("failed opening disk log with args:"
                  "~n   Filename: ~p"
                  "~n   MaxBytes: ~p"
                  "~n   MaxFiles: ~p"
                  "~n   Opts0:    ~p"
                  "~nfor reason:"
                  "~n   ~p", [Filename, MaxBytes, MaxFiles, Opts0, Reason]),
            {error, 
             ?NICE("Can't create " ++ Filename ++ 
                   lists:flatten(io_lib:format(", ~p",[Reason])))};
        _ ->
            {error, ?NICE("Can't create "++Filename)}
    end.

open2(Opts, Size) ->
    case disk_log:open(Opts) of
        {error, {badarg, size}} ->
            %% File did not exist, add the size option and try again
            disk_log:open([{size, Size} | Opts]);
        Else ->
            Else
    end.


%%----------------------------------------------------------------------
%% Actually writes the entry to the disk_log. If the log is an 
%% internal disk_log write it with log otherwise with blog.
%%----------------------------------------------------------------------  
write(Log, Entry, internal) ->
    disk_log:log(Log, Entry);

write(Log, Entry, _) ->
    disk_log:blog(Log, Entry).

%% Close the log file
close(Log) ->
    disk_log:close(Log).