-
-
Save nexus166/581472463579c73e15aa71e6db610cb7 to your computer and use it in GitHub Desktop.
sexy logging for shell scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
msg() | |
{ | |
tstamp() | |
{ | |
tput setaf 7; | |
date "+%d.%m.%Y %T.%N %Z"; | |
tput sgr0 | |
}; | |
speak() | |
{ | |
printf "\\n(%s)\\t%s\\n\\t\\t[ %s ]\\n\\n" "$_FACT_" "$(tstamp)" "$@" | |
}; | |
_FACT_="$1"; | |
shift; | |
case "$_FACT_" in | |
d | D | debug | DEBUG) | |
_FACT_="DEBUG"; | |
tput setaf 5 | |
;; | |
e | E | err | ERR) | |
_FACT_="ERR"; | |
tput setaf 1 | |
;; | |
i | I | info | INFO) | |
_FACT_="INFO"; | |
tput setaf 6 | |
;; | |
o | O | ok | OK) | |
_FACT_="OK"; | |
tput setaf 2 | |
;; | |
w | W | warn | WARN) | |
_FACT_="WARN"; | |
tput setaf 3 | |
;; | |
*) | |
_FACT_="MAN"; | |
tput setaf 6 | |
;; | |
esac; | |
case "$_FACT_" in | |
DEBUG) | |
if [ "$DEBUG" != 0 ] && [ ! -z "$DEBUG" ]; then | |
speak "$@"; | |
fi | |
;; | |
MAN) | |
speak "usage: msg KIND(DEBUG,ERR,INFO,OK,WARN) MESSAGE" | |
;; | |
*) | |
speak "$@" | |
;; | |
esac; | |
tput sgr0 | |
} | |
# tests | |
case "$1" in | |
t|T|test|TEST) | |
msg info "start of test suite!" | |
echo "debug no" | |
DEBUG=0 | |
msg debug "debug message that will be not printed" | |
echo "debug yes" | |
DEBUG=1 | |
msg debug "debug message that will be printed" | |
msg e "not good :(" | |
msg I "time to sleep?" | |
msg ok "all good :)" | |
msg WARN "hold up!" | |
;; | |
esac | |
msg ok "msg is loaded" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment