# File and display logging functions.
# Rick Smereka, Copyright (C) 2005.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, get a copy via the Internet at
# http://gnu.org/copyleft/gpl.html or write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# You can contact the author via email at rsmereka@future-lab.com

# Initial tcl version Mar/2005, Rick Smereka

# module globals
set log_fname ""
set p_islog 0
set p_has_init 0
set p_console_output 0

proc log_start {fname} {
   # Start logging and register log file name. If logging has
   # been successfully started with the file name at least
   # once, you are allowed to pass an empty string for 'fname'
   # which indicates to re-start the same log file. Returns 1
   # upon success, 0 otherwise.
   global p_has_init log_fname p_islog
   set len [string length $fname]

   if {!$p_has_init} {
      if {!$len} {
         return 0
      }
   }

   if {$len} {
      set log_fname $fname
   }

   if {[catch {open $fname a} fid]} {
      return 0
   }
   
   close $fid
   set p_islog 1
   set p_has_init 1
   return 1
}

proc logger {mes} {
   # Send a message to the log file and to the console if the
   # console output flag is high.
   global p_islog

   if {!$p_islog} {
      return
   }

   log_file $mes

   if {[log_get_console]} {
      puts $mes
   }
}

proc log_file {mes} {
   # Output to the log file file. No output is sent to the
   # console.
   global p_islog log_fname

   if {!$p_islog} {
      return
   }

   if {![string length $log_fname]} {
      return
   }

   if {![string length $mes]} {
      return
   }

   if {[catch {open $log_fname a} fid]} {
      puts "logger:log_file:uable to open $log_fname for append"
      return
   }

   puts $fid $mes
   close $fid
}

proc log_file_date {mes} {
   # Output a message to the current log file with the current
   # date and time as the prefix. The message is also output to
   # the screen if the console output flag is high.
   global p_islog log_fname

   if {!$p_islog} {
      return
   }

   if {![string length $log_fname]} {
      return
   }

   if {![string length $mes]} {
      return
   }

   set tmch [get_time]
   
   if {[catch {open $log_fname a} fid]} {
      puts "logger:log_file_date:uable to open $log_fname for append"
      return
   }

   puts $fid "\[$tmch\]$mes"
   close $fid

   if {[log_get_console]} {
      puts "\[$tmch\]$mes"
   }
}
      
proc log_disp {mes} {
   # Output to the display regardless of the value of
   # 'p_console_output'.
   global p_islog

   if {!$p_islog} {
      return
   }

   puts $mes
}

proc is_log_active {} {
   # Return the value of the 'p_islog' flag.
   global p_islog

   return $p_islog
}

proc log_end {} {
   # Stop logging by changing the 'p_islog' flag.
   global p_islog

   set p_islog 0
}

proc log_file_name {lfname} {
   # Get and return the current log file name.
   # Personal logging must have successfully been
   # initialized at least once. Returns 1 upon
   # success with the log file name loaded into
   # 'lfname'. Returns 0 upon failure.
   global p_has_init log_fname
   upvar $lfname lk
   set lk ""

   if {!$p_has_init} {
      return 0
   }

   set lk $log_fname
   return 1
}

proc log_console {flag} {
   # Set whether logging output will be sent to the
   # console/screen. Output will be turned off if
   # 'flag' is zero. Any other value will cause
   # console output to be turned on.
   global p_console_output

   if {$flag} {
      set p_console_output 1
   } else {
      set p_console_output 0
   }
}

proc log_get_console {} {
   # Return the current value of the console/screen output
   # flag ('p_console_output').
   global p_console_output

   return $p_console_output
}
