From 859aea44f22ffb7d9de1ba508578b4ba543621fe Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Thu, 27 Mar 2025 16:04:24 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E6=97=A5=E5=BF=97=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/iot/src/iot_logger.erl | 37 +++++-------------------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/apps/iot/src/iot_logger.erl b/apps/iot/src/iot_logger.erl index 79b62dc..37e8ff6 100644 --- a/apps/iot/src/iot_logger.erl +++ b/apps/iot/src/iot_logger.erl @@ -13,7 +13,6 @@ %% 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]). @@ -26,7 +25,7 @@ %% 用来保存历史文件信息 queue = queue:new(), counter = 5, - file + file :: file:io_device() }). %%%=================================================================== @@ -61,7 +60,7 @@ init([FileName, Counter]) -> FilePath = make_file(FileName, erlang:date()), {ok, IoDevice} = file:open(FilePath, [append, binary]), - {ok, #state{file = {IoDevice, FilePath}, queue = queue:from_list([FilePath]), file_name = FileName, counter = Counter, date = get_date()}}. + {ok, #state{file = IoDevice, queue = queue:from_list([FilePath]), file_name = FileName, counter = Counter, date = get_date()}}. %% @private %% @doc Handling call messages @@ -82,16 +81,13 @@ handle_call(_Request, _From, State = #state{}) -> {noreply, NewState :: #state{}} | {noreply, NewState :: #state{}, timeout() | hibernate} | {stop, Reason :: term(), NewState :: #state{}}). -handle_cast({write, Data}, State = #state{file = {OldIoDevice, OldFilePath}, queue = Q, counter = Counter, file_name = FileName, date = Date}) -> +handle_cast({write, Data}, State = #state{file = OldIoDevice, queue = Q, counter = Counter, file_name = FileName, date = Date}) -> Line = iolist_to_binary([time_prefix(), <<" ">>, format(Data), <<$\n>>]), NDate = erlang:date(), case maybe_new_file(Date, NDate) of true -> ok = file:close(OldIoDevice), - %% 归档 - erlang:spawn(fun() -> archive_file(OldFilePath) end), - %% 开启新的文件 NewFilePath = make_file(FileName, NDate), {ok, NewIoDevice} = file:open(NewFilePath, [append, binary]), @@ -105,7 +101,7 @@ handle_cast({write, Data}, State = #state{file = {OldIoDevice, OldFilePath}, que false -> queue:in(NewFilePath, Q) end, - {noreply, State#state{file = {NewIoDevice, NewFilePath}, queue = NQ, date = get_date()}}; + {noreply, State#state{file = NewIoDevice, queue = NQ, date = get_date()}}; false -> ok = file:write(OldIoDevice, Line), {noreply, State} @@ -175,27 +171,4 @@ get_date() -> %% 通过日志判断是否需要生成新的日志文件 -spec maybe_new_file(Date :: calendar:date(), Date0 :: calendar:date()) -> boolean(). maybe_new_file({Y, M, D}, {Y0, M0, D0}) -> - not (Y =:= Y0 andalso M =:= M0 andalso D =:= D0). - -%-spec delete_old_files(FileName :: string(), Days :: integer()) -> no_return(). -%delete_old_files(FileName, Days) when is_list(FileName), is_integer(Days) -> -% Seconds0 = calendar:datetime_to_gregorian_seconds(calendar:local_time()), -% Seconds = Seconds0 - Days * 86400, -% lists:foreach(fun(Day) -> -% {Date, _} = calendar:gregorian_seconds_to_datetime(Seconds - Day * 86400), -% 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). \ No newline at end of file + not (Y =:= Y0 andalso M =:= M0 andalso D =:= D0). \ No newline at end of file