std sh til-kron
[sunny256-utils.git] / logging
blob988d3cf30067c7e78208acdacf4e324618e6cbfc
1 #!/usr/bin/env bash
3 #=======================================================================
4 # logging
5 # File ID: af318500-fb9b-11dd-bb0f-000475e441b9
7 # Log the current terminal session with script(1)
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #=======================================================================
13 progname=logging
14 VERSION=0.1.3
16 ARGS="$(getopt -o "\
20 " -l "\
21 help,\
22 quiet,\
23 verbose,\
24 version,\
25 " -n "$progname" -- "$@")"
26 test "$?" = "0" || exit 1
27 eval set -- "$ARGS"
29 opt_help=0
30 opt_quiet=0
31 opt_verbose=0
32 while :; do
33 case "$1" in
34 -h|--help) opt_help=1; shift ;;
35 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
36 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
37 --version) echo $progname $VERSION; exit 0 ;;
38 --) shift; break ;;
39 *) echo $progname: Internal error >&2; exit 1 ;;
40 esac
41 done
42 opt_verbose=$(($opt_verbose - $opt_quiet))
44 export LOGDIR=$HOME/log/script/$(date -u +"%Y/%m")
46 if test "$opt_help" = "1"; then
47 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
48 cat <<END
50 Log the current terminal session with script(1) to
51 "$LOGDIR/".
53 Usage: $progname [options] [SESSION_NAME]
55 Options:
57 -h, --help
58 Show this help.
59 -q, --quiet
60 Be more quiet. Can be repeated to increase silence.
61 -v, --verbose
62 Increase level of verbosity. Can be repeated.
63 --version
64 Print version information.
66 END
67 exit 0
70 hex="[0-9A-Fa-f]"
71 v1_templ="$hex{8}-$hex{4}-1$hex{3}-$hex{4}-$hex{12}";
73 scriptprocs() {
74 ps auxw | grep -E "script .*/[0-9]{8}T[0-9]{6}Z\.$v1_templ\.$1\.scrlog"
77 kill_dups() {
78 local pids=$(scriptprocs $1 | tr -s ' ' | cut -f 2 -d ' ')
79 kill $pids
82 host=$1
83 test -z "$host" && host=$([ -f ~/.compname ] && cat ~/.compname || hostname)
85 runcheck="$(scriptprocs $host)"
86 if [ -n "$runcheck" ]; then
87 scrcount=$(echo "$runcheck" | wc -l)
88 if [ "$scrcount" = "1" ]; then
89 pl_s=""
90 pl_are="is"
91 pl_them="it"
92 else
93 pl_s="s"
94 pl_are="are"
95 pl_them="them"
97 echo -n $progname: $scrcount >&2
98 echo " typescript$pl_s with the same name $pl_are already running" >&2
99 echo -n "Kill $pl_them? " >&2
100 while :; do
101 echo -n "[y/n]: " >&2
102 read choice
103 [ "$choice" = "y" ] && { kill_dups $host; break; }
104 [ "$choice" = "n" ] && break
105 done
108 [ -d $LOGDIR/. ] || mkdir -p $LOGDIR || {
109 echo logging: $LOGDIR: Cannot create log directory >&2;
110 exit 1;
112 export LDATE=$(u)
113 uuid=$(suuid -t logging -w eo -c "logging $host") || {
114 echo logging: Error when generating UUID, logging not started >&2;
115 exit 1;
117 export LNAME="$LOGDIR/$LDATE.$uuid.$host"
118 SESS_UUID="${SESS_UUID}logging/$uuid," script -ft $LNAME.scrlog 2>$LNAME.timing
119 suuid -w e -t logging -c "Typescript session $uuid finished."
120 echo "Typescript session $uuid finished." >&2