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