Potential alternative to File :: Tee?

I wanted to implement some logging for the threaded script that I have, and I came across File :: Tee . However, when an attempt was made to a ppmmodule in a Windows window, it was not found (and according to activation, it is not supported in Windows).

I really liked that you can block access to the file, although by doing something like:

tee STDOUT, {mode => '>>', open => '$ENV{DOM}\\threaded_build.log', lock => 1};
tee STDERR, {mode => '>>', open => '$ENV{DOM}\\threaded_debug.log', lock => 1};

Is there a cross-platform, thread-safe alternative?

+1
source share
1 answer

File::Tee , , system XS-, perlio. , Windows.

IO::Tee -, , . File::Tee :

                    flock($teefh, LOCK_EX) if $target->{lock};
                    print $teefh $cp;
                    flock($teefh, LOCK_UN) if $target->{lock};

IO::Tee, :

use Fcntl ':flock';

no warnings 'redefine';
sub IO::Tee::PRINT
{
    my $self = shift;
    my $ret = 1;
    foreach my $fh (@$self) {
        flock($fh, LOCK_EX);
        undef $ret unless print $fh @_;
        flock($fh, LOCK_UN);
    }
    return $ret;
}
sub IO::Tee::PRINTF
{
    my $self = shift;
    my $fmt = shift;
    my $ret = 1;
    foreach my $fh (@$self) { 
        flock($fh, LOCK_EX);
        undef $ret unless printf $fh $fmt, @_;
        flock($fh, LOCK_UN);
    }
    return $ret;
}
+3

Source: https://habr.com/ru/post/1537935/


All Articles