Repository navigation
Expand file tree
/
Copy pathlogging_ctl.sh
More file actions
86 lines (75 loc) · 2.46 KB
/
Copy pathlogging_ctl.sh
File metadata and controls
86 lines (75 loc) · 2.46 KB
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
#!/bin/bash
is_subtest()
{
# PPID: The process ID of the shell's parent.
# get current script parent process name
local ppcmd
ppcmd=$(ps -p $PPID -o cmd --noheader|awk '{print $2;}')
local ext_message="" cmd
# confirm this script is loaded by other script, and add the flag for it
file "$ppcmd" 2>/dev/null |grep -q 'shell script'
}
# using aliases to cover log defines --- more like C log functions
_func_log_cmd()
{
# the local & remote are different:
# the local is login
# the remote is running the commands
# for example:
# remote: ssh xxx@xxx cmd
# local: ssh xxx@xxx
# $ cmd
# We can use the system's private environment to know whether this script is
# run from local or remote
# Notice: this command only verified on Ubuntu
local key __LOG_PREFIX=""
local -A LOG_LIST
[[ ! "$LS_COLORS" ]] && __LOG_PREFIX="REMOTE_"
LOG_LIST['dlogi']="[$__LOG_PREFIX""INFO]"
LOG_LIST['dloge']="[$__LOG_PREFIX""ERROR]"
LOG_LIST['dlogc']="[$__LOG_PREFIX""COMMAND]"
LOG_LIST['dlogw']="[$__LOG_PREFIX""WARNING]"
# open aliases for script, so it can use the dlogx commands instead of
# writing functions
shopt -s expand_aliases
if is_subtest; then
ext_message=" Sub-Test:"
fi
for key in "${!LOG_LIST[@]}";
do
# shellcheck disable=SC2016
local date_cmd='$(date -u "+%F %T %Z")'
local cmd="$key() { printf '%s%s %s\n' \"$date_cmd\" '$ext_message ${LOG_LIST[$key]}' \"\$*\" ; }"
eval "$cmd"
done
}
# without setting up the LOG_ROOT keyword, now create the log directory for it
_func_log_directory()
{
# LOG_ROOT is the top of a single test run. SOF_LOGS_BASE is higher
# up and for all tests and all runs.
if [ -n "$LOG_ROOT" ]; then
mkdir -p "$LOG_ROOT"
return
fi
# default value
: "${SOF_LOGS_BASE:=${SCRIPT_HOME}}"
local case_name log_dir timetag
case_name=$(basename -- "$SCRIPT_NAME")
case_name=${case_name%.*}
log_dir="${SOF_LOGS_BASE}/logs/$case_name"
timetag=$(date +%F"-"%T)"-$RANDOM"
mkdir -p "$log_dir/$timetag"
# now using the last link for the time tag
[[ -L $log_dir/last ]] && rm "$log_dir"/last
if [[ ! -e $log_dir/last ]]; then
ln -s "$log_dir/$timetag" "$log_dir"/last
fi
export LOG_ROOT=$log_dir/$timetag
}
for _func_ in $(declare -F|grep _func_log_|awk '{print $NF;}')
do
$_func_
eval "unset $_func_"
done
unset _func_