logger.sh revision 1624:4a82073cc315
144229Sdavidn#!/bin/bash 244229Sdavidn# 344229Sdavidn# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 444229Sdavidn# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 544229Sdavidn# 644229Sdavidn# This code is free software; you can redistribute it and/or modify it 744229Sdavidn# under the terms of the GNU General Public License version 2 only, as 844229Sdavidn# published by the Free Software Foundation. 944229Sdavidn# 1044229Sdavidn# This code is distributed in the hope that it will be useful, but WITHOUT 1144229Sdavidn# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1244229Sdavidn# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1344229Sdavidn# version 2 for more details (a copy is included in the LICENSE file that 1444229Sdavidn# accompanied this code). 1544229Sdavidn# 1644229Sdavidn# You should have received a copy of the GNU General Public License version 1744229Sdavidn# 2 along with this work; if not, write to the Free Software Foundation, 1844229Sdavidn# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1944229Sdavidn# 2044229Sdavidn# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2144229Sdavidn# or visit www.oracle.com if you need additional information or have any 2244229Sdavidn# questions. 2344229Sdavidn# 2444229Sdavidn 2544229Sdavidn# Usage: ./logger.sh theloggfile acommand arg1 arg2 2644229Sdavidn# 2744229Sdavidn# Execute acommand with args, in such a way that 2844229Sdavidn# both stdout and stderr from acommand are appended to 2944229Sdavidn# theloggfile. 3050479Speter# 3144229Sdavidn# Preserve stdout and stderr, so that the stdout 3244229Sdavidn# from logger.sh is the same from acommand and equally 33244743Sbapt# for stderr. 34244743Sbapt# 35244743Sbapt# Propagate the result code from acommand so that 36244743Sbapt# ./logger.sh exits with the same result code. 3744229Sdavidn 3844229Sdavidn# Create a temporary directory to store the result code from 3944229Sdavidn# the wrapped command. 4044229SdavidnRCDIR=`mktemp -dt jdk-build-logger.tmp.XXXXXX` || exit $? 4144229Sdavidntrap "rm -rf \"$RCDIR\"" EXIT 4244229SdavidnLOGFILE=$1 4344229Sdavidnshift 4444229Sdavidn 4544229Sdavidn# We need to handle command likes like "VAR1=val1 /usr/bin/cmd VAR2=val2". 4644229Sdavidn# Do this by shifting away prepended variable assignments, and export them 4744229Sdavidn# instead. 4844229Sdavidnis_prefix=true 4944229Sdavidnfor opt; do 5044229Sdavidn if [[ "$is_prefix" = true && "$opt" =~ ^.*=.*$ ]]; then 5144229Sdavidn export $opt 5244229Sdavidn shift 5344229Sdavidn else 5444229Sdavidn is_prefix=false 5544229Sdavidn fi 5644229Sdavidndone 5744229Sdavidn 5844229Sdavidn(exec 3>&1 ; ("$@" 2>&1 1>&3; echo $? > "$RCDIR/rc") | tee -a $LOGFILE 1>&2 ; exec 3>&-) | tee -a $LOGFILE 5944229Sdavidnexit `cat "$RCDIR/rc"` 6044229Sdavidn