-
Notifications
You must be signed in to change notification settings - Fork 3
/
format-log-entries
executable file
·126 lines (107 loc) · 2.65 KB
/
format-log-entries
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/env bash
show_help() {
cat <<'EOF'
This program parses the lines from a iRODS log file into log entries.
PARAMETERS:
YEAR: The four digit year the log entries where recorded in
This program reads from standard input and writes to standard output.
Here's an example entry:
YEAR-01-21 18:20:57 pid:17536 ERROR: [-] iRODS/server/core/src/rsApiHandler.cpp:520:readAndProcClientMsg : status [SYS_HEADER_READ_LEN_ERR] errno [] -- message []\n [-] iRODS/lib/core/src/sockComm.cpp:199:readMsgHeader : status [SYS_HEADER_READ_LEN_ERR] errno [] -- message [failed to call 'read header']
All control characters including line feed (new line) are replaced with there
escaped hex ASCII code, e.g., line feed => \x0a, carriage return => \x0d, etc.
Any bytes sequences that are invalid UTF-8 are silently discarded.
NB: This program requires gawk to be installed.
EOF
}
set -o errexit -o nounset -o pipefail
main() {
if (( $# < 1 )); then
show_help >&2
return 1
fi
local year="$1"
iconv -c --from-code=UTF-8 --to-code=UTF-8 | format "$year"
}
format() {
local year="$1"
awk --assign YEAR="$year" --file - <(cat) <<'EOF'
function escape_ctrls(line) {
patsplit(line, ctrlChars, /[\x01-\x1f]/, segs);
escLine = segs[0];
for (idx in ctrlChars) {
escLine = escLine ctrlEsc[ctrlChars[idx]] segs[idx];
}
return escLine;
}
function month_number(month) {
switch(month) {
case "Jan":
return "01";
case "Feb":
return "02";
case "Mar":
return "03";
case "Apr":
return "04";
case "May":
return "05";
case "Jun":
return "06";
case "Jul":
return "07";
case "Aug":
return "08";
case "Sep":
return "09";
case "Oct":
return "10";
case "Nov":
return "11";
case "Dec":
return "12";
default:
return "??";
}
}
BEGIN {
currentEntry = "";
lastMonth = "";
for (n = 1; n <= 0x1f; n++) {
ctrlEsc[sprintf("%c", n)] = sprintf("\\x%02x", n);
}
if (YEAR !~ /[0-9][0-9]*/) {
print "The YEAR variable must be set to a number on the command line" > "/dev/stderr";
exit 1;
}
}
/^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [ 1-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] / {
if (currentEntry != "") {
print currentEntry;
}
month = month_number($1);
day = $2;
rest = substr($0, match($0, $3) - 1);
# Ensure days are two digits
if (day ~ /^[1-9]$/) {
day = "0" day;
}
if (lastMonth == "12" && month == "01") {
YEAR = YEAR + 1;
}
currentEntry = YEAR "-" month "-" day escape_ctrls(rest);
lastMonth = month;
next;
}
NF > 0 {
if (currentEntry != "") {
currentEntry = currentEntry ctrlEsc["\x0a"] escape_ctrls($0);
}
}
END {
if (currentEntry != "") {
print currentEntry;
}
}
EOF
}
main "$@"