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.
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
+}