Sha256: 675f91d503e288183c6482f3b7a399fca342ee65e0e3df0790cf79bcc865b585

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

-module (tail_log).
-compile(export_all).
-include ("cloudpanel.hrl").

start(File) ->
	start(File, fun display/1, "/var/log").

start(File, Callback) ->
  Dir = "/var/log",
  start(File, Callback, Dir).

start(File, Callback, Dir) ->
  spawn_link(?MODULE, init, [File, Callback, Dir]).

stop(Pid) ->
	Pid ! stop.

init(File, Callback, Dir) ->	
  Cmd = "/usr/bin/tail -f "++ Dir ++ "/" ++ File,
  Port = open_port({spawn, Cmd}, [ {cd, Dir}, stderr_to_stdout, {line, 256}, exit_status, binary]), 	
	tail_loop(Port, Callback).

snap(Pid) ->	
	Pid ! {snap, self() },
	receive		
		{Port, Callback} ->
			display(element(2, {Port, Callback}));
		Any ->
			io:format("Received: ~p~n", [Any]),
			Any
	after ?TIMEOUT ->
		"Timeout error"
	end.

tail_loop(Port, Callback) ->
	receive
		{Port, {data, {eol, Bin}}} ->
			Callback(Bin),
			tail_loop(Port, Callback);
		{Port, {data, Bin}} ->
			Callback(Bin),
			tail_loop(Port, Callback);
		{snap, Who} ->
			Who ! {Port, Callback},
			tail_loop(Port, Callback);
		stop ->
			port_close(Port),
			{ok, stop};
		{From, post, Mess} ->
			io:format("Received post message: ~p ~p~n", [From, Mess]),
			From ! {From, post, Mess};
		{From, subscribe} ->
			io:format("Received subscribe message from ~p~n", [From]),
			From ! {From, subscribe};
		{_, {exit_status, Num}} ->
			io:format("Received error messate: ~p~n", [Num]),
			port_close(Port),
			{ok, stop};
		Any ->
			io:format("Received unknown Message: ~p~n", [Any]),
			tail_loop(Port, Callback)
	end.

display(Bin) ->
  Content = iolist_to_binary(Bin),
  utils:subst("[INFO] ~s~n", [Content]).

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
auser-poolparty-0.2.40 lib/erlang/cloudpanel/src/tail_log.erl
auser-poolparty-0.2.41 lib/erlang/cloudpanel/src/tail_log.erl
auser-poolparty-0.2.42 lib/erlang/cloudpanel/src/tail_log.erl
auser-poolparty-0.2.44 lib/erlang/cloudpanel/src/tail_log.erl
auser-poolparty-0.2.45 lib/erlang/cloudpanel/src/tail_log.erl