Skip to content

Commit 54b3036

Browse files
author
Martin Ramm
committed
log order wrong for historic logs
This commit fixed the issue described in #5, by 1) writing all logs to a tmp file, including timestamp 2) sort all lines in tmp file by timestamp 3) output the sorted lines to stdout 4) since 1) is continued for all non-stoped containers, tail the tmp file (starting at the first unsorted line) to avoid waisting hdd storage, every 10 seconds the tmpFile is emptied fixes #5
1 parent a71fe00 commit 54b3036

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

docker-fzf

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,67 @@ __docker_logs() (
5656
return 0
5757
fi
5858

59-
local resetColor="\033[39m\033[49m"
59+
local resetColor="\x1b[39m\x1b[49m"
6060
#list of 48 distinct colors
61-
local allColors="\033[90m\n\033[92m\n\033[93m\n\033[94m\n\033[95m\n\033[96m\n\033[97m\n\033[30m\n\033[31m\n\033[32m\n\033[33m\n\033[34m\n\033[35m\n\033[36m\n\033[40m\033[90m\n\033[40m\033[91m\n\033[40m\033[92m\n\033[40m\033[94m\n\033[40m\033[95m\n\033[40m\033[96m\n\033[40m\033[97m\n\033[41m\033[90m\n\033[41m\033[93m\n\033[41m\033[95m\n\033[41m\033[97m\n\033[42m\033[90m\n\033[42m\033[93m\n\033[42m\033[97m\n\033[43m\033[90m\n\033[43m\033[93m\n\033[43m\033[97m\n\033[44m\033[91m\n\033[44m\033[92m\n\033[44m\033[93m\n\033[44m\033[95m\n\033[44m\033[97m\n\033[45m\033[93m\n\033[45m\033[97m\n\033[46m\033[90m\n\033[46m\033[91m\n\033[46m\033[92m\n\033[46m\033[93m\n\033[46m\033[96m\n\033[46m\033[97m\n\033[47m\033[90m\n\033[47m\033[95m\n\033[47m\033[96m\n"
61+
local allColors="\x1b[90m\n\x1b[92m\n\x1b[93m\n\x1b[94m\n\x1b[95m\n\x1b[96m\n\x1b[97m\n\x1b[30m\n\x1b[31m\n\x1b[32m\n\x1b[33m\n\x1b[34m\n\x1b[35m\n\x1b[36m\n\x1b[40m\x1b[90m\n\x1b[40m\x1b[91m\n\x1b[40m\x1b[92m\n\x1b[40m\x1b[94m\n\x1b[40m\x1b[95m\n\x1b[40m\x1b[96m\n\x1b[40m\x1b[97m\n\x1b[41m\x1b[90m\n\x1b[41m\x1b[93m\n\x1b[41m\x1b[95m\n\x1b[41m\x1b[97m\n\x1b[42m\x1b[90m\n\x1b[42m\x1b[93m\n\x1b[42m\x1b[97m\n\x1b[43m\x1b[90m\n\x1b[43m\x1b[93m\n\x1b[43m\x1b[97m\n\x1b[44m\x1b[91m\n\x1b[44m\x1b[92m\n\x1b[44m\x1b[93m\n\x1b[44m\x1b[95m\n\x1b[44m\x1b[97m\n\x1b[45m\x1b[93m\n\x1b[45m\x1b[97m\n\x1b[46m\x1b[90m\n\x1b[46m\x1b[91m\n\x1b[46m\x1b[92m\n\x1b[46m\x1b[93m\n\x1b[46m\x1b[96m\n\x1b[46m\x1b[97m\n\x1b[47m\x1b[90m\n\x1b[47m\x1b[95m\n\x1b[47m\x1b[96m\n"
6262
#list of `$count` number of distinct colors
6363
local colors=$(echo -e "$allColors" | shuf -n $count)
6464

65-
local pids=()
65+
local allPids=()
66+
local writeToTmpFilePids=()
67+
local tmpFile="/tmp/fzf-docker-logs-$(date +'%s')"
68+
69+
function _exit {
70+
for pid in "${allPids[@]}"; do
71+
# ignore if process is not alive anymore
72+
kill -9 $pid > /dev/null 2> /dev/null
73+
done
74+
75+
test -e $tmpFile && rm -f $tmpFile
76+
}
77+
trap _exit INT TERM SIGTERM
78+
6679
while read -r name; do
6780
# last color from list
6881
local color=$(echo -e "$colors" | tail -n 1)
6982
# update list - remove last color from list
7083
colors=$(echo -e "$colors" | head -n -1)
7184

7285
# in bash, to get the pid for `docker logs` (so we can kill it in _exit), use `command1 > >(command2)` instead of `command1 | command2` - see https://stackoverflow.com/a/8048493/2732818
73-
eval "docker logs -f $since\"$name\" 2>&1 > >(sed -e \"s/^/`printf \"${color}[$name]${resetColor}\"` /\") &"
74-
pids+=($!)
86+
# sed -u needed as explained in https://superuser.com/a/792051
87+
eval "docker logs --timestamps -f $since\"$name\" 2>&1 > >(sed -u -e \"s/^/${color}[${name}]${resetColor} /\" >> $tmpFile) &"
88+
local pid=($!)
89+
90+
allPids+=($pid)
91+
writeToTmpFilePids+=($pid)
7592
done <<< "$2" # bash executes while loops in pipe in subshell, meaning pids will not be available outside of loop when using `echo -e "$2" | while...`
7693

77-
function _exit {
78-
for pid in "${pids[@]}"; do
79-
# ignore if process is not alive anymore
80-
kill -9 $pid > /dev/null 2> /dev/null
81-
done
82-
}
83-
trap _exit INT TERM SIGTERM
94+
#wait for all historc logs being written into $tmpFile
95+
sleep 2
8496

85-
for pid in "${pids[@]}"; do
97+
local removeTimestamp='sed -r -u "s/((\x1b\[[0-9]{2}m){0,2}\[.*\]\x1b\[39m\x1b\[49m )[^ ]+ /\1/"'
98+
99+
#sort historic logs
100+
local numOfLines=$(wc -l < $tmpFile)
101+
eval "head -n $numOfLines $tmpFile | sort --stable --key=2 | $removeTimestamp"
102+
103+
#show new logs
104+
local numOfLines=$((numOfLines+1))
105+
#2>/dev/null because "tail: /tmp/fzf-docker-logs: file truncated" is outputed every time $tmpFile is emptied
106+
eval "tail -f -n +$numOfLines $tmpFile > >($removeTimestamp) 2>/dev/null &"
107+
allPids+=($!)
108+
109+
#we don't really need to keep the logs on the hdd in $tmpFile so every minute empty it. But keep one line, so the "tail -f" can keep track
110+
eval "while true; do tail -n 1 $tmpFile > $tmpFile; sleep 10s; done &"
111+
allPids+=($!)
112+
113+
#wait for all docker containers have stoped, i.e. no more logs can be generated
114+
for pid in "${writeToTmpFilePids[@]}"; do
86115
wait $pid
87116
done
117+
118+
#this also kills the `tail -f $tmpFile` process
119+
_exit
88120
)
89121

90122
__docker_compose_fileref_generator() {

0 commit comments

Comments
 (0)