-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.sh
89 lines (70 loc) · 2.42 KB
/
log.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
################################################################################
# Description :
## "echo"es usage of LOG().
# Arguments : NA
# Return Value:
## 0 for success. No other return value.
# Additional note: NA
################################################################################
log_usage() {
echo "<"$FUNCNAME"> Incorrect arguments."
echo "<"$FUNCNAME"> Usage:"
echo "<"$FUNCNAME"> LOG <0|1> <logfile> <log_message> [if timestamp-flag is 1: \$FUNCNAME] [if timestamp-flag is 1: \$LINENO]"
return 0
}
################################################################################
# Description :
## - Writes logs into logfile defined by argument $2.
## - This function should ideally be made a part of some common code which can
## be imported in some other shell script source code.
## for instance:
## . /opt/MyProdRoot/DummyProd/common/common_functions.sh
## - As stated above, LOG() can be put into common_functions.sh
# Arguments :
## $1: timestamp-flag
## 0: doesn't write timestamp.
## 1: writes timestamp.
## $2: logfile.
## $3: Log message.
## $4: Caller name. Optional. Passed iff $1 is 1.
## $5: Line number in the caller. Optional. Passed iff $1 is 1.
# Return Value: 0: succcess, 1: failure
# Additional note:
## Examples of how to invoke:
## 1> LOG 1 "$LOG_FILE" "ERR: Source file $src_file does not exist." "$FUNCNAME" "$LINENO"
## 2> LOG 0 "$LOG_FILE" "#########################################." "$FUNCNAME"
## $FUNCNAME is shell variable and caller of LOG() have these variables set
## in the context of the calling function.
## $LINENO is also a shell variable. Scope of this variable is across the entire script.
################################################################################
LOG() {
local timestamp_flag="$1"
local log_current_timestamp="$(date "+%d-%b-%Y-%H%M%S:%3N-%Z")"
local log_file="$2"
local log_message="$3"
local function_name="na"
local lineno=0
if [ $# -ne 3 ] && [ $# -ne 5 ] ; then
log_usage
exit "$EV_FAIL"
fi
let "timestamp_flag += 0"
if [ -z "$log_message" ] ; then
echo >> "$log_file"
return 0
fi
if [ "$timestamp_flag" -eq 1 ] && [ $# -ne 5 ] ; then
log_usage
exit 1
fi
if [ "$timestamp_flag" -eq 0 ] ; then
echo "$log_message" >> "$log_file"
return "$STATUS_OK"
fi
function_name="$4"
lineno="$5"
let "lineno += 0"
echo "["$log_current_timestamp"]["$function_name":"$lineno"]:: $log_message" >> "$log_file"
return 0
}