# Copyright (c) 2008 Grant Rettke (grettke@acm.org) # # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # $LastChangedDate: 2008-04-27 21:28:21 -0500 (Sun, 27 Apr 2008) $ # $LastChangedRevision: 1697 $ # $HeadURL: svn://osiris/smoothtail/tags/1.00/smoothtail.rb $ # Smoothtail is a tail utility written for Microsoft Windows that is simple, free, and provides regex based pattern matching. It depends on Ruby 1.8.4 and File::Tail version 0.1.3. # Believe it or not, Microsoft Windows has not got any tail utilies # that are simultaneously: # * good # * free # * easy to use # # To remedy this situation I wrote what is essentially a helper script # that uses Florian Frank's excellent File::Tail library. # # Combine an explanation (in the script itself) of how to add this # tail utility to the Windows "Send To" menu with pattern matching in the tail # itself and the result is a nice little tail utility. # How to install in Windows SendTo Shell # # Please subsitute paths as desired # # 1. In explorer open C:\Documents and Settings\\SendTo # 2. Right click the window and choose New->Shorcut. # 3. In the location of the item paste # %SystemRoot%\system32\cmd.exe /k ruby \smoothtail.rb # 4. Name the shortcut "smoothtail". # # You need to run this with: ruby 1.8.4 (2006-04-14) [i386-mswin32] # Import the 'file-tail' library via its gem # This script was written against version 0.1.3 # Install it by typing at the command line # 'gem install file-tail-0.1.3.gem' require 'file/tail' # At least one argument, the file name to tail, must be provided. # Otherwise display usage and stop the program file_name = ARGV.shift or fail "Usage: #$0 filename" print "Please enter the desired regex pattern (or hit enter to display every line instead): " regex_pattern = gets regex_pattern = regex_pattern.chomp # Create a file-tail Logfile object and get a block in which we have got a reference do it (log_file) File::Tail::Logfile.open(file_name) do |log_file| # The start value of the sleep interval. This value # goes against max_interval if the tailed # file is silent for a sufficient time. log_file.interval = 1 # The maximum interval File::Tail sleeps, before it tries # to take some action like reading the next few lines # or reopening the file. log_file.max_interval = 5 # If this attribute is set to a true value, File::Tail persists # on reopening a deleted file waiting max_interval seconds # between the attempts. This is useful if logfiles are # moved away while rotation occurs but are recreated at # the same place after a while. It defaults to true. log_file.reopen_deleted = true # If this attribute is set to a true value, File::Tail # attempts to reopen it's tailed file after # suspicious_interval seconds of silence. log_file.reopen_suspicious = true # This attribute is the invterval in seconds before File::Tail # gets suspicious that something has happend to it's tailed file # and an attempt to reopen it is made. # # If the attribute reopen_suspicious is # set to a non true value, suspicious_interval is # meaningless. It defaults to 60 seconds. log_file.suspicious_interval = 20 # Default arguments, called here for example, are (n = 0, bufsiz = nil) # Rewind the last n lines of this file, starting # from the end. The default is to start tailing directly from the # end of the file. # # The additional argument bufsiz is # used to determine the buffer size that is used to step through # the file backwards. It defaults to the block size of the # filesystem this file belongs to or 8192 bytes if this cannot # be determined. # # This call starts a the last two lines of the file log_file.rewind(20) # If the user passed in a pattern on which to match, only print matching lines. # Otherwise print every line. work = nil if regex_pattern.length == 0 work = lambda { |line| print "#{line}" } else work = lambda do |line| regex = Regexp.new(regex_pattern) if regex.match(line) print "#{line}" end end end # Start tailing the file log_file.tail do |line| work.call(line) end # end block: tailing the file, only CTRL-C gets you out of this block end # end block: the log file reference