log.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env bash
  2. ##
  3. # Borrowed from github.com/kubernetes/kubernetes/hack/lib/logging.sh
  4. ##
  5. # -----------------------------------------------------------------------------
  6. # Logger variables helpers. These functions need the
  7. # following variables:
  8. #
  9. # LOG_LEVEL - The level of logger, default is "debug".
  10. log_level="${LOG_LEVEL:-"debug"}"
  11. log_colorful="${LOG_COLORFUL:-"true"}"
  12. # Handler for when we exit automatically on an error.
  13. gpustack::log::errexit() {
  14. local err="${PIPESTATUS[*]}"
  15. # if the shell we are in doesn't have errexit set (common in subshells) then
  16. # don't dump stacks.
  17. set +o | grep -qe "-o errexit" || return
  18. set +o xtrace
  19. gpustack::log::panic "${BASH_SOURCE[1]}:${BASH_LINENO[0]} '${BASH_COMMAND}' exited with status ${err}" "${1:-1}"
  20. }
  21. gpustack::log::install_errexit() {
  22. # trap ERR to provide an error handler whenever a command exits nonzero, this
  23. # is a more verbose version of set -o errexit
  24. trap 'gpustack::log::errexit' ERR
  25. # setting errtrace allows our ERR trap handler to be propagated to functions,
  26. # expansions and subshells
  27. set -o errtrace
  28. }
  29. # Debug level logging.
  30. gpustack::log::debug() {
  31. [[ ${log_level} == "debug" ]] || return 0
  32. local message="${2:-}"
  33. local timestamp
  34. timestamp="$(date +"[%m%d %H:%M:%S]")"
  35. echo -e "[DEBG] ${timestamp} ${1-}" >&2
  36. shift 1
  37. for message; do
  38. echo -e " ${message}" >&2
  39. done
  40. }
  41. # Info level logging.
  42. gpustack::log::info() {
  43. [[ ${log_level} == "debug" ]] || [[ ${log_level} == "info" ]] || return 0
  44. local message="${2:-}"
  45. local timestamp
  46. timestamp="$(date +"[%m%d %H:%M:%S]")"
  47. if [[ ${log_colorful} == "true" ]]; then
  48. echo -e "\033[34m[INFO]\033[0m ${timestamp} ${1-}" >&2
  49. else
  50. echo -e "[INFO] ${timestamp} ${1-}" >&2
  51. fi
  52. shift 1
  53. for message; do
  54. echo -e " ${message}" >&2
  55. done
  56. }
  57. # Warn level logging.
  58. gpustack::log::warn() {
  59. local message="${2:-}"
  60. local timestamp
  61. timestamp="$(date +"[%m%d %H:%M:%S]")"
  62. if [[ ${log_colorful} == "true" ]]; then
  63. echo -e "\033[33m[WARN]\033[0m ${timestamp} ${1-}" >&2
  64. else
  65. echo -e "[WARN] ${timestamp} ${1-}" >&2
  66. fi
  67. shift 1
  68. for message; do
  69. echo -e " ${message}" >&2
  70. done
  71. }
  72. # Error level logging, log an error but keep going, don't dump the stack or exit.
  73. gpustack::log::error() {
  74. local message="${2:-}"
  75. local timestamp
  76. timestamp="$(date +"[%m%d %H:%M:%S]")"
  77. if [[ ${log_colorful} == "true" ]]; then
  78. echo -e "\033[31m[ERRO]\033[0m ${timestamp} ${1-}" >&2
  79. else
  80. echo -e "[ERRO] ${timestamp} ${1-}" >&2
  81. fi
  82. shift 1
  83. for message; do
  84. echo -e " ${message}" >&2
  85. done
  86. }
  87. # Fatal level logging, log an error but exit with 1, don't dump the stack or exit.
  88. gpustack::log::fatal() {
  89. local message="${2:-}"
  90. local timestamp
  91. timestamp="$(date +"[%m%d %H:%M:%S]")"
  92. if [[ ${log_colorful} == "true" ]]; then
  93. echo -e "\033[41;33m[FATA]\033[0m ${timestamp} ${1-}" >&2
  94. else
  95. echo -e "[FATA] ${timestamp} ${1-}" >&2
  96. fi
  97. shift 1
  98. for message; do
  99. echo -e " ${message}" >&2
  100. done
  101. exit 1
  102. }
  103. # Panic level logging, dump the error stack and exit.
  104. # Args:
  105. # $1 Message to log with the error
  106. # $2 The error code to return
  107. # $3 The number of stack frames to skip when printing.
  108. gpustack::log::panic() {
  109. local message="${1:-}"
  110. local code="${2:-1}"
  111. local timestamp
  112. timestamp="$(date +"[%m%d %H:%M:%S]")"
  113. if [[ ${log_colorful} == "true" ]]; then
  114. echo -e "\033[41;33m[FATA]\033[0m ${timestamp} ${message}" >&2
  115. else
  116. echo -e "[FATA] ${timestamp} ${message}" >&2
  117. fi
  118. # print out the stack trace described by $function_stack
  119. if [[ ${#FUNCNAME[@]} -gt 2 ]]; then
  120. if [[ ${log_colorful} == "true" ]]; then
  121. echo -e "\033[31m call stack:\033[0m" >&2
  122. else
  123. echo -e " call stack:" >&2
  124. fi
  125. local i
  126. for ((i = 1; i < ${#FUNCNAME[@]} - 2; i++)); do
  127. echo -e " ${i}: ${BASH_SOURCE[${i} + 2]}:${BASH_LINENO[${i} + 1]} ${FUNCNAME[${i} + 1]}(...)" >&2
  128. done
  129. fi
  130. if [[ ${log_colorful} == "true" ]]; then
  131. echo -e "\033[41;33m[FATA]\033[0m ${timestamp} exiting with status ${code}" >&2
  132. else
  133. echo -e "[FATA] ${timestamp} exiting with status ${code}" >&2
  134. fi
  135. popd >/dev/null 2>&1 || exit "${code}"
  136. exit "${code}"
  137. }