terom@82: #!/bin/bash terom@82: # vim: set ft=sh : terom@82: # terom@82: # Logging output terom@82: terom@82: # Output message to stderr. terom@82: function log_msg { terom@82: echo "$*" >&2 terom@82: } terom@82: terom@82: # Output message to stderr, optionally with given color, if TTY. terom@82: function log_color { terom@82: local code=$1; shift terom@82: terom@82: if [ $IS_TTY ]; then terom@82: echo $'\e['${code}'m'"$*"$'\e[00m' >&2 terom@82: else terom@82: echo "$*" >&2 terom@82: fi terom@82: } terom@82: terom@82: ## Log at various log-levels terom@82: # plain terom@82: function log { terom@82: [ $LOG ] && log_msg "$*" || true terom@82: } terom@82: terom@82: function log_error { terom@82: [ $LOG_ERROR ] && log_color '31' "$*" || true terom@82: } terom@82: terom@82: function log_warn { terom@82: [ $LOG_WARN ] && log_color '33' "$*" || true terom@82: } terom@82: terom@82: function log_force { terom@82: [ $LOG_FORCE ] && log_color '2;33' " $*" || true terom@82: } terom@82: terom@82: function log_update { terom@82: [ $LOG_UPDATE ] && log_color '36' " $*" || true terom@82: } terom@82: terom@90: function log_check { terom@90: [ $LOG_UPDATE ] && log_color '37' " $*" || true terom@90: } terom@90: terom@82: function log_noop { terom@82: [ $LOG_NOOP ] && log_color '2;34' " $*" || true terom@82: } terom@82: terom@82: function log_skip { terom@82: [ $LOG_SKIP ] && log_color '1;34' " $*" || true terom@82: } terom@82: terom@82: function log_debug { terom@82: [ $LOG_DEBUG ] && log_color '32' " $*" || true terom@82: } terom@82: terom@82: function log_cmd { terom@82: [ $LOG_CMD ] && log_color '35' " \$ $*" || true terom@82: } terom@82: terom@82: # Output stacktrace, broken. terom@82: function log_stack { terom@82: local level=1 terom@82: terom@82: while info=$(caller $level); do terom@82: echo $info | read line sub file terom@82: terom@82: log_msg "$file:$lineno $sub()" terom@82: terom@82: level=$(($level + 1)) terom@82: done terom@82: } terom@82: terom@82: # Output calling function's name. terom@82: function func_caller { terom@82: caller 1 | cut -d ' ' -f 2 terom@82: } terom@82: terom@82: ### High-level logging output terom@82: # Log with func_caller at log_debug terom@82: function debug { terom@82: printf -v prefix "%s" $(func_caller) terom@82: terom@82: log_debug "$prefix: $*" terom@82: } terom@82: terom@85: function warn { terom@85: log_warn "$(func_caller): $*" terom@85: } terom@85: terom@82: # Log with func_caller at log_error and exit, intended for internal errors... terom@82: function fail { terom@82: log_error "$(func_caller): $*" terom@82: terom@82: exit 2 terom@82: } terom@82: terom@82: # Log at log_error and exit terom@82: function die { terom@82: log_error "$*" terom@82: exit 1 terom@82: } terom@82: terom@82: