Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * sfVarLogger logs messages within its instance for later use.
13
 *
14
 * @package    symfony
15
 * @subpackage log
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfVarLogger.class.php 26989 2010-01-21 12:43:21Z FabianLange $
18
 */
19
class sfVarLogger extends sfLogger
20
{
21
  protected
22
    $logs          = array(),
23
    $xdebugLogging = false;
24
 
25
  /**
26
   * Initializes this logger.
27
   *
28
   * Available options:
29
   *
30
   * - xdebug_logging: Whether to add xdebug trace to the logs (false by default).
31
   *
32
   * @param  sfEventDispatcher $dispatcher  A sfEventDispatcher instance
33
   * @param  array             $options     An array of options.
34
   *
35
   * @return Boolean           true, if initialization completes successfully, otherwise false.
36
   */
37
  public function initialize(sfEventDispatcher $dispatcher, $options = array())
38
  {
39
    $this->xdebugLogging = isset($options['xdebug_logging']) ? $options['xdebug_logging'] : false;
40
 
41
    // disable xdebug when an HTTP debug session exists (crashes Apache, see #2438)
42
    if (isset($_GET['XDEBUG_SESSION_START']) || isset($_COOKIE['XDEBUG_SESSION']))
43
    {
44
      $this->xdebugLogging = false;
45
    }
46
 
47
    return parent::initialize($dispatcher, $options);
48
  }
49
 
50
  /**
51
   * Gets the logs.
52
   *
53
   * Each log entry has the following attributes:
54
   *
55
   *  * priority
56
   *  * time
57
   *  * message
58
   *  * type
59
   *  * debugStack
60
   *
61
   * @return array An array of logs
62
   */
63
  public function getLogs()
64
  {
65
    return $this->logs;
66
  }
67
 
68
  /**
69
   * Returns all the types in the logs.
70
   *
71
   * @return array An array of types
72
   */
73
  public function getTypes()
74
  {
75
    $types = array();
76
    foreach ($this->logs as $log)
77
    {
78
      if (!in_array($log['type'], $types))
79
      {
80
        $types[] = $log['type'];
81
      }
82
    }
83
 
84
    sort($types);
85
 
86
    return $types;
87
  }
88
 
89
  /**
90
   * Returns all the priorities in the logs.
91
   *
92
   * @return array An array of priorities
93
   */
94
  public function getPriorities()
95
  {
96
    $priorities = array();
97
    foreach ($this->logs as $log)
98
    {
99
      if (!in_array($log['priority'], $priorities))
100
      {
101
        $priorities[] = $log['priority'];
102
      }
103
    }
104
 
105
    sort($priorities);
106
 
107
    return $priorities;
108
  }
109
 
110
  /**
111
   * Returns the highest priority in the logs.
112
   *
113
   * @return integer The highest priority
114
   */
115
  public function getHighestPriority()
116
  {
117
    $priority = 1000;
118
    foreach ($this->logs as $log)
119
    {
120
      if ($log['priority'] < $priority)
121
      {
122
        $priority = $log['priority'];
123
      }
124
    }
125
 
126
    return $priority;
127
  }
128
 
129
  /**
130
   * Logs a message.
131
   *
132
   * @param string $message   Message
133
   * @param string $priority  Message priority
134
   */
135
  protected function doLog($message, $priority)
136
  {
137
    // get log type in {}
138
    $type = 'sfOther';
139
    if (preg_match('/^\s*{([^}]+)}\s*(.+?)$/s', $message, $matches))
140
    {
141
      $type    = $matches[1];
142
      $message = $matches[2];
143
    }
144
 
145
    $this->logs[] = array(
146
      'priority'        => $priority,
147
      'priority_name'   => $this->getPriorityName($priority),
148
      'time'            => time(),
149
      'message'         => $message,
150
      'type'            => $type,
151
      'debug_backtrace' => $this->getDebugBacktrace(),
152
    );
153
  }
154
 
155
  /**
156
   * Returns the debug stack.
157
   *
158
   * @return array
159
   *
160
   * @see debug_backtrace()
161
   */
162
  protected function getDebugBacktrace()
163
  {
164
    // if we have xdebug and dev has not disabled the feature, add some stack information
165
    if (!$this->xdebugLogging || !function_exists('debug_backtrace'))
166
    {
167
      return array();
168
    }
169
 
170
    $traces = debug_backtrace();
171
 
172
    // remove sfLogger and sfEventDispatcher from the top of the trace
173
    foreach ($traces as $i => $trace)
174
    {
175
      $class = isset($trace['class']) ? $trace['class'] : substr($file = basename($trace['file']), 0, strpos($file, '.'));
176
 
177
      if (
178
        !class_exists($class)
179
        ||
180
        (!in_array($class, array('sfLogger', 'sfEventDispatcher')) && !is_subclass_of($class, 'sfLogger') && !is_subclass_of($class, 'sfEventDispatcher'))
181
      )
182
      {
183
        $traces = array_slice($traces, $i);
184
        break;
185
      }
186
    }
187
 
188
    return $traces;
189
  }
190
}