fix logger

This commit is contained in:
anlicheng 2025-02-19 10:27:08 +08:00
parent b274b48e3d
commit 3b46f8fba4

View File

@ -13,6 +13,7 @@
%% API
-export([start_link/1, start_link/2, write/2]).
-export([archive_file/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
@ -89,12 +90,7 @@ handle_cast({write, Data}, State = #state{file = {OldIoDevice, OldFilePath}, que
true ->
ok = file:close(OldIoDevice),
%%
erlang:spawn(fun() ->
%%
ok = erl_tar:create(OldFilePath ++ ".tar", [OldFilePath], [compressed]),
%%
ok = file:delete(OldFilePath)
end),
erlang:spawn(fun() -> archive_file(OldFilePath) end),
%%
NewFilePath = make_file(FileName, NDate),
@ -190,3 +186,16 @@ maybe_new_file({Y, M, D}, {Y0, M0, D0}) ->
% FilePath = make_file(FileName, Date),
% filelib:is_file(FilePath) andalso file:delete(FilePath)
% end, lists:seq(1, 10)).
-spec archive_file(FilePath :: string()) -> ok.
archive_file(FilePath) when is_list(FilePath) ->
TarFile = FilePath ++ ".tar",
{ok, Tar} = erl_tar:open(TarFile, [write, compressed]),
BaseName = filename:basename(FilePath),
{ok, Data} = file:read_file(FilePath),
ok = erl_tar:add(Tar, Data, BaseName, []),
ok = erl_tar:close(Tar),
%%
ok = file:delete(FilePath).