Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace SmartData;
require_once( __DIR__ . '/Config.php');
abstract class Logger
{
public static $IDENT = 'SmartData';
private static $TO_FILE = false;
private static $LEVEL = LOG_DEBUG;
public static $TO_SCREEN = false;
private static $_buffer = null;
// LOG_ERR
// LOG_NOTICE
// LOG_INFO
// LOG_DEBUG
public static function time_elapsed($reset=false)
{
static $last = null;
$now = microtime(true)*1000000;
if ($last != null && !$reset) {
Logger::debug('<!-- ' . ($now - $last) . ' us -->', true);
}
$last = $now;
}
public static function debug(string $message, bool $force=false) {
self::_log($message, LOG_DEBUG, $force);
}
public static function debug_X(string $message, $ctrl=false) {
if(is_string($ctrl))
$message = "{$ctrl}> {$message}";
self::_log($message, LOG_DEBUG, ( ($ctrl === true || Config::config()::HYSTERICALLY_DEBUGGED || (Config::config()::DEBUGGED[$ctrl] ?? false) ) ));
}
public static function exception(\Exception $e) {
$message = get_class($e) . "> {$e->getFile()}:{$e->getLine()}> {$e->getMessage()}\n";
$message .= $e->getTraceAsString()."\n";
self::_log($message, LOG_ERR, true);
}
public static function print_r($object, bool $force=false) {
ob_start();
print_r($object);
$log = ob_get_clean();
self::debug($log, $force);
}
public static function var_dump($object, bool $force=false) {
ob_start();
var_dump($object);
$log = ob_get_clean();
self::debug($log, $force);
}
public static function flush() {
if(Config::config()::BUFFERED_LOG && Logger::$_buffer != null){
self::_write(Logger::$_buffer);
}
Logger::$_buffer = null;
}
private static function _log(string $message, int $priority, bool $force=false) {
global $__FORCE_SCREEN_LOG;
$log = date("Y-m-d h:i:s").' '.$message;
$flag = (filesize(Config::config()::LOG_FILE) > (10 * 1024 * 1024)) ? 0 : FILE_APPEND;
if (Logger::$TO_SCREEN || Logger::$TO_FILE || $force){
if(Config::config()::BUFFERED_LOG && !isset($__FORCE_SCREEN_LOG) && $priority !== LOG_ERR){
Logger::$_buffer = (Logger::$_buffer ?? '') . $log . "\n";
} else {
self::_write($log);
}
}
}
private static function _write(string $message) {
global $__FORCE_SCREEN_LOG;
if (Logger::$TO_SCREEN || (isset($__FORCE_SCREEN_LOG) && $__FORCE_SCREEN_LOG === true) ){
echo $message."\n";
} else {
$flag = (filesize(Config::config()::LOG_FILE) > (200 * 1024 * 1024)) ? 0 : FILE_APPEND;
file_put_contents(Config::config()::LOG_FILE, $message."\n", $flag);
}
}
}
openlog(Logger::$IDENT, LOG_PID | LOG_PERROR, LOG_LOCAL0);