1
0
Fork 0
dotfiles/.local/bin/logwrap

32 lines
702 B
Plaintext
Raw Normal View History

2024-02-03 18:09:40 +01:00
#!/bin/sh
LOGFILE="${LOGFILE:-${HOME}/.local/log/${1}.log}"
BOLD="$(tput bold)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
NC="$(tput sgr0)"
infolog() {
while IFS= read -r line; do
echo "[$(date +%X)][${BOLD}${GREEN}INF${NC}][$1] $line" >>"${LOGFILE}"
done
}
errorlog() {
while IFS= read -r line; do
echo "[$(date +%X)][${BOLD}${RED}ERR${NC}][$1] $line" >>"${LOGFILE}"
done
}
start() {
stdout="$XDG_RUNTIME_DIR/dwm-stdout"
stderr="$XDG_RUNTIME_DIR/dwm-stderr"
[ -p "$stdout" ] || mkfifo "$stdout"
[ -p "$stderr" ] || mkfifo "$stderr"
cat <"$stdout" | infolog "$1" &
cat <"$stderr" | errorlog "$1" &
pidof -sx "$1" || "$@" >"$stdout" 2>"$stderr"
}
start "$@"