#!/bin/sh # WARN: this will clobber logfiles 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 "$@"