#!/bin/bash
# * https://github.com/7ippo/logForShell
# * Author: 子博
# * Date: 2018

# source ./logForShell.sh default
# Show "Hello World"
# 2018-09-10 [10:07:21] Hello World

# source ./logForShell.sh [Shell]
# Show "Using TAG to LOG"
# 2018-09-10 [10:08:26] [Shell] Using TAG to LOG

# If you are running a group of shell scripts, please call CleanFlagBeforeStart in the first script. 
# This can clean the failure flag which may be generated by the last execution or other scripts. 
# If there is a failure flag, the method Show will not print anything.
# CleanFlagBeforeStart

# In the beginning of a shell script before doing something 
# you can call BeforeShell $@ This will print the running script's name and all the input parameters.
# BeforeShell $@
# 2018-09-05 [17:52:21]
# 2018-09-05 [17:52:21]    +   test.sh  BEGIN   +
# 2018-09-05 [17:52:21]    +   test.sh -n -r -android 90   +
# 2018-09-05 [17:52:21]

# Also you can call AfterShell to tell the ending of a script.
# AfterShell
# 2018-09-05 [17:52:21]
# 2018-09-05 [17:52:21]    +   test.sh  FINISH   +

# When running a new step, you can call Step function and input a step number and step name. This will print format logs for steps.
# Step 1 "Copy Unity Resource"
# 2018-09-05 [17:52:21]
# 2018-09-05 [17:52:21] ========== test.sh  STEP 1 : Copy Unity Resource ==========
# 2018-09-05 [17:52:21]

# When you detect some steps succeed, you should call ReportSuccess to tell the success.
# ReportSuccess "Build Dynamic Framework Project"  
# ****     Build Dynamic Framework Project SUCCESS      ****

# Also when you find a failure or wrong conditions, you should call ReportFailure. 
# This will automatically print the calling stack logs to locate the problem in source code easily. 
# And this will generate failure flag to stop the incoming Show method's output. In another word, logs will end here.
# ReportFailure
# ****     line 10 test.sh REPORTED FAILURE     ****
    # CALLER LIST
    # - line 10 a test.sh
    # - line 14 b test.sh
    # - line 17 main test.sh















# Global execution result flag bit, log will not continue to output after one step error
# Use the tag file in the same directory as the file as a flag for information transfer and judgment 
export GLOBAL_FAIL_FLAG="$(cd $(dirname ${BASH_SOURCE[0]}); pwd )/globalFailFlag.tag"

# Whether to add the tag TAG flag, the default is empty, please enter default, otherwise ${1} will read the execution parameters of the calling script
# Such as:. ./LogForShell.sh default
# When you need to add TAG, pass in the TAG parameter when calling the script
# Such as:. ./LogForShell.sh [Shell]
if [ "${1}" != "default" ];then
  TAG_FOR_SHELL="${1} "
else
  TAG_FOR_SHELL=""
fi

# Core printing function: used to distinguish between critical and non-critical logs
# Add a timestamp before the normal log, so that the content printed by commands such as svn cp does not have a timestamp
# Add a custom label before printing the log to facilitate filtering and retrieval 
function Show(){
  dateStr=`echo $(date +%Y-%m-%d) $(date +[%H:%M:%S])`
  if [ ! -f ${GLOBAL_FAIL_FLAG} ]; then
    echo "${dateStr} ${TAG_FOR_SHELL}$@"
  fi
}

# Blank line printing function: please print a blank line before non-critical log commands such as svn, cp, wget, etc. 
function BlankLine(){
  echo ""
}

# Pure print function: no time stamp, no label, no format 
function CleanShow(){
  echo "$@"
}

# Step print function: Need to pass in $1: step number, $2: step description
# Such as: Step 1 "Copy Unity Resource"
function Step(){
  sourceFileName=`caller`
  Show ""
  Show "==================== ${sourceFileName##*/}  STEP $1 : $2 ===================="
  Show ""
}


# Enter the script function: execute at the beginning of the key script, all parameters of the script need to be passed in when calling
# Such as: BeforeShell $@ 
function BeforeShell(){
  sourceFileName=`caller`
  Show ""
  Show "   +   ${sourceFileName##*/}  BEGIN   +"
  Show "   +   ${sourceFileName##*/} $@   +"
  Show ""
}


# Exit script function: execute at the end of the script
# Such as: AfterShell
function AfterShell(){
  sourceFileName=`caller`
  Show ""
  Show "   +   ${sourceFileName##*/}  FINISH   +"
}


# Error report function: judge the execution result after a certain step is over, if an error occurs, call this function
# Such as: ReportFailure
# This function will change the execution result flag to prevent the log from continuing to output after an error
function ReportFailure(){
  touch ${GLOBAL_FAIL_FLAG}
  echo ""
  echo ""
  echo "****     line `caller` REPORTED FAILURE     ****"
  echo ""
  echo ""
  # Judge the caller return by looping, if it is not empty, it will continue to print, and print up to 5 lines
  echo "    CALLER LIST    "
  echo " - line `caller 0`"
  for loop in 1 2 3 4
  do
    if [ "`caller ${loop}`" != "" ];then
      echo " - line `caller ${loop}`"
    else
      echo ""
      break
    fi
  done
}


# Success report function: judge the execution result after the key step is over, call this function if it succeeds
# General steps do not need to call this function to output
# Such as: ReportSuccess "Init and check related params"
function ReportSuccess(){
  echo ""
  echo "****     $1 SUCCESS      ****"
  echo ""
}


# Clear flag function: After loading the logForShell function, it must be called before all statements in the main entry script are executed
function CleanFailFlagBeforeStart(){
  if [ -f ${GLOBAL_FAIL_FLAG} ]; then
    rm -f ${GLOBAL_FAIL_FLAG}
  fi
}