log-over-socket
Code (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
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
<?php
define('LOG_SOCKET_PATH', '/tmp/log_socket');
function log_recieve($callback) {
if(file_exists(LOG_SOCKET_PATH)) {
unlink(LOG_SOCKET_PATH);
}
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_bind($socket, LOG_SOCKET_PATH);
chmod(LOG_SOCKET_PATH, 0777);
socket_listen($socket);
socket_set_block($socket);
try {
while(true) {
if(($connection = socket_accept($socket)) !== false) {
while(socket_recv($connection, $buffer, 1024, 0)) {
$callback($buffer);
}
socket_close($connection);
}
}
} catch(Exception $e) {
socket_close($socket);
}
}
function log_send($message) {
$socket = @socket_create(AF_UNIX, SOCK_STREAM, 0);
if(!$socket) {
return false;
}
$success = false;
if(@socket_connect($socket, LOG_SOCKET_PATH) && socket_write($socket, $message)) {
$success = true;
}
socket_close($socket);
return $success;
}
?>
define('LOG_SOCKET_PATH', '/tmp/log_socket');
function log_recieve($callback) {
if(file_exists(LOG_SOCKET_PATH)) {
unlink(LOG_SOCKET_PATH);
}
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_bind($socket, LOG_SOCKET_PATH);
chmod(LOG_SOCKET_PATH, 0777);
socket_listen($socket);
socket_set_block($socket);
try {
while(true) {
if(($connection = socket_accept($socket)) !== false) {
while(socket_recv($connection, $buffer, 1024, 0)) {
$callback($buffer);
}
socket_close($connection);
}
}
} catch(Exception $e) {
socket_close($socket);
}
}
function log_send($message) {
$socket = @socket_create(AF_UNIX, SOCK_STREAM, 0);
if(!$socket) {
return false;
}
$success = false;
if(@socket_connect($socket, LOG_SOCKET_PATH) && socket_write($socket, $message)) {
$success = true;
}
socket_close($socket);
return $success;
}
?>