# Building a log by watching a file

**URL:** <https://www.ruby-forum.com/t/building-a-log-by-watching-a-file/65041>\
**Category:** Ruby\
**Created:** [August 15, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/building-a-log-by-watching-a-file/65041 "2006-08-15T20:21:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 15, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/building-a-log-by-watching-a-file/65041/1 "2006-08-15T20:21:19Z")

</div>

I’ve been paying attention to some mind-numbing chores  
I’ve been tasked to do lately, and I think Ruby might  
be able to help. Let me know what you think.

We have _many_ crusty Fortran (and FORTRAN) codes lying  
around that do their job just fine, except they are  
a pain to run.

One scenario might go like this,

$ ./code \< input\_deck  
[edit a bunch of things in input\_deck]

This two step is repeated ad nauseam until you’re satisfied  
with the output. The kicker is you do this once to find  
the path to the solution, then you run 3,000 variations  
on this central theme.

One burden is logging your sequence of input\_deck  
edits, so that you can replay them for the  
next 3000 cases (adding logging to the crusty code  
is unthinkable).

So, I was thinking of turning on a “watcher” that  
would save off inputs decks every time an edit is  
detected.

Anyone have a one-liner?

Thanks,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 15, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/building-a-log-by-watching-a-file/65041/2 "2006-08-15T20:36:27Z")

</div>

Bil K. wrote:

> So, I was thinking of turning on a “watcher” that  
> would save off inputs decks every time an edit is  
> detected.
> 
> Anyone have a one-liner?

Definitely not a one-liner, but I can offer:  
[http://phrogz.net/RubyLibs/rdoc/files/DirectoryWatcher\_rb.html](http://phrogz.net/RubyLibs/rdoc/files/DirectoryWatcher_rb.html)

With that, it’d be as easy as:

edit\_logger = Dir::DirectoryWatcher.new( ‘directory/to/watch’, 2 )

# Only look at certain files in the directory

edit\_logger.name\_regexp = /.fortran$/

edit\_logger.on\_modify = Proc.new{ |the\_file, stats\_hash|  
puts “Hey, #{the\_file.inspect} just changed.”  
# Do something cool here.  
}

edit\_logger.start\_watching

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 16, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/building-a-log-by-watching-a-file/65041/3 "2006-08-16T13:38:03Z")

</div>

Christian N. wrote:

> Bil K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> > So, I was thinking of turning on a “watcher” that  
> > would save off inputs decks every time an edit is  
> > detected.
> 
> while true; do git commit -m “$(date)” input\_deck; sleep 1; done

Excellent.

(Turns out, this is also my officemate’s answer, although  
he didn’t supply the command to do it.)

Thanks,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 16, 2006, 12:40pm UTC](https://www.ruby-forum.com/t/building-a-log-by-watching-a-file/65041/4 "2006-08-16T12:40:57Z")

</div>

Bil K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> $ ./code \< input\_deck  
> is unthinkable).
> 
> So, I was thinking of turning on a “watcher” that  
> would save off inputs decks every time an edit is  
> detected.
> 
> Anyone have a one-liner?

What I’d do:

while true; do git commit -m “$(date)” input\_deck; sleep 1; done

Ought to work with any reasonable VCS.

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [August 16, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/building-a-log-by-watching-a-file/65041/5 "2006-08-16T13:53:04Z")

</div>

Phrogz wrote:

> edit\_logger.on\_modify = Proc.new{ |the\_file, stats\_hash|  
> puts “Hey, #{the\_file.inspect} just changed.”  
> # Do something cool here.  
> }

Thanks.

How would I inject a running index into the “Do something  
cool here” part so I could save off input\_deck.0,  
input\_deck.1, and so on?

Regards,
