20
20
-module (rabbit_logger_std_h ).
21
21
22
22
-ifdef (TEST ).
23
- -define (io_put_chars (DEVICE , DATA ), begin
24
- % % We log to Common Test log as well.
25
- % % This is the file we use to check
26
- % % the message made it to
27
- % % stdout/stderr.
28
- ct :log (" ~ts " , [DATA ]),
29
- io :put_chars (DEVICE , DATA )
30
- end ).
31
-
32
23
-export ([parse_date_spec /1 , parse_day_of_week /2 , parse_day_of_month /2 , parse_hour /2 , parse_minute /2 ]).
33
- -else .
34
- -define (io_put_chars (DEVICE , DATA ), io :put_chars (DEVICE , DATA )).
35
24
-endif .
25
+
36
26
-define (file_write (DEVICE , DATA ), file :write (DEVICE , DATA )).
37
27
-define (file_datasync (DEVICE ), file :datasync (DEVICE )).
38
28
50
40
-export ([log /2 , adding_handler /1 , removing_handler /1 , changing_config /3 ,
51
41
filter_config /1 ]).
52
42
43
+ % % Internal export to allow the use of meck.
44
+ -export ([io_put_chars /2 ]).
45
+
53
46
-define (DEFAULT_CALL_TIMEOUT , 5000 ).
54
47
55
48
% %%===================================================================
@@ -524,22 +517,40 @@ ensure_file(#{inode:=INode0,file_name:=FileName,modes:=Modes}=State) ->
524
517
State #{last_check => timestamp ()};
525
518
_ ->
526
519
close_log_file (State ),
527
- case file :open (FileName ,Modes ) of
528
- {ok ,Fd } ->
529
- {ok ,# file_info {inode = INode }} =
530
- file :read_file_info (FileName ,[raw ]),
531
- State #{fd => Fd ,inode => INode ,
532
- last_check => timestamp (),
533
- synced => true ,sync_res => ok };
534
- Error ->
535
- exit ({could_not_reopen_file ,Error })
536
- end
520
+ {ok , Fd } = ensure_open (FileName , Modes ),
521
+ {ok ,# file_info {inode = INode }} =
522
+ file :read_file_info (FileName ,[raw ]),
523
+ State #{fd => Fd ,inode => INode ,
524
+ last_check => timestamp (),
525
+ synced => true ,sync_res => ok }
537
526
end ;
538
527
ensure_file (State ) ->
539
528
State .
540
529
530
+ ensure_open (Filename , Modes ) ->
531
+ case filelib :ensure_dir (Filename ) of
532
+ ok ->
533
+ case file :open (Filename , Modes ) of
534
+ {ok , Fd } ->
535
+ {ok , Fd };
536
+ Error ->
537
+ exit ({could_not_reopen_file ,Error })
538
+ end ;
539
+ Error ->
540
+ exit ({could_not_create_dir_for_file ,Error })
541
+ end .
542
+
543
+ write_to_dev (Bin ,#{dev := standard_io }= State ) ->
544
+ try
545
+ ? MODULE :io_put_chars (user , Bin )
546
+ catch _E :_R ->
547
+ ? MODULE :io_put_chars (
548
+ standard_error , " Failed to write log message to stdout, trying stderr\n " ),
549
+ ? MODULE :io_put_chars (standard_error , Bin )
550
+ end ,
551
+ State ;
541
552
write_to_dev (Bin ,#{dev := DevName }= State ) ->
542
- ? io_put_chars (DevName , Bin ),
553
+ ? MODULE : io_put_chars (DevName , Bin ),
543
554
State ;
544
555
write_to_dev (Bin , State ) ->
545
556
State1 = #{fd := Fd } = maybe_ensure_file (State ),
@@ -548,6 +559,9 @@ write_to_dev(Bin, State) ->
548
559
maybe_notify_error (write ,Result ,State2 ),
549
560
State2 #{synced => false ,write_res => Result }.
550
561
562
+ io_put_chars (DevName , Bin ) ->
563
+ io :put_chars (DevName , Bin ).
564
+
551
565
sync_dev (#{synced := false }= State ) ->
552
566
State1 = #{fd := Fd } = maybe_ensure_file (State ),
553
567
Result = ? file_datasync (Fd ),
0 commit comments