From ff2bc5d0931d4a460d4d74183b6f9dd9394c40a0 Mon Sep 17 00:00:00 2001 From: clsr Date: Wed, 22 Mar 2017 21:07:10 +0100 Subject: Initial commit --- modules/log.bash | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 modules/log.bash (limited to 'modules/log.bash') diff --git a/modules/log.bash b/modules/log.bash new file mode 100644 index 0000000..e9fc665 --- /dev/null +++ b/modules/log.bash @@ -0,0 +1,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 14> >(tee -a -- "$LOG_IRC") + else + exec 14>>"$LOG_IRC" + fi +elif [[ ${LOG_SHOW_IRC:-0} -ne 0 ]]; then + exec 14>&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" >&14 +} + +# log sent messages +on_sendmsg() { # args: $1 - raw message, $2 - command, $3... - args + printf "%s >>> %s\n" "$(log_timestamp)" "$1" >&14 +} + +log_timestamp() { + TZ=UTC date -u +%s.%N +} -- cgit