2
0

_log.sh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # import log utility and check the detail log file is ok
  2. # @param log the log file path, default to /dev/null
  3. #######################################
  4. # color echo.
  5. #######################################
  6. RED="\\033[31m"
  7. GREEN="\\033[32m"
  8. YELLOW="\\033[33m"
  9. BLACK="\\033[0m"
  10. POS="\\033[94G"
  11. # if need to log to file, change the log path.
  12. if [[ ! $log ]]; then
  13. log=/dev/null;
  14. fi
  15. ok_msg(){
  16. echo -e "${1}${POS}${BLACK}[${GREEN} OK ${BLACK}]"
  17. # write to log file.
  18. echo "[info] ${1}" >> $log
  19. }
  20. warn_msg(){
  21. echo -e "${1}${POS}${BLACK}[ ${YELLOW}WARN${BLACK} ]"
  22. # write to log file.
  23. echo "[error] ${1}" >> $log
  24. }
  25. failed_msg(){
  26. echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]"
  27. # write to log file.
  28. echo "[error] ${1}" >> $log
  29. }
  30. function check_log(){
  31. log_dir="`dirname $log`"
  32. (mkdir -p ${log_dir} && chmod 777 ${log_dir} && touch $log)
  33. ret=$?; if [[ $ret -ne 0 ]]; then failed_msg "create log failed, ret=$ret"; return $ret; fi
  34. ok_msg "create log( ${log} ) success"
  35. echo "bravo-vms setup `date`" >> $log
  36. ok_msg "see detail log: tailf ${log}"
  37. return 0
  38. }