forked from pflanze/chj-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat_xchatlogs
executable file
·126 lines (109 loc) · 2.96 KB
/
cat_xchatlogs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/perl -w
# Mon Dez 9 16:15:49 MET 2002
# Thu, 9 Jan 2003 02:21:19 +0100 bug fixed. (vergass das Endteil bei fehlend END auch auf zunehmen)
use strict;
$0=~ /([^\/]+)$/s or die "?";
my $myname=$1;
sub usage {
print "$myname [files]
Print the logging entries in the files (or from stdin) reordered after time.
";
exit @_;
}
my @files;
for (my $i=0; $i<=$#ARGV; $i++) {
local $_=$ARGV[$i];
if (/^--?h(elp)?$/) {
usage
} elsif ($_ eq '--') {
push @files, @ARGV[$i+1..$#ARGV];
last;
} elsif (/^-/) {
warn "Unknown option '$_'\n";
usage(1)
} else {
push @files, $_
}
}
use Date::Parse;
sub mydie {
print "\nERROR\n"; # so I'll see it for sure in less
#die $@;
die @_
}
sub mywarn {
print STDERR "WARNING: ",@_;
print "WARNING: ",@_;
}
my %sections; # timestamp->string.
sub readfile ( $ $ $ ) {
my ($in, $qfile, $do_note_file)=@_;
# $qfile is quoted file path or stdin, only for message texts
# $do_note_file is bool, true means output file
local $_;
my ($time_begin,@lines);
while (<$in>) {
if (my ($time)=/^\s*\*\*\*\*.*BEGIN.*AT\s+(.*)/m) {
$time= str2time($time)
or mydie "$myname: timestamp '$time' in file $qfile is unparseable. Bailing out.\n";
##mywarn("Found timestamp: ".localtime($time)."\n");
if ($time_begin) {
mywarn "Another BEGIN ('$_') before END reached - probably xchat unusually terminated\n";
$sections{$time_begin}= join("",@lines);
@lines=();
#undef $time_begin;
}
$time_begin= $time;
push @lines,"File: $qfile\n"
if $do_note_file;
push @lines,$_;
} elsif (($time)=/^\s*\*\*\*\*.*END.*AT\s+(.*)/m) {
my $endtime= str2time($time)
or mydie "$myname: timestamp '$time' in file $qfile is unparseable. Bailing out.\n";
# (für was parse ich den überhaupt??)
#push @lines,$_;
if ($time_begin) {
$sections{$time_begin}= join("",@lines,$_,"\n");
@lines=();
undef $time_begin;
} else {
mywarn "**ERROR** in file $qfile: END without a begin, don't know the time, throwing away text part!\n";
@lines=();
undef $time_begin;
}
} else {
if ($time_begin) {
push @lines,$_;
}
}
}
if (@lines) {
if ($time_begin) {
$sections{$time_begin}= join("",@lines,"\n");
@lines=();
undef $time_begin;
} else {
mywarn "**ERROR (can this happen?) ** in file $qfile: end of file without a begin, don't know the time, throwing away text part!\n";
@lines=();
undef $time_begin;
}
}
}
if (@files) {
for my $file (@files) {
open my $in,"<$file"
or mydie "$myname: could not open '$file': $!\n";
readfile $in, "'$file'", 1;#really 1 ?
close $in
or die "close '$file': $!";
}
} else {
readfile *STDIN{IO}, "stdin", 0;
close STDIN
or die "close stdin: $!";
}
for (sort keys %sections) {
print $sections{$_};
}
__END__
Idee?: nur pointers speichern. Das eigentliche Textholen erst am Schluss aus den Files. Dadurch praktisch kein Memorygebrauch.