Skip to content

Instantly share code, notes, and snippets.

@loopyd
Last active February 11, 2025 09:34
Show Gist options
  • Save loopyd/b7b39ae83d7289cf133ce939d97ef43f to your computer and use it in GitHub Desktop.
Save loopyd/b7b39ae83d7289cf133ce939d97ef43f to your computer and use it in GitHub Desktop.
[bash] deilog
#!/bin/bash -i
set -euo pipefail
shopt -s extglob
# As cheap as bash logging gets (without hand-gulping each func to a single line) - relentlessly typed by hand in 7 minutes
# - by loopyd. You like this ascii vomit??? drop me some SOL: 7Y7hG3o7o1FDgCUQtiQkN6gDxumTh1QeM9CQKnCHCb2x
CSCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
C_RED="\033[31m" C_GREEN="\033[32m" C_YELLOW="\033[33m" C_BLUE="\033[34m" C_CYAN="\033[36m" C_MAGENTA="\033[35m" C_GREY="\033[90m" C_BOLD="\033[1m" C_RESET="\033[0m"
QUIET=${QUIET:-0} LOG_FILE=${LOG_FILE:-} LOG_LEVEL=${LOG_LEVEL:-INFO}
ENV_CMD="source ${NVM_DIR}/nvm.sh && nvm use 20.18.2 --save && conda activate the-gimp"
strip_ansi() { local _msg="$*"; sed -E 's/\x1B\[[0-9;]*[mK]//g; s/^[[:space:]]+//; s/[[:space:]]+$//' <<< "$_msg"; }
log() {
local _lvl="${1:-}"; shift; local _msg="$*";
local _msg_val _cur_val _hdr _ln _ts _cln _fln
case "$_lvl" in
ERROR) _msg_val=0; _hdr="${C_RED}[ERROR]${C_RESET}" ;;
WARN) _msg_val=1; _hdr="${C_YELLOW}[WARN]${C_RESET}" ;;
INFO) _msg_val=2; _hdr="${C_GREEN}[INFO]${C_RESET}" ;;
DEBUG) _msg_val=3; _hdr="${C_BLUE}[DEBUG]${C_RESET}" ;;
*) _msg_val=2; _hdr="[${_lvl}]" ;;
esac
case "$LOG_LEVEL" in ERROR) _cur_val=0 ;; WARN) _cur_val=1 ;; DEBUG) _cur_val=3 ;; INFO) _cur_val=2 ;; *) _cur_val=2 ;; esac
while IFS= read -r _ln; do
_ts=$(date +"%Y-%m-%d %H:%M:%S"); _cln=$(strip_ansi "${_ln}")
[[ -z "${_cln//[[:space:]]/}" ]] && continue
_fln="${C_GREY}${_ts}${C_RESET} ${_hdr} ${_cln}"
if [[ "${QUIET}" -ne 1 ]] && [[ "${_msg_val}" -le "${_cur_val}" ]]; then
case "${_lvl}" in
WARN|ERROR) echo -e "${_fln}" >&2 ;; *) echo -e "${_fln}" ;;
esac
fi
if [[ -n "${LOG_FILE}" ]]; then echo "${_fln}" >> "${LOG_FILE}"; fi
done <<< "${_msg}"
}
run_cmd_raw() {
local _cmd="$*"
local stdout_pipe stderr_pipe warn_pipe
stdout_pipe=$(mktemp -u "/tmp/run_cmd_raw_stdout.XXXXXX")
stderr_pipe=$(mktemp -u "/tmp/run_cmd_raw_stderr.XXXXXX")
warn_pipe=$(mktemp -u "/tmp/run_cmd_raw_warn.XXXXXX")
mkfifo "$stdout_pipe" "$stderr_pipe" "$warn_pipe"
( while IFS= read -r line; do echo "$line" | grep -qi "error" && log ERROR "$line" || ( echo "$line" | grep -qiE "warn(ing)?" || log DEBUG "$line" ); done < "$stdout_pipe" ) & local stdout_pid=$!
( while IFS= read -r line; do echo "$line" | grep -qiE "warn(ing)?" || log ERROR "$line"; done < "$stderr_pipe" ) & local stderr_pid=$!
( while IFS= read -r line; do echo "$line" | grep -qi "error" && log ERROR "$line" || ( echo "$line" | grep -qiE "warn(ing)?" && log WARN "$line" ); done < "$warn_pipe" ) & local warn_pid=$!
eval "$_cmd" > >(tee "$stdout_pipe" "$warn_pipe" > /dev/null) 2> >(tee "$stderr_pipe" "$warn_pipe" > /dev/null)
local ret=$?
wait "$stdout_pid" 2>/dev/null; wait "$stderr_pid" 2>/dev/null; wait "$warn_pid" 2>/dev/null
rm "$stdout_pipe" "$stderr_pipe" "$warn_pipe"
return $ret
}
run_cmd() { run_cmd_raw "${ENV_CMD} && $*"; }
run_until_timeout() {
local _cmd="$*"
local _timeout="${TIMEOUT:-30s}"
run_cmd_raw "timeout --kill-after=${_timeout} --preserve-status ${_timeout} /bin/bash -i -c \"$ENV_CMD && ${_cmd}\""
local _exit_code=$?
if [ $_exit_code -eq 124 ] || [ $_exit_code -eq 143 ]; then
return 0
fi
return $_exit_code
}
@loopyd
Copy link
Author

loopyd commented Feb 11, 2025

Example Output:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment