Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Color die and warn messages

by Anonymous Monk
on Feb 15, 2024 at 23:50 UTC ( [id://11157720]=CUFP: print w/replies, xml ) Need Help??

This scratched an itch for me, no guarantees.
use warnings::colored; warn "warning"; # yellow system "non-existant-command"; # red say "test"; # none eval { die "caught" }; # none say $@; # none die "died"; # red
And the implementation:
package warnings::colored; use strict; use warnings; use Term::ANSIColor; # Color die and warn messages. # NOTE: Doesn' play well with Carp::Always because it replaces our die + and # warn sighandlers. $ENV{NO_COLOR} = 1 if ! exists $ENV{NO_COLOR} and (! -t STDIN or ! -t STDERR); BEGIN { my $died; *CORE::GLOBAL::die = sub { $died = 1, print STDERR color('bold red') if @_ and ! $^S; END { $died && print STDERR color('reset') } $] >= 5.016 && goto &CORE::die; CORE::die @_; }; *CORE::GLOBAL::warn = sub { unshift @_, color('bold yellow'); # $SIG{__WARN__} will print the reset sequence. $] >= 5.016 && goto &CORE::warn; CORE::warn @_; }; } # Unintentional warning messages should display with high severity. # E.g.`system 'nonexistent-command'`. $SIG{__WARN__} = sub { my $msg = shift; if ($] < 5.016 and __FILE__ eq (caller)[1]) { my ($file, $line) = (caller(1))[1, 2]; $msg =~ s/( at .*? line \d+\.$)/ at $file line $line/m; } CORE::warn color('bold red'), $msg; print STDERR color('reset'); }; $SIG{__DIE__} = sub { my $msg = shift; if (__FILE__ eq (caller)[1]) { my ($file, $line) = (caller(1))[1, 2]; $msg =~ s/( at .*? line \d+\.$)/ at $file line $line/m; die $msg; } } if $] < 5.016 ; 1;

Replies are listed 'Best First'.
Re: Color die and warn messages
by dbuckhal (Chaplain) on Mar 02, 2024 at 06:28 UTC

    Hey, just silly thought I had regarding your "NOTE"

    # NOTE: Doesn' play well with Carp::Always because it replaces our die

    Since you are colorizing, instead of "die", use "dye" to prevent clobbering.

    Otherwise, cool concept!

    ... I really hope Anonymous Monk gets this response ...
      Your quote is incomplete: "it replaces our die and warn sighandlers". The point of the code is to be drop-in and not require any other changes in your code. In fact, it would be better to call it in an eval so if it's not installed, it doesn't break anything: BEGIN { eval 'use warnings::colored' };.

      Changing die to dye would require you to change any code that uses it, and also it wouldn't then work on anything that internally called die or warn, like the example: system 'nonexistent-command'.

      If the %SIG entries could be tied variables, then it might be possible to make this play nice with Carp::Always. I haven't looked into that because it works fine enough as it is for me. That's why I released it anonymously and not to CPAN. If somebody wants to improve on it and deal with the edge cases, feel free to release it.

Re: Color die and warn messages
by Anonymous Monk on Mar 05, 2025 at 01:05 UTC
    i tried using it and noticed occasionally when there is much output, the reset isn't set properly, so all remaining output is colored, even if not from warn/die. sorry, i couldn't figure out a minimal test case to demonstrate.
      It's probably because something is messing with STDERR and putting it into line-buffered mode. I've seen some modules do that. This could probably be fixed using IO::Handle::printflush to print the terminal reset sequence.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://11157720]
Approved by Discipulus
Front-paged by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2025-06-16 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.