/logForShell

:shell:a fine log method for Shell scripts

Primary LanguageShellApache License 2.0Apache-2.0

logForShell

Used for Shell scripts. Focus on :

  1. Fast problems location.
  2. Using "Caller" to generate call stacks of Shell source code.
  3. Format of logs.
  4. Clearly understanding what the scripts are doing by logs.

Usage & Examples

Import logForShell in your first shell script:

source ./logForShell.sh default
Show "Hello World"

2018-09-10 [10:07:21] Hello World

If you want some tag for logs, you can use:

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

You can find something about Shell in my personal blog. Contact author please email me 子博 : tingsven@163.com