<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package libraries
 * @version $Id$
*/

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

/**
 * Gain access to XOR
 */
class My_Log extends CI_Log
{
    var $_dav_threshold = 1;
    var $_email_threshold = 1;
    var $_interfaces_threshold = 1;
    
    /**
    * Constructor
    */
    public function __construct()
    {
        parent::__construct();
        
        $config =& get_config();

        if (is_numeric($config['dav_log_threshold']))
        {
            $this->_dav_threshold = $config['dav_log_threshold'];
        }
        if (is_numeric($config['email_log_threshold']))
        {
            $this->_email_threshold = $config['email_log_threshold'];
        }
        if (is_numeric($config['interfaces_log_threshold']))
        {
            $this->_interfaces_threshold = $config['interfaces_log_threshold'];
        }
    }
    
    /**
	 * Write Log File
	 *
	 * Generally this function will be called using the global log_message() function
	 *
	 * @access	public
	 * @param	string	the error level
	 * @param	string	the error message
	 * @param	bool	whether the error is a native PHP error
	 * @return	bool
	 */
	function write_log($level = 'error', $msg = '', $php_error = FALSE, $prefix='log')
	{
            if ($this->_enabled === FALSE) {
                return FALSE;
            }

            $level = strtoupper($level);

            $filepath = null;
            switch($prefix) {
                case 'log':
                    if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) {
                        return FALSE;
                    }

                    $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
                    break;
                case 'dav':
                    if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_dav_threshold)) {
                        return FALSE;
                    }

                    $filepath = $this->log_path.'log-'.date('Y-m-d').'_dav'.EXT;
                    break;
            }

            $message  = '';

            if ( ! file_exists($filepath))
            {
                $message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
            }

            if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
            {
                return FALSE;
            }

            $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";

            flock($fp, LOCK_EX);
            fwrite($fp, $message);
            flock($fp, LOCK_UN);
            fclose($fp);

            @chmod($filepath, FILE_WRITE_MODE);
            return TRUE;
	}

} // END class My_Log
