kvz / bash3boilerplate

Templates to write better Bash scripts
http://bash3boilerplate.sh
MIT License
2.12k stars 198 forks source link

Improve debug mode #103

Closed QwertyZW closed 5 years ago

QwertyZW commented 6 years ago

Allow bash3boilerplate to provide the ultimate debugging experience. PS4 provided by http://wiki.bash-hackers.org/scripting/debuggingtips

virgilwashere commented 5 years ago

Allow bash3boilerplate to provide the ultimate debugging experience.

If you want add timestamps and some colour to that:

set_debug_prompt() {
  # ------------------------------------------------------------------
  # | PS4 - Debug prompt string  (when using `set -x`)               |
  # ------------------------------------------------------------------
  #
  # When debugging a shell script via `set -x` this tricked-out prompt is used.
  # \011 - tab

  # The first character (+) is used and repeated for stack depth.
  # Then, we log the current time, filename and line number,
  # followed by function name, followed by actual source line.

  # [!] Don't change to double quotes
  export PS4='+\011\e[1;30m\t  \e[1;34m${BASH_SOURCE}\e[0m:\e[1;36m${LINENO}\e[0m \011 ${FUNCNAME[0]:+\e[0;35m${FUNCNAME[0]}\e[1;30m()\e[0m:\011 }'

  # if debug, but not colour, back to this one:
  # export PS4='+\011\t  ${BASH_SOURCE}:${LINENO} \011 ${FUNCNAME[0]:+${FUNCNAME[0]}():\011 }'

}

main() {
  set_debug_prompt
}

main

# Cleanup.
unset -f set_debug_prompt
unset -f main
kvz commented 5 years ago

Hey that's pretty cool! Sorry for not noticing earlier! Merging this. I'd be open to accepting a PR for colors, too!

virgilwashere commented 5 years ago

Hey that's pretty cool! I'd be open to accepting a PR for colors, too!

Working on it. Testing to see if I can (and should) include colour prompt in the error backtracing TRAP.

zbeekman commented 5 years ago

@kvz FYI something is failing on CI

virgilwashere commented 5 years ago

Will take this to #105 for followup and closure.