-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathWriteLog.php
95 lines (77 loc) · 1.93 KB
/
WriteLog.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
<?php
function LogPath($gameName, $path="./")
{
return "{$path}Games/$gameName/gamelog.txt";
}
function CreateLog($gameName, $path="./")
{
fclose(fopen(LogPath($gameName, $path), "w"));
}
function FmtPlayer($name, $id) {
return "<span class='p$id-label'>$name</span>";
}
function FmtKeyword($keyword) {
return "<span class='keyword'>$keyword</span>";
}
function WriteLog($text, $player = 0, $highlight=false, $path="./")
{
global $gameName;
if(!($handler = fopen(LogPath($gameName, $path), "a"))) {
//File does not exist
return;
}
$output = $highlight ? "<mark style='background-color: brown; color:azure;'>$text</mark>" : $text;
$output = $player != 0 ? FmtPlayer($output, $player) : $output;
$output = "<p class='log-entry'>$output</p>";
$output = $output . "\r\n";
fwrite($handler, $output);
fclose($handler);
}
function ClearLog($n=20)
{
global $gameName;
/*
$filename = "./Games/" . $gameName . "/gamelog.txt";
$handler = fopen($filename, "w");
fclose($handler);
*/
$filename = LogPath($gameName);
$handle = fopen($filename, "r");
$lines = array_fill(0, $n-1, '');
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle);
$lines[] = $buffer;
array_shift($lines);
}
fclose($handle);
}
$handle = fopen($filename, "w");
fwrite($handle, implode("", $lines));
fclose($handle);
}
function WriteError($text)
{
WriteLog("ERROR: " . $text);
}
function EchoLog($gameName)
{
$filename = LogPath($gameName);
$filesize = filesize($filename);
if ($filesize > 0 && ($handler = fopen($filename, "r"))) {
echo(fread($handler, $filesize));
fclose($handler);
}
}
function JSONLog($gameName, $path="./")
{
$filename = LogPath($gameName, $path);
$filesize = filesize($filename);
if ($filesize <= 0) {
return "";
}
$handler = fopen($filename, "r");
$response = fread($handler, $filesize);
fclose($handler);
return $response;
}