-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadfpm.php
More file actions
88 lines (73 loc) · 1.86 KB
/
threadfpm.php
File metadata and controls
88 lines (73 loc) · 1.86 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
<?php
if(!function_exists('ts_var_declare')) die("Not threadfpm\n");
define('LOG_SIZE', 2*1024*1024);
define('LOG_FILE', 'threadfpm.log');
define('LOG_IDX', 'threadfpm.idx');
define('LOG_FMT', 'threadfpm-%02d.log');
define('N', 50);
$running = true;
$sighandle = function($sig) use(&$running) {
$running = false;
echo "SIG: $sig\n";
};
pcntl_async_signals(true);
pcntl_signal(SIGTERM, $sighandle, false);
pcntl_signal(SIGINT, $sighandle, false);
pcntl_signal(SIGUSR1, $sighandle, false);
pcntl_signal(SIGUSR2, $sighandle, false);
$logVar = ts_var_declare('logger', null, true);
$logFd = ts_var_fd($logVar);
$fp = fopen(LOG_FILE, 'a');
$line = str_repeat('-', N);
$line2 = str_repeat('=', N);
$nlog = (int) @file_get_contents(LOG_IDX);
echo "phpfile begin\n";
while($running || ts_var_count($logVar)) {
$reads = [$logFd];
$writes = $excepts = [];
if(!@socket_select($reads, $writes, $excepts, 1)) {
continue;
}
$n = strlen(socket_read($logFd, 128)?:'');
for($i=0; $i<$n; $i++) {
$log = ts_var_shift($logVar, $key);
fwrite($fp, "$line2\nKEY: $key\n");
if(is_array($log) || is_object($log)) {
foreach($log as $k=>$v) {
$k = strtoupper($k);
$v = format($v);
if(strpos($v, "\n")) {
$v = "\n$v";
}
fwrite($fp, "$line\n$k: $v\n");
}
} else {
fwrite($fp, "$log\n");
}
fwrite($fp, "\n");
}
$i = strlen((string) $n);
$pi = (int) ((N - 2 - $i) / 2);
$prefix = str_repeat('=', $pi);
$suffix = str_repeat('=', N - 2 - $i - $pi);
fwrite($fp, "$prefix $n $suffix\n");
fflush($fp);
if(ftell($fp) > LOG_SIZE) {
fclose($fp);
$nlog++;
if($nlog > N) {
$nlog = 1;
}
file_put_contents(LOG_IDX, $nlog);
rename(LOG_FILE, sprintf(LOG_FMT, $nlog));
$fp = fopen(LOG_FILE, 'a');
}
}
echo "phpfile end\n";
function format($v) {
if(is_array($v) || is_object($v)) {
return var_export($v, true);
} else {
return (string) $v;
}
}