Source for file File.php

Documentation is available at File.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  File.php
  5.  *
  6.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: a03c5dfd82953109b1b1c638a859908197672cb4 $
  10.  */
  11.  
  12. // {{{ Ethna_Plugin_Logwriter_File
  13. /**
  14.  *  ログ出力クラス(File)
  15.  *
  16.  *  @author     Masaki Fujimoto <fujimoto@php.net>
  17.  *  @access     public
  18.  *  @package    Ethna
  19.  */
  20. {
  21.     /**#@+
  22.      *  @access private
  23.      */
  24.  
  25.     /** @private    int     ログファイルハンドル */
  26.     private $fp;
  27.  
  28.     /** @private    int     ログファイルパーミッション */
  29.     private $mode 0666;
  30.  
  31.     /**#@-*/
  32.  
  33.     /**
  34.      *  Fileクラスのコンストラクタ
  35.      *
  36.      *  @access public
  37.      */
  38.     public function __construct()
  39.     {
  40.         $this->fp null;
  41.     }
  42.  
  43.     /**
  44.      *  ログオプションを設定する
  45.      *
  46.      *  @access public
  47.      *  @param  int     $option     ログオプション(LOG_FILE,LOG_FUNCTION...)
  48.      */
  49.     function setOption($option)
  50.     {
  51.         parent::setOption($option);
  52.         
  53.         if (isset($option['file'])) {
  54.             $this->file $option['file'];
  55.         else {
  56.             $this->file $this->_getLogFile();
  57.         }
  58.  
  59.         if (isset($option['mode'])) {
  60.             $this->mode $option['mode'];
  61.         }
  62.     }
  63.  
  64.     /**
  65.      *  ログ出力を開始する
  66.      *
  67.      *  @access public
  68.      */
  69.     function begin()
  70.     {
  71.         $this->fp fopen($this->file'a');
  72.         $st fstat($this->fp);
  73.         if (function_exists("posix_getuid"&& posix_getuid(== $st[4]{
  74.             chmod($this->fileintval($this->mode8));
  75.         }
  76.     }
  77.  
  78.     /**
  79.      *  ログを出力する
  80.      *
  81.      *  @access public
  82.      *  @param  int     $level      ログレベル(LOG_DEBUG, LOG_NOTICE...)
  83.      *  @param  string  $message    ログメッセージ(+引数)
  84.      */
  85.     function log($level$message)
  86.     {
  87.         if ($this->fp == null{
  88.             return;
  89.         }
  90.  
  91.         $prefix strftime('%Y/%m/%d %H:%M:%S '$this->ident;
  92.         if (array_key_exists("pid"$this->option)) {
  93.             $prefix .= sprintf('[%d]'getmypid());
  94.         }
  95.         $prefix .= sprintf('(%s): '$this->_getLogLevelName($level));
  96.         if (array_key_exists("function"$this->option||
  97.             array_key_exists("pos"$this->option)) {
  98.             $tmp "";
  99.             $bt $this->_getBacktrace();
  100.             if ($bt && array_key_exists("function"$this->option&& $bt['function']{
  101.                 $tmp .= $bt['function'];
  102.             }
  103.             if ($bt && array_key_exists("pos"$this->option&& $bt['pos']{
  104.                 $tmp .= $tmp sprintf('(%s)'$bt['pos']$bt['pos'];
  105.             }
  106.             if ($tmp{
  107.                 $prefix .= $tmp ": ";
  108.             }
  109.         }
  110.         fwrite($this->fp$prefix $message "\n");
  111.  
  112.         return $prefix $message;
  113.     }
  114.  
  115.     /**
  116.      *  ログ出力を終了する
  117.      *
  118.      *  @access public
  119.      */
  120.     function end()
  121.     {
  122.         if ($this->fp{
  123.             fclose($this->fp);
  124.             $this->fp null;
  125.         }
  126.     }
  127.  
  128.     /**
  129.      *  ログファイルの書き出し先を取得する(ログファシリティに
  130.      *  LOG_FILEが指定されている場合のみ有効)
  131.      *
  132.      *  ログファイルの書き出し先を変更したい場合はこのメソッドを
  133.      *  オーバーライドします
  134.      *
  135.      *  @access protected
  136.      *  @return string  ログファイルの書き出し先
  137.      */
  138.     function _getLogFile()
  139.     {
  140.         $controller Ethna_Controller::getInstance();
  141.  
  142.         if (array_key_exists("dir"$this->option)) {
  143.             $dir $this->option['dir'];
  144.         else {
  145.             $dir $controller->getDirectory('log');
  146.         }
  147.  
  148.         return sprintf('%s/%s.log',
  149.             $dir,
  150.             strtolower($controller->getAppid())
  151.         );
  152.     }
  153. }
  154. // }}}

Documentation generated on Fri, 11 Nov 2011 03:58:07 +0900 by phpDocumentor 1.4.3