forked from hanslub42/rlwrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger
executable file
·59 lines (44 loc) · 1.63 KB
/
logger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env perl
use lib ($ENV{RLWRAP_FILTERDIR} or ".");
use RlwrapFilter;
use strict;
use Getopt::Std;
our ($opt_l);
getopts("l");
my $logfile = $ARGV[0] || "/tmp/filterlog.$$";
open LOG, ">$logfile" or die "Couldn't write to $logfile: $!\n";
my $oldfh = select(LOG); $| = 1; select($oldfh); # flush LOG after each write
my $filter = new RlwrapFilter(message_handler => \&logit);
$filter -> help_text(
"Usage: rlwrap -z 'logger [-l] logfile' <command>\n" .
"log messages to a file (for debugging)\n".
"give logfile name as an argument, -l for long format\n" .
"useful in a pipeline (rlwrap -z 'pipeline logger in:filter:logger out')");
$filter -> run;
# a message_handler is seldom used (as it cannot change messages, only examine them)
# It gets called with the tag as its second argument
sub logit {
my ($message, $tag) = @_;
my $tagname = $filter -> tag2name($tag);
$tagname =~ s/^TAG_//;
my $mangled = $message;
$mangled =~ s/\n/\\n/g; # make unprintable characters printable
$mangled =~ s/\r/\\r/g;
$mangled =~ s/\t/\\t/g;
$mangled =~ s/\x1b/\\e/g;
$mangled =~ s/([[:cntrl:]])/sprintf("\\x%02x", unpack("C", $1))/ge;
format LOG =
@<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$tagname, $mangled
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$mangled
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$mangled
.
if ($opt_l) {
print LOG "$tagname $mangled\n";
} else {
write LOG;
}
return $message;
}