CDebugLog   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A __destruct() 0 16 2
C write() 0 40 7
1
<?php
2
3
//------------------------------------------------------------------------------
4
//
5
//  eTraxis - Records tracking web-based system
6
//  Copyright (C) 2005-2011  Artem Rodygin
7
//
8
//  This program is free software: you can redistribute it and/or modify
9
//  it under the terms of the GNU General Public License as published by
10
//  the Free Software Foundation, either version 3 of the License, or
11
//  (at your option) any later version.
12
//
13
//  This program is distributed in the hope that it will be useful,
14
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
//  GNU General Public License for more details.
17
//
18
//  You should have received a copy of the GNU General Public License
19
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//------------------------------------------------------------------------------
22
23
/**
24
 * Debugging
25
 *
26
 * This module implements debug logging to trace functions calls, values of parameters, performance, etc.
27
 * Module is configurable via {@link DEBUG_MODE} and {@link DEBUG_LOGS}.
28
 *
29
 * @package Engine
30
 * @subpackage Debugging
31
 */
32
33
//------------------------------------------------------------------------------
34
//  Definitions.
35
//------------------------------------------------------------------------------
36
37
/**#@+
38
 * Debug mode.
39
 */
40
define('DEBUG_MODE_OFF',   0);  // no debug logging
41
define('DEBUG_MODE_TRACE', 1);  // data-safe debug logging
42
define('DEBUG_MODE_FULL',  2);  // full debug logging
43
/**#@-*/
44
45
/**#@+
46
 * Type of debug message.
47
 */
48
define('DEBUG_LOG_OPENED',  1);  // log is opened
49
define('DEBUG_LOG_CLOSED',  2);  // log is closed
50
define('DEBUG_ERROR',       3);  // error
51
define('DEBUG_WARNING',     4);  // warning
52
define('DEBUG_NOTICE',      5);  // information notice
53
define('DEBUG_TRACE',       6);  // trace route
54
define('DEBUG_PERFORMANCE', 7);  // performance
55
define('DEBUG_DUMP',        8);  // user data dump
56
/**#@-*/
57
58
//------------------------------------------------------------------------------
59
//  Classes.
60
//------------------------------------------------------------------------------
61
62
/**
63
 * Debug logging, implemented via Singleton pattern.
64
 * @package Engine
65
 * @subpackage Debugging
66
 * @ignore
67
 */
68
class CDebugLog
69
{
70
    // Static object of itself.
71
    private static $object = NULL;
72
73
    // Handle of opened debug log.
74
    private $handle = FALSE;
75
76
    // Timestamp when page is started to execute.
77
    // Used to profile execution performance.
78
    private $timer = NULL;
79
80
    // If debugging is not disabled (see {@link DEBUG_MODE}),
81
    // creates debug log file (see {@link DEBUG_LOGS}) and opens it for appending.
82
    private function __construct ()
83
    {
84
        if (DEBUG_MODE != DEBUG_MODE_OFF)
85
        {
86
            $this->handle = fopen(DEBUG_LOGS . session_id() . '.log', 'a');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen(DEBUG_LOGS . session_id() . '.log', 'a') of type resource is incompatible with the declared type boolean of property $handle.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
88
            if ($this->handle !== FALSE)
89
            {
90
                list($msec, $sec) = explode(' ', microtime());
91
                $this->timer = (float)$msec + (float)$sec;
92
            }
93
        }
94
    }
95
96
    // Closes opened debug log file.
97
    public function __destruct()
98
    {
99
        if ($this->handle !== FALSE)
100
        {
101
            list($msec, $sec) = explode(' ', microtime());
102
103
            $timer = (float)$msec + (float)$sec;
104
            $timer -= $this->timer;
105
106
            self::write(DEBUG_PERFORMANCE, 'PHP time = ' . $timer);
107
            self::write(DEBUG_LOG_CLOSED);
108
109
            fclose($this->handle);
110
            $this->handle = FALSE;
111
        }
112
    }
113
114
    // Writes specified message to opened debug log.
115
    public static function write ($type, $str = NULL)
116
    {
117
        if (is_null(self::$object))
118
        {
119
            self::$object = new CDebugLog();
120
            self::write(DEBUG_LOG_OPENED, isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : NULL);
121
        }
122
123
        if ($type == DEBUG_DUMP && DEBUG_MODE != DEBUG_MODE_FULL)
124
        {
125
            return TRUE;
126
        }
127
128
        $res = FALSE;
129
130
        if (self::$object->handle !== FALSE)
131
        {
132
            $prefix = array
133
            (
134
                DEBUG_LOG_OPENED  => '[OPENED]   ',
135
                DEBUG_LOG_CLOSED  => "[CLOSED]\n",
136
                DEBUG_ERROR       => '[ERROR]    ',
137
                DEBUG_WARNING     => '[WARNING]  ',
138
                DEBUG_NOTICE      => '[NOTICE]   ',
139
                DEBUG_TRACE       => '[TRACE]    ',
140
                DEBUG_PERFORMANCE => '[PERFORM]  ',
141
                DEBUG_DUMP        => '[DUMP]     ',
142
            );
143
144
            $today = date('Y-m-d  H:i:s  ');
145
            $res = (fwrite(self::$object->handle, "{$today}{$prefix[$type]}{$str}\n") != -1);
146
147
            if ($type == DEBUG_ERROR)
148
            {
149
                error_log("eTraxis Error: {$str}");
150
            }
151
        }
152
153
        return $res;
154
    }
155
}
156
157
//------------------------------------------------------------------------------
158
//  Functions.
159
//------------------------------------------------------------------------------
160
161
/**
162
 * Writes specified message to debug log.
163
 *
164
 * @param int $type Type of debug message.
165
 * @param string $str The message to be written.
166
 * @return bool TRUE on success, FALSE otherwise.
167
 */
168
function debug_write_log ($type, $str = NULL)
169
{
170
    return CDebugLog::write($type, $str);
171
}
172
173
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
174