utility_logging 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/usr/bin/env bash
  2. __rvm_log_command_caclulate_log_timestamp()
  3. {
  4. export rvm_log_timestamp="$(__rvm_date "+%s")"
  5. rvm_debug "Log prefix: ${rvm_log_path}/${rvm_log_timestamp}${rvm_ruby_string:+_}${rvm_ruby_string:-}/"
  6. }
  7. __rvm_log_command_caclulate_log_filesystem()
  8. {
  9. export rvm_log_filesystem="$(
  10. __rvm_mount 2>/dev/null | __rvm_awk -v rvm_path=$rvm_path '
  11. BEGIN{longest=""; fstype=""}
  12. {if (index(rvm_path,$3)==1 && length($3)>length(longest)){longest=$3; fstype=$5}}
  13. END{print fstype}
  14. '
  15. )"
  16. rvm_debug "Log filesystem: ${rvm_log_filesystem}"
  17. }
  18. __rvm_log_command_caclulate_log_namelen()
  19. {
  20. case "${rvm_log_filesystem}" in
  21. (ecryptfs) export rvm_log_namelen=138 ;;
  22. (*) export rvm_log_namelen=250 ;;
  23. esac
  24. rvm_debug "Log max name length: ${rvm_log_namelen}"
  25. }
  26. __rvm_log_command_caclulate_log_file_name()
  27. {
  28. [[ -n "${rvm_log_timestamp:-}" ]] || __rvm_log_command_caclulate_log_timestamp
  29. [[ -n "${rvm_log_filesystem:-}" ]] || __rvm_log_command_caclulate_log_filesystem
  30. [[ -n "${rvm_log_namelen:-}" ]] || __rvm_log_command_caclulate_log_namelen
  31. name="${name//[ \/]/_}"
  32. _log="${rvm_log_path}/${rvm_log_timestamp}${rvm_ruby_string:+_}${rvm_ruby_string:-}/${name}"
  33. if [[ -n "${ZSH_VERSION:-}" ]]
  34. then _log="${_log[0,${rvm_log_namelen}]}.log"
  35. else _log="${_log:0:${rvm_log_namelen}}.log"
  36. fi
  37. }
  38. # Run a specified command and log it (once).
  39. __rvm_log_command()
  40. {
  41. \typeset name message _command_start _command_name
  42. \typeset -a _command
  43. name="${1:-}"
  44. message="${2:-}"
  45. shift 2
  46. _command=( "$@" ) # store full command so we can manipulate it
  47. _command_start="$1" # store first part so we can detect variables
  48. while (( $# )) && [[ "$1" == *"="* ]] # skip variables from beginning
  49. do shift
  50. done
  51. _command_name="$1" # store the real command so we can detect functions
  52. [[ "${_command_start}" != *"="* ]] || _command=( "env" "${_command[@]}" )
  53. if __function_on_stack __rvm_log_command_internal
  54. then __rvm_log_command_simple "$@" || return $?
  55. else __rvm_log_command_internal "$@" || return $?
  56. fi
  57. }
  58. # basic debugging information for the command
  59. __rvm_log_command_debug()
  60. {
  61. printf "%b" "[$(__rvm_date +'%Y-%m-%d %H:%M:%S')] ${_command_name}\n"
  62. if is_a_function "${_command_name}"
  63. then \typeset -f "${_command_name}"
  64. fi
  65. printf "%b" "current path: $PWD\n"
  66. env | __rvm_grep -E '^GEM_HOME=|^GEM_PATH=|^PATH='
  67. printf "%b" "command(${#_command[@]}): ${_command[*]}\n"
  68. }
  69. # Run a specified command and do not log it again.
  70. __rvm_log_command_simple()
  71. {
  72. __rvm_log_command_debug
  73. rvm_log "$message"
  74. "$@" || return $?
  75. }
  76. # Run a specified command and log it.
  77. __rvm_log_command_internal()
  78. {
  79. \typeset _log
  80. (( ${rvm_niceness:-0} == 0 )) || _command=( nice -n $rvm_niceness "${_command[@]}" )
  81. __rvm_log_command_caclulate_log_file_name
  82. rvm_debug "Log file: ${_log}"
  83. [[ -d "${_log%\/*}" ]] || \command \mkdir -p "${_log%\/*}"
  84. [[ -f "${_log}" ]] || \command \rm -f "${_log}"
  85. __rvm_log_command_debug | tee "${_log}" | rvm_debug_stream
  86. __rvm_log_dotted "${_log}" "$message" "${_command[@]}" ||
  87. {
  88. \typeset result=$?
  89. \typeset __show_lines="${rvm_show_log_lines_on_error:-0}"
  90. rvm_error "Error running '${_command[*]}',"
  91. case "${__show_lines}" in
  92. (0)
  93. rvm_error "please read ${_log}"
  94. ;;
  95. (all)
  96. rvm_error "content of log ${_log}"
  97. cat "${_log}" >&6
  98. ;;
  99. (*)
  100. rvm_error "showing last ${__show_lines} lines of ${_log}"
  101. tail -n "${__show_lines}" "${_log}" >&6
  102. ;;
  103. esac
  104. return ${result}
  105. }
  106. }
  107. __rvm_debug_command()
  108. {
  109. rvm_debug "Running($#): $*"
  110. "$@" || return $?
  111. }
  112. __rvm_pager_or_cat_v()
  113. {
  114. eval "${PAGER:-\command \cat} '$1'"
  115. }
  116. __rvm_ask_for()
  117. {
  118. \typeset response
  119. rvm_warn "$1"
  120. printf "%b" "(anything other than '$2' will cancel) > "
  121. # need and IF to properly handle CTRL+C ... wtf?
  122. if read response && [[ "$2" == "$response" ]]
  123. then return 0
  124. else return 1
  125. fi
  126. }
  127. __rvm_dotted()
  128. {
  129. set +x
  130. \typeset flush
  131. if (( $# ))
  132. then printf "%b" "${rvm_notify_clr:-}$*${rvm_reset_clr:-}"
  133. fi
  134. if __rvm_awk '{fflush;}' <<<EO 2>/dev/null
  135. then flush=fflush
  136. else flush=flush
  137. fi
  138. # the nasty '${flush}' inside is for compatibility,
  139. # old awk does not allow running function from variable
  140. awk -v go_back="$( tput cub1 2>/dev/null || true)" \
  141. '
  142. BEGIN{
  143. spin[0]="|"go_back;
  144. spin[1]="/"go_back;
  145. spin[2]="-"go_back;
  146. spin[3]="\\"go_back }
  147. {
  148. if ((NR-1)%(10)==9)
  149. printf ".";
  150. else
  151. if (go_back!="") printf spin[(NR-1)%4];
  152. '${flush}' }
  153. END{
  154. print "." }
  155. '
  156. }
  157. __rvm_log_dotted()
  158. {
  159. \typeset __log_file __message __iterator __result __local_rvm_trace_flag
  160. __log_file="$1"
  161. __message="$2"
  162. shift 2
  163. __result=0
  164. __local_rvm_trace_flag=${rvm_trace_flag:-0}
  165. if
  166. (( ${rvm_trace_flag:-0} ))
  167. then
  168. {
  169. set -x
  170. "$@" 2>&1 | tee -a "${__log_file}"
  171. __rvm_check_pipestatus ${PIPESTATUS[@]} ${pipestatus[@]} || __result=$?
  172. (( __local_rvm_trace_flag > 0 )) || set +x
  173. } 1>&2
  174. elif
  175. [[ -n "${ZSH_VERSION:-}" ]]
  176. then
  177. rvm_log "${__message} - please wait"
  178. {
  179. set -x
  180. "$@" > "${__log_file}" 2>&1 || __result=$?
  181. (( __local_rvm_trace_flag > 0 )) || set +x
  182. } 2>/dev/null
  183. else
  184. {
  185. set -x
  186. "$@" 2>&1 | tee -a "${__log_file}" | __rvm_dotted "${__message}"
  187. __rvm_check_pipestatus ${PIPESTATUS[@]} ${pipestatus[@]} || __result=$?
  188. (( __local_rvm_trace_flag > 0 )) || set +x
  189. } 2>/dev/null
  190. fi
  191. return $__result
  192. }
  193. __rvm_check_pipestatus()
  194. {
  195. for __iterator
  196. do
  197. case "${__iterator}" in
  198. ("") true ;;
  199. (0) true ;;
  200. (*) return ${__iterator} ;;
  201. esac
  202. done
  203. return 0
  204. }
  205. __rvm_wait_anykey()
  206. {
  207. if [[ -n "${1:-}" ]]
  208. then echo "$1"
  209. fi
  210. \typeset _read_char_flag
  211. if [[ -n "${ZSH_VERSION:-}" ]]
  212. then _read_char_flag=k
  213. else _read_char_flag=n
  214. fi
  215. builtin read -${_read_char_flag} 1 -s -r anykey
  216. }
  217. __rvm_table_br()
  218. {
  219. \typeset width=${COLUMNS:-78}
  220. width=$(( width > 116 ? 116 : width ))
  221. printf "%-${width}s\n" " " | __rvm_sed 's/ /*/g'
  222. }
  223. __rvm_fold()
  224. {
  225. if fold -s -w 10 <<<bla >/dev/null
  226. then fold -s -w $1
  227. else fold -w $1
  228. fi
  229. }
  230. __rvm_table_wrap_text()
  231. {
  232. \typeset width=${COLUMNS:-78}
  233. width=$(( width > 116 ? 116 : width ))
  234. width=$(( width - 4 )) # "* <content> *"
  235. __rvm_fold $width | __rvm_awk -v width=$width '{printf "* %-"width"s *\n", $0}'
  236. }
  237. # echo text | __rvm_table [header]
  238. __rvm_table()
  239. {
  240. if
  241. [[ -n "${1:-}" ]]
  242. then
  243. __rvm_table_br
  244. echo "$1" | __rvm_table_wrap_text
  245. fi
  246. __rvm_table_br
  247. \command \cat "${2:--}" | __rvm_table_wrap_text
  248. __rvm_table_br
  249. }