50-ubuntu-logging 3.4 KB

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