From becafeba45c3bf51de8ee3c523d19ae68dd6754a Mon Sep 17 00:00:00 2001 From: Minijackson Date: Tue, 9 Nov 2021 11:01:52 +0100 Subject: initial commit with basic logging --- demo.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 demo.sh (limited to 'demo.sh') diff --git a/demo.sh b/demo.sh new file mode 100644 index 0000000..256d091 --- /dev/null +++ b/demo.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +readonly BASH_LIB_NAME=DEMO +source ./bash-lib.sh + +# Set `DEMO_DEBUG=1` to enable debug messages + +trace "this is a trace message" "with additional data" +debug "this is a debugging message" "with additional data" +info "this is an informational message" "with additional data" +warn "this is a warning message" "with additional data" +error "this is an error message" "with additional data" + +# Like echo, but to stderr +echoe '------------------------------' + +if is_debug; then + info "Debugging messages:" on +else + info "Debugging messages:" off +fi + +if is_trace; then + info "Trace messages:" on +else + info "Trace messages:" off +fi + +echoe '------------------------------' + +# Use critical with `set +e` if you want to trap errors +( + function handle_error() { + info "handling error..." + } + + set +e + trap "handle_error" ERR + + critical "this is a critical error:" "this will allow some error recovery" + info "but with 'set +e', the program continues its course" +) + +echoe '------------------------------' + +info "use 'fatal()' in either case if you want to quit with an error message" +fatal "some unrecoverable error occurred, exiting" -- cgit v1.2.3