terom@605: # Logging output terom@605: tero@627: log_GETOPTS="qvDV" terom@624: tero@627: LOG=y terom@624: LOG_ERROR=y terom@624: LOG_WARN=y terom@624: LOG_FORCE=y tero@627: LOG_APPLY=y tero@632: LOG_CHECK= tero@718: LOG_CHANGED= terom@624: LOG_NOOP=y terom@624: LOG_SKIP= terom@624: LOG_DEBUG= terom@624: LOG_CMD= terom@624: LOG_DIFF=y terom@624: tero@627: # use color output? tero@627: IS_TTY= tero@627: tero@627: function log_help { tero@627: cat <&2 terom@605: } terom@605: terom@605: # Output message to stderr, optionally with given color, if TTY. terom@605: function log_color { terom@605: local code=$1; shift terom@605: terom@605: if [ $IS_TTY ]; then terom@605: echo $'\e['${code}'m'"$*"$'\e[00m' >&2 terom@605: else terom@605: echo "$*" >&2 terom@605: fi terom@605: } terom@605: terom@605: ## Log at various log-levels terom@605: # plain terom@605: function log { terom@605: [ $LOG ] && log_msg "$*" || true terom@605: } terom@605: terom@605: function log_error { terom@605: [ $LOG_ERROR ] && log_color '31' "$*" || true terom@605: } terom@605: terom@605: function log_warn { terom@605: [ $LOG_WARN ] && log_color '33' "$*" || true terom@605: } terom@605: terom@605: function log_force { terom@605: [ $LOG_FORCE ] && log_color '2;33' " $*" || true terom@605: } terom@605: tero@627: function log_apply { tero@627: [ $LOG_APPLY ] && log_color '36' " $*" || true terom@605: } terom@605: terom@613: function log_check { tero@632: [ $LOG_CHECK ] && log_color '32' " $*" || true terom@613: } terom@613: tero@718: function log_changed { tero@718: [ $LOG_CHANGED ] && log_color '1;32' " $*" || true tero@718: } tero@718: terom@605: function log_noop { terom@605: [ $LOG_NOOP ] && log_color '2;34' " $*" || true terom@605: } terom@605: terom@605: function log_skip { terom@605: [ $LOG_SKIP ] && log_color '1;34' " $*" || true terom@605: } terom@605: terom@605: function log_debug { tero@632: [ $LOG_DEBUG ] && log_color '34' " $*" || true terom@605: } terom@605: terom@605: function log_cmd { terom@605: [ $LOG_CMD ] && log_color '35' " \$ $*" || true terom@605: } terom@605: terom@605: # Output stacktrace, broken. terom@605: function log_stack { terom@605: local level=1 terom@605: terom@605: while info=$(caller $level); do terom@605: echo $info | read line sub file terom@605: terom@605: log_msg "$file:$lineno $sub()" terom@605: terom@605: level=$(($level + 1)) terom@605: done terom@605: } terom@605: terom@605: ### High-level logging output terom@605: # Log with func_caller at log_debug terom@605: function debug { terom@605: printf -v prefix "%s" $(func_caller) terom@605: terom@605: log_debug "$prefix: $*" terom@605: } terom@605: terom@608: function warn { terom@608: log_warn "$(func_caller): $*" terom@608: } terom@608: tero@631: function error { tero@631: log_error "$(func_caller): $*" tero@631: } tero@631: terom@605: # Log with func_caller at log_error and exit, intended for internal errors... terom@605: function fail { terom@605: log_error "$(func_caller): $*" terom@605: terom@605: exit 2 terom@605: } terom@605: terom@605: # Log at log_error and exit terom@605: function die { terom@605: log_error "$*" terom@605: exit 1 terom@605: }