[sf-lug] Fwd: stderr and stdout, merged in a single file, in different colors
Jesse Zbikowski
embeddedlinuxguy at gmail.com
Sat Sep 27 14:20:56 PDT 2008
Here is a quick 'n' dirty Perl implementation of the named pipes idea.
I named it cl.
For instance
cl cmd args | less -r
# less -r keeps color codes
You may want to timestamp the stderr/stdout to prevent re-ordering.
cl cmd args | sort -g | cut -d' ' -f2- |less -r
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes;
# set to 1 to use timestamps
my $use_timestamp = 0;
sub colorize;
# Uncomment if you want autoflush
#use IO::Handle;
#STDERR->autoflush(1);
#STDOUT->autoflush(1);
my $outpipe = "/tmp/out.$$";
my $errpipe = "/tmp/err.$$";
if (system ('mkfifo', $outpipe) >> 8) {
die $?;
}
if (system ('mkfifo', $errpipe) >> 8) {
die $?;
}
#30 Black
#31 Red
#32 Green
#33 Yellow
#34 Blue
#35 Magenta
#36 Cyan
#37 White
if (fork == 0) {
colorize(32, $errpipe);
exit;
}
if (fork == 0) {
colorize(31, $outpipe);
exit;
}
open STDOUT, ">$outpipe";
open STDERR, ">$errpipe";
my $cmd = shift @ARGV;
system($cmd, @ARGV);
unlink $outpipe;
unlink $errpipe;
sub colorize {
my $color_code = shift;
my $pipe = shift;
open STDIN, "<$pipe";
for my $line (<STDIN>) {
if ($use_timestamp) {
my ($s, $us) = Time::HiRes::gettimeofday;
print $s . $us . " \e[${color_code}m${line}\e[0m";
} else {
print "\e[${color_code}m${line}\e[0m";
}
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cl
Type: application/octet-stream
Size: 1040 bytes
Desc: not available
URL: <http://linuxmafia.com/pipermail/sf-lug/attachments/20080927/16855ed6/attachment.obj>
More information about the sf-lug
mailing list