logging 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/env bash
  2. __rvm_set_color_single()
  3. {
  4. case "$1" in
  5. # emphasized (bolded) colors
  6. (bold) __buffer+='7' ;;
  7. (offbold) __buffer+='27' ;;
  8. # regular colors
  9. (black) __buffer+='30' ;;
  10. (red) __buffer+='31' ;;
  11. (green) __buffer+='32' ;;
  12. (yellow) __buffer+='33' ;;
  13. (blue) __buffer+='34' ;;
  14. (magenta) __buffer+='35' ;;
  15. (cyan) __buffer+='36' ;;
  16. (white) __buffer+='37' ;;
  17. (default) __buffer+='39' ;;
  18. # intense (bright) colors
  19. (iblack) __buffer+='30;1' ;;
  20. (ired) __buffer+='31;1' ;;
  21. (igreen) __buffer+='32;1' ;;
  22. (iyellow) __buffer+='33;1' ;;
  23. (iblue) __buffer+='34;1' ;;
  24. (imagenta) __buffer+='35;1' ;;
  25. (icyan) __buffer+='36;1' ;;
  26. (iwhite) __buffer+='37;1' ;;
  27. # background colors
  28. (bblack) __buffer+='40' ;;
  29. (bred) __buffer+='41' ;;
  30. (bgreen) __buffer+='42' ;;
  31. (byellow) __buffer+='43' ;;
  32. (bblue) __buffer+='44' ;;
  33. (bmagenta) __buffer+='45' ;;
  34. (bcyan) __buffer+='46' ;;
  35. (bwhite) __buffer+='47' ;;
  36. (bdefault) __buffer+='49' ;;
  37. # Reset
  38. (*) __buffer+='0' ;;
  39. esac
  40. }
  41. __rvm_set_color()
  42. {
  43. \typeset __buffer __variable
  44. __buffer=$'\E['
  45. __variable="$1"
  46. shift
  47. while
  48. (( $# ))
  49. do
  50. __rvm_set_color_single "$1"
  51. shift
  52. if (( $# ))
  53. then __buffer+=';'
  54. fi
  55. done
  56. __buffer+='m'
  57. if [[ "${__variable}" == "" || "${__variable}" == "print" ]]
  58. then printf "${__buffer}"
  59. else eval "${__variable}='${__buffer}'"
  60. fi
  61. }
  62. # check if user wants colors and if output goes to terminal
  63. # rvm_pretty_print_flag:
  64. # - 0|no - disabled always
  65. # - 1|auto - automatic depending if the output goes to terminal (default)
  66. # - 2|force - forced always
  67. # to select which terminal output should be checked use first param:
  68. # - stdout - for stdout (default)
  69. # - stderr - for stderr
  70. # - number - for the given terminal fd
  71. # - else - for both stdout and stderr
  72. rvm_pretty_print()
  73. {
  74. case "${rvm_pretty_print_flag:=auto}" in
  75. (0|no)
  76. return 1
  77. ;;
  78. (1|auto)
  79. case "${TERM:-dumb}" in
  80. (dumb|unknown) return 1 ;;
  81. esac
  82. case "$1" in
  83. (stdout) [[ -t 1 ]] || return 1 ;;
  84. (stderr) [[ -t 2 ]] || return 1 ;;
  85. ([0-9]) [[ -t $1 ]] || return 1 ;;
  86. (any) [[ -t 1 || -t 2 ]] || return 1 ;;
  87. (*) [[ -t 1 && -t 2 ]] || return 1 ;;
  88. esac
  89. return 0
  90. ;;
  91. (2|force)
  92. return 0
  93. ;;
  94. esac
  95. }
  96. __rvm_set_colors()
  97. {
  98. case "${TERM:-dumb}" in
  99. (dumb|unknown)
  100. rvm_error_clr=""
  101. rvm_warn_clr=""
  102. rvm_debug_clr=""
  103. rvm_notify_clr=""
  104. rvm_code_clr=""
  105. rvm_comment_clr=""
  106. rvm_reset_clr=""
  107. ;;
  108. (*)
  109. __rvm_set_color rvm_error_clr "${rvm_error_color:-red}"
  110. __rvm_set_color rvm_warn_clr "${rvm_warn_color:-yellow}"
  111. __rvm_set_color rvm_debug_clr "${rvm_debug_color:-magenta}"
  112. __rvm_set_color rvm_notify_clr "${rvm_notify_color:-green}"
  113. __rvm_set_color rvm_code_clr "${rvm_code_color:-blue}"
  114. __rvm_set_color rvm_comment_clr "${rvm_comment_color:-iblack}"
  115. __rvm_set_color rvm_reset_clr "${rvm_reset_color:-reset}"
  116. ;;
  117. esac
  118. }
  119. __rvm_replace_colors()
  120. {
  121. \typeset ___text
  122. ___text="${1//<error>/$rvm_error_clr}"
  123. ___text="${___text//<warn>/$rvm_warn_clr}"
  124. ___text="${___text//<debug>/$rvm_debug_clr}"
  125. ___text="${___text//<notify>/$rvm_notify_clr}"
  126. ___text="${___text//<code>/$rvm_code_clr}"
  127. ___text="${___text//<comment>/$rvm_comment_clr}"
  128. ___text="${___text//<log>/$rvm_reset_clr}"
  129. ___text="${___text//<\/error>/$rvm_reset_clr}"
  130. ___text="${___text//<\/warn>/$rvm_reset_clr}"
  131. ___text="${___text//<\/debug>/$rvm_reset_clr}"
  132. ___text="${___text//<\/notify>/$rvm_reset_clr}"
  133. ___text="${___text//<\/code>/$rvm_reset_clr}"
  134. ___text="${___text//<\/comment>/$rvm_reset_clr}"
  135. ___text="${___text//<\/log>/$rvm_reset_clr}"
  136. printf "%b" "${___text}$rvm_reset_clr"
  137. }
  138. rvm_printf_to_stderr()
  139. {
  140. printf "$@" >&6
  141. }
  142. rvm_error()
  143. {
  144. if rvm_pretty_print stderr
  145. then __rvm_replace_colors "<error>$*</error>\n" >&6
  146. else printf "%b" "$*\n" >&6
  147. fi
  148. }
  149. rvm_help()
  150. {
  151. "${rvm_scripts_path}/help" "$@"
  152. }
  153. rvm_error_help()
  154. {
  155. rvm_error "$1"
  156. shift
  157. rvm_help "$@"
  158. }
  159. rvm_fail()
  160. {
  161. rvm_error "$1"
  162. exit "${2:-1}"
  163. }
  164. rvm_warn()
  165. {
  166. if rvm_pretty_print stderr
  167. then __rvm_replace_colors "<warn>$*</warn>\n" >&6
  168. else printf "%b" "$*\n" >&6
  169. fi
  170. }
  171. rvm_notify()
  172. {
  173. if rvm_pretty_print stdout
  174. then __rvm_replace_colors "<notify>$*</notify>\n"
  175. else printf "%b" "$*\n"
  176. fi
  177. }
  178. rvm_log()
  179. {
  180. [[ ${rvm_quiet_flag} == 1 ]] && return
  181. printf "%b" "$*\n"
  182. }
  183. rvm_debug()
  184. {
  185. (( ${rvm_debug_flag:-0} )) || return 0
  186. if rvm_pretty_print stderr
  187. then __rvm_replace_colors "<debug>$*</debug>\n" >&6
  188. else printf "%b" "$*\n" >&6
  189. fi
  190. }
  191. rvm_debug_stream()
  192. {
  193. if (( ${rvm_debug_flag:-0} == 0 && ${rvm_trace_flag:-0} == 0 ))
  194. then cat - >/dev/null # suppress output when not debugging/tracing
  195. elif rvm_pretty_print stdout
  196. then \command \cat - | __rvm_awk '{print "'"${rvm_debug_clr:-}"'"$0"'"${rvm_reset_clr:-}"'"}' >&6
  197. else \command \cat - >&6
  198. fi
  199. }
  200. rvm_verbose_log()
  201. {
  202. if (( ${rvm_verbose_flag:=0} == 1 ))
  203. then rvm_log "$@"
  204. fi
  205. }
  206. rvm_out()
  207. {
  208. printf "$*\n"
  209. }
  210. __rvm_set_colors
  211. # Redirect &6 to stderr
  212. exec 6>&2