Skip to content

Commit

Permalink
use simple output instead of STDOUT in debug helper, fix #809
Browse files Browse the repository at this point in the history
  • Loading branch information
ramunasd committed Aug 24, 2020
1 parent a057b9c commit d9ed0a6
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions PhpAmqpLib/Helper/DebugHelper.php
@@ -1,4 +1,5 @@
<?php

namespace PhpAmqpLib\Helper;

use PhpAmqpLib\Wire\Constants;
Expand All @@ -25,19 +26,20 @@ class DebugHelper
*/
public function __construct(Constants $constants)
{
if(!defined('STDOUT')) {
define('STDOUT', fopen('php://stdout', 'w'));
}

$this->debug = defined('AMQP_DEBUG') ? AMQP_DEBUG : false;
$this->debug_output = defined('AMQP_DEBUG_OUTPUT') ? AMQP_DEBUG_OUTPUT : STDOUT;
if (defined('AMQP_DEBUG_OUTPUT')) {
$this->debug_output = AMQP_DEBUG_OUTPUT;
} else {
$this->debug_output = fopen('php://output', 'wb');
}
$this->constants = $constants;
}

/**
* @param string $msg
*/
public function debug_msg($msg) {
public function debug_msg($msg)
{
if ($this->debug) {
$this->print_msg($msg);
}
Expand All @@ -46,27 +48,32 @@ public function debug_msg($msg) {
/**
* @param array $allowed_methods
*/
public function debug_allowed_methods($allowed_methods) {
if ($allowed_methods) {
$msg = 'waiting for ' . implode(', ', $allowed_methods);
} else {
$msg = 'waiting for any method';
public function debug_allowed_methods($allowed_methods)
{
if ($this->debug) {
if ($allowed_methods) {
$msg = 'waiting for ' . implode(', ', $allowed_methods);
} else {
$msg = 'waiting for any method';
}
$this->debug_msg($msg);
}
$this->debug_msg($msg);
}

/**
* @param string $method_sig
*/
public function debug_method_signature1($method_sig) {
public function debug_method_signature1($method_sig)
{
$this->debug_method_signature('< %s:', $method_sig);
}

/**
* @param string $msg
* @param string $method_sig
*/
public function debug_method_signature($msg, $method_sig) {
public function debug_method_signature($msg, $method_sig)
{
if ($this->debug) {
$constants = $this->constants;
$methods = $constants::$GLOBAL_METHOD_NAMES;
Expand All @@ -78,13 +85,16 @@ public function debug_method_signature($msg, $method_sig) {
/**
* @param string $data
*/
public function debug_hexdump($data) {
public function debug_hexdump($data)
{
if ($this->debug) {
$this->debug_msg(sprintf(
'< [hex]: %s%s',
PHP_EOL,
MiscHelper::hexdump($data, $htmloutput = false, $uppercase = true, $return = true)
));
$this->debug_msg(
sprintf(
'< [hex]: %s%s',
PHP_EOL,
MiscHelper::hexdump($data, $htmloutput = false, $uppercase = true, $return = true)
)
);
}
}

Expand All @@ -95,23 +105,27 @@ public function debug_hexdump($data) {
* @param array $mechanisms
* @param array $locales
*/
public function debug_connection_start($version_major, $version_minor, $server_properties, $mechanisms, $locales) {
public function debug_connection_start($version_major, $version_minor, $server_properties, $mechanisms, $locales)
{
if ($this->debug) {
$this->debug_msg(sprintf(
'Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s',
$version_major,
$version_minor,
MiscHelper::dump_table($server_properties),
implode(', ', $mechanisms),
implode(', ', $locales)
));
$this->debug_msg(
sprintf(
'Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s',
$version_major,
$version_minor,
MiscHelper::dump_table($server_properties),
implode(', ', $mechanisms),
implode(', ', $locales)
)
);
}
}

/**
* @param string $s
*/
protected function print_msg($s) {
protected function print_msg($s)
{
fwrite($this->debug_output, $s . PHP_EOL);
}
}

0 comments on commit d9ed0a6

Please sign in to comment.