Location: PHPKode > projects > KvCMS Content Management System > kvframework/kvframework/logger/kvframework_log_session.class.php
<?php

class kvframework_log_session
{
    protected $parent_application;
    protected $message_categories;
    public $syslog_levels;

    protected $console_opened;
    protected $console_msg_queue;
    protected $console_queue_mode;

    protected $email_opened;
    protected $email_log_recipient;
    protected $email_log_sender;
    protected $email_log_subject;
    protected $email_msg_queue;
    protected $email_queue_mode;

// Constructor

    public function __construct($parent_app)
    {
        $this->log_mode_array = explode(",", kvframework_log::LOG_MODES_LIST);
        $this->message_categories = explode(",", kvframework_log::MESSAGE_CATEGORIES_LIST);
        $this->syslog_levels = explode(",", kvframework_log::SYSLOG_LEVELS_LIST);
        $this->parent_application = $parent_app;
    }

    public function enable_log_instance($type)
    {
        switch ($type)
        {
            case "local":
                {
                    $this->log_instances[] = new kvframework_local_logger($this, $this->parent_application);
                    $instance_index = sizeof ($this->log_instances) - 1;
                    break;
                }
            case "syslog":
                {
                    $this->log_instances[] = new kvframework_syslog_logger($this, $this->parent_application);
                    $instance_index = sizeof ($this->log_instances) - 1;
                    break;
                }
            case "email":
                {
                    $this->log_instances[] = new kvframework_email_logger($this, $this->parent_application);
                    $instance_index = sizeof ($this->log_instances) - 1;
                    break;
                }
            case "console":
                {
                    $this->log_instances[] = new kvframework_console_logger($this, $this->parent_application);
                    $instance_index = sizeof ($this->log_instances) - 1;
                    break;
                }
            default:
                {
                    $this->write_logger_log("php-logger->enable_log_instance called with invalid instance type ".$type."\n");
                    return (FALSE);
                }
        }
        return ($instance_index);
    }

    public function disable_log_instance($instance)
    {
        if ( is_num ($instance) )
        {
            if ( $instance < sizeof ($this->log_instances) )
            {
                $this->log_instances[$instance]->close();
            }
            else
            {
                $msg = sprintf ("php-logger->disable_log_instance called with non instantiated instance %s", $instance);
                $this->write_logger_log ($msg);
                return (FALSE);
            }
        }
        else
        {
            $msg = sprintf ("php-logger->disable_log_instance called with non integer parameter %s requires instance number", $instance);
            $this->write_logger_log ($msg);
            return (FALSE);
        }
    }



// Log entry public function - meat and potatoes time

