-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.php
More file actions
147 lines (138 loc) · 3.97 KB
/
Console.php
File metadata and controls
147 lines (138 loc) · 3.97 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* @category Lib @package Test Suit @copyright 2011, 2012 Dmitry Sheiko (http://dsheiko.com) @license GNU
*/
/**
* Logger in FireBug fashion
*/
class Console {
private static $MODE = false;
private static $LOGS = false;
private static $LOGS_PATH = false;
private $messages = array();
public $mtime;
public static $log_fun = "console.log";
public function __construct(){
$this->mtime = microtime( true );
}
public static function set($set) {
self::$MODE = $set;
}
public static function createLogFiles($set) {
self::$LOGS = $set;
if(self::$LOGS){
self::$LOGS_PATH = get_include_path() . Config::get('BUILD_PATH').'/logs';
if (!file_exists(self::$LOGS_PATH)) {
if(!mkdir(self::$LOGS_PATH, 0777, true)){
die('Failed to create folders:'.self::$LOGS_PATH);
};
}
}
}
/**
* Decorator for trace info
*
* @param array $trace
* - debug_backtrace results
* @return string
*/
private static function _debugDecorator($trace) {
return sprintf ( "%s : %d >", str_replace ( BASE_PATH, "", $trace [0] ["file"] ), $trace [0] ["line"] );
}
/**
* Logger implementation
*
* @param array $args
* @param string $logName
*/
private static function _log($args, $logName = "error") {
if(self::$LOGS && $args=null){
$logExt = "." . date ( "Y-m-d" ) . ".log";
foreach ( $args as $data ) {
file_put_contents ( self::$LOGS_PATH . "/" . $logName . $logExt,
print_r ( $data, 1 ) . "\n##################################################################\n",
FILE_APPEND );
}
}
if (self::$MODE && isset($data)) {
echo $data . "<br/>";
}
}
/**
* Generic logger alias
* Usage: console::log(mixed, mixed, .
* .)
*/
public static function log() {
$trace = debug_backtrace ();
$args = func_get_args ();
$args = array_merge ( array (
self::_debugDecorator ( $trace )
), $args );
self::_log ( $args, 'info' );
}
public static function error() {
$trace = debug_backtrace ();
$args = func_get_args ();
$args = array_merge ( array (
self::_debugDecorator ( $trace )
), $args );
self::_log ( $args, 'error' );
}
public static function exception(Exception $e) {
$trace = debug_backtrace ();
$args = func_get_args ();
$args = array_merge ( array (
self::_debugDecorator ( $trace )
), $args );
self::_log ( $args, 'exception' );
}
public function browser($msgData,$trace=null,$logFun="console.log"){
if($trace==null){
$trace = debug_backtrace();
}
$this->messages[][$trace[0]["file"].'('.$trace[0]["line"].')'] = array( 'msg' => $msgData, 'fun' => $logFun);
}
/**
* Output any return data to the javascript console/source of page
* Usage (assuming minifier is initiated as $minifier):
* <?php $console->printlogs(); ?>
*
* @param none
* @return string
*/
public function printlogs(){
//Add the timer the console.log output if desired
$this->messages[]['Rudrax:done'] = 'Script processed and loaded in '. ( microtime( true ) - $this->mtime ) .' seconds';
if( !empty( $this->messages ) ){
echo '<script>';
foreach( $this->messages as $this->data ){
foreach( $this->data as $file => $msgObj){
if(is_string($msgObj)){
echo self::$log_fun.'("'.$file .' : ","'. $msgObj.'");' . PHP_EOL;
} else {
echo $msgObj['fun'].'("'.$file .' : ",'. $msgObj['msg'].');' . PHP_EOL;
}
}
}
echo '</script>';
} //end !empty( $this-messages )
} //end logs()
public function printlogsOnHeader(){
//Add the timer the console.log output if desired
$this->messages[]['Rudrax:done'] = 'Script processed and loaded in '. ( microtime( true ) - $this->mtime ) .' seconds';
$logs = "";
if( !empty( $this->messages ) ){
foreach( $this->messages as $this->data ){
foreach( $this->data as $file => $msgObj){
if(is_string($msgObj)){
$logs.=('["'.$file .' : ","'. $msgObj.'"]');
} else {
$logs.=('["'.$file .' : ",'. $msgObj['msg'].'];');
}
}
}
header("X-BLOGS:".$logs);
} //end !empty( $this-messages )
} //end logs()
}