Skip to content

Commit a6aaf5c

Browse files
Display system info before the stack trace (#2347)
* Display system info before the stack trace * refactor: improve log file truncation using tee * Update menu_main.sh * Remove duplicate `indent_text` function * Fix mis-spelling of `architecture` * Fix the system info not being read * Update docs/basics/command-line-usage.md Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --------- Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
1 parent 9486b9b commit a6aaf5c

File tree

2 files changed

+111
-30
lines changed

2 files changed

+111
-30
lines changed

docs/basics/command-line-usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ You may also need to fill in or adjust any other variables prefixed with the `<A
7676

7777
This is the best place to change the app's external default ports.
7878

79-
There will also be an application specific variable file created at `~/.dockstarter/compose/.env.app.<appname>`. This may or not be populated with variables. The variables included, if any, will entierly depend on the application installed. If you need to edit the values of the variables, or include any new variables for the app, it is entirely safe to do so.
79+
There will also be an application specific variable file created at `~/.dockstarter/compose/.env.app.<appname>`. This may or may not be populated with variables. The variables included, if any, will entirely depend on the application installed. If you need to edit the values of the variables, or include any new variables for the app, it is entirely safe to do so.
8080

8181
#### Removing Apps
8282

main.sh

Lines changed: 110 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bash
22
shopt -s extdebug
3+
set +o posix
34
set -Eeuo pipefail
45
IFS=$'\n\t'
56

@@ -175,6 +176,78 @@ declare -Agr C=( # Pre-defined colors
175176
["UsageVar"]="${F[M]}"
176177
)
177178

179+
indent_text() {
180+
local -i IndentSize=${1}
181+
shift
182+
pr -e -t -o ${IndentSize} <<< "$(
183+
printf "%s\n" "$@"
184+
)"
185+
}
186+
187+
get_system_info() {
188+
local -a Output=()
189+
190+
Output+=(
191+
"${C["Version"]-}${APPLICATION_NAME-}${NC-} [${C["Version"]-}${APPLICATION_VERSION-}${NC-}]"
192+
""
193+
"Currently running as: $0 (PID $$)"
194+
"Shell name from /proc/$$/exe: $(readlink /proc/$$/exe)"
195+
""
196+
"ARCH: ${ARCH-}"
197+
"SCRIPTPATH: ${SCRIPTPATH-}"
198+
"SCRIPTNAME: ${SCRIPTNAME-}"
199+
""
200+
"DETECTED_PUID: ${DETECTED_PUID-}"
201+
"DETECTED_UNAME: ${DETECTED_UNAME-}"
202+
"DETECTED_PGID: ${DETECTED_PGID-}"
203+
"DETECTED_UGROUP: ${DETECTED_UGROUP-}"
204+
"DETECTED_HOMEDIR: ${DETECTED_HOMEDIR-}"
205+
)
206+
207+
Output+=(
208+
""
209+
"${C["RunningCommand"]}echo \${BASH_VERSION}${NC}:"
210+
"${BASH_VERSION-}"
211+
)
212+
213+
[[ -f /etc/os-release ]] &&
214+
Output+=(
215+
""
216+
"${C["RunningCommand"]}cat /etc/os-release${NC}:"
217+
"$(cat /etc/os-release)"
218+
)
219+
220+
[[ -f /etc/lsb-release ]] &&
221+
Output+=(
222+
""
223+
"${C["RunningCommand"]}cat /etc/lsb-release${NC}:"
224+
"$(cat /etc/lsb-release)"
225+
)
226+
227+
command -v lsb_release &> /dev/null &&
228+
Output+=(
229+
""
230+
"${C["RunningCommand"]}lsb_release -a${NC}:"
231+
"$(lsb_release -a)"
232+
)
233+
234+
command -v uname &> /dev/null &&
235+
Output+=(
236+
""
237+
"${C["RunningCommand"]}uname -a${NC}:"
238+
"$(uname -a)"
239+
)
240+
241+
command -v system_profiler &> /dev/null &&
242+
Output+=(
243+
""
244+
"${C["RunningCommand"]}system_profiler SPSoftwareDataType${NC}:"
245+
"$(system_profiler SPSoftwareDataType)"
246+
)
247+
248+
printf '%s\n' "${Output[@]}"
249+
}
250+
178251
# Log Functions
179252
MKTEMP_LOG=$(mktemp -t "${APPLICATION_NAME}.log.XXXXXXXXXX") || echo -e "Failed to create temporary log file.\nFailing command: ${C["FailingCommand"]}mktemp -t \"${APPLICATION_NAME}.log.XXXXXXXXXX\""
180253
readonly MKTEMP_LOG
@@ -220,7 +293,7 @@ warn() { log true "$(timestamped_log "${C["Warn"]-}[WARN ]${NC-}" "$@")"; }
220293
error() { log true "$(timestamped_log "${C["Error"]-}[ERROR ]${NC-}" "$@")"; }
221294
fatal_notrace() {
222295
local LogMessage
223-
LogMessage="$(timestamped_log "${C["Fatal"]-}[FATAL ]${NC}" "$@")"
296+
LogMessage=$(timestamped_log "${C["Fatal"]-}[FATAL ]${NC}" "$@")
224297
log true "${LogMessage}"
225298
if declare -F strip_ansi_colors > /dev/null; then
226299
LogMessage=$(strip_ansi_colors "${LogMessage-}")
@@ -230,7 +303,12 @@ fatal_notrace() {
230303
}
231304
fatal() {
232305
local -i thisFuncLine=$((LINENO - 1))
306+
233307
local -a Stack=()
308+
309+
readarray -t Stack < <(get_system_info)
310+
Stack+=("")
311+
234312
local StackSize=${#FUNCNAME[@]}
235313
local -i FrameNumberLength=${#StackSize}
236314
local NoFile="<nofile>"
@@ -244,17 +322,19 @@ fatal() {
244322
local -i CmdArgCount=0
245323
local indent=""
246324
local FramePrefix=" "
325+
local -i StartFrame=1
247326
for ((Frame = 0; Frame <= StackSize; Frame++)); do
248-
local StackLineFormat=" ${C["TraceFrameNumber"]}%${FrameNumberLength}d${NC}:${indent}${FramePrefix}${C["TraceSourceFile"]}%s${NC}:${C["TraceLineNumber"]}%d${NC} (${C["TraceFunction"]}%s${NC})"
327+
local StackLineFormat="${C["TraceFrameNumber"]}%${FrameNumberLength}d${NC}:${indent}${FramePrefix}${C["TraceSourceFile"]}%s${NC}:${C["TraceLineNumber"]}%d${NC} (${C["TraceFunction"]}%s${NC})"
328+
329+
if [[ Frame -ge StartFrame ]]; then
330+
# shellcheck disable=SC2059 # Don't use variables in the printf format string.
331+
Stack+=(
332+
"$(printf "${StackLineFormat}" "${Frame}" "${SourceFile}" "${line}" "${func}")"
333+
)
334+
fi
249335

250-
# shellcheck disable=SC2059 # Don't use variables in the printf format string.
251-
Stack+=(
252-
"$(
253-
printf "${StackLineFormat}" "${Frame}" "${SourceFile}" "${line}" "${func}"
254-
)"
255-
)
256336
if [[ -n ${cmd-} ]]; then
257-
local FrameCmdPrefix="${C["TraceFrameLines"]}${NC}"
337+
local FrameCmdPrefix="${C["TraceFrameLines"]}${NC}"
258338
local FrameArgPrefix="${C["TraceFrameLines"]}${NC}"
259339
local cmdString="${C["TraceCmd"]}${cmd}${NC}"
260340
local -a cmdArray=()
@@ -267,35 +347,36 @@ fatal() {
267347
cmdArgString="${cmdArgString//\\/\\\\}"
268348
cmdArgString="${NC}«${C["TraceCmdArgs"]}${cmdArgString}${NC}»"
269349
while read -r cmdLine; do
270-
cmdArray+=("${FrameArgPrefix}${C["TraceCmdArgs"]}${cmdLine}")
350+
cmdArray+=(
351+
"${FrameArgPrefix}${C["TraceCmdArgs"]}${cmdLine}"
352+
)
271353
done <<< "${cmdArgString}"
272354
done
273355
fi
274-
local StackCmdIndent
275-
StackCmdIndent="$(printf " %${FrameNumberLength}s${indent} " "")"
276-
local cmdLines
277-
cmdLines="$(
278-
pr -t -o ${#StackCmdIndent} <<< "$(
279-
printf "%s\n" "${cmdArray[@]}"
280-
)"
281-
)"
282-
# shellcheck disable=SC2059 # Don't use variables in the printf format string.
283-
Stack+=("${cmdLines}")
356+
if [[ Frame -ge StartFrame ]]; then
357+
local -i StackCmdIndent
358+
StackCmdIndent=$((FrameNumberLength + 1 + ${#indent} + 2))
359+
Stack+=(
360+
"$(indent_text ${StackCmdIndent} "${cmdArray[@]}")"
361+
)
362+
fi
284363
fi
285364
SourceFile="${BASH_SOURCE[Frame + 1]:-$NoFile}"
286365
line="${BASH_LINENO[Frame]:-0}"
287366
func="${FUNCNAME[Frame + 1]:-$NoFunction}"
288367
cmd="${FUNCNAME[Frame]:-$NoFunction}"
289368
CurrentArg+=${CmdArgCount}
290369
CmdArgCount=${BASH_ARGC[Frame]-}
291-
indent+=" "
292-
FramePrefix="${C["TraceFrameLines"]}└─${NC}"
370+
if [[ Frame -ge StartFrame ]]; then
371+
indent+=" "
372+
FramePrefix="${C["TraceFrameLines"]}└─${NC}"
373+
fi
293374
done
294375

295376
fatal_notrace \
296-
"${C["TraceHeader"]}### BEGIN STACK TRACE ###" \
297-
"${Stack[@]}" \
298-
"${C["TraceFooter"]}### END STACK TRACE ###" \
377+
"${C["TraceHeader"]}### BEGIN SYSTEM INFORMATION AND STACK TRACE ###" \
378+
"$(indent_text 2 "${Stack[@]}")" \
379+
"${C["TraceFooter"]}### END SYSTEM INFORMATION AND STACK TRACE ###" \
299380
"" \
300381
"$@" \
301382
"" \
@@ -315,10 +396,10 @@ fatal() {
315396

316397
# Check for supported CPU architecture
317398
check_arch() {
318-
if [[ ${ARCH} != "aarch64" ]] && [[ ${ARCH} != "x86_64" ]]; then
399+
if [[ ${ARCH} != "arm64" ]] && [[ ${ARCH} != "aarch64" ]] && [[ ${ARCH} != "x86_64" ]]; then
319400
fatal_notrace \
320-
"Unsupported architeture." \
321-
"Supported architetures are 'aarch64' or 'x86_64', running architeture is '${ARCH}'."
401+
"Unsupported architecture." \
402+
"Supported architectures are 'aarch64' or 'x86_64', running architecture is '${ARCH}'."
322403
fi
323404
}
324405

@@ -373,7 +454,7 @@ cleanup() {
373454
if [[ -n ${MKTEMP_LOG-} && -f ${MKTEMP_LOG} ]]; then
374455
sudo rm -f "${MKTEMP_LOG}" &> /dev/null || true
375456
fi
376-
sudo sh -c "echo \"$(tail -1000 "${APPLICATION_LOG}")\" > ${APPLICATION_LOG}" || true
457+
tail -1000 "${APPLICATION_LOG}" | sudo tee "${APPLICATION_LOG}" > /dev/null || true
377458
if [[ -n ${TEMP_FOLDER-} && -d ${TEMP_FOLDER} ]]; then
378459
sudo rm -rf "${TEMP_FOLDER?}" &> /dev/null || true
379460
fi

0 commit comments

Comments
 (0)