Fwolflib::LogGet()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package		fwolflib
4
 * @subpackage	class
5
 * @copyright	Copyright © 2010, Fwolf
6
 * @author		Fwolf <[email protected]>
7
 * @since		2010-01-30
8
 */
9
10
11
require_once(dirname(__FILE__) . '/../fwolflib.php');
12
require_once(FWOLFLIB . 'func/env.php');
13
14
15
/**
16
 * Basic class of all.
17
 *
18
 * Log included.
19
 *
20
 * @deprecated  Use classes in Fwlib namespace, see PSR-0/1/2
21
 * @package		fwolflib
22
 * @subpackage	class
23
 * @copyright	Copyright © 2010, Fwolf
24
 * @author		Fwolf <[email protected]>
25
 * @since		2010-01-30
26
 */
27
class Fwolflib {
28
29
	/**
30
	 * Configuation
31
	 *
32
	 * Notice: re-define var in sub-class will overwrite var in parent class.
33
	 * @var	array
34
	 */
35
	public $aCfg = array();
36
37
	/**
38
	 * Default time format
39
	 * @var	string
40
	 */
41
	public $sFormatTime = 'Y-m-d H:i:s';
42
43
	/**
44
	 * Log msg.
45
	 *
46
	 * Log level 5(or 4) can be used as error or warning.
47
	 *
48
	 * array(
49
	 * 	i	=> array(
50
	 * 		level	=> 1/notice, 3/common, 5/imortant
51
	 * 			Can also be 2 or 4, even 1.5(not suggested),
52
	 * 			Default is 3.
53
	 * 		time	=>
54
	 * 		msg		=>
55
	 * 	)
56
	 * )
57
	 *
58
	 * @var	array
59
	 */
60
	public $aLog = array();
61
62
63
	/**
64
	 * constructor
65
	 *
66
	 * @param	array	$ar_cfg
67
	 */
68
	public function __construct ($ar_cfg = array()) {
69
		$this->SetCfgDefault();
70
		if (!empty($ar_cfg))
71
			$this->SetCfg($ar_cfg);
72
		$this->Init();
73
	} // end of func __construct
74
75
76
	/**
77
	 * Dummy destructor
78
	 */
79
	public function __destruct () {
80
	} // end of func __destruct
81
82
83
	/**
84
	 * Auto new obj if not set, for some special var only
85
	 *
86
	 * @param	string	$name
87
	 * @return	object
88
	 */
89 View Code Duplication
	public function __get ($name) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
		if ('o' == $name{0}) {
91
			$s_func = 'NewObj' . substr($name, 1);
92
			if (method_exists($this, $s_func)) {
93
				// New object
94
				$this->$name = $this->$s_func();
95
				return $this->$name;
96
			}
97
		}
98
99
		return null;
100
	} // end of func __get
101
102
103
	/**
104
	 * Init func
105
	 * Do things according default then user config.
106
	 *
107
	 * @return	object
108
	 */
109
	public function Init () {
110
		return $this;
111
	} // end of func Init
112
113
114
	/**
115
	 * Record log msg.
116
	 *
117
	 * @param	string	$msg
118
	 * @param	int		$level
119
	 * @return	$this
120
	 */
121
	public function Log ($msg, $level = 3) {
122
		$ar = array(
123
			'level'	=> $level,
124
			'time'	=> date($this->sFormatTime),
125
			'msg'	=> $msg,
126
		);
127
		$this->aLog[] = $ar;
128
129
		// Log to errorlog ?
130
		if ($this->aCfg['log-errorlog'] <= $level) {
131
			$s_error = "Log {$ar['level']}: {$ar['msg']}\n";
132
133
			// Log backtrace
134
			if ($this->aCfg['log-backtrace']) {
135
				$ar = debug_backtrace();
136
				foreach ($ar as $error) {
137
					$s_error .= "\tLine " . $error['line']
138
						. ' in ' . $error['file'] . "\n";
139
					if (!empty($error['class']))
140
						$s_error .= "\t\t" . $error['class']
141
							. '::' . $error['function'] . "()\n";
142
					elseif (!empty($error['function']))
143
						$s_error .= "\t\t" . $error['function'] . "()\n";
144
				}
145
			}
146
147
			error_log($s_error);
148
		}
149
150
		return $this;
151
	} // end of func Log
152
153
154
	/**
155
	 * Get all log msg.
156
	 *
157
	 * @param	$level	Only output log which's level >= $level
158
	 * @return	string
159
	 */
160
	public function LogGet ($level = 3) {
161
		if (IsCli())
0 ignored issues
show
Deprecated Code introduced by
The function IsCli() has been deprecated with message: Use Fwlib\Util\Env::isCli()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
162
			$s_split = "\n";
163
		else
164
			$s_split = "<br />\n";
165
166
		$s = '';
167
		if (!empty($this->aLog))
168
			foreach ($this->aLog as $log) {
169
				if ($level <= $log['level'])
170
					$s .= "[${log['time']}] ${log['msg']}"
171
						. $s_split;
172
			}
173
174
		return $s;
175
	} // end of func LogGet
176
177
178
	/**
179
	 * Clear all rules
180
	 *
181
	 * @param	boolean		$b_init	Re-do init.
182
	 */
183
	public function Reset ($b_init = false) {
184
		$this->aRule = array();
0 ignored issues
show
Bug introduced by
The property aRule does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
185
		$this->SetCfgDefault();
186
		if (true == $b_init)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
187
			$this->Init();
188
	} // end of func Reset
189
190
191
	/**
192
	 * Set config array
193
	 *
194
	 * @see		$aCfg
195
	 * @param	array	$k
196
	 * @param	mixed	$v
197
	 * @return	this
198
	 */
199 View Code Duplication
	public function SetCfg ($k, $v = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
		if (is_array($k)) {
201
			// Use array $k only, ignore $v
202
			if (!empty($k)) {
203
				foreach ($k as $key => $val)
204
					$this->aCfg[$key] = $val;
205
			}
206
		}
207
		else {
208
			// Use array $k => $v
209
			$this->aCfg[$k] = $v;
210
		}
211
		return $this;
212
	} // end of func SetCfg
213
214
215
	/**
216
	 * Set default config.
217
	 *
218
	 * @return	object
219
	 */
220
	 protected function SetCfgDefault () {
221
		// Log level eq/gt this will write to php errorlog
222
		$this->aCfg['log-errorlog']	= 4;
223
		// Print backtrace in log
224
		$this->aCfg['log-backtrace'] = false;
225
226
		return $this;
227
	 } // end of func SetCfgDefault
228
229
230
} // end of class Fwolflib
231
?>
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...
232