diff options
author | Andrew Cady <d@jerkface.net> | 2022-10-09 16:15:02 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2022-10-09 16:27:00 -0400 |
commit | 6cf2a9da6789cfd9cb204371062e6c325efa38e6 (patch) | |
tree | c511b24164614cfe101da5bbd47408a2684ea7b1 /dot/local | |
parent | 3e888e59b3add9a9a5e7418c647654b559751bdb (diff) |
automatically log all tmux sessions if $HOME/.tmux-log-everything/ exists
Diffstat (limited to 'dot/local')
-rwxr-xr-x | dot/local/bin/tmux-log-everything | 40 |
1 files changed, 40 insertions, 0 deletions
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 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | # Inspired by https://github.com/tmux-plugins/tmux-logging via | ||
4 | # https://unix.stackexchange.com/questions/623054/how-to-automatically-log-all-my-tmux-sessions-to-a-file-in-directory-accordin | ||
5 | # but massively simplified. | ||
6 | |||
7 | # The tmux-logging plugin usees a different global variable for each pane (named | ||
8 | # after the pane number) to check whether it is already logging. When the pane | ||
9 | # is destroyed, the variable stays set. When a new pane is opened, it can | ||
10 | # recycle the same pane number as before. The plugin will not log to any pane | ||
11 | # with a recycled pane number! | ||
12 | # | ||
13 | # Luckily, tmux supports pane-local variables, which are automatically destroyed | ||
14 | # when the pane is destroyed! So here we just use one of those. | ||
15 | |||
16 | tmux_log_everything() | ||
17 | { | ||
18 | if ! [ "$TMUX_PANE" ] | ||
19 | then | ||
20 | return # We are not in tmux | ||
21 | fi | ||
22 | |||
23 | local LOGDIR LOGFILE | ||
24 | LOGDIR=$HOME/.tmux-log-everything | ||
25 | if ! [ -d "$LOGDIR" ] | ||
26 | then | ||
27 | return # User has not "requested" logging by creating log dir | ||
28 | fi | ||
29 | |||
30 | if [ "$(tmux show-option -pqv @pane-is-logging)" ] | ||
31 | then | ||
32 | return | ||
33 | fi | ||
34 | |||
35 | LOGFILE=$LOGDIR/$(date -Is).$$.log.gz | ||
36 | tmux set-option -pq @pane-is-logging y | ||
37 | tmux pipe-pane "gzip -c > $LOGFILE" | ||
38 | } | ||
39 | |||
40 | tmux_log_everything | ||