Last active
August 16, 2018 06:11
-
-
Save coord-e/e453db5bd1673cf8fb9a1fe864bb5238 to your computer and use it in GitHub Desktop.
Log the command's stdout&stderr to $ERRLOGPATH if failed. e.g. _le clang a.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _le { | |
# Usage: _le command | |
local file=$(mktemp) | |
# Run the supplied command in screen, and print the exit code on exit | |
local screen_cmd='trap '"'"'echo -en "\n"$?'"'"' EXIT; '$@ | |
screen -Logfile $file -L bash -c "$screen_cmd" | |
# Overwrite "screen is terminating" message with empty | |
echo -ne '\033[F' | |
# echo -n "[screen is terminating]" | |
echo -n " " | |
# Last line shows the exit code | |
local exit_status=$(tail -1 $file | tr -d '[:space:]') | |
sed -i '$d' $file | |
# Show the log as if it was directly printed to console | |
cat $file | |
if [ "$exit_status" == "0" ]; then | |
# If the command succeeded, remove the log | |
rm $file | |
else | |
mkdir -p $ERRLOGPATH | |
local uuid=$(python -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)') | |
local cmd=$(echo "$@" | cut -d ' ' -f1 | tr -d '[:space:]') | |
local new_file="$ERRLOGPATH/$cmd-$exit_status-$uuid.log" | |
mv $file $new_file | |
echo "Saved to $new_file" | |
fi | |
return $exit_status | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment