PageRenderTime 82ms CodeModel.GetById 75ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/lsb-base-logging.sh

http://github.com/brinkman83/bashrc
Shell | 149 lines | 112 code | 22 blank | 15 comment | 28 complexity | 5126106e3af09b522cdbd5e4685942af MD5 | raw file
  1# Default init script logging functions suitable for Ubuntu.
  2# See /lib/lsb/init-functions for usage help.
  3
  4log_use_usplash () {
  5    if [ "${loop:-n}" = y ]; then
  6        return 1
  7    fi
  8    type usplash_write >/dev/null 2>&1
  9}
 10
 11log_success_msg () {
 12    if log_use_usplash; then
 13        usplash_write "TEXT   $*" || true
 14    fi
 15
 16    echo " * $@"
 17}
 18
 19log_failure_msg () {
 20    if log_use_usplash; then
 21        usplash_write "TEXT   $*" || true
 22    fi
 23
 24    if log_use_fancy_output; then
 25        RED=`$TPUT setaf 1`
 26        NORMAL=`$TPUT op`
 27        echo " $RED*$NORMAL $@"
 28    else
 29        echo " * $@"
 30    fi
 31}
 32
 33log_warning_msg () {
 34    if log_use_usplash; then
 35        usplash_write "TEXT   $*" || true
 36    fi
 37
 38    if log_use_fancy_output; then
 39        YELLOW=`$TPUT setaf 3`
 40        NORMAL=`$TPUT op`
 41        echo " $YELLOW*$NORMAL $@"
 42    else
 43        echo " * $@"
 44    fi
 45}
 46
 47log_begin_msg () {
 48    log_daemon_msg "$1"
 49}
 50
 51log_daemon_msg () {
 52    if [ -z "$1" ]; then
 53        return 1
 54    fi
 55
 56    if log_use_usplash; then
 57        usplash_write "TEXT $*" || true
 58    fi
 59
 60    if log_use_fancy_output && $TPUT xenl >/dev/null 2>&1; then
 61        COLS=`$TPUT cols`
 62        if [ "$COLS" ] && [ "$COLS" -gt 6 ]; then
 63            COL=`$EXPR $COLS - 7`
 64        else
 65	    COLS=80
 66            COL=73
 67        fi
 68        # We leave the cursor `hanging' about-to-wrap (see terminfo(5)
 69        # xenl, which is approximately right). That way if the script
 70        # prints anything then we will be on the next line and not
 71        # overwrite part of the message.
 72
 73        # Previous versions of this code attempted to colour-code the
 74        # asterisk but this can't be done reliably because in practice
 75        # init scripts sometimes print messages even when they succeed
 76        # and we won't be able to reliably know where the colourful
 77        # asterisk ought to go.
 78
 79        printf " * $*       "
 80        # Enough trailing spaces for ` [fail]' to fit in; if the message
 81        # is too long it wraps here rather than later, which is what we
 82        # want.
 83        $TPUT hpa `$EXPR $COLS - 1`
 84        printf ' '
 85    else
 86        echo " * $@"
 87        COL=
 88    fi
 89}
 90
 91log_progress_msg () {
 92    :
 93}
 94
 95log_end_msg () {
 96    if [ -z "$1" ]; then
 97        return 1
 98    fi
 99
100    if log_use_usplash; then
101        if [ "$1" -eq 0 ]; then
102            usplash_write "SUCCESS OK" || true
103        else
104            usplash_write "FAILURE failed" || true
105        fi
106    fi
107
108    if [ "$COL" ] && [ -x "$TPUT" ]; then
109        printf "\r"
110        $TPUT hpa $COL
111        if [ "$1" -eq 0 ]; then
112            echo "[ OK ]"
113        else
114            printf '['
115            $TPUT setaf 1 # red
116            printf fail
117            $TPUT op # normal
118            echo ']'
119        fi
120    else
121        if [ "$1" -eq 0 ]; then
122            echo "   ...done."
123        else
124            echo "   ...fail!"
125        fi
126    fi
127    return $1
128}
129
130log_action_msg () {
131    if log_use_usplash; then
132        usplash_write "TEXT $*" || true
133    fi
134
135    echo " * $@"
136}
137
138log_action_begin_msg () {
139    log_daemon_msg "$@..."
140}
141
142log_action_cont_msg () {
143    log_daemon_msg "$@..."
144}
145
146log_action_end_msg () {
147    # In the future this may do something with $2 as well.
148    log_end_msg "$1" || true
149}