Skip to content

Instantly share code, notes, and snippets.

@jacksoncage
Created November 17, 2016 13:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacksoncage/54743cf17121f6bc5b8eb3013dfd06ac to your computer and use it in GitHub Desktop.
Save jacksoncage/54743cf17121f6bc5b8eb3013dfd06ac to your computer and use it in GitHub Desktop.
# Copyright 2003, 2004 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
progname=$(basename $0)
addtime ()
{
while IFS= read -r line; do
echo "`date ${FMT}` $1: $line"
done
if [ ! -z "$line" ]; then
echo -n "`date ${FMT}` $1: $line"
fi
}
usage ()
{
echo \
"Usage: $progname [options] program [args ...]
Run program and annotate STDOUT/STDERR with a timestamp.
Options:
+FORMAT - Controls the timestamp format as per date(1)
-h, --help - Show this message"
}
FMT="+%H:%M:%S"
while [ "$1" ]; do
case "$1" in
+*)
FMT="$1"
shift
;;
-h|-help|--help)
usage
exit 0
;;
*)
break
;;
esac
done
if [ $# -lt 1 ]; then
usage
exit 1
fi
cleanup() { __st=$?; rm -rf "$tmp"; exit $__st; }
trap cleanup 0
trap 'exit $?' 1 2 13 15
tmp=$(mktemp -d --tmpdir annotate.XXXXXX) || exit 1
OUT=$tmp/out
ERR=$tmp/err
mkfifo $OUT $ERR || exit 1
addtime O < $OUT &
addtime E < $ERR &
echo "`date ${FMT}` I: Started $@"
"$@" > $OUT 2> $ERR ; EXIT=$?
rm -f $OUT $ERR
wait
echo "`date ${FMT}` I: Finished with exitcode $EXIT"
exit $EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment