OSDN Git Service

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