Guest User

Untitled

a guest
Dec 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. DEBUG_LOG="${0##*/}.log"
  2.  
  3. # copy stdout to log always and echo to console
  4. exec > >(tee -a ${DEBUG_LOG})
  5.  
  6. # copy stderr to log only, unless debugging is enabled
  7. [ $DEBUG_TEST = "true" ]
  8. && exec 2> >(tee -a ${DEBUG_LOG} >&2)
  9. || exec 2>> ${DEBUG_LOG}
  10.  
  11. # Expand escaped characters, wrap at 70 chars on spaces,
  12. # and indent wrapped lines
  13. msg_log() {
  14. echo -e "$(date +%T) ${0##*/}: $1"
  15. | fold -w70 -s | sed '2~1s/^/ /' >&2;
  16. }
  17. msg_con() {
  18. if [ "${DEBUG_TEST}" = "true" ]; then
  19. msg_log "$1"
  20. else
  21. echo -e "$1" | fold -w70 -s | sed '2~1s/^/ /';
  22. fi
  23. }
  24.  
  25. function startLogging {
  26. exec > >(gawk -v pid=$$ '{ print strftime("%F-%T"),pid,$0; fflush(); }' | tee -a $logfile)
  27. [ ! -z "$DEBUG" ] && exec 2>&1 || exec 2> >(gawk -v pid=$$ '{ print strftime("%F-%T"),pid,$0; fflush(); }' >>$logfile)
  28. echo "=== Log started for $$ at $(date +%F-%T) ==="
  29. }
  30.  
  31. exec 1>>${DEBUG_LOG}
  32. exec 2>>${DEBUG_LOG}
  33.  
  34. ./my_script 1>>${DEBUG_LOG} 2>>${DEBUG_LOG}
  35.  
  36. echo -n $(date) >> $DEBUG_LOG
  37. command 2>&1 | tee -a $DEBUG_LOG
  38.  
  39. echo -n $(date) >> $DEBUG_LOG
  40. command >> $DEBUG_LOG 2>&1
Add Comment
Please, Sign In to add comment