From 6cf2a9da6789cfd9cb204371062e6c325efa38e6 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Sun, 9 Oct 2022 16:15:02 -0400 Subject: automatically log all tmux sessions if $HOME/.tmux-log-everything/ exists --- dot/bashrc | 2 ++ dot/local/bin/tmux-log-everything | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 dot/local/bin/tmux-log-everything diff --git a/dot/bashrc b/dot/bashrc index 36d6b6b..121ad51 100644 --- a/dot/bashrc +++ b/dot/bashrc @@ -60,6 +60,8 @@ memoize_retval() add_path -a /usr/games add_path "$HOME/.cabal/bin" "$HOME/.local/bin" "$HOME/bin" + tmux-log-everything + # if have stack; then # add_stack_binpath # fi diff --git a/dot/local/bin/tmux-log-everything b/dot/local/bin/tmux-log-everything new file mode 100755 index 0000000..1e7588b --- /dev/null +++ b/dot/local/bin/tmux-log-everything @@ -0,0 +1,40 @@ +#!/bin/sh + +# Inspired by https://github.com/tmux-plugins/tmux-logging via +# https://unix.stackexchange.com/questions/623054/how-to-automatically-log-all-my-tmux-sessions-to-a-file-in-directory-accordin +# but massively simplified. + +# The tmux-logging plugin usees a different global variable for each pane (named +# after the pane number) to check whether it is already logging. When the pane +# is destroyed, the variable stays set. When a new pane is opened, it can +# recycle the same pane number as before. The plugin will not log to any pane +# with a recycled pane number! +# +# Luckily, tmux supports pane-local variables, which are automatically destroyed +# when the pane is destroyed! So here we just use one of those. + +tmux_log_everything() +{ + if ! [ "$TMUX_PANE" ] + then + return # We are not in tmux + fi + + local LOGDIR LOGFILE + LOGDIR=$HOME/.tmux-log-everything + if ! [ -d "$LOGDIR" ] + then + return # User has not "requested" logging by creating log dir + fi + + if [ "$(tmux show-option -pqv @pane-is-logging)" ] + then + return + fi + + LOGFILE=$LOGDIR/$(date -Is).$$.log.gz + tmux set-option -pq @pane-is-logging y + tmux pipe-pane "gzip -c > $LOGFILE" +} + +tmux_log_everything -- cgit v1.2.3