|
1 | 1 | -module(rabbit_log_tail). |
2 | 2 |
|
3 | 3 | -export([tail_n_lines/2]). |
4 | | -% -export([init_tail_stream/2, tail_send/2, tail_receive/2]). |
| 4 | +-export([init_tail_stream/3]). |
5 | 5 |
|
6 | 6 | -define(GUESS_OFFSET, 200). |
7 | 7 |
|
| 8 | +init_tail_stream(Filename, Pid, Ref) -> |
| 9 | + RPCProc = self(), |
| 10 | + Reader = spawn(fun() -> |
| 11 | + link(Pid), |
| 12 | + case file:open(Filename, [read, binary]) of |
| 13 | + {ok, File} -> |
| 14 | + {ok, _} = file:position(File, eof), |
| 15 | + RPCProc ! {Ref, opened}, |
| 16 | + read_loop(File, Pid, Ref); |
| 17 | + {error, _} = Err -> |
| 18 | + RPCProc ! {Ref, Err} |
| 19 | + end |
| 20 | + end), |
| 21 | + receive |
| 22 | + {Ref, opened} -> {ok, Ref}; |
| 23 | + {Ref, {error, Err}} -> {error, Err} |
| 24 | + after 5000 -> |
| 25 | + exit(Reader, timeout), |
| 26 | + {error, timeout} |
| 27 | + end. |
| 28 | + |
| 29 | +read_loop(File, Pid, Ref) -> |
| 30 | + case file:read(File, ?GUESS_OFFSET) of |
| 31 | + {ok, Data} -> |
| 32 | + Pid ! {Ref, Data, confinue}, |
| 33 | + read_loop(File, Pid, Ref); |
| 34 | + eof -> |
| 35 | + timer:sleep(1000), |
| 36 | + read_loop(File, Pid, Ref); |
| 37 | + {error, _} = Err -> |
| 38 | + Pid ! {Ref, Err, finished} |
| 39 | + end. |
| 40 | + |
8 | 41 | tail_n_lines(Filename, N) -> |
9 | 42 | case file:open(Filename, [read, binary]) of |
10 | 43 | {ok, File} -> |
|
0 commit comments