Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Why can't I write to a custom log from a Catalyst application on SELinux/CentOS

by LanX (Saint)
on Feb 11, 2021 at 17:41 UTC ( [id://11128249]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Why can't I write to a custom log from a Catalyst application on SELinux/CentOS
in thread Why can't I write to a custom log from a Catalyst application on SELinux/CentOS

> Or are they only line-buffered, and the OP forgot the trailing "\n"?

Nope, from testing in the debugger, do new handles automatically autoflush

DB<280> open LOG, ">>", "log.txt" DB<281> ! type log.txt DB<282> print LOG "bla" DB<283> ! type log.txt bla DB<284> DB<284> print LOG "\n" DB<285> ! type log.txt bla DB<286>

probably is Catalyst bending the defaults?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

  • Comment on Re^3: Why can't I write to a custom log from a Catalyst application on SELinux/CentOS
  • Download Code

Replies are listed 'Best First'.
Re^4: Why can't I write to a custom log from a Catalyst application on SELinux/CentOS
by choroba (Cardinal) on Feb 11, 2021 at 18:01 UTC
    Not sure about the debugger, but code shows otherwise:
    #!/usr/bin/perl use strict; use warnings; use feature qw{ say }; open OUT, '>', 'log.txt' or die $!; print OUT "bla\n"; open IN, '<', 'log.txt' or die $!; my $nothing = <IN>; say "Read: $nothing.\n"; close OUT; my $bla = <IN>; say "Read: $bla.\n";
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      so yes, autoflush is not default, but you don't need to close after explicitly setting it

      #!/usr/bin/perl use strict; use warnings; use feature qw{ say }; unlink "./log.txt" or warn "$!"; # in case open OUT, '>>', 'log.txt' or die $!; { my $previous_default = select(OUT); # save previous default $|++; # autoflush OUT select($previous_default); # restore previous default } print OUT "before close"; open IN, '<', 'log.txt' or die $!; my $read; $read = <IN>; chomp $read; say "Before close: <$read>"; close OUT;

      Before close: <before close>

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        even easier is using method autoflush directly.

        Got it from HaukeX' recent post.

        I should have read the FAQ: https://perldoc.perl.org/perlfaq5#How-do-I-flush/unbuffer-an-output-filehandle?-Why-must-I-do-this? more carefully :)

        #!/usr/bin/perl use strict; use warnings; use feature qw{ say }; unlink "./log.txt" or warn "$!"; open OUT, '>>', 'log.txt' or die $!; if (0){ # as you like my $previous_default = select(OUT); # save previous default $|++; # autoflush OUT select($previous_default); # restore previous default } else { #use PerlIO; # needed for Perl <5.14 OUT->autoflush(); } print OUT "before close"; open IN, '<', 'log.txt' or die $!; my $read; $read = <IN>; chomp $read; say "Before close: <$read>"; close OUT;

        -*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Fri Feb 12 02:23:56 C:/Perl_524/bin\perl.exe -w d:/tmp/pm/auto_flush.pl Before close: <before close> Compilation finished at Fri Feb 12 02:23:56

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      > Not sure about the debugger, but code shows otherwise:

      It's not the debugger, I think the system command (aka backticks) is causing a flush

      #!/usr/bin/perl use strict; use warnings; use feature qw{ say }; unlink "./log.txt" or warn "$!"; open OUT, '>>', 'log.txt' or die $!; print OUT "before system\n"; print `type log.txt`; print OUT "after system\n"; open IN, '<', 'log.txt' or die $!; my $nothing = <IN>; say "Before close: $nothing.\n"; close OUT; my $bla = <IN>; say "After close: $bla.\n";

      -*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Thu Feb 11 19:29:04 C:/Perl_524/bin\perl.exe -w d:/tmp/pm/auto_flush.pl before system Before close: before system . After close: after system . Compilation finished at Thu Feb 11 19:29:05

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11128249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-16 20:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found