summaryrefslogtreecommitdiffstats
path: root/modules/log.bash
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2017-03-22 21:07:10 +0100
committerclsr <clsr@clsr.net>2017-03-22 21:07:10 +0100
commitff2bc5d0931d4a460d4d74183b6f9dd9394c40a0 (patch)
tree13dbf5a1395ae4e3dcd458bf114d962b28a8657c /modules/log.bash
downloadmbibot-ff2bc5d0931d4a460d4d74183b6f9dd9394c40a0.tar.gz
mbibot-ff2bc5d0931d4a460d4d74183b6f9dd9394c40a0.zip
Initial commitv0.1.0
Diffstat (limited to 'modules/log.bash')
-rw-r--r--modules/log.bash44
1 files changed, 44 insertions, 0 deletions
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
+}