From 86e29a3cee9cd47d7cb0ec3db9b25c42833e20ab Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 9 Dec 2018 10:13:51 +1100 Subject: [PATCH] Changed logging to include client PID --- app/Classes/Server.php | 42 ++++++++++++++++++++------------- app/Console/Commands/Server.php | 4 ++-- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/app/Classes/Server.php b/app/Classes/Server.php index f100d64..d2081cc 100644 --- a/app/Classes/Server.php +++ b/app/Classes/Server.php @@ -13,7 +13,8 @@ use App\Models\Mode; abstract class Server { private $mo = NULL; // Our Mode object - protected $blp = 0; // Size of Bottom Line Pollution + protected $blp = 0; // Size of Bottom Line Pollution + protected $pid = NULL; // Client PID public function __construct(Mode $o) { @@ -35,6 +36,11 @@ abstract class Server { define('MSG_TIMEWARP', WHITE.'OTHER VERSIONS EXIST'.GREEN.'KEY *02 TO VIEW'); } + public function log(string $mode,string $message,array $data=[]) + { + Log::$mode(sprintf('%s: %s',$this->pid,$message),$data); + } + /** * Connection handler * @@ -43,8 +49,6 @@ abstract class Server { * @throws SocketException */ function onConnect(SocketClient $client) { - Log::info('Connection from: ',['client'=>$client->getAddress()]); - $pid = pcntl_fork(); if ($pid == -1) @@ -54,7 +58,8 @@ abstract class Server { elseif ($pid) return; - $pid = getmypid(); + $this->pid = getmypid(); + $this->log('info','Connection from: ',['client'=>$client->getAddress(),'server'=>$this->mo->name]); // We are now the child. try { @@ -108,7 +113,7 @@ abstract class Server { // Client initiation input // TELNET http://pcmicro.com/netfoss/telnet.html if ($read == TCP_IAC OR $session_init OR $session_option) { - Log::debug(sprintf('Session Char (%s)', ord($read)), ['init' => $session_init, 'option' => $session_option]); + $this->log('debug',sprintf('Session Char (%s)',ord($read)),['init'=>$session_init,'option'=>$session_option]); switch ($read) { // Command being sent. @@ -125,7 +130,7 @@ abstract class Server { case TCP_SE: $session_option = $session_init = FALSE; - Log::debug('Session Terminal: ' . $session_term); + $this->log('debug',sprintf('Session Terminal: %s',$session_term)); break; @@ -152,7 +157,8 @@ abstract class Server { $session_note .= 'ECHO'; $session_init = FALSE; $read = ''; - Log::debug($session_note); + + $this->log('debug',sprintf('Session Note: %s',$session_note)); continue; @@ -160,7 +166,8 @@ abstract class Server { $session_note .= 'SUPPRESS GO AHEAD'; $session_init = FALSE; $read = ''; - Log::debug($session_note); + + $this->log('debug',sprintf('Session Note: %s',$session_note)); continue; @@ -168,7 +175,8 @@ abstract class Server { $session_note .= 'WINDOWSIZE'; $session_init = FALSE; $read = ''; - Log::debug($session_note); + + $this->log('debug',sprintf('Session Note: %s',$session_note)); continue; @@ -178,7 +186,7 @@ abstract class Server { $read = ''; } else { - Log::debug(sprintf('Unhandled char in session_init :%s (%s)', $read, ord($read))); + $this->log('debug',sprintf('Unhandled char in session_init: %s (%s)',$read,ord($read))); } } } @@ -416,7 +424,7 @@ abstract class Server { // Not doing anything in particular. case MODE_COMPLETE: case FALSE: - Log::debug('Idle', ['pid' => $pid]); + $this->log('debug','Idle'); $cmd = ''; switch ($read) { @@ -547,7 +555,7 @@ abstract class Server { break; default: - Log::debug('Not sure what we were doing?'); + $this->log('debug','Not sure what we were doing?'); } } @@ -582,7 +590,7 @@ abstract class Server { $page = $history->last(); - Log::debug('Backing up to: '.$page['frame'].$page['index']); + $this->log('debug','Backing up to:',$page); // Go to next index frame. case ACTION_NEXT: @@ -601,6 +609,8 @@ abstract class Server { ? $this->mo->frame(FrameModel::findOrFail($timewarpalt)) : $this->mo->frameLoad($page['frame'],$page['index'],$this); + $this->log('debug',sprintf('Fetched frame: %s',$fo->id())); + } catch (ModelNotFoundException $e) { $this->sendBaseline($client,ERR_PAGE); $mode = $action = FALSE; @@ -766,9 +776,9 @@ abstract class Server { // Did the client disconnect if ($read === NULL || socket_last_error()) { $client->close(); - Log::debug('Client Disconnected: '.$client->getaddress()); + $this->log('debug',sprintf('Client Disconnected: %s',$client->getaddress())); - return FALSE; + return; } } @@ -778,7 +788,7 @@ abstract class Server { } $client->close(); - Log::debug('Disconnected: '.$client->getaddress()); + $this->log('debug',sprintf('Disconnected: %s',$client->getaddress())); } /** diff --git a/app/Console/Commands/Server.php b/app/Console/Commands/Server.php index 9d265a3..da6a1bc 100644 --- a/app/Console/Commands/Server.php +++ b/app/Console/Commands/Server.php @@ -71,9 +71,9 @@ class Server extends Command } catch (SocketException $e) { if ($e->getMessage() == 'Can\'t accept connections: "Success"') - Log::debug('Server Terminated.'); + Log::debug(sprintf('Server Terminated: %s',$mo->name)); else - Log::emergency('Uncaught Message: '.$e->getMessage()); + Log::emergency('Uncaught Message: '.$e->getMessage(),['name'=>$mo->name]); } } } \ No newline at end of file