+2
-2
.gitignore
+2
-2
.gitignore
+56
home/bin/moulog
+56
home/bin/moulog
···
1
+
#!/bin/sh
2
+
# A much better way to handle my logging
3
+
4
+
#- Settings -#
5
+
# Place to store log files
6
+
: "${MOULOG_DIR:=${XDG_CACHE_HOME:-$HOME/.cache}/moulog}"
7
+
# Max number of old dirs to keep
8
+
: "${MOULOG_MAX:=5}"
9
+
10
+
usage() {
11
+
while read -r line
12
+
do printf '%b\n' "$line"
13
+
done <<-EOF
14
+
${0##*/} [options] [command]
15
+
example: moulog -n
16
+
example: moulog wlsunset -l 0 -L 0
17
+
18
+
options:
19
+
-h | --help - display this message
20
+
-n | --new - create a new logging directory
21
+
- useful if being run multiple times in the background
22
+
23
+
environment vars:
24
+
MOULOG_DIR - place to store logs (default: $XDG_CACHE_HOME/moulog)
25
+
MOULOG_MAX - max number of old dirs to keep (default: 5)
26
+
EOF
27
+
28
+
exit "${1:-1}"
29
+
}
30
+
31
+
new_dir() {
32
+
touch /tmp/moulog
33
+
[ -d "$MOULOG_DIR.$MOULOG_MAX" ] && rm -r "$MOULOG_DIR.$MOULOG_MAX"
34
+
35
+
i=$((MOULOG_MAX))
36
+
until [ $i = 1 ]
37
+
do
38
+
[ -d "$MOULOG_DIR.$((i-1))" ] && mv "$MOULOG_DIR.$((i-1))" "$MOULOG_DIR.$i"
39
+
i=$((i-1))
40
+
done
41
+
42
+
[ -d "$MOULOG_DIR" ] && mv "$MOULOG_DIR" "$MOULOG_DIR.1"
43
+
mkdir -p "$MOULOG_DIR"
44
+
}
45
+
46
+
case $1 in
47
+
-h|--help ) usage 0 ;;
48
+
-n|--new ) new_dir; exit 0 ;;
49
+
esac
50
+
51
+
# I'm using this file to decide if i should make a new directory
52
+
[ -f /tmp/moulog ] || new_dir
53
+
54
+
55
+
printf '%s\n' "#### moulog start time: $(date +%s) ####" >> "$MOULOG_DIR/$1.log"
56
+
exec "$@" >> "$MOULOG_DIR/$1.log" 2>&1