From b62aea45068ffc589e88d78db9549cc42015564f Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sun, 8 Aug 2021 11:20:08 +0900 Subject: [PATCH] include/context: Add method to log directly to a context 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 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/context.sh b/include/context.sh index 90432d3..6bddd2e 100644 --- a/include/context.sh +++ b/include/context.sh @@ -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 +} -- 2.47.3