summaryrefslogtreecommitdiff
path: root/dot/local/bin/tmux-log-everything
blob: 1e7588b15e5a0e12336d2b09a6057bbef44328a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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