forked from sagemath/sage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sage-logger
executable file
·130 lines (115 loc) · 3.75 KB
/
sage-logger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
#
# sage-logger [-p] COMMAND LOGFILE
#
# Evaluate shell command COMMAND while logging stdout and stderr to
# LOGFILE. If either the command or the logging failed, return a
# non-zero exit status.
#
# If the -p argument is given, each line printed to stdout is prefixed
# with the name of the log file.
#
# AUTHOR:
#
# - Jeroen Demeyer (2015-07-26): initial version based on old pipestatus
# script (#18953)
#
#*****************************************************************************
# Copyright (C) 2015 Jeroen Demeyer <jdemeyer@cage.ugent.be>
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
use_prefix=false
if [[ "$1" = -p ]]; then
use_prefix=true
shift
fi
cmd="$1"
logfile="$2"
export SAGE_LOGFILE="$logfile"
logname="$(basename $logfile .log)"
logdir=`dirname "$logfile"`
if [[ $use_prefix = true ]]; then
prefix="[${logname}] "
else
prefix=""
fi
# Use sed option to reduce buffering, to make the output appear more
# smoothly. For GNU sed, this is the --unbuffered option.
# For BSD sed (which is also on OS X), this is the -l option.
if [ -n "$prefix" ]; then
if sed </dev/null 2>/dev/null --unbuffered ""; then
SED="sed --unbuffered"
elif sed </dev/null 2>/dev/null -l ""; then
SED="sed -l"
else
SED="sed"
fi
# eval needed to get the quoting around the regexp right
SED="eval $SED 's/^/$prefix/'"
else
# Make SED a useless use of cat
SED=cat
fi
timefile="$logdir/$logname.time"
rm -f "$timefile"
if /usr/bin/time -h -o /dev/null true; then
TIME="/usr/bin/time -h -o $timefile"
else
TIME=""
fi
report_time ()
{
time=$(echo $(cat $timefile))
case "$time" in
*m*real*|*h*real*|*[1-9][0-9].*real*|*[1-9][0-9],*real*)
# at least 10 seconds wall time
echo "$time"
;;
esac
}
mkdir -p "$logdir"
# Do all logging of child processes with V=1 to ensure that no
# information is lost.
export MAKEFLAGS="$MAKEFLAGS V=1"
if [ "$V" = 0 ]; then
export SAGE_SILENT_BUILD=yes
fi
if [ -n "$SAGE_SILENT_BUILD" -a ${use_prefix} = true ]; then
# Silent build.
# Similar to https://www.gnu.org/software/automake/manual/html_node/Automake-Silent-Rules.html#Automake-Silent-Rules
echo "[$logname] installing. Log file: $logfile"
( exec>> $logfile 2>&1 ; $TIME sh -c "$cmd"; status=$?;
[ -r $timefile ] && cat $timefile; exit $status )
status=$?
if [[ $status != 0 ]]; then
echo " [$logname] error installing, exit status $status. End of log file:"
tail -n 120 "$logfile" | sed "/Please email sage-devel/,$ d;s;^; [$logname] ;" >&2
echo " [$logname] Full log file: $logfile"
else
time=$(report_time)
if [ -n "$time" ]; then
echo " [$logname] successfully installed ($time)."
else
echo " [$logname] successfully installed."
fi
fi
exit $status
else
# Redirect stdout and stderr to a subprocess running tee.
# We trap SIGINT such that SIGINT interrupts the main process being
# run, not the logging.
( exec 2>&1;
$TIME sh -c "$cmd"; status=$?; report_time;
exit $status ) | \
( trap '' SIGINT; if [ -n "$GITHUB_ACTIONS" -a -n "$prefix" ]; then echo "::group::${logname}"; fi; tee -a "$logfile" | $SED; if [ -n "$GITHUB_ACTIONS" -a -n "$prefix" ]; then echo "::endgroup::"; fi )
pipestatus=(${PIPESTATUS[*]})
if [ ${pipestatus[1]} -ne 0 ]; then
exit ${pipestatus[1]}
else
exit ${pipestatus[0]}
fi
fi