Changed logging to include client PID

This commit is contained in:
Deon George 2018-12-09 10:13:51 +11:00
parent 0eb71ef7c6
commit 86e29a3cee
2 changed files with 28 additions and 18 deletions

View File

@ -14,6 +14,7 @@ use App\Models\Mode;
abstract class Server { abstract class Server {
private $mo = NULL; // Our Mode object 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) public function __construct(Mode $o)
{ {
@ -35,6 +36,11 @@ abstract class Server {
define('MSG_TIMEWARP', WHITE.'OTHER VERSIONS EXIST'.GREEN.'KEY *02 TO VIEW'); 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 * Connection handler
* *
@ -43,8 +49,6 @@ abstract class Server {
* @throws SocketException * @throws SocketException
*/ */
function onConnect(SocketClient $client) { function onConnect(SocketClient $client) {
Log::info('Connection from: ',['client'=>$client->getAddress()]);
$pid = pcntl_fork(); $pid = pcntl_fork();
if ($pid == -1) if ($pid == -1)
@ -54,7 +58,8 @@ abstract class Server {
elseif ($pid) elseif ($pid)
return; return;
$pid = getmypid(); $this->pid = getmypid();
$this->log('info','Connection from: ',['client'=>$client->getAddress(),'server'=>$this->mo->name]);
// We are now the child. // We are now the child.
try { try {
@ -108,7 +113,7 @@ abstract class Server {
// Client initiation input // Client initiation input
// TELNET http://pcmicro.com/netfoss/telnet.html // TELNET http://pcmicro.com/netfoss/telnet.html
if ($read == TCP_IAC OR $session_init OR $session_option) { 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) { switch ($read) {
// Command being sent. // Command being sent.
@ -125,7 +130,7 @@ abstract class Server {
case TCP_SE: case TCP_SE:
$session_option = $session_init = FALSE; $session_option = $session_init = FALSE;
Log::debug('Session Terminal: ' . $session_term); $this->log('debug',sprintf('Session Terminal: %s',$session_term));
break; break;
@ -152,7 +157,8 @@ abstract class Server {
$session_note .= 'ECHO'; $session_note .= 'ECHO';
$session_init = FALSE; $session_init = FALSE;
$read = ''; $read = '';
Log::debug($session_note);
$this->log('debug',sprintf('Session Note: %s',$session_note));
continue; continue;
@ -160,7 +166,8 @@ abstract class Server {
$session_note .= 'SUPPRESS GO AHEAD'; $session_note .= 'SUPPRESS GO AHEAD';
$session_init = FALSE; $session_init = FALSE;
$read = ''; $read = '';
Log::debug($session_note);
$this->log('debug',sprintf('Session Note: %s',$session_note));
continue; continue;
@ -168,7 +175,8 @@ abstract class Server {
$session_note .= 'WINDOWSIZE'; $session_note .= 'WINDOWSIZE';
$session_init = FALSE; $session_init = FALSE;
$read = ''; $read = '';
Log::debug($session_note);
$this->log('debug',sprintf('Session Note: %s',$session_note));
continue; continue;
@ -178,7 +186,7 @@ abstract class Server {
$read = ''; $read = '';
} else { } 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. // Not doing anything in particular.
case MODE_COMPLETE: case MODE_COMPLETE:
case FALSE: case FALSE:
Log::debug('Idle', ['pid' => $pid]); $this->log('debug','Idle');
$cmd = ''; $cmd = '';
switch ($read) { switch ($read) {
@ -547,7 +555,7 @@ abstract class Server {
break; break;
default: 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(); $page = $history->last();
Log::debug('Backing up to: '.$page['frame'].$page['index']); $this->log('debug','Backing up to:',$page);
// Go to next index frame. // Go to next index frame.
case ACTION_NEXT: case ACTION_NEXT:
@ -601,6 +609,8 @@ abstract class Server {
? $this->mo->frame(FrameModel::findOrFail($timewarpalt)) ? $this->mo->frame(FrameModel::findOrFail($timewarpalt))
: $this->mo->frameLoad($page['frame'],$page['index'],$this); : $this->mo->frameLoad($page['frame'],$page['index'],$this);
$this->log('debug',sprintf('Fetched frame: %s',$fo->id()));
} catch (ModelNotFoundException $e) { } catch (ModelNotFoundException $e) {
$this->sendBaseline($client,ERR_PAGE); $this->sendBaseline($client,ERR_PAGE);
$mode = $action = FALSE; $mode = $action = FALSE;
@ -766,9 +776,9 @@ abstract class Server {
// Did the client disconnect // Did the client disconnect
if ($read === NULL || socket_last_error()) { if ($read === NULL || socket_last_error()) {
$client->close(); $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(); $client->close();
Log::debug('Disconnected: '.$client->getaddress()); $this->log('debug',sprintf('Disconnected: %s',$client->getaddress()));
} }
/** /**

View File

@ -71,9 +71,9 @@ class Server extends Command
} catch (SocketException $e) { } catch (SocketException $e) {
if ($e->getMessage() == 'Can\'t accept connections: "Success"') if ($e->getMessage() == 'Can\'t accept connections: "Success"')
Log::debug('Server Terminated.'); Log::debug(sprintf('Server Terminated: %s',$mo->name));
else else
Log::emergency('Uncaught Message: '.$e->getMessage()); Log::emergency('Uncaught Message: '.$e->getMessage(),['name'=>$mo->name]);
} }
} }
} }