Debugging with zero cost in production

php.internals

David Rodrigues

9 years ago
Hello, folks. I'm thinking about a debug function to allow us to create "zero cost declarations" in production code, as assert() does. From what I understand, if I have something like assert(method()), method() will not be called in production, only in development. This behaviour is great because we can help the PHPUnit, for instance, to capture messages in specific places to check if some process have generated the correct value without test it alone. For instance: // On test()'ed method: debug(TestStack::put($varValue)); // On test method: test(); static::assertSame(123, TestStack::pop()); How it should be a debug code, should be important that it not be generated and executed in production. Could I use assert() to do that, in all case? Then I could consider it as "already implemented". Bye!
-- David Rodrigues

Fleshgrinder

9 years ago
On 10/24/2016 7:10 PM, David Rodrigues wrote:
> Hello, folks. > > I'm thinking about a debug function to allow us to create "zero cost > declarations" in production code, as assert() does. > > From what I understand, if I have something like assert(method()), > method() will not be called in production, only in development. > > This behaviour is great because we can help the PHPUnit, for instance, > to capture messages in specific places to check if some process have > generated the correct value without test it alone. > > For instance: > > // On test()'ed method: > debug(TestStack::put($varValue)); > > // On test method: > test(); > static::assertSame(123, TestStack::pop()); > > How it should be a debug code, should be important that it not be > generated and executed in production. > > Could I use assert() to do that, in all case? Then I could consider it > as "already implemented". > > Bye! >
You can already use assert in exactly this manner.
-- Richard "Fleshgrinder" Fussenegger

Niklas Keller

9 years ago
2016-10-24 19:12 GMT+02:00 Fleshgrinder <php@fleshgrinder.com>:
> On 10/24/2016 7:10 PM, David Rodrigues wrote: > > Hello, folks. > > > > I'm thinking about a debug function to allow us to create "zero cost > > declarations" in production code, as assert() does. > > > > From what I understand, if I have something like assert(method()), > > method() will not be called in production, only in development. > > > > This behaviour is great because we can help the PHPUnit, for instance, > > to capture messages in specific places to check if some process have > > generated the correct value without test it alone. > > > > For instance: > > > > // On test()'ed method: > > debug(TestStack::put($varValue)); > > > > // On test method: > > test(); > > static::assertSame(123, TestStack::pop()); > > > > How it should be a debug code, should be important that it not be > > generated and executed in production. > > > > Could I use assert() to do that, in all case? Then I could consider it > > as "already implemented". > > > > Bye! > > > > You can already use assert in exactly this manner. > > -- > Richard "Fleshgrinder" Fussenegger > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > You can use assert as long as you return a value that evaluates to true.
We use it in Aerys to have zero-cost debugging output: https://github.com/amphp/aerys/blob/6335cc224e54999185119a28684804e91be458ae/lib/Server.php#L169 However, I think it would be nice to have a separate statement / function that doesn't have that requirement. Then we could just use `debug($this->logger->debug(...))` instead of `assert($this->logDebug(...))` or `assert($this->logger->debug(...) || true);`. Regards, Niklas