contrib/NM-log: improve script and make it sourceable

- use "grep -a" so that grep doesn't refuse to work in binary input.
- make the script source-able to only define the NM-colorize and
  NM-show-journal
- In case the script is sourced, it also defines a NM-log function,
  which does the same as the script itself.
- rename internal functions so that they have names starting with "NM"
  in case of sourcing.
This commit is contained in:
Thomas Haller 2017-09-07 13:58:31 +02:00
parent 5ad6c8471d
commit cffde0101e

View file

@ -13,7 +13,13 @@
#
# You can pass multiple filenames.
show-journal() {
if [[ "$_" == "$0" ]]; then
NM_not_sourced=1
else
unset NM_not_sourced
fi
NM-show-journal() {
local since="$(systemctl show NetworkManager | sed -n 's/^ExecMainStartTimestamp=\(.*\) [A-Z0-9]\+$/\1/p')"
if [[ "$since" == "" ]]; then
@ -24,29 +30,37 @@ show-journal() {
fi
}
colorize() {
NM-colorize() {
if [[ "$NM_LOG_NO_COLOR" == "" ]]; then
GREP_COLOR='01;33' grep --color=always '^\|^\(.* \)\?<\(warn>\|info> \|error>\) \(.*\<is starting\>.*$\)\?' | \
GREP_COLOR='01;37' grep --color=always '^\|\<platform: signal: .*$' | \
GREP_COLOR='01;34' grep --color=always '^\|\<platform-linux: link: change \|\<platform: link: setting .*$\|\<platform: \(route\|address\): .*$\|\<platform-linux: sysctl: setting .*$' | \
GREP_COLOR='01;35' grep --color=always '^\| audit: .*$' | \
GREP_COLOR='01;32' grep --color=always '^\|\<device (.*): state change: '
# poor man's coloring using grep.
# TODO: do it somehow better (and more efficient).
GREP_COLOR='01;33' grep -a --color=always '^\|^\(.* \)\?<\(warn>\|info> \|error>\) \(.*\<is starting\>.*$\)\?' | \
GREP_COLOR='01;37' grep -a --color=always '^\|\<platform: signal: .*$' | \
GREP_COLOR='01;34' grep -a --color=always '^\|\<platform-linux: link: change \|\<platform: link: setting .*$\|\<platform: \(route\|address\): .*$\|\<platform-linux: sysctl: setting .*$' | \
GREP_COLOR='01;35' grep -a --color=always '^\| audit: .*$' | \
GREP_COLOR='01;32' grep -a --color=always '^\|\<device (.*): state change: '
else
cat -
/usr/bin/cat -
fi
}
(
if [ "$1" == "j" ]; then
shift
show-journal "$@"
elif [ "$#" -eq 0 -a -t 0 ]; then
show-journal
else
a="${1--}"
shift
/usr/bin/cat "$a" "$@"
fi
) | \
colorize | \
LESS=FRSXMK exec less -R
NM-log() {
(
if [ "$1" == "j" ]; then
shift
NM-show-journal "$@"
elif [ "$#" -eq 0 -a -t 0 ]; then
NM-show-journal
else
a="${1--}"
shift
/usr/bin/cat "$a" "$@"
fi
) | \
NM-colorize | \
LESS=FRSXMK exec less -R
}
if [[ "$NM_not_sourced" != "" ]]; then
NM-log "$@"
fi