OSDN Git Service

Remove PHP closing tag
[ethna/ethna.git] / class / Plugin / Logwriter.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Logwriter.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
13 /**
14  *  ログ出力基底クラス
15  *
16  *  @author     Masaki Fujimoto <fujimoto@php.net>
17  *  @access     public
18  *  @package    Ethna
19  */
20 class Ethna_Plugin_Logwriter extends Ethna_Plugin_Abstract
21 {
22     /**#@+
23      *  @access private
24      */
25
26     /** @var    string  ログアイデンティティ文字列 */
27     var $ident;
28
29     /** @var    int     ログファシリティ */
30     var $facility;
31
32     /** @var    int     ログオプション */
33     var $option;
34
35     /** @var    bool    バックトレースが取得可能かどうか */
36     var $have_backtrace;
37
38     /** @var    array   ログレベル名テーブル */
39     var $level_name_table = array(
40         LOG_EMERG   => 'EMERG',
41         LOG_ALERT   => 'ALERT',
42         LOG_CRIT    => 'CRIT',
43         LOG_ERR     => 'ERR',
44         LOG_WARNING => 'WARNING',
45         LOG_NOTICE  => 'NOTICE',
46         LOG_INFO    => 'INFO',
47         LOG_DEBUG   => 'DEBUG',
48     );
49
50     /**#@-*/
51
52     /**
53      *  Logwriterクラスのコンストラクタ
54      *
55      *  @access public
56      *  @param  string  $log_ident      ログアイデンティティ文字列(プロセス名等)
57      *  @param  int     $log_facility   ログファシリティ
58      *  @param  string  $log_file       ログ出力先ファイル名(LOG_FILEオプションが指定されている場合のみ)
59      *  @param  int     $log_option     ログオプション(LOG_FILE,LOG_FUNCTION...)
60      */
61     /*
62     function Ethna_Plugin_Logwriter()
63     {
64     }
65     */
66
67     /**
68      *  ログオプションを設定する
69      *
70      *  @access public
71      *  @param  int     $option     ログオプション(LOG_FILE,LOG_FUNCTION...)
72      */
73     function setOption($option)
74     {
75         $this->ident = $option['ident'];
76         $this->facility = $option['facility'];
77         $this->option = $option;
78         $this->have_backtrace = function_exists('debug_backtrace');
79     }
80
81     /**
82      *  ログ出力を開始する
83      *
84      *  @access public
85      */
86     function begin()
87     {
88     }
89
90     /**
91      *  ログを出力する
92      *
93      *  @access public
94      *  @param  int     $level      ログレベル(LOG_DEBUG, LOG_NOTICE...)
95      *  @param  string  $message    ログメッセージ(+引数)
96      */
97     function log($level, $message)
98     {
99     }
100
101     /**
102      *  ログ出力を終了する
103      *
104      *  @access public
105      */
106     function end()
107     {
108     }
109
110     /**
111      *  ログアイデンティティ文字列を取得する
112      *
113      *  @access public
114      *  @return string  ログアイデンティティ文字列
115      */
116     function getIdent()
117     {
118         return $this->ident;
119     }
120
121     /**
122      *  ログレベルを表示文字列に変換する
123      *
124      *  @access private
125      *  @param  int     $level  ログレベル(LOG_DEBUG,LOG_NOTICE...)
126      *  @return string  ログレベル表示文字列(LOG_DEBUG→"DEBUG")
127      */
128     function _getLogLevelName($level)
129     {
130         if (isset($this->level_name_table[$level]) == false) {
131             return null;
132         }
133         return $this->level_name_table[$level];
134     }
135
136     /**
137      *  ログ出力箇所の情報(関数名/ファイル名等)を取得する
138      *
139      *  @access private
140      *  @return array   ログ出力箇所の情報
141      */
142     function _getBacktrace()
143     {
144         $skip_method_list = array(
145             array('ethna', 'raise'),
146             array(null, 'raiseerror'),
147             array(null, 'handleerror'),
148             array('ethna_logger', null),
149             array('ethna_plugin_logwriter', null),
150             array('ethna_error', null),
151             array('ethna_apperror', null),
152             array('ethna_actionerror', null),
153             array('ethna_backend', 'log'),
154             array(null, 'ethna_error_handler'),
155             array(null, 'trigger_error'),
156         );
157
158         if ($this->have_backtrace == false) {
159             return null;
160         }
161
162         $bt = debug_backtrace();
163         $i = 0;
164         while ($i < count($bt)) {
165             if (isset($bt[$i]['class']) == false) {
166                 $bt[$i]['class'] = null;
167             }
168             if (isset($bt[$i]['file']) == false) {
169                 $bt[$i]['file'] = null;
170             }
171             if (isset($bt[$i]['line']) == false) {
172                 $bt[$i]['line'] = null;
173             }
174
175             $skip = false;
176
177             // メソッドスキップ処理
178             foreach ($skip_method_list as $method) {
179                 $class = $function = true;
180                 if ($method[0] != null) {
181                     $class = preg_match("/^$method[0]/i", $bt[$i]['class']);
182                 }
183                 if ($method[1] != null) {
184                     $function = preg_match("/^$method[1]/i", $bt[$i]['function']);
185                 }
186                 if ($class && $function) {
187                     $skip = true;
188                     break;
189                 }
190             }
191
192             if ($skip) {
193                 $i++;
194             } else {
195                 break;
196             }
197         }
198
199         $c =& Ethna_Controller::getInstance();
200         $basedir = $c->getBasedir();
201
202         $function = sprintf("%s.%s", isset($bt[$i]['class']) ? $bt[$i]['class'] : 'global', $bt[$i]['function']);
203
204         $file = $bt[$i]['file'];
205         if (strncmp($file, $basedir, strlen($basedir)) == 0) {
206             $file = substr($file, strlen($basedir));
207         }
208         if (strncmp($file, ETHNA_BASE, strlen(ETHNA_BASE)) == 0) {
209             $file = preg_replace('#^/+#', '', substr($file, strlen(ETHNA_BASE)));
210         }
211         $line = $bt[$i]['line'];
212         return array('function' => $function, 'pos' => sprintf('%s:%s', $file, $line));
213     }
214 }
215 // }}}