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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 | % %CopyrightBegin%
%
%
% | /¯\ /¯_ /¯_ |_¯ |¯\ |¯\ | (` |_/ | /¯\ /¯_ | |
% |__ \_/ \_/ \_/ |__ |¯\ ___ |_/ | \_) | \ ___ |__ \_/ \_/ ___ |¯| . E R L
%\
%%
%% Copyright Ericsson AB 2017-2018. All Rights Reserved.
%%
%%
%% Whitespace Beautified by ScriptCulture © JANUARY 2019
%% Sit Back · Feet Up · Learn Erlang ¯¯¯¯¯¯¯
%% For use as a reference only
%% www.scriptculture.com
%% Not check-summed
%% kernel-6.1.1
%%
%% 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%
-module(logger_disk_log_h).
-include("logger.hrl" ).
-include("logger_internal.hrl").
-include("logger_h_common.hrl").
%%% API
-export([ info/1
, filesync/1
, reset/1
]).
%% logger_h_common callbacks
-export([ init/2
, check_config/4
, reset_state/2
, filesync/3
, write/4
, handle_info/3
, terminate/3
]).
%% logger callbacks
-export([ log/2
, adding_handler/1
, removing_handler/1
, changing_config/3
, filter_config/1
]).
%%%===================================================================
%%% API
%%%===================================================================
%%%-----------------------------------------------------------------
%%%
-spec filesync(Name) -> ok | {error,Reason}
when
Name :: atom(),
Reason :: handler_busy
| {badarg,term()}.
% Write buffered data to disk.
filesync(Name) ->
logger_h_common:filesync(?MODULE,Name).
%%%-----------------------------------------------------------------
%%%
-spec info(Name) -> Info | {error,Reason}
when
Name :: atom() ,
Info :: term() ,
Reason :: handler_busy
| {badarg,term()}.
info(Name) ->
logger_h_common:info(?MODULE,Name).
%%%-----------------------------------------------------------------
%%%
-spec reset(Name) -> ok | {error,Reason}
when
Name :: atom() ,
Reason :: handler_busy
| {badarg, term()}.
reset(Name) ->
logger_h_common:reset(?MODULE,Name).
%%%===================================================================
%%% logger callbacks
%%%===================================================================
%%%-----------------------------------------------------------------
%%% Handler being added
adding_handler( Config)
->
logger_h_common:adding_handler(Config).
%%%-----------------------------------------------------------------
%%% Updating handler config
changing_config( SetOrUpdate, OldConfig, NewConfig)
->
logger_h_common:changing_config(SetOrUpdate, OldConfig, NewConfig).
%%%-----------------------------------------------------------------
%%% Handler being removed
removing_handler( Config)
->
logger_h_common:removing_handler(Config).
%%%-----------------------------------------------------------------
%%% Log a string or report
-spec log(LogEvent, Config) -> ok
when
LogEvent :: logger:log_event(),
Config :: logger:handler_config().
log( LogEvent, Config)
->
logger_h_common:log(LogEvent, Config).
%%%-----------------------------------------------------------------
%%% Remove internal fields from configuration
filter_config( Config)
->
logger_h_common:filter_config(Config).
%%%===================================================================
%%% logger_h_common callbacks
%%%===================================================================
init(Name, #{ file := File
, type := Type
, max_no_bytes := MNB
, max_no_files := MNF
})
->
case
open_disk_log(Name, File, Type, MNB, MNF)
of
ok ->
{ ok , #{ log_opts => #{ file => File,
type => Type,
max_no_bytes => MNB ,
max_no_files => MNF }
,
prev_log_result => ok
, prev_sync_result => ok
, prev_disk_log_info => undefined
}
}
;Error ->
Error
end
.
check_config( Name , set , undefined , HConfig0 )
->
HConfig = merge_default_logopts( Name
, maps:merge(get_default_config(),HConfig0)
)
,
check_config(HConfig)
;check_config(_Name, SetOrUpdate , OldHConfig , NewHConfig0 )
->
WriteOnce = maps:with([type,file,max_no_files,max_no_bytes],OldHConfig),
Default =
case
SetOrUpdate
of
set -> %% Do not reset write-once fields to defaults
maps:merge(get_default_config(),WriteOnce)
;update ->
OldHConfig
end
,
NewHConfig = maps:merge(Default,NewHConfig0),
%% Fail if write-once fields are changed
case
maps:with([type,file,max_no_files,max_no_bytes],NewHConfig)
of
WriteOnce -> check_config(NewHConfig)
;Other -> {Old,New} = logger_server:diff_maps(WriteOnce,Other),
{error,{illegal_config_change,?MODULE,Old,New}}
end
.
check_config(HConfig) ->
case
check_h_config(maps:to_list(HConfig))
of
ok -> {ok,HConfig}
;{error,{Key,Value}} -> {error,{invalid_config,?MODULE,#{Key=>Value}}}
end.
check_h_config([{ file , File }|Config]) when is_list(File) -> check_h_config(Config);
check_h_config([{max_no_files,undefined}|Config]) -> check_h_config(Config);
check_h_config([{max_no_files, N }|Config]) when is_integer(N), N > 0 -> check_h_config(Config);
check_h_config([{max_no_bytes, infinity}|Config]) -> check_h_config(Config);
check_h_config([{max_no_bytes, N }|Config]) when is_integer(N), N > 0 -> check_h_config(Config);
check_h_config([{ type , Type }|Config]) when Type==wrap; Type==halt -> check_h_config(Config);
check_h_config([ Other | _ ]) -> {error,Other};
check_h_config( [] ) -> ok.
get_default_config() ->
#{}.
merge_default_logopts(Name, HConfig)
->
Type = maps:get(type, HConfig, wrap)
,
{DefaultNoFiles,DefaultNoBytes} = case
Type
of
halt -> {undefined,infinity}
;_wrap -> { 10 ,1048576 }
end
,
{ok,Dir} = file:get_cwd(),
Defaults = #{ file => filename:join(Dir,Name),
max_no_files => DefaultNoFiles,
max_no_bytes => DefaultNoBytes,
type => Type
}
,
maps:merge(Defaults, HConfig)
.
filesync(Name,_Mode,State)
->
Result = ?disk_log_sync(Name),
maybe_notify_error(Name, filesync, Result, prev_sync_result, State)
.
write(Name, Mode, Bin, State)
->
Result = ?disk_log_write(Name, Mode, Bin),
maybe_notify_error(Name, log, Result, prev_log_result, State).
reset_state(_Name, State) ->
State#{prev_log_result => ok
,prev_sync_result => ok
,prev_disk_log_info => undefined
}
.
%% The disk log owner must handle status messages from disk_log.
handle_info( Name,{disk_log,_Node,Log,Info = { truncated ,_NoLostItems}}, State)-> maybe_notify_status(Name,Log,Info,prev_disk_log_info,State)
;handle_info(Name,{disk_log,_Node,Log,Info = { blocked_log, _Items}}, State)-> maybe_notify_status(Name,Log,Info,prev_disk_log_info,State)
;handle_info(Name,{disk_log,_Node,Log,Info = full }, State)-> maybe_notify_status(Name,Log,Info,prev_disk_log_info,State)
;handle_info(Name,{disk_log,_Node,Log,Info = {error_status, _Status}}, State)-> maybe_notify_status(Name,Log,Info,prev_disk_log_info,State)
;handle_info( _ , _ , State)-> State.
terminate(Name, _Reason, _State) ->
_ = close_disk_log(Name, normal),
ok.
%%%-----------------------------------------------------------------
%%% Internal functions
open_disk_log(Name, File, Type, MaxNoBytes, MaxNoFiles) ->
case
filelib:ensure_dir(File)
of
ok ->
Size = if Type == halt -> MaxNoBytes ;
Type == wrap -> {MaxNoBytes,MaxNoFiles}
end
,
Opts = [{name, Name },
{file, File },
{size, Size },
{type, Type },
{linkto, self() },
{repair, false },
{format, external },
{notify, true },
{quiet, true },
{mode, read_write }]
,
case
disk_log:open(Opts)
of
{ ok , Name } -> ok;
Error = {error, _Reason} -> Error
end
;Error ->
Error
end.
close_disk_log(Name, _) ->
_ = ?disk_log_sync( Name),
_ = disk_log:lclose(Name),
ok
.
disk_log_write(Name, sync, Bin) -> disk_log:blog( Name, Bin);
disk_log_write(Name, async, Bin) -> disk_log:balog(Name, Bin).
%%%-----------------------------------------------------------------
%%% Print error messages, but don't repeat the same message
maybe_notify_error( Name, Op, Result, Key, #{log_opts:=LogOpts}=State)
->
{Result,error_notify_new( { Name, Op, LogOpts, Result} , Result , Key , State) }.
maybe_notify_status( Name, Log, Info, Key, State)
->
error_notify_new({disk_log, Name, Log, Info}, Info, Key, State).
error_notify_new(Term, What, Key, State)
->
error_notify_new(What, maps:get(Key, State), Term),
State#{Key => What}.
error_notify_new( ok , _Prev, _Term ) -> ok
;error_notify_new( Same , Same, _Term ) -> ok
;error_notify_new( _New , _Prev, Term ) -> logger_h_common:error_notify(Term) .
%%%% LOGGER DISK LOG H - APPENDIX A - DESCRIPTION
%
% This is a handler for Logger that offers circular
% (wrapped) logs by using disk_log.
% Multiple instances of this handler can be added to Logger,
% and each instance prints to its own disk log file, created
% with the name and settings specified in the handler configuration.
%
% The default standard handler, logger_std_h, can be replaced
% by a disk_log handler at startup of the Kernel application.
% See an example of this below.
%
% The handler has an overload protection mechanism that keeps
% the handler process and the Kernel application alive during
% high loads of log events. How overload protection works,
% & how to configure it, is described in the User's Guide.
%
% To add a new instance of the disk_log handler, use logger:add_handler/3.
%
% The handler configuration argument is a map
% which can contain general configuration parameters,
% as documented in the User's Guide, and handler specific parameters.
% The specific data is stored in a sub map with the key config,
% and can contain the following parameters:
%
%
% file
% This is the full name of the disk log file.
% The option corresponds to the name property
% in the dlog_option() datatype.
%
% The value is set when the handler is added,
% and it can not be changed in runtime.
%
% Defaults to the same name as the handler identity,
% in the current directory.
%
%
% type
% This is the disk log type, wrap or halt.
% The option corresponds to the type property
% in the dlog_option() datatype.
%
% The value is set when the handler is added,
% and it can not be changed in runtime.
%
% Defaults to wrap.
%
%
% max_no_files
%
% This is the maximum number of files that
% disk_log uses for its circular logging.
% The option corresponds to the MaxNoFiles
% element in the size property in the
% dlog_option() datatype.
%
% The value is set when the handler is added,
% and it can not be changed in runtime.
%
% Defaults to 10.
%
% The setting has no effect on a halt log.
%
%
%
% max_no_bytes
% This is the maximum number of bytes that is written
% to a log file before disk_log proceeds with the
% next file in order, or generates an error in case
% of a full halt log. The option corresponds to the
% MaxNoBytes element in the size property in the
% dlog_option() datatype.
%
% The value is set when the handler is added,
% and it can not be changed in runtime.
%
% Defaults to 1048576 bytes for a wrap log, and infinity for a halt log.
%
%
%
% filesync_repeat_interval
%
% This value, in milliseconds, specifies how often
% the handler does a disk_log sync operation to write
% buffered data to disk. The handler attempts the operation
% repeatedly, but only performs a new sync if something has
% actually been logged.
%
% Defaults to 5000 milliseconds.
%
% If no_repeat is set as value, the repeated sync operation is disabled.
% The user can also call the filesync/1 function to perform a disk_log sync.
%
% Other configuration parameters exist,
% to be used for customizing the overload protection behaviour.
% The same parameters are used both in the standard handler and
% the disk_log handler, and are documented in the User's Guide.
%
% Notice that when changing the configuration of the handler in
% runtime, the disk_log options (file, type, max_no_files, max_no_bytes)
% must not be modified.
%
% Example of adding a disk_log handler:
%
% logger:add_handler(my_disk_log_h, logger_disk_log_h,
% #{config => #{file => "./my_disk_log",
% type => wrap,
% max_no_files => 4,
% max_no_bytes => 10000},
% filesync_repeat_interval => 1000}}).
%
% To use the disk_log handler instead of the default standard
% handler when starting an Erlang node,
% change the Kernel default logger to use
% logger_disk_log_h. Example:
%
% erl -kernel logger '[{handler,default,logger_disk_log_h,
% #{config => #{file => "./system_disk_log"}}}]'
%
%
%
|