Browse Source

initial proof of concept zsh/bash

Daniel Sheffield 4 months ago
commit
bf54321ae5
2 changed files with 83 additions and 0 deletions
  1. 45 0
      autolog.sh
  2. 38 0
      recall.sh

+ 45 - 0
autolog.sh

@@ -0,0 +1,45 @@
+#!/bin/bash
+set -euo pipefail
+if [ "$#" == "0" ]
+then
+	echo capture
+	exit 0
+fi
+LOG_ROOT="$HOME"/.local/var/log/shell # source this
+
+SUBPROG=""
+PROG="$1"
+if [ "$PROG" == "sudo" ]
+then
+	SUBPROG="$2"
+fi
+
+TIMESTAMP=$(date +%Y%m%d_%H%M%S_%N)
+LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"/"$TIMESTAMP"
+LOG_IDIR="$LOG_ROOT"/"$TIMESTAMP"/"$PROG"/"$SUBPROG"
+
+mkdir -p "$LOG_DIR" "${LOG_IDIR%/*}"
+ln -s "$LOG_DIR" "$LOG_IDIR"
+
+OUT="$LOG_DIR"/stdout
+ERR="$LOG_DIR"/stderr
+DAT="$LOG_DIR"/dat
+INF="$LOG_DIR"/info
+CMD="$@"
+
+info(){
+	cat <<EOF > "$INF"
+$CMD
+status=$?
+EOF
+	exec 2>&-
+	exec 1>&-
+	wait "$err_pid" "$out_pid"
+}
+
+trap 'info' EXIT
+exec 2> >(tee -a "$DAT" "$ERR")
+err_pid="$!"
+exec 1> >(tee -a "$DAT" "$OUT")
+out_pid="$!"
+"$@"

+ 38 - 0
recall.sh

@@ -0,0 +1,38 @@
+#!/bin/bash
+set -euo pipefail
+if [ "$#" == "0" ]
+then
+	echo recall
+	exit 0
+fi
+LOG_ROOT="$HOME"/.local/var/log/shell # source this
+
+SUBPROG=""
+PROG="$1"
+
+#TODO: eval, really?
+#      not fully working anyway (aliases, functions)
+#while [ "$(eval "$PROG")" == "capture" ]
+#do
+	#TODO: eval not sufficient if
+	#      capture is aliased
+	#      or invoked by a function
+#	shift 1; PROG=$1;
+#done
+
+# TODO: and maybe su, runuser et al
+if [ "$PROG" == "sudo" ]
+then
+	SUBPROG="$2"
+fi
+
+LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"
+LOG_DIR="$LOG_DIR"/"$(ls -1 "$LOG_DIR" | sort -V | tail -n1)"
+
+OUT="$LOG_DIR"/stdout
+ERR="$LOG_DIR"/stderr
+DAT="$LOG_DIR"/dat
+INF="$LOG_DIR"/info
+. <(tail -n +2 "$INF")
+cat "$OUT"
+exit "$status"