#!/bin/bash
# output - Script output support library
# lib_level: 0
set -e
######
# Settings
# timestamps - If true, prefixes stderr timestamps
timestamps=${timestamps:-true}
# nanoseconds - If non-empty, timestamp displays nanoseconds.
nanoseconds=${nanoseconds}
# app_log_hook - name of function that implements app_log_pipe
app_log_hook=app_log_stub
# error_usage_hook - name of function that implements error_usage
error_usage_hook=error_usage_stub
core_output_config_init() {
lib_setting_vars timestamps
lib_setting_vars --null nanoseconds
lib_setting_vars app_log_hook error_usage_hook
}
######
# Logging Hook
# app_log - Calls app_log_hook
app_log() { $app_log_hook "$@"; }
# script_log_pipe_stub - This definition is a temporary handler for debugging
# library loading. When the script library is loaded, it is replaced
# by the final version located therein.
app_log_stub() { cat; }
######
# Output
timestamp() { ! $timestamps || date +"[%H:%M:%S${nanoseconds:+.%N}]"; }
_app_echo() { if $logging; then echo "$@" | app_log; else echo "$@"; fi; }
app_echo() { _app_echo -e $(timestamp) "$*"; }
app_echo_n() { _app_echo -en $(timestamp) "$*"; }
app_msg() { app_echo "${script[*]} $*"; }
app_msg_if() {
local x=$1
shift
if $x; then
app_msg "$@" >&2
else
app_msg "$@" >/dev/null
fi
}
######
# Information and errors
error() { app_msg_if true "ERROR: $*"; stack_trace; false; }
warn() { app_msg_if $(as_bool_not $quiet) "WARN: $*"; }
info() { app_msg_if $verbose "INFO: $*"; }
debug() { app_msg_if $debug "DEBUG: $*"; }
######
# Error Usage Hook
# error_usage - print an error and current command usage
error_usage() { $error_usage_hook "$@"; }
# error_usage_stub - This stub prints an unadorned error; the 'commands'
# library provides a version to print the usage information.
error_usage_stub() { error "$1"; }
######
# Assertion support
_assert() {
local _efunc=$1
local _err=$2
shift 2
"$@" || $_efunc "$_err"
}
assert() { _assert error "$@"; }
assert_usage() { _assert error_usage "$@"; }
######
# Stack traces
stack_raw() {
echo "FUNCNAME=( ${FUNCNAME[*]} )"
echo "BASH_SOURCES=( ${BASH_SOURCE[*]} )"
echo "BASH_LINENO=( ${BASH_LINENO[*]} )"
}
stack_trace() {
debug "Stack trace:"
local depth="${#FUNCNAME[@]}"
for_each _stack_dump $(seq 2 $(($depth - 1)))
}
_stack_dump() {
local frame=$1
local func="${FUNCNAME[$frame]}"
local file=${BASH_SOURCE[$frame]}
file=${file#$libdir/}
file=${file%.sh}
local lineno="${BASH_LINENO[$(($frame - 1))]}"
debug "$((frame - 2)): $func ($file:$lineno)"
}
Generated on Tue Apr 25 21:20:13 PDT 2017 by mcsh i7 v0.18.0.