aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/src
diff options
context:
space:
mode:
authorSiri Hansen <[email protected]>2018-12-03 11:37:31 +0100
committerSiri Hansen <[email protected]>2018-12-03 11:37:31 +0100
commit3cad3753659ca96e08694c8d5f74bab5a5c7b517 (patch)
tree763750be606087d9cefc317753e6524ce3a36426 /lib/kernel/src
parent721b04340b666d476732dfba31f5c89091085d6e (diff)
parentc64420ee0c50958c61958fcec4947fbbe721a661 (diff)
downloadotp-3cad3753659ca96e08694c8d5f74bab5a5c7b517.tar.gz
otp-3cad3753659ca96e08694c8d5f74bab5a5c7b517.tar.bz2
otp-3cad3753659ca96e08694c8d5f74bab5a5c7b517.zip
Merge branch 'siri/logger/std-recreate-file-if-rotated/OTP-15469' into maint
* siri/logger/std-recreate-file-if-rotated/OTP-15469: [logger] Re-create log file if deleted
Diffstat (limited to 'lib/kernel/src')
-rw-r--r--lib/kernel/src/logger_std_h.erl45
1 files changed, 31 insertions, 14 deletions
diff --git a/lib/kernel/src/logger_std_h.erl b/lib/kernel/src/logger_std_h.erl
index ebe741e331..63d1dbaba2 100644
--- a/lib/kernel/src/logger_std_h.erl
+++ b/lib/kernel/src/logger_std_h.erl
@@ -328,27 +328,30 @@ file_ctrl_loop(Fd, DevName, Synced,
receive
%% asynchronous event
{log,Bin} ->
- Result = write_to_dev(Fd, Bin, DevName, PrevWriteResult, HandlerName),
- file_ctrl_loop(Fd, DevName, false,
+ Fd1 = ensure(Fd, DevName),
+ Result = write_to_dev(Fd1, Bin, DevName, PrevWriteResult, HandlerName),
+ file_ctrl_loop(Fd1, DevName, false,
Result, PrevSyncResult, HandlerName);
%% synchronous event
{{log,Bin},{From,MRef}} ->
- check_exist(Fd, DevName),
- Result = write_to_dev(Fd, Bin, DevName, PrevWriteResult, HandlerName),
+ Fd1 = ensure(Fd, DevName),
+ Result = write_to_dev(Fd1, Bin, DevName, PrevWriteResult, HandlerName),
From ! {MRef,ok},
- file_ctrl_loop(Fd, DevName, false,
+ file_ctrl_loop(Fd1, DevName, false,
Result, PrevSyncResult, HandlerName);
filesync ->
- Result = sync_dev(Fd, DevName, Synced, PrevSyncResult, HandlerName),
- file_ctrl_loop(Fd, DevName, true,
+ Fd1 = ensure(Fd, DevName),
+ Result = sync_dev(Fd1, DevName, Synced, PrevSyncResult, HandlerName),
+ file_ctrl_loop(Fd1, DevName, true,
PrevWriteResult, Result, HandlerName);
{filesync,{From,MRef}} ->
- Result = sync_dev(Fd, DevName, Synced, PrevSyncResult, HandlerName),
+ Fd1 = ensure(Fd, DevName),
+ Result = sync_dev(Fd1, DevName, Synced, PrevSyncResult, HandlerName),
From ! {MRef,ok},
- file_ctrl_loop(Fd, DevName, true,
+ file_ctrl_loop(Fd1, DevName, true,
PrevWriteResult, Result, HandlerName);
stop ->
@@ -356,11 +359,25 @@ file_ctrl_loop(Fd, DevName, Synced,
stopped
end.
-check_exist(DevName, DevName) when is_atom(DevName) ->
- ok;
-check_exist(_Fd, FileName) ->
- _ = spawn_link(fun() -> {ok,_} = file:read_file_info(FileName) end),
- ok.
+%% In order to play well with tools like logrotate, we need to be able
+%% to re-create the file if it has disappeared (e.g. if rotated by
+%% logrotate)
+ensure(Fd,DevName) when is_atom(DevName) ->
+ Fd;
+ensure(Fd,FileName) ->
+ case file:read_file_info(FileName) of
+ {ok,_} ->
+ Fd;
+ _ ->
+ _ = file:close(Fd),
+ _ = file:close(Fd), % delayed_write cause close not to close
+ case do_open_log_file({file,FileName}) of
+ {ok,Fd1} ->
+ Fd1;
+ Error ->
+ exit({could_not_reopen_file,Error})
+ end
+ end.
write_to_dev(DevName, Bin, _DevName, _PrevWriteResult, _HandlerName)
when is_atom(DevName) ->