From 85c944340757751e0e33ff4aaf20ece247c05283 Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Sun, 2 Jul 2017 10:55:43 +0200 Subject: [PATCH] Watcher state and constant --- apps/log_monitor/src/watcher.erl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/apps/log_monitor/src/watcher.erl b/apps/log_monitor/src/watcher.erl index e5a4ae5..ec1ead6 100644 --- a/apps/log_monitor/src/watcher.erl +++ b/apps/log_monitor/src/watcher.erl @@ -18,8 +18,9 @@ terminate/2, code_change/3]). -define(SERVER, ?MODULE). +-define(TAIL_CMD, "/usr/bin/tail -n0 --follow=name "). --record(state, {}). +-record(state, {file, supervisor, port}). %%%=================================================================== %%% API @@ -53,9 +54,9 @@ start_link(SupPid, File) -> init([SupPid, File]) -> timer:sleep(1000), config ! {watcher_init, File}, - Cmd = "/usr/bin/tail -n0 --follow=name " ++ File, + Cmd = ?TAIL_CMD ++ File, Port = open_port({spawn, Cmd}, [stderr_to_stdout, exit_status, binary]), - {ok, [File, SupPid, Port]}. + {ok, #state{file = File, supervisor = SupPid, port = Port}}. %%-------------------------------------------------------------------- %% @private @@ -98,14 +99,14 @@ handle_cast(_Msg, State) -> %% {stop, Reason, State} %% @end %%-------------------------------------------------------------------- -handle_info(Msg, [File, SupPid, Port]) -> +handle_info(Msg, State = #state{file = _, supervisor = SupPid, port = Port}) -> case Msg of {Port, {data, Text}} -> GathererPid = gatherer_pid(SupPid), GathererPid ! {log_line, binary_to_list(Text)}, - {noreply, [File, SupPid, Port]}; + {noreply, State}; {Port, {exit_status, _Status}} -> - {stop, tail_exit, [File]} + {stop, tail_exit, State} end. %%-------------------------------------------------------------------- @@ -119,8 +120,8 @@ handle_info(Msg, [File, SupPid, Port]) -> %% @spec terminate(Reason, State) -> void() %% @end %%-------------------------------------------------------------------- -terminate(_Reason, [File]) -> - config ! {watcher_terminate, File}, +terminate(_Reason, State) -> + config ! {watcher_terminate, State#state.file}, shutdown. %%--------------------------------------------------------------------