Skip to content

Instantly share code, notes, and snippets.

@avtehnik
Last active February 7, 2020 11:08
Show Gist options
  • Save avtehnik/20bc2e437be18825186484a18f04e034 to your computer and use it in GitHub Desktop.
Save avtehnik/20bc2e437be18825186484a18f04e034 to your computer and use it in GitHub Desktop.
php udp debugger
<?php
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, "0.0.0.0", 8001);
$id = null;
while (true){
$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 65535, 0, $from, $port);
$data = json_decode($buf);
if($data->id!=$id){
echo PHP_EOL.PHP_EOL;
echo "\33[1;31m".date('Y-m-d H:i:s',$data->id)." $from $port \33[m".PHP_EOL;
}
echo "\33[1;32m",$data->caller."\33[m".PHP_EOL;
echo json_encode($data->data).PHP_EOL.PHP_EOL;
$id = $data->id;
}
<?php
class UdpDebugger
{
private static $sock;
private static $id;
public static function Debug()
{
$bt = debug_backtrace();
$caller = array_shift($bt);
if (!self::$sock) {
self::$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
self::$id = time();
}
$msg = json_encode([
'id' => self::$id,
'data' => func_get_args(),
'caller' =>$caller['file'].':'.$caller['line'],
'type'=>'debug'
]);
$len = strlen($msg);
if ($len < 6555) {
socket_sendto(self::$sock, $msg, $len, 0, '127.0.0.1', 8001);
}
}
}
UdpDebugger::Debug("something");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment