log-file-tail ( path limit -- lines omitted-bytes )


Vocabulary
logging.parser

Inputs
patha log file path
limita positive byte count


Outputs
linesa sequence of strings
omitted-bytesthe number of omitted bytes


Word description
Reads at most the last limit bytes of a log file, discarding a partial first line before decoding UTF-8. The file is closed on both success and failure.

Definition


:: log-file-tail ( path limit -- lines omitted-bytes )
limit integer? [ limit 0 > ] [ f ] if
[ limit invalid-log-tail-limit ] unless path binary [
input-stream get stream-length 0 or limit - 0 max
:> start start 0 >
[ start 1 - seek-absolute seek-input read1 10 = ] [ t ]
if :> line-boundary? limit read B{ } or
:> bytes line-boundary?
[ 0 ] [ 10 bytes index [ 1 + ] [ bytes length ] if* ] if
:> partial bytes partial tail utf8 decode
split-lines start partial +
] with-file-reader ;