    public function log_entry()
    {
        $numargs = func_num_args();

        $severity = NULL; //Just to ensure that we don't have bleedover between calls
        $category = FALSE;
        $good_args = TRUE;

        $override = array();

        switch ($numargs)
        {
            case 0:
            case 1:     $log_error_msg = "Function: log_entry called with 0 or 1 arguement, that's not good";
                        $this->write_logger_log($log_error_msg);
                        $good_args = FALSE;
                        break;

            case 2:     $inst = func_get_arg(0);
                        $msg = func_get_arg(1);
                        break;

            default:    $args = func_get_args();
                        $inst = $args[0];
                        for ($i = 1; $i < sizeof($args)-1; $i++)
                        {
                            $good_arg = FALSE;
                            $arg = $args[$i];
                            if (ereg("[\+-].*", $arg)) // This conditional has to come before the severity test since "+/-[0-9]" return true for is_numeric
                            {
                                $action = substr($arg, 0, 1);
                                $requested_instance = substr($arg, 1);
                                for ($x = 0; $x < sizeof ($this->log_instances); $x++)
                                {
                                    $instance = &$this->log_instances[$x];
                                    // OK - this is stupid - but it's because PHP returns true on is_numeric ( string of ints) but does not return true on string of ints===ints
                                    if ( ( ( is_numeric ( $requested_instance ) )& ($requested_instance == $x ) ) | ( $requested_instance === $instance->get_instance_type() ) )
                                    {
                                        $override[$x] = "$action";
                                        $good_arg = TRUE;
                                    }
                                }
                                if (!$good_arg)
                                {
                                    $msg = sprintf ("instance override requested for non enabled instance %s with action %s\n", $requested_instance, $action);
                                    $this->write_logger_log($msg);
                                }
                            }
                            elseif ((is_numeric($arg))&&($arg < 8)) // numeric values < 8 are always interpreted as syslog sev levels
                            {
                                if ($severity)
                                {
                                    $log_error_msg = "Severity was already set - cannot reset!";
                                    $this->write_logger_log($log_error_msg);
                                    $good_arg = FALSE;
                                }
                                else
                                {
                                    $severity = $arg;
                                    $good_arg = TRUE;
                                }
                            }
                            else
                            {
                                foreach ($this->message_categories as $message_category)
                                {
                                    if ($arg == $message_category)
                                    {
                                        $category = $arg;
                                        $good_arg = TRUE;
                                    }
                                }
                            }
                            if (!$good_arg)
                            {
                                $good_args = FALSE;
                            }
                        }
                        $msg = $args[sizeof($args)-1];
                        break;
        }
        if (!$good_args || !$this->check_inst($inst))
        {
            $log_error_msg = "";
            $log_error_msg .= str_pad(date("r"), 36)." LOGGER CALLED WITH BAD ARGUMENTS from application ".$this->parent_application."\n";
            for ($i = 0; $i < sizeof($args); $i++)
            {
                $log_error_msg .= "\tArguement #".$i." => ".$args[$i]."\n";
            }
            $this->write_logger_log($log_error_msg);
        } else {

          if (!$category)
          {
              $category = "OTHER";
          }

          if ( $severity === NULL )
          {
              $severity = kvframework_log::DEFAULT_MSG_SEVERITY;
          }

          /*for ( $i = 0; $i < sizeof ($this->log_instances); $i++ )
           *{
           *  $force = FALSE;
           *  if ( $override[$i] == "+" )
           *  {
           *      $force = TRUE;
           *  }
           *  if ( !( $override[$i] == "-" ) )
           *  {
           *      $this->log_instances[$i]->log_entry ($force, $category, $severity, $msg);
           *  }
           *  else
           *  {
           *  }
           *}
           */

          $orinst = false;

          foreach($override as $i => $val){
            $force = FALSE;
            if($i == $inst){
              $orinst = true;
            }
            if($val == "+"){
              $force = TRUE;
            }
            if(!($val == "-")){
              $this->log_instances[$i]->log_entry ($force, $category, $severity, $msg);
            }
          }

          if(!$orinst && (!array_key_exists($inst, $override) || $override[$inst] != "-")){
            $force = FALSE;
            $this->log_instances[$inst]->log_entry ($force, $category, $severity, $msg);
          }
        }
    }

// End log entry public function - meat and potatoes time is over

    public function configure_instance ()
    {
        $found_valid_instance = FALSE;

        $args = func_get_args();
        if ( sizeof( $args ) < 2 )
        {
            $msg = sprintf ("Configure instance requires at least 2 arguments - received %s", kvframework_base::array_to_string($args));
            return (FALSE);
        }

        $requested_instance = array_shift ($args);
        $function = array_shift ($args);
        $params = array_shift ($args);
        while ($param_temp = array_shift ($args) )
        {
            $params .= ", ".$param_temp;
        }

        for ($i = 0; $i < sizeof ($this->log_instances); $i++)
        {
            $instance = &$this->log_instances[$i];
            if ( ( $requested_instance === $i ) | ( $requested_instance === $instance->get_instance_type() ) )
            { // OK - either the requested instance was one of our types or it was an integer for a particular instance
                if ( method_exists ($instance, $function) )
                {
                    $instance->$function($params);
                    $found_valid_instance = TRUE;
                }
                else
                {
                    $msg = sprintf ("Attempt to use a non existant configuration %s public function for logger instance %s\n", $function, $requested_instance);
                    $this->write_logger_log($msg);
                    return (FALSE);
                }
            }
        }
        if ( !$found_valid_instance )
        {
            $msg = sprintf ("Attempt to modify a non existant logger instance %s\n", $requested_instance);
            $this->write_logger_log($msg);
            return (FALSE);
        }
        else
        {
            return (TRUE);
        }
    }

// public function to log the logger - YIKES!

    public function write_logger_log($msg)
    {
        echo ('LOGGER'.KVF_LOG_LCRITICAL.$msg);
        openlog ("LOGGER", LOG_ODELAY | LOG_PID | LOG_CONS, kvframework_log::DEFAULT_LOGGER_LOG_FACILITY );
        syslog (KVF_LOG_LCRITICAL, str_pad("LOGGER", 13).str_pad("CRITICAL", 13).$msg);
        // closelog();
    }

// public function to close all open logs

    public function close_log()
    {
        foreach($this->log_instances as $instance)
        {
            $instance->close();
        }
    }

    protected function check_inst($i){
      if(!array_key_exists($i, $this->log_instances)){
        return false;
      } else {
        return true;
      }
    }
}
?>
Return current item: KvCMS Content Management System