<?php
/**
 * -----------------------------------------------------------------------------
 *
 * SyL - Web Application Framework for PHP
 *
 * PHP version 4 (>= 4.3.x) or 5
 *
 * Copyright (C) 2006 k.watanabe
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -----------------------------------------------------------------------------
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_Loggers.php,v 1.8 2006/12/25 17:30:18 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

/**
 * ログクラス
 */
require_once 'SyL_Logger.php';

/**
 * ログ出力カテゴリクラスを複数保持するクラス
 * 
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_Loggers.php,v 1.8 2006/12/25 17:30:18 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_Loggers
{
    /**
     * ログ保存フラグ
     * 
     * @access public
     * @var bool
     */
    var $log = SYL_LOG;

    /**
     * ログカテゴリオブジェクト格納配列
     * 
     * @access private
     * @var array
     */
    var $loggers = array();
    /**
     * ログレベル格納配列
     * 
     * @access private
     * @var array
     */
    var $modes = array();

    /**
     * SyL_Loggersクラスのインスタンス取得
     *
     * singletonモデルを採用しているので、
     * 常に1つのインスタンスの参照が取得される。
     *
     * ※取得方法
     *   $log =& SyL_Loggers::getInstance();
     *
     * @access public
     */
    function &getInstance()
    {
        static $singleton;
        if (!is_object($singleton)) {
            $singleton = new SyL_Loggers();
        }
        return $singleton;
    }

    /**
     * ログオブジェクトのセット
     * 
     * @access public
     * @param object ログオブジェクト
     * @param int ログレベル
     */
    function setLogger(&$logger, $mode=SYL_LOG_NOTICE)
    {
        $this->loggers[] =& $logger;
        $this->modes[]   = $mode;
    }

    /**
     * ログの出力（ERROR）
     *
     * @access public
     * @param string メッセージ
     */
    function error($message)
    {
        $log =& SyL_Loggers::getInstance();
        if ($log->log) {
            foreach (array_keys($log->loggers) as $key) {
                if ($log->modes[$key] >= SYL_LOG_ERROR) {
                    $log->loggers[$key]->log('error', $message);
                }
            }
        }
    }

  /**
   * ログの出力（WARN）
   *
   * @access public
   * @param string メッセージ
   */
  function warn($message)
  {
        $log =& SyL_Loggers::getInstance();
        if ($log->log) {
            foreach (array_keys($log->loggers) as $key) {
                if ($log->modes[$key] >= SYL_LOG_WARN) {
                    $log->loggers[$key]->log('warn', $message);
                }
            }
        }
    }

  /**
   * ログの出力（NOTICE）
   *
   * @access public
   * @param string メッセージ
   */
  function notice($message)
  {
        $log =& SyL_Loggers::getInstance();
        if ($log->log) {
            foreach (array_keys($log->loggers) as $key) {
                if ($log->modes[$key] >= SYL_LOG_NOTICE) {
                    $log->loggers[$key]->log('warn', $message);
                }
            }
        }
    }

    /**
     * ログの出力（INFO）
     *
     * @access public
     * @param string メッセージ
     */
    function info($message)
    {
        $log =& SyL_Loggers::getInstance();
        if ($log->log) {
            foreach (array_keys($log->loggers) as $key) {
                if ($log->modes[$key] >= SYL_LOG_INFO) {
                    $log->loggers[$key]->log('info', $message);
                }
            }
        }
    }

    /**
     * ログの出力（DEBUG）
     *
     * @access public
     * @param string メッセージ
     */
    function debug($message)
    {
        $log =& SyL_Loggers::getInstance();
        if ($log->log) {
            foreach (array_keys($log->loggers) as $key) {
                if ($log->modes[$key] >= SYL_LOG_DEBUG) {
                    $log->loggers[$key]->log('debug', $message);
                }
            }
        }
    }

    /**
     * 後処理
     *
     * @access public
     */
    function close()
    {
        foreach (array_keys($this->loggers) as $key) {
            $this->loggers[$key]->close();
        }
    }
}

?>
