db-foo: Permanently adding bash -x tracing
Related to the "db-move crashed halfway" issue in IRC yesterday:
14:43 (anthraxx|M) grawity: can you open an issue and dump the info in there.
Instead of "hold on let me reproduce with bash -x", the -x
tracing could be permanently enabled and redirected to a file using BASH_XTRACEFD
so that it will not clutter stderr during normal operation, but still leaves trace files in case the script dies unexpectedly.
-
Open a file descriptor for the log file, set
BASH_XTRACEFD
to its fd number:exec {BASH_XTRACEFD}>> "$HOME/dbscripts.trace"
or with a hand-picked fd number:
exec 42>> ~/dbscripts.trace BASH_XTRACEFD=42
(My original idea was to deliberately keep writing to the same trace file, so that if one script invokes another the result will be a linear trace instead of several split files, but OTOH that might be undesirable. Still,
mktemp
randomization seems unnecessary,$HOME/dbscripts-$$.trace
should do the job.) -
Also have the scripts set their
PS4
to something fancy, in order to log not just the command but also the origin file:line:function.PS4='+\e[34m${BASH_SOURCE:--}:\e[1m$LINENO\e[m:${FUNCNAME:+\e[33m$FUNCNAME()\e[m} '
-
Actually enable tracing:
set -x
Foxboron's patch from yesterday:
diff --git a/db-functions b/db-functions
index 35f080c..18f6f78 100644
--- a/db-functions
+++ b/db-functions
@@ -547,3 +547,9 @@ check_reproducible() {
}
. "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")/db-functions-${VCS}"
+
+if ((DBSCRIPTS_TRACE)); then
+ PS4='+\e[34m${BASH_SOURCE:--}:\e[1m$LINENO\e[m:${FUNCNAME:+\e[33m$FUNCNAME\e[m} '
+ exec {BASH_XTRACEFD}>>"/tmp/dbtrace-$USER.trace"
+ set -x
+fi