-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.php
More file actions
executable file
·106 lines (92 loc) · 3.77 KB
/
Copy pathLog.php
File metadata and controls
executable file
·106 lines (92 loc) · 3.77 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
<?php
/**
* Make Log File with Content, Support Magic Variables for Path.
*
* Table of Magic Variables:
* Year `{{YYYY}}`|`{{YY}}` ; Month `{{MM}}` ; Day `{{DD}}` ;
* Hour `{{HH}}` ; Minute `{{MI}}` ; Second `{{SS}}` ;
* Time Zone `{{TZ}}` ; Time Zone String `{{TZS}}` .
*/
function MakeLog(string $Content, string $Path = "Log/{{YYYY}}-{{MM}}-{{DD}}.txt"): array {
$Response = [
"Ec" => 0,
"Em" => "",
"Path" => ""
];
$TimeConst = time();
$Path = str_replace("{{YYYY}}", date("Y", $TimeConst), $Path); // 年, Eg: 2021
$Path = str_replace("{{YY}}" , date("y", $TimeConst), $Path); // 年, Eg: 21
$Path = str_replace("{{MM}}" , date("m", $TimeConst), $Path); // 月, Eg: 01
$Path = str_replace("{{DD}}" , date("d", $TimeConst), $Path); // 日, Eg: 01
$Path = str_replace("{{HH}}" , date("H", $TimeConst), $Path); // 时, Eg: 00
$Path = str_replace("{{MI}}" , date("i", $TimeConst), $Path); // 分, Eg: 00
$Path = str_replace("{{SS}}" , date("s", $TimeConst), $Path); // 秒, Eg: 00
$Path = str_replace("{{TZ}}" , date("O", $TimeConst), $Path); // 时区, Eg: +0800
$Path = str_replace("{{TZS}}" , date("T", $TimeConst), $Path); // 时区, Eg: CST
try {
if (dirname($Path) && !file_exists(dirname($Path))) {
mkdir(dirname($Path), 0777, true);
}
} catch (Exception $Error) {
$Response["Ec"] = 50001; $Response["Em"] = MakeErrorMessage($Error, $Response["Ec"]); return $Response;
}
try {
$Fp = fopen($Path, "a");
if ($Fp === false) {
throw new Exception("Unable To Open File: $Path");
}
if (flock($Fp, LOCK_EX)) {
fwrite($Fp, sprintf("[%s] %s\n", date("Y-m-d H:i:s", $TimeConst), $Content));
fflush($Fp);
flock($Fp, LOCK_UN);
} else {
throw new Exception("Unable To Lock File: $Path");
}
fclose($Fp);
$Response["Path"] = $Path;
} catch (Exception $Error) {
$Response["Ec"] = 50002; $Response["Em"] = MakeErrorMessage($Error, $Response["Ec"]); return $Response;
}
return $Response;
}
/**
* Make Error Message, Include File, Line Number and Function Name, for Debugging.
*/
function MakeErrorMessage(Throwable $Error, mixed $Code = null): string {
if ($Error->getFile() && $Error->getLine()) {
$Modu = str_replace(getcwd() . DIRECTORY_SEPARATOR, "", $Error->getFile()) ?: "N/A";
$Func = isset($Error->getTrace()[0]["function"]) ? "Function <" . $Error->getTrace()[0]["function"] . ">" : "N/A";
$Line = $Error->getLine() ?: "N/A";
} else {
$Modu = "N/A";
$Func = "N/A";
$Line = "N/A";
}
if (get_class($Error)) {
$Type = get_class($Error);
} else {
$Type = "Error";
}
$S_Message = rtrim($Error->getMessage(), ".");
$T_Message = mb_convert_case($S_Message, MB_CASE_TITLE, "UTF-8");
if (mb_strlen($S_Message, "UTF-8") === mb_strlen($T_Message, "UTF-8")) {
$Message = "";
$S_Chars = preg_split("//u", $S_Message, -1, PREG_SPLIT_NO_EMPTY);
$T_Chars = preg_split("//u", $T_Message, -1, PREG_SPLIT_NO_EMPTY);
if ($S_Chars !== false && $T_Chars !== false) {
foreach ($S_Chars as $Index => $S) {
$T = $T_Chars[$Index];
$Message .= (
$S === mb_strtoupper($S, "UTF-8") &&
$S !== mb_strtolower($S, "UTF-8")
) ? $S : $T;
}
} else {
$Message = $T_Message;
}
} else {
$Message = $T_Message;
}
return sprintf("File \"%s\", Line %s, in %s %s@%s: %s.", $Modu, $Line, $Func, $Code ?? "", $Type, $Message);
}
?>