aboutsummaryrefslogtreecommitdiff
path: root/lib/logging.bash
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2020-03-22 11:48:23 +0100
committerPatrick Spek <p.spek@tyil.nl>2020-03-22 11:48:23 +0100
commit1d983e9f934bf6aeb9333c763fe1a603b8d8e5c4 (patch)
treeff96afaf569e8669a28ba35e85a1441621117d0b /lib/logging.bash
Initial commit
Diffstat (limited to 'lib/logging.bash')
-rw-r--r--lib/logging.bash60
1 files changed, 60 insertions, 0 deletions
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