preaction / Log-Any

Simple, fast Perl logging API compatible with any logging system
Other
13 stars 19 forks source link

binmode attribute not work #72

Closed MadLord80 closed 6 years ago

MadLord80 commented 6 years ago

In Log::Any::Adapter::File package very strange definition: my $binmode ||= ':utf8'; This dont work if we use (example): Log::Any::Adapter->set('File', $file, log_level => 'trace', binmode => ':raw'); Binmode remainds default (utf8).

My suggestion:

- my $binmode ||= ':utf8';
- $binmode = ":$binmode" unless substr($binmode,0,1) eq ':';
+ my $binmode = (substr($self->{binmode},0,1) ne ':') ? ':utf8' : $self->{binmode};
preaction commented 6 years ago

Thanks for the report! An easier solution would be to my $binmode = $self->{binmode} || ':utf8'; which I think is what the code was supposed to be. I've added the fix and released v1.705 to CPAN.

MadLord80 commented 6 years ago

No, this is wrong

my $binmode = $self->{binmode} || ':utf8';
$binmode = ":$binmode" unless substr($binmode,0,1) eq ':';

If i set Log::Any::Adapter->set('File', $file, binmode => 'raw'); (binmode without ':'), then binmode set incorrectly

my $binmode = $self->{binmode} || ':utf8'; # $binmode = 'raw'
$binmode = ":$binmode" unless substr($binmode,0,1) eq ':'; # condition not work, $binmode still 'raw', without ':'
preaction commented 6 years ago

Why does the condition not work?

$ perl -E'$binmode = "raw"; say substr( $binmode, 0, 1 );'
r
$ perl -E'$binmode = ":raw"; say substr( $binmode, 0, 1 );'
:
$ perl -E'$binmode = "raw"; $binmode = ":$binmode" unless substr( $binmode, 0, 1 ) eq ":"; say $binmode'
:raw
$ perl -E'$binmode = ":raw"; $binmode = ":$binmode" unless substr( $binmode, 0, 1 ) eq ":"; say $binmode'
:raw

However, notice what happens if I do not use parens:

$ perl -E'$binmode = "raw"; $binmode = ":$binmode" unless substr $binmode, 0, 1 eq ":"; say $binmode'
:raw
$ perl -E'$binmode = ":raw"; $binmode = ":$binmode" unless substr $binmode, 0, 1 eq ":"; say $binmode'
::raw

That doubles the colon, but it's still adding a colon. In none of these cases is a colon not added to $binmode.

You're going to have to give me a failing test case that I can reproduce on my machine.

MadLord80 commented 6 years ago

ok, sorry...i was wrong )))...