From 1d983e9f934bf6aeb9333c763fe1a603b8d8e5c4 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 22 Mar 2020 11:48:23 +0100 Subject: Initial commit --- lib/logging.bash | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lib/logging.bash (limited to 'lib/logging.bash') diff --git a/lib/logging.bash b/lib/logging.bash new file mode 100644 index 0000000..33ead42 --- /dev/null +++ b/lib/logging.bash @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +# TODO: Only use colors on terminals that are known to work well with them. + +# The base function to output logging information. This should *not* be used +# directly, but the helper functions can be used safely. +log() { + local OPTIND + local color + local format="[%s] %s\n" + + while getopts ":c:" opt + do + case "$opt" in + c) color=$OPTARG ;; + esac + done + + shift $(( OPTIND - 1 )) + + printf "$color[%s] %s\e[0m\n" "$(date +%FT%T)" "$*" >&2 +} + +debug() { + [[ -z $RSTAR_DEBUG ]] && return + log -c "\e[1;30m" -- "$*" +} + +info() { + log -- "$*" +} + +notice() { + log -c "\e[0;34m" -- "$*" +} + +warn() { + log -c "\e[0;33m" -- "$*" +} + +crit() { + log -c "\e[0;31m" -- "$*" +} + +alert() { + log -c "\e[1;31m" -- "$*" +} + +emerg() { + log -c "\e[1;4;31m" -- "$*" +} + +export -f log +export -f debug +export -f info +export -f notice +export -f warn +export -f crit +export -f alert +export -f emerg -- cgit v1.1