/src/Plugin/Logwriter.php

https://github.com/DQNEO/ethnam · PHP · 199 lines · 106 code · 23 blank · 70 comment · 23 complexity · 4edda7718670bbb0aa4b49a47fbafd7f MD5 · raw file

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