Better logging

This commit is contained in:
Daniil Gentili 2019-09-12 18:56:26 +02:00
parent edfd387743
commit 0db1196afb
10 changed files with 97 additions and 13 deletions

View File

@ -108,7 +108,7 @@ class DataCenterConnection implements JsonSerializable
private $decWrite = 10; private $decWrite = 10;
/** /**
* Backed up messages * Backed up messages.
* *
* @var array * @var array
*/ */
@ -389,13 +389,19 @@ class DataCenterConnection implements JsonSerializable
$this->robinLoop->signal(true); $this->robinLoop->signal(true);
$this->robinLoop = null; $this->robinLoop = null;
} }
$before = count($this->backup); $before = \count($this->backup);
$list = '';
foreach ($this->connections as $connection) { foreach ($this->connections as $connection) {
$this->backup = \array_merge($this->backup, $connection->backupSession()); $backup = $connection->backupSession();
foreach ($backup as $message) {
$list .= $message['_'] ?? '-';
$list .= ', ';
}
$this->backup = \array_merge($this->backup, $backup);
$connection->disconnect(); $connection->disconnect();
} }
$count = count($this->backup) - $before; $count = \count($this->backup) - $before;
$this->API->logger->logger("Backed up $count messages from DC {$this->datacenter}"); $this->API->logger->logger("Backed up $count (added {$list}to $before existing messages) from DC {$this->datacenter}");
$this->connections = []; $this->connections = [];
$this->availableConnections = []; $this->availableConnections = [];
@ -414,7 +420,7 @@ class DataCenterConnection implements JsonSerializable
} }
/** /**
* Restore backed up messages * Restore backed up messages.
* *
* @return void * @return void
*/ */
@ -422,7 +428,7 @@ class DataCenterConnection implements JsonSerializable
{ {
$backup = $this->backup; $backup = $this->backup;
$this->backup = []; $this->backup = [];
$count = count($backup); $count = \count($backup);
$this->API->logger->logger("Restoring $count messages to DC {$this->datacenter}"); $this->API->logger->logger("Restoring $count messages to DC {$this->datacenter}");
foreach ($backup as $message) { foreach ($backup as $message) {
Tools::callFork($this->getConnection()->sendMessage($message, false)); Tools::callFork($this->getConnection()->sendMessage($message, false));

View File

@ -153,7 +153,7 @@ class ReadLoop extends SignalLoop
$API->logger->logger($e->getReason()); $API->logger->logger($e->getReason());
if (\strpos($e->getReason(), ' ') === 0) { if (\strpos($e->getReason(), ' ') === 0) {
$payload = -\substr($e->getReason(), 7); $payload = -\substr($e->getReason(), 7);
$API->logger->logger("Received $payload from DC ".$datacenter, \danog\MadelineProto\Logger::ULTRA_VERBOSE); $API->logger->logger("Received $payload from DC ".$datacenter, \danog\MadelineProto\Logger::ERROR);
return $payload; return $payload;
} }

View File

@ -78,4 +78,18 @@ class PermAuthKey extends AuthKey
'authorized' => $this->authorized 'authorized' => $this->authorized
]; ];
} }
/**
* Sleep function.
*
* @return array
*/
public function __sleep()
{
return [
'authKey',
'id',
'serverSalt',
'authorized'
];
}
} }

View File

@ -170,4 +170,21 @@ class TempAuthKey extends AuthKey implements JsonSerializable
'connection_inited' => $this->inited 'connection_inited' => $this->inited
]; ];
} }
/**
* Sleep function
*
* @return array
*/
public function __sleep()
{
return [
'authKey',
'id',
'serverSalt',
'bound',
'expires',
'inited'
];
}
} }

View File

@ -62,7 +62,7 @@ trait CallHandler
$this->ack_outgoing_message_id($message_id); $this->ack_outgoing_message_id($message_id);
$this->got_response_for_outgoing_message_id($message_id); $this->got_response_for_outgoing_message_id($message_id);
} else { } else {
$this->logger->logger('Could not resend '.isset($this->outgoing_messages[$message_id]['_']) ? $this->outgoing_messages[$message_id]['_'] : $message_id); $this->logger->logger('Could not resend '.(isset($this->outgoing_messages[$message_id]['_']) ? $this->outgoing_messages[$message_id]['_'] : $message_id));
} }
} }
if (!$postpone) { if (!$postpone) {

View File

@ -70,13 +70,13 @@ abstract class Session
} }
/** /**
* Backup eventual unsent messages before session deletion * Backup eventual unsent messages before session deletion.
* *
* @return array * @return array
*/ */
public function backupSession(): array public function backupSession(): array
{ {
$pending = array_values($this->pending_outgoing); $pending = \array_values($this->pending_outgoing);
foreach ($this->new_outgoing as $id) { foreach ($this->new_outgoing as $id) {
$pending[] = $this->outgoing_messages[$id]; $pending[] = $this->outgoing_messages[$id];
} }

View File

@ -636,6 +636,7 @@ trait AuthKeyHandler
public function init_authorization_async(): \Generator public function init_authorization_async(): \Generator
{ {
if ($this->pending_auth) { if ($this->pending_auth) {
$this->logger("Pending auth, not initing auth");
return; return;
} }
$initing = $this->initing_authorization; $initing = $this->initing_authorization;

View File

@ -25,7 +25,6 @@ use Amp\DoH\Rfc8484StubResolver;
use Amp\Loop; use Amp\Loop;
use Amp\Loop\Driver; use Amp\Loop\Driver;
use ReflectionClass; use ReflectionClass;
use ReflectionObject;
use function Amp\ByteStream\getInputBufferStream; use function Amp\ByteStream\getInputBufferStream;
use function Amp\ByteStream\getStdin; use function Amp\ByteStream\getStdin;
@ -244,7 +243,7 @@ class Magic
$reflectionClass = new ReflectionClass(Driver::class); $reflectionClass = new ReflectionClass(Driver::class);
$reflectionProperty = $reflectionClass->getProperty('watchers'); $reflectionProperty = $reflectionClass->getProperty('watchers');
$reflectionProperty->setAccessible(true); $reflectionProperty->setAccessible(true);
foreach (array_keys($reflectionProperty->getValue($driver)) as $key) { foreach (\array_keys($reflectionProperty->getValue($driver)) as $key) {
$driver->unreference($key); $driver->unreference($key);
} }
} }

View File

@ -19,6 +19,8 @@
namespace danog\MadelineProto\TL; namespace danog\MadelineProto\TL;
use danog\MadelineProto\Tools;
trait PrettyException trait PrettyException
{ {
public $tl_trace; public $tl_trace;

View File

@ -25,6 +25,8 @@ use Amp\Loop;
use Amp\Promise; use Amp\Promise;
use Amp\Success; use Amp\Success;
use phpseclib\Math\BigInteger; use phpseclib\Math\BigInteger;
use ReflectionGenerator;
use function Amp\ByteStream\getOutputBufferStream; use function Amp\ByteStream\getOutputBufferStream;
use function Amp\ByteStream\getStdin; use function Amp\ByteStream\getStdin;
use function Amp\ByteStream\getStdout; use function Amp\ByteStream\getStdout;
@ -447,4 +449,47 @@ trait Tools
$var instanceof Traversable && $var instanceof Traversable &&
$var instanceof Countable); $var instanceof Countable);
} }
/**
* Custom backtrace for working with generators
*
* @param boolean $ignoreArgs Whether to ignore method arguments
* @param array $trace Trace to work with
*
* @return array
*/
public static function backtrace(bool $ignoreArgs = false, array $trace = []): array
{
return iterator_to_array(self::backtraceGenerator($ignoreArgs, $trace));
}
/**
* Custom backtrace for working with generators
*
* @param boolean $ignoreArgs Whether to ignore method arguments
* @param array $trace Trace to work with
*
* @return \Generator
*/
public static function backtraceGenerator(bool $ignoreArgs = false, array $trace = []): \Generator
{
$flags = DEBUG_BACKTRACE_PROVIDE_OBJECT;
if ($ignoreArgs) {
$flags |= DEBUG_BACKTRACE_IGNORE_ARGS;
}
if (!$trace) {
$trace = debug_backtrace($flags);
array_shift($trace);
}
foreach ($trace as $frame) {
if (isset($frame['object']) && $frame['object'] instanceof \Generator) {
$reflection = new ReflectionGenerator($frame['object']);
yield from self::backtrace($ignoreArgs, $reflection->getTrace($flags));
return;
}
//var_dump(get_class($frame['object'] ?? new class {}));
unset($frame['object']);
yield $frame;
}
}
} }