~clint-fewbar/ubuntu/precise/rabbitmq-server/package-2.7.1

« back to all changes in this revision

Viewing changes to src/rabbit_error_logger_file_h.erl

  • Committer: Clint Byrum
  • Date: 2012-01-27 19:48:27 UTC
  • mfrom: (0.2.15)
  • Revision ID: clint@ubuntu.com-20120127194827-9a4oooq196pslm54
New upstream release. (LP: #922600)

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
%% with the result of closing the old handler when swapping handlers.
27
27
%% The first init/1 additionally allows for simple log rotation
28
28
%% when the suffix is not the empty string.
 
29
%% The original init/2 also opened the file in 'write' mode, thus
 
30
%% overwriting old logs.  To remedy this, init/2 from
 
31
%% lib/stdlib/src/error_logger_file_h.erl from R14B3 was copied as
 
32
%% init_file/2 and changed so that it opens the file in 'append' mode.
29
33
 
30
34
%% Used only when swapping handlers in log rotation
31
35
init({{File, Suffix}, []}) ->
32
 
    case rabbit_misc:append_file(File, Suffix) of
33
 
        ok  -> ok;
 
36
    case rabbit_file:append_file(File, Suffix) of
 
37
        ok -> file:delete(File),
 
38
              ok;
34
39
        {error, Error} ->
35
40
            rabbit_log:error("Failed to append contents of "
36
41
                             "log file '~s' to '~s':~n~p~n",
45
50
%% log rotation
46
51
init({File, []}) ->
47
52
    init(File);
48
 
init({File, _Type} = FileInfo) ->
49
 
    rabbit_misc:ensure_parent_dirs_exist(File),
50
 
    error_logger_file_h:init(FileInfo);
 
53
%% Used only when taking over from the tty handler
 
54
init({{File, []}, _}) ->
 
55
    init(File);
 
56
init({File, {error_logger, Buf}}) ->
 
57
    rabbit_file:ensure_parent_dirs_exist(File),
 
58
    init_file(File, {error_logger, Buf});
51
59
init(File) ->
52
 
    rabbit_misc:ensure_parent_dirs_exist(File),
53
 
    error_logger_file_h:init(File).
 
60
    rabbit_file:ensure_parent_dirs_exist(File),
 
61
    init_file(File, []).
 
62
 
 
63
init_file(File, {error_logger, Buf}) ->
 
64
    case init_file(File, error_logger) of
 
65
        {ok, {Fd, File, PrevHandler}} ->
 
66
            [handle_event(Event, {Fd, File, PrevHandler}) ||
 
67
                {_, Event} <- lists:reverse(Buf)],
 
68
            {ok, {Fd, File, PrevHandler}};
 
69
        Error ->
 
70
            Error
 
71
    end;
 
72
init_file(File, PrevHandler) ->
 
73
    process_flag(trap_exit, true),
 
74
    case file:open(File, [append]) of
 
75
        {ok,Fd} -> {ok, {Fd, File, PrevHandler}};
 
76
        Error   -> Error
 
77
    end.
54
78
 
55
79
handle_event(Event, State) ->
56
80
    error_logger_file_h:handle_event(Event, State).