]> git.corax.cc Git - foundry/commitdiff
include/context: Add method to log directly to a context
authorMatthias Kruk <m@m10k.eu>
Sun, 8 Aug 2021 02:20:08 +0000 (11:20 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 8 Aug 2021 02:20:08 +0000 (11:20 +0900)
When logging messages to a context, a process needs to write messages
to a logfile and then add the logfile to a context. This makes the
control flow rather complicated because temporary files need to be
created, written to, and removed correctly when errors occur.
This commit adds the foundry_context_log() method that allows the
caller to append messages directly to a logfile.

include/context.sh

index 90432d3717954bd8f3eda1767e19654c3a75cdd8..6bddd2ed984fe5ee293c822bd5050649240d6e81 100644 (file)
@@ -159,3 +159,31 @@ foundry_context_get_logs() {
        array_to_lines "${logs[@]}"
        return 0
 }
+
+foundry_context_log() {
+       local context="$1"
+       local logtype="$2"
+       local messages=("${@:3}")
+
+       local logdir
+       local logname
+
+       logdir="$__foundry_context_root/$context/logs/$logtype"
+       logname="$logdir/default.log"
+
+       if ! mkdir -p "$logdir"; then
+               return 1
+       fi
+
+       if (( ${#messages[@]} > 0 )); then
+               if ! array_to_lines "${messages[@]}" >> "$logname"; then
+                       return 1
+               fi
+       else
+               if ! cat /dev/stdin >> "$logname"; then
+                       return 1
+               fi
+       fi
+
+       return 0
+}