lib/update.log
branchdns-new
changeset 605 26a307558602
child 608 4ad9c9b7cd0e
equal deleted inserted replaced
604:9a23fca9167a 605:26a307558602
       
     1 #!/bin/bash
       
     2 # vim: set ft=sh :
       
     3 #
       
     4 # Logging output
       
     5 
       
     6 # Output message to stderr.
       
     7 function log_msg {
       
     8     echo "$*" >&2
       
     9 }
       
    10 
       
    11 # Output message to stderr, optionally with given color, if TTY.
       
    12 function log_color {
       
    13     local code=$1; shift
       
    14 
       
    15     if [ $IS_TTY ]; then
       
    16         echo $'\e['${code}'m'"$*"$'\e[00m' >&2
       
    17     else
       
    18         echo "$*" >&2
       
    19     fi
       
    20 }
       
    21 
       
    22 ## Log at various log-levels
       
    23 # plain
       
    24 function log {
       
    25     [ $LOG          ] && log_msg            "$*"            || true
       
    26 }
       
    27 
       
    28 function log_error {
       
    29     [ $LOG_ERROR    ] && log_color '31'     "$*"            || true
       
    30 }
       
    31 
       
    32 function log_warn {
       
    33     [ $LOG_WARN     ] && log_color '33'     "$*"            || true
       
    34 }
       
    35 
       
    36 function log_force {
       
    37     [ $LOG_FORCE    ] && log_color '2;33'   "  $*"          || true
       
    38 }
       
    39 
       
    40 function log_update {
       
    41     [ $LOG_UPDATE   ] && log_color '36'     "  $*"          || true
       
    42 }
       
    43 
       
    44 function log_noop {
       
    45     [ $LOG_NOOP     ] && log_color '2;34'   "  $*"          || true
       
    46 }
       
    47 
       
    48 function log_skip {
       
    49     [ $LOG_SKIP     ] && log_color '1;34'   "  $*"          || true
       
    50 }
       
    51 
       
    52 function log_debug {
       
    53     [ $LOG_DEBUG    ] && log_color '32'     "    $*"        || true
       
    54 }
       
    55 
       
    56 function log_cmd {
       
    57     [ $LOG_CMD      ] && log_color '35'     "        \$ $*" || true
       
    58 }
       
    59 
       
    60 # Output stacktrace, broken.
       
    61 function log_stack {
       
    62     local level=1
       
    63 
       
    64     while info=$(caller $level); do
       
    65         echo $info | read line sub file
       
    66 
       
    67         log_msg "$file:$lineno $sub()"
       
    68 
       
    69         level=$(($level + 1))
       
    70     done
       
    71 }
       
    72 
       
    73 # Output calling function's name.
       
    74 function func_caller {
       
    75     caller 1 | cut -d ' ' -f 2
       
    76 }
       
    77 
       
    78 ### High-level logging output
       
    79 # Log with func_caller at log_debug
       
    80 function debug {
       
    81     printf -v prefix "%s" $(func_caller)
       
    82 
       
    83     log_debug "$prefix: $*"
       
    84 }
       
    85 
       
    86 # Log with func_caller at log_error and exit, intended for internal errors...
       
    87 function fail {
       
    88     log_error "$(func_caller): $*"
       
    89 
       
    90     exit 2
       
    91 }
       
    92 
       
    93 # Log at log_error and exit
       
    94 function die {
       
    95     log_error "$*"
       
    96     exit 1
       
    97 }
       
    98 
       
    99