/
NWLog.php
167 lines (129 loc) · 3.54 KB
/
NWLog.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/*
/\
/ \ NitricWare Studios
/ /\ \ Linz/Danube
/\/\/\/
\/
NitricWare presents
NWWriteLog 1.0.3
Development started
15. December 2012
Github release
7. May 2015
This function is designed to create a log
and add lines to the created log.
The filename is the timestamp of the
current day at 00:00 by default. This
function automatically finds the correct
log in the given directory. If log does
not exist, file will be created.
The time (hh:mm:ss) is added too.
*/
namespace NitricWare;
/*
NWWriteLog
Adds a line to the log. Filename is timestamp.
$line
The line you want to add.
$customBacktrace
Send your own debug_backtrace() with the function call.
This can be handy when you call NWWriteLog() from within
an error handling function.
$path
The path to your logs.
*/
function NWWriteLog($line, $customBacktrace = false, $path = "./Logs/"){
$date = strtotime(date("d-m-y", time()));
$file = "log_".$date.".txt";
$time = date("h:i:s", time());
$backtrace = debug_backtrace();
if ($customBacktrace){
$backtrace = $customBacktrace;
}
$pathinfo = pathinfo($backtrace[0]["file"]);
if (array_key_exists(1, $backtrace)){
$t = $backtrace[1];
} else {
$t = array("function" => "file");
}
if ($t["function"] == "include"){
$sender = $t["function"]."(".$pathinfo["basename"].":".$backtrace[0]["line"].")";
} elseif (array_key_exists("class", $t)){
// Function is called from within a class
$sender = $t["class"].$t["type"].$t["function"]."() (".$pathinfo["basename"].":".$backtrace[0]["line"].")";
} else {
$sender = $t["function"]."() (".$pathinfo["basename"].":".$backtrace[0]["line"].")";
}
file_put_contents($path.$file, "$time $sender - $line\n", FILE_APPEND);
}
/*
NWDeleteLog
Deletes a log file.
$date
"today": today's timestamp is used as filename
any int: timestamp of the file
$path
The path to your logs.
*/
function NWDeleteLog($date = "today", $path = "./Logs/"){
if ($date = "today"){
$date = strtotime(date("d-m-y", time()));
}
$file = "log_".$date.".txt";
if (file_exists($path.$file)){
if (!NWDelete($path.$file, false)){
if (DEBUG) NWWriteLog("Couldn't clear history.");
return false;
} else {
if (DEBUG) NWWriteLog("Log cleared.");
return true;
}
} else {
return false;
}
}
/*
NWPrintLog
Returns the specified lines of a log file.
$offset
int: where to begin
$maxLenghts
int: where to stop
false: til the end
$date
"today": today's timestamp is used as filename
any int: timestamp of the file
$path
The path to your logs.
*/
function NWPrintLog($offset = 0, $maxLenght = false, $date = "today",$path = "./Logs/"){
if ($date = "today"){
$date = strtotime(date("d-m-y", time()));
}
$file = "log_".$date.".txt";
if ($logRaw = file($path.$file)){
$logLinesCount = count($logRaw);
} else {
if (DEBUG) NWWriteLog("Requested log does not exist.");
return false;
}
if ($offset > $logLinesCount OR $offset < 0){
if (DEBUG) NWWriteLog("Parameter offset failed.");
return false;
}
if (!$maxLenght){
$maxLenght = $logLinesCount - 1;
}
if ($maxLenght > $logLinesCount OR $maxLenght < 0){
if (DEBUG) NWWriteLog("Parameter maxLenght failed.");
return false;
}
$logString = "";
foreach($logRaw as $key => $value){
if ($key >= $offset AND $key <= $maxLenght){
$logString .= $value;
}
}
return $logString;
}