public function errorHandler($errno, $errstr, $errfile, $errline, array $errcontext) { $error = compact("errno", "errstr", "errfile", "errline"); if ((int) $errno & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_WARNING)) { $this->trigger("error", $error); } log::error(var_export($error, 1)); }
protected function onInStreamClose() { $this->inStream->off("close", [$this, "onInStreamClose"]); // If stream is closed, but no STOP event received if ($this->isProcOpen()) { $pid = $this->getPID(); $sig = $this->getTermSig(); $error = "Unexpected child death ({$pid}): " . ($sig ? "signaled {$sig}" : "unknown reason"); log::error($error); $this->trigger("error", $error); $this->trigger("stop"); } }
public static function send($fifo, $message) { $message = serialize($message); $messageLen = hex2bin(str_pad(dechex(strlen($message)), self::MESSAGE_LEN_FIELD_LENGTH_BYTES * 2, "0", STR_PAD_LEFT)); $writeStream = fopen($fifo, "c+"); if (!$writeStream) { throw new \Exception("Could not open fifo {$fifo} for writing: " . var_export(error_get_last(), 1)); } if (!fwrite($writeStream, $messageLen . $message)) { log::warning("Could not write to fifo {$fifo}"); throw new \Exception("Could not write to fifo {$fifo}"); } else { log::debug("Written message to {$fifo}"); } fclose($writeStream); }
/** * Send message to all pipes in directory, optionally using StreamSet * * @param string $fifoPath Path to directory with pipes * @param mixed $message Message should be castable to string * @param mixed $useStreamSet If \Phasty\Stream\StreamSet use it for streams \ * If false no need using streams \ * If null use default StreamSet instance */ public static function send($fifoPath, $message, $useStreamSet = false) { $fifos = glob("{$fifoPath}/*"); $message = (string) $message; log::debug("Mx1 send to " . count($fifos) . " fifos"); foreach ($fifos as $fifo) { if (!isset(self::$streams[$fifo])) { self::$streams[$fifo] = new \Phasty\Stream\Reader\NamedPipe($fifo); if ($useStreamSet !== false) { if (is_null($useStreamSet)) { self::$streams[$fifo]->setStreamSet(\Phasty\Stream\StreamSet::instance()); } else { self::$streams[$fifo]->setStreamSet($useStreamSet); } } self::$streams[$fifo]->on("close", function () use($fifo) { unset(self::$streams[$fifo]); }); } self::$streams[$fifo]->write($message); } }
protected function log($msg) { $error = false; if ($msg instanceof \Exception) { $msg = sprintf("Exception (%d) %s in %s:%d", $msg->getCode(), $msg->getMessage(), $msg->getFile(), $msg->getLine()); $error = true; } if (!$this->canLog) { echo "{$msg}\n"; return; } if ($this->verbose) { echo "{$msg}\n"; } $error ? log::error($msg) : log::info($msg); }
public function onData(Event $event) { $this->messages .= $data = $event->getData(); $i = 0; while (false !== ($msg = $this->extractMessage())) { $i++; $eventObject = unserialize($msg); foreach ($this->listeners as $listener) { $listener->trigger($eventObject); } } if ($this->messages && !$i) { $msg = $this->messages; if (strlen($msg) < 100) { $msg = "0x" . bin2hex($msg); } log::error("Got broken message:\n{$msg}"); log::error("last data: " . var_export($data, 1)); } }
#!/usr/bin/php <?php $dir = dirname(dirname(__FILE__)); if (is_file("{$dir}/vendor/autoload.php")) { $autoloadFile = "{$dir}/vendor/autoload.php"; } else { $autoloadFile = dirname(dirname($dir)) . "/autoload.php"; } include_once $autoloadFile; if ($argc < 4) { echo "Wrong argument count"; exit(1); } $requiredFiles = unserialize(base64_decode($argv[1])); foreach ($requiredFiles as $requiredFile) { require $requiredFile; } $calleeEntity = unserialize(base64_decode($argv[2])); $calleeMethod = unserialize(base64_decode($argv[3])); $arguments = $argc > 4 ? unserialize(base64_decode($argv[4])) : []; if (is_string($calleeEntity)) { $calleeEntity = new $calleeEntity(); } $child = new \Phasty\Process\Child($calleeEntity); try { call_user_func_array([$calleeEntity, $calleeMethod], $arguments); } catch (\Exception $e) { $child->trigger("error", $e->getMessage()); \Phasty\Log\File::error($e->getMessage()); }
public function write($data) { log::debug("Writing data to {$this->fifoFile}"); parent::write($data); }
protected function logByType($msg, $type) { if (!$this->canLog) { echo "{$msg}\n"; return; } if ($this->verbose) { echo "{$msg}\n"; } switch ($type) { case 'error': log::error($msg); break; case 'debug': log::debug($msg); break; default: log::info($msg); } }