# Log manager library module.
# Rick Smereka, Copyright (C) 2005-2006.

# 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
# Single user only for now. No client/server
# 'sys_log' support until next version. 

# Changed 'logman_start'. Removed braces from call to 'logman_end'
# if log manager is already active. Apr/2006, Rick Smereka

# globals
set logman_name ""
set logman_flag 0
set logman_has_init 0
set logman_is_syslog 0
set logman_console_output 0

proc logman_start {dest {apname ""}} {
   # Start logging through the log manager. This routine will
   # either start a local log file (using 'logger.tcl') or the
   # system log interface (using 'sys_log.tcl') depending on the
   # contents of 'dest'. The parameter 'apname' is the application
   # name and is only required (always) for the system logger.
   # If logging has been successfully stared with a name
   # ('dest') at least once, you are allowed to pass an empty
   # string for 'dest' which indicates to re-start the same log.
   # Returns a system log code if the system log was requested 
   # or zero (0) upon success one (1) otherwise.
   global logman_has_init logman_flag logman_name logman_is_syslog
   set len [string length $dest]

   # error if log manager has not been init and log name is empty
   if {!$logman_has_init} {
      if {!$len} {
         return 1
      }
   }

   # if log manager already active, stop it
   if {$logman_flag} {
      logman_end
   }

   if {$len} {
      set logman_name $dest
   }

   set logman_flag 1
   set logman_has_init 1

   # if name matches the system logger serice name,
   # indicate system log
   #if {[string match $SYS_LOG_SERVICE_NAME $logman_name]} {
   #   set logman_is_syslog 1
   #}

   # initialize logging, either system logging or
   # personal logging
   if {$logman_is_syslog} {
      # use system log
      # error if 'apname' not given
      if {![string length $apname]} {
         set logman_name ""
         set logman_flag 0
         set logman_has_init 0
         return 1
      }

      # if TCP IPC, init socloc if not already active
      #if {[socloc_is_init != $SL_OK]} {
      #   if {![logman_ipc_socloc_init]} {
      #      set logman_name ""
      #      set logman_flag 0
      #      set logman_has_init 0
      #      return 1
      #   }
      #}

      #set ret [sys_log_init $apname]

      #if {$ret != $SYS_LOG_OK} {
      #   set logman_name ""
      #   set logman_flag 0
      #   set logman_has_init 0
      #   return $ret
      #}

      # no 'sys_log', always return 1 for now
      return 1
   } else {
      # use personal logger
      if {![log_start $dest]} {
         set logman_name ""
         set logman_flag 0
         set logman_has_init 0
         return 1
      }
   }

   return 0
}

proc logman_end {} {
   # Stop the log manager and all logging.
   global logman_is_syslog logman_flag

   if {$logman_is_syslog} {
      puts "logman_end:should not be here"
      # sys_log_end
   } else {
      log_end
   }

   set logman_flag 0
   set logman_is_syslog 0
}

proc logman {mes} {
   # Output a message through the log manager to the current
   # destination. Message is only output if the log manager
   # flag is high.
   global logman_flag logman_is_syslog

   if {!$logman_flag} {
      return
   }

   if {$logman_is_syslog} {
      puts "logman:should not be here"
      # set ret [sys_log $mes]
   } else {
      log_file_date $mes
   }
}

proc logman_is_active {} {
   # Is the log manager active. Returns 1 if the
   # log manager is active, 0 otherwise.
   global logman_flag

   return $logman_flag
}

proc logman_get_name {} {
   # Obtain and return the current log name. Returns the log
   # name.
   global logman_name

   return $logman_name
}

proc logman_console {flag} {
   # Set whether logging output will be sent to the console/screen.
   # Output will be turned off if 'flag' is 0. Any other value
   # will cause console output to be turned on.
   global logman_console_output logman_flag logman_is_syslog

   if {!$flag} {
      set logman_console_output 0
   } else {
      set logman_console_output 1
   }

   # if logging is active, communicate change to appropriate logger
   if {$logman_flag} {
      if {$logman_is_syslog} {
         puts "logman_console:we should not be here"
         # sys_log_console $logman_console_output
      } else {
         log_console $logman_console_output
      }
   }
}
   
proc logman_get_console {} {
   # Return the current value of the console/screen output flag
   # ('logman_console_output').
   global logman_console_output

   return $logman_console_output
}
