Log
[ class tree: Log ] [ index: Log ] [ all elements ]

Source for file Log.php

Documentation is available at Log.php

  1. <?php
  2. /**
  3.  * Mai_Log
  4.  * 
  5.  * Copyright (C) 2009  Mai (Me Artificial Intelligence)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  * 
  20.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  21.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  22.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  23.  * @link http://www.launchpad.net/mai
  24.  * 
  25.  * @category Mai
  26.  * @package Log
  27.  * 
  28.  */
  29.  
  30. /** Required files */
  31. {
  32.     //Log and http classes are needed
  33.     require_once 'Zend/Log/Writer/Stream.php';
  34.     if(Mai::getEnv(=== Mai::ENV_DEVELOPMENT)
  35.     {
  36.         require_once 'Zend/Log/Writer/Firebug.php';
  37.         require_once 'Zend/Controller/Request/Http.php';
  38.         require_once 'Zend/Controller/Response/Http.php';
  39.         require_once 'Zend/Wildfire/Channel/HttpHeaders.php';
  40.     }
  41. }
  42.  
  43. /**
  44.  * Mai_Log is a logger for Mai. Based on Zend_Log, firebug and firephp.
  45.  * 
  46.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  47.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  48.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  49.  * @link http://www.launchpad.net/mai
  50.  * 
  51.  * @category Mai
  52.  * @package Log
  53.  * 
  54.  */
  55.  
  56. class Mai_Log
  57. {
  58.     /**
  59.      * @var Mai_Log Singleton instance
  60.      */
  61.     protected static $_instance;
  62.  
  63.     /**
  64.      * @var Zend_Log 
  65.      */
  66.     protected static $_logger;
  67.  
  68.     /**
  69.      * @var Zend_Controller_Request_Http 
  70.      */
  71.     protected static $_request;
  72.  
  73.     /**
  74.      * @var Zend_Controller_Response_Http 
  75.      */
  76.     protected static $_response;
  77.  
  78.      /**
  79.      * @var Zend_Wildfire_Channel_HttpHeaders 
  80.      */
  81.     protected static $_channel;
  82.  
  83.     /*
  84.      * @var bool Status of the log process
  85.      */
  86.     protected static $_init false;
  87.     
  88.     /**
  89.      * Private constructor to prevent creation of this object.
  90.      * Creates a log object.
  91.      * @return void 
  92.      */
  93.     private function __construct({}
  94.  
  95.  
  96.     /**
  97.      * Retrieve singleton instance
  98.      * @param $creator Creator of this class.
  99.      * @return Mai_Log 
  100.      */
  101.     public static function getInstance($creator)
  102.     {            
  103.         if(!Mai::isInit(&& !($creator instanceof Mai))
  104.         {
  105.             trigger_error('Mai has not been setup yet. Please use Mai::init().'E_USER_ERROR);
  106.         }
  107.         
  108.         if (null === self::$_instance{
  109.             self::$_instance new self();
  110.         }
  111.         return self::$_instance;
  112.     }
  113.  
  114.     /**
  115.      * Create the required objects. Starts up the log process.
  116.      * @param $creator Creator of this class.
  117.      * @return void 
  118.      */
  119.     public static function init($creator)
  120.     {
  121.         self::getInstance($creator);
  122.         
  123.         //Log to firePHP if env is development and firePHP is enabled.
  124.         if(Mai::getEnv(=== Mai::ENV_DEVELOPMENT && Mai_Config::getVar()->log->firephp)
  125.         {
  126.             //Create new log object
  127.             $writer new Zend_Log_Writer_Firebug();
  128.             self::$_logger new Zend_Log($writer);
  129.  
  130.             //Create request and reponse object
  131.             self::$_request new Zend_Controller_Request_Http();
  132.             self::$_response new Zend_Controller_Response_Http();
  133.  
  134.             //Create a new channel and set the request and response object
  135.             self::$_channel Zend_Wildfire_Channel_HttpHeaders::getInstance();
  136.             self::$_channel->setRequest(self::$_request);
  137.             self::$_channel->setResponse(self::$_response);
  138.  
  139.             //Start buffer
  140.             ob_start();
  141.         }
  142.         else
  143.         {
  144.             //Create a new log object
  145.             $writer new Zend_Log_Writer_Stream(Mai::baseDir("/Mai/Log/Files/mai_log.log");
  146.             self::$_logger new Zend_Log($writer);
  147.         }
  148.         
  149.         self::$_init true;
  150.     }
  151.  
  152.     /**
  153.      * Send a info message to the log.
  154.      * @return Zend_Log 
  155.      */
  156.     public static function info($message)
  157.     {
  158.         if(empty($message))
  159.         {
  160.             trigger_error("Message is empty or not set."E_USER_NOTICE);
  161.         }
  162.         
  163.         if(Mai_Config::getVar()->log->enabled &&
  164.           (Mai::getEnv(=== Mai::ENV_DEVELOPMENT || Mai_Config::getVar()->log->all))
  165.         {
  166.             if(!self::$_init)
  167.             {
  168.                 trigger_error("Logger not yet started. Please use init() first."E_USER_ERROR);
  169.             }
  170.  
  171.             self::$_logger->info($message);
  172.         }
  173.     }
  174.  
  175.     /**
  176.      * Send output to the browser when firePHP is used.
  177.      * @return void 
  178.      */
  179.     public static function flush()
  180.     {
  181.         if(Mai::getEnv(=== Mai::ENV_DEVELOPMENT && Mai_Config::getVar()->log->firephp)
  182.         {
  183.             if(!self::$_init)
  184.             {
  185.                 trigger_error("Logger not yet started. Please use init() first."E_USER_ERROR);
  186.             }
  187.  
  188.             self::$_channel->flush();
  189.             self::$_response->sendHeaders();
  190.         }
  191.     }
  192.  
  193.     /**
  194.      * Cloning of a singleton class is not allowed.
  195.      * @return void 
  196.      */
  197.     final public function __clone()
  198.     {
  199.         trigger_error('Clone is not allowed.'E_USER_ERROR);
  200.     }
  201.  
  202.     /**
  203.      * Function to prevent deserializing
  204.      * @return void 
  205.      */
  206.     public function __wakeup()
  207.     {
  208.         trigger_error('Deserializing is not allowed.'E_USER_ERROR);
  209.     }
  210. }

Documentation generated on Mon, 27 Jul 2009 19:55:08 +0200 by phpDocumentor 1.4.1