-3

There is some (compiled and not my) program A, which starts .log file from the beginning each time, when I launch it. And I wrote the program B to work with this log using this approach:

File.open("qconsole.log") do |log|
    log.gets nil # I am interested only in new lines
    loop do
        next sleep 0.1 unless line = log.gets
        line.chomp!
        puts line
        # some code
    end
end

For example, after two new lines in the .log file I see this output:

 player1: i talk blabla
 player2: no way!

But when I quit and restart the program A:

]\quit 
----- Server Shutdown -----
==== ShutdownGame ====
AAS shutdown.
---------------------------
----- CL_Shutdown -----
RE_Shutdown( 1 )
Shutting down OpenGL subsystem
...wglMakeCurrent( NULL, NULL ): success
...deleting GL context: success
...releasing DC: success
...destroying window
...shutting down QGL
...unloading OpenGL DLL
-----------------------

the program B seems to lose the .log file after this. It doesn't print me new lines anymore. I suppose, it gets eternal nil from log.gets.

So how can I know, that I need to stop doing log.gets and reopen the .log file?

UPD: Windows 7

Nakilon
  • 34,866
  • 14
  • 107
  • 142

1 Answers1

0

My current dirty solution:

logfilesize = File.stat(logfile).size
log = File.open logfile
log.gets nil

loop do
    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
    # some code
end
Nakilon
  • 34,866
  • 14
  • 107
  • 142