Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Last active December 12, 2015 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixpulvis/4732407 to your computer and use it in GitHub Desktop.
Save nixpulvis/4732407 to your computer and use it in GitHub Desktop.
shell logging
# Color escapes
RESET="\e[0m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
LOGFILE_DIR="$HOME"
DATETIME=`date "+%m_%d_%Y_%H.%M.%S"`
LOGFILE="$LOGFILE_DIR/build_$DATETIME.log"
touch $LOGFILE
# Log and display both STDOUT and STDERR
log() {
$@ 2>&1 | tee -a $LOGFILE
}
# Log both STDOUT and STDERR but display only STDERR
log!() {
($@ >> $LOGFILE) 2>&1 | tee -a $LOGFILE >&2
}
log_and_print_red() {
printf "$RED"; log $@; printf "$RESET"
}
log_and_print_red!() {
printf "$RED"; log! $@; printf "$RESET"
}
log_and_print_green() {
printf "$GREEN"; log $@; printf "$RESET"
}
log_and_print_green!() {
printf "$GREEN"; log! $@; printf "$RESET"
}
log_and_print_yellow() {
printf "$YELLOW"; log $@; printf "$RESET"
}
log_and_print_yellow!() {
printf "$YELLOW"; log! $@; printf "$RESET"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment