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