ruby - Automatic reopening file for reading if it was recreated -
there (compiled , not my) programme a, starts .log file origin each time, when launch it. , wrote programme b work log using approach:
file.open("qconsole.log") |log| log.gets nil # interested in new lines loop next sleep 0.1 unless line = log.gets line.chomp! puts line # code end end
for example, after 2 new lines in .log file see output:
player1: talk blabla player2: no way!
but when quit , restart programme a:
class="lang-none prettyprint-override">]\quit ----- server shutdown ----- ==== shutdowngame ==== aas shutdown. --------------------------- ----- cl_shutdown ----- re_shutdown( 1 ) shutting downwards opengl subsystem ...wglmakecurrent( null, null ): success ...deleting gl context: success ...releasing dc: success ...destroying window ...shutting downwards qgl ...unloading opengl dll -----------------------
the programme b seems lose .log file after this. doesn't print me new lines anymore. suppose, gets eternal nil
log.gets
.
so how can know, need stop doing log.gets
, reopen .log file?
upd: windows 7
my current dirty solution:
logfilesize = file.stat(logfile).size log = file.open logfile log.gets nil loop logfilesize = file.stat(logfile).size.tap{ |t| if t < logfilesize log.close log = file.open logfile end } next sleep 0.1 unless line = log.gets line.chomp! puts line # code end
ruby file-io tail
No comments:
Post a Comment