lib/pvl/log.sh
changeset 627 a81206440be2
parent 624 889ecd6dcb4e
child 628 b10ad946d01d
equal deleted inserted replaced
626:5cd99761fe4d 627:a81206440be2
       
     1 # Logging output
       
     2 
       
     3 log_GETOPTS="qvDV"
       
     4 
       
     5 LOG=y
       
     6 LOG_ERROR=y
       
     7 LOG_WARN=y
       
     8 LOG_FORCE=y
       
     9 LOG_APPLY=y
       
    10 LOG_NOOP=y
       
    11 LOG_SKIP=
       
    12 LOG_DEBUG=
       
    13 LOG_CMD=
       
    14 LOG_DIFF=y
       
    15 
       
    16 # use color output?
       
    17 IS_TTY=
       
    18 
       
    19 function log_help {
       
    20     cat <<END
       
    21 Logging:    
       
    22     -q      quiet
       
    23     -v      verbose
       
    24     -D      debug
       
    25     -V      debug commands
       
    26 END
       
    27 }
       
    28 
       
    29 function log_opt {
       
    30     local opt=$1
       
    31     local optarg="$2"
       
    32 
       
    33     case $opt in
       
    34         q)  
       
    35             LOG= 
       
    36             LOG_WARN=
       
    37             LOG_APPLY=
       
    38             LOG_FORCE=
       
    39             LOG_NOOP=
       
    40             LOG_DIFF=
       
    41             ;;
       
    42 
       
    43         v)  LOG_SKIP=y ;;
       
    44         D)  
       
    45             LOG_DEBUG=y
       
    46             LOG_INFO=y
       
    47             ;;
       
    48         V)  LOG_CMD=y ;;
       
    49 
       
    50         *)  return 1
       
    51     esac
       
    52 }
       
    53 
       
    54 function log_init {
       
    55     [ -t 1 ] && IS_TTY=y
       
    56 }
       
    57 
       
    58 # Output message to stderr.
       
    59 function log_msg {
       
    60     echo "$*" >&2
       
    61 }
       
    62 
       
    63 # Output message to stderr, optionally with given color, if TTY.
       
    64 function log_color {
       
    65     local code=$1; shift
       
    66 
       
    67     if [ $IS_TTY ]; then
       
    68         echo $'\e['${code}'m'"$*"$'\e[00m' >&2
       
    69     else
       
    70         echo "$*" >&2
       
    71     fi
       
    72 }
       
    73 
       
    74 ## Log at various log-levels
       
    75 # plain
       
    76 function log {
       
    77     [ $LOG          ] && log_msg            "$*"            || true
       
    78 }
       
    79 
       
    80 function log_error {
       
    81     [ $LOG_ERROR    ] && log_color '31'     "$*"            || true
       
    82 }
       
    83 
       
    84 function log_warn {
       
    85     [ $LOG_WARN     ] && log_color '33'     "$*"            || true
       
    86 }
       
    87 
       
    88 function log_force {
       
    89     [ $LOG_FORCE    ] && log_color '2;33'   "  $*"          || true
       
    90 }
       
    91 
       
    92 function log_apply {
       
    93     [ $LOG_APPLY    ] && log_color '36'     "  $*"          || true
       
    94 }
       
    95 
       
    96 function log_check {
       
    97     [ $LOG_APPLY    ] && log_color '37'     "  $*"          || true
       
    98 }
       
    99 
       
   100 function log_noop {
       
   101     [ $LOG_NOOP     ] && log_color '2;34'   "  $*"          || true
       
   102 }
       
   103 
       
   104 function log_skip {
       
   105     [ $LOG_SKIP     ] && log_color '1;34'   "  $*"          || true
       
   106 }
       
   107 
       
   108 function log_debug {
       
   109     [ $LOG_DEBUG    ] && log_color '32'     "    $*"        || true
       
   110 }
       
   111 
       
   112 function log_cmd {
       
   113     [ $LOG_CMD      ] && log_color '35'     "        \$ $*" || true
       
   114 }
       
   115 
       
   116 # Output stacktrace, broken.
       
   117 function log_stack {
       
   118     local level=1
       
   119 
       
   120     while info=$(caller $level); do
       
   121         echo $info | read line sub file
       
   122 
       
   123         log_msg "$file:$lineno $sub()"
       
   124 
       
   125         level=$(($level + 1))
       
   126     done
       
   127 }
       
   128 
       
   129 ### High-level logging output
       
   130 # Log with func_caller at log_debug
       
   131 function debug {
       
   132     printf -v prefix "%s" $(func_caller)
       
   133 
       
   134     log_debug "$prefix: $*"
       
   135 }
       
   136 
       
   137 function warn {
       
   138     log_warn "$(func_caller): $*"
       
   139 }
       
   140 
       
   141 # Log with func_caller at log_error and exit, intended for internal errors...
       
   142 function fail {
       
   143     log_error "$(func_caller): $*"
       
   144 
       
   145     exit 2
       
   146 }
       
   147 
       
   148 # Log at log_error and exit
       
   149 function die {
       
   150     log_error "$*"
       
   151     exit 1
       
   152 }