Eivind Eklund wrote:
> # ... and continue refactoring into an object.  
> 
> I can do another pass if you finish the refactoring job.
> 
> Eivind.
> 
> 

Thanks. Okay, here's refactoring pass 1:

=============================================

#!/usr/bin/ruby -w
#########################################################
#
# log_CID.rb
#
# Ruby CID logging script
#
#    Logs all activity at a modem set to decipher CID
#    (Caller ID) information. Archives the daily logs as well.
#
##########################################################

require 'zip/zip'
require 'serialport/serialport.so'

############################################
#
# local settings here.
#
# name of archive file--daily logs are moved to this archive
backup_zip_filename = "CID_Data.zip"
# modem initialization string.
# need to set to log verbose caller ID information (+VCID=1 or #CID=1, etc)
# also need to set to NOT answer, just monitor line (usually the default)
modem_init_string = "AT+VCID=1\r\n" # for USR verbose CID output
# directory to kep log files
backup_dir = "c:/modemlog"
# base log name for daily log files
# daily log file name is this, plus YYYYMMDD date, plus .log extension
# eg. CID20041004.log
base_log_name = "CID"
# the comm port having the CID-capable modem
port_name = 'COM3:'
# days that a daily log file is kept prior to archiving the file
days_before_archive = 7
# maximum port read errors allowed before aborting run
MAX_PORT_ERRORS = 3000
# debug on or off?
DEBUG = true
#
#
##############################################


#################################

# script local class

class DailyLogWithArchive
     attr_reader :backup_dir, :base_log_name, :backup_zip_filename,
       :archive_days_secs, :backup_days_interval

     def initialize(backup_dir, base_log_name, backup_zip_filename,
       days_before_backup, backup_days_interval = 1)
         @backup_dir = backup_dir
         @base_log_name = base_log_name
         @backup_zip_filename = backup_zip_filename
         @archive_days_secs = 60 * 60 * 24 * days_before_backup
         @backup_days_interval = backup_days_interval
         # var for calendar based backup, start with invalid value.
         @last_backup_day = -1
     end

     def current_fname
         "#{backup_dir}/#{base_log_name}#{Time.now.strftime("%Y%m%d")}.log"
     end

     def archive_old_to_zip
         time = Time.now
         dir = Dir.open(backup_dir)
         moved = dir.inject(0) do | move_count, logfile |
             next unless logfile.index(base_log_name) == 0
             next unless time > File.stat(logfile).mtime + archive_days_secs
             if(move_to_archive(logfile))
                 next move_count + 1
             else
                 next move_count
             end
         end
         dir.close
         return moved
     end

     def move_to_archive(fname)
         Zip::ZipFile.open(backup_zip_filename, 1) {
             | zfile |
             zfile.add(fname, fname)
         }
         File.delete(fname)
     end

     def log_text(txt)
         logfile = File.new(current_fname, "a")
         logfile.print(txt)
         logfile.close
         # archive old logs daily
         time = Time.now
         yday = time.yday
         if(yday != last_backup_day and
           (yday >= last_backup_day + backup_days_interval or yday == 0) )
             archive_old_to_zip
             last_backup_day = yday
         end
     end

end


###############################
# begin main program

# var to hold port read error count
port_err_count = 0

# move to the dir for backup
Dir.chdir(backup_dir)

# Open the port and set up CID via modem_init_string.
port = SerialPort.new(port_name)
# indefinite wait for a string to appear at the port
port.read_timeout = 0
port.puts(modem_init_string)

print  "Starting run with port ",  port_name,
   " and logging to dir ", backup_dir, "\n" if DEBUG

# set up the logging class
logger =
   DailyLogWithArchive.new(backup_dir, base_log_name, backup_zip_filename)

# Loop with pauses to look for data at the port we can record.
while(true)
     while(text = port.gets)
         print text if DEBUG
         # log text unless it is just spaces
         if(text =~ /\S/)
             # squeeze double \r, etc to just \n
             text.sub!(/[\r\n]+/, "\n")
             logger.log_text(text)
         end
     end
     msg = "#{Time.now.to_s}: dropped out of system call, restarting 
loop.\n"
     print msg if DEBUG
     logger.log_text(msg) if DEBUG
     port_err_count += 1
     if(port_err_count > MAX_PORT_ERRORS)
         msg = "Too many port errors...exiting\n"
         print msg
         logger.log_text(msg)
         return port_err_count
     end
end


return 0

===========================================