summaryrefslogtreecommitdiffstats
path: root/modules/log.bash
blob: cd4e0380ec561f3d9f4544e64b0524ee18b6d71f (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
41
42
43
44
# modules/log.bash
#
# Logs IRC messages and bot debug/error output.
#
# Settings:
# LOG_IRC: log file for IRC protocol traffic
# LOG_ERR: log file for stderr messages
# LOG_SHOW_IRC: set to non-zero to show IRC output on stdout


if [ -z "$IRCBOT_MODULE" ]; then
	printf "error: %s is a module for ircbot.bash and should not be run separately\n" "$0"
	exit 1
fi


if [[ -n ${LOG_IRC:-} ]]; then
	if [[ ${LOG_SHOW_IRC:-0} -ne 0 ]]; then
		exec {log_fd}> >(tee -a -- "$LOG_IRC")
	else
		exec {log_fd}>>"$LOG_IRC"
	fi
elif [[ ${LOG_SHOW_IRC:-0} -ne 0 ]]; then
	exec {log_fd}>&1
fi

if [[ -n ${LOG_ERR:-} ]]; then
	exec 2> >(tee -a -- "$LOG_ERR" >&2)
fi


# log received messages
on_readmsg() { # args: $1 - raw message, $2 - source, $3 - command, $4... - args
	printf "%s <<< %s\n" "$(log_timestamp)" "$1" >&$log_fd
}

# log sent messages
on_sendmsg() { # args: $1 - raw message, $2 - command, $3... - args
	printf "%s >>> %s\n" "$(log_timestamp)" "$1" >&$log_fd
}

log_timestamp() {
	TZ=UTC date -u +%s.%N
}