Repository navigation
Expand file tree
/
Copy pathlogging.sh
More file actions
107 lines (88 loc) · 2.52 KB
/
Copy pathlogging.sh
File metadata and controls
107 lines (88 loc) · 2.52 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
__dcp_contains() {
local search="$1"; shift
while (($# > 0)); do
[[ "$1" = "${search}" ]] && return || shift
done
return 1
}
declare DCP_LOG_COLOR DCP_LOG_LEVEL >/dev/null
if ! __dcp_contains "${DCP_LOG_COLOR}" always auto never; then
DCP_LOG_COLOR="auto"
fi
if ! __dcp_contains "${DCP_LOG_LEVEL}" debug info warn error hush none off quiet silent; then
DCP_LOG_LEVEL="info"
fi
if { printf '' >&3; } 2>/dev/null; then
__dcp_printf() { printf "$@" >&3; }
__dcp_printf_isatty() { [[ -t 3 ]]; }
else
__dcp_printf() { printf "$@" >&2; }
__dcp_printf_isatty() { [[ -t 2 ]]; }
fi
if [[ "${DCP_LOG_COLOR}" = "always" ]] || { [[ "${DCP_LOG_COLOR}" = "auto" ]] && __dcp_printf_isatty; }; then
__dcp_log() {
(($# < 3)) && return 1 || :
local level="$1"; shift
local level_color="$1"; shift
local format="$1"; shift
__dcp_printf '\033[2;39;49m%s %b%5s \033[0;35m%s \033[2;39m: \033[0m'"${format}"'\n' \
"$(date '+%F %T')" "${level_color}" "${level}" "$$" "$@"
}
__dcp_log_cmd_name() {
__dcp_printf '\033[2;39;49m> \033[0;32m%s\033[0m' "$1"
}
else
__dcp_log() {
(($# < 3)) && return 1 || :
local level="$1"; shift; shift
local format="$1"; shift
__dcp_printf '%s [%5s] %s : '"${format}"'\n' \
"$(date '+%F %T')" "${level}" "$$" "$@"
}
__dcp_log_cmd_name() {
__dcp_printf '> %s' "$1"
}
fi
if __dcp_contains "${DCP_LOG_LEVEL}" debug; then
log_debug() { __dcp_log "DEBUG" '\033[0;32m' "$@"; }
else
log_debug() { :; }
fi
if __dcp_contains "${DCP_LOG_LEVEL}" debug info; then
log_info() { __dcp_log "INFO" '\033[0;34m' "$@"; }
else
log_info() { :; }
fi
if __dcp_contains "${DCP_LOG_LEVEL}" debug info warn; then
log_warn() { __dcp_log "WARN" '\033[0;33m' "$@"; }
else
log_warn() { :; }
fi
if __dcp_contains "${DCP_LOG_LEVEL}" debug info warn error; then
log_error() { __dcp_log "ERROR" '\033[0;31m' "$@"; }
else
log_error() { :; }
fi
log_cmd() {
(($# == 0)) && return || :
[[ -z "$1" ]] && return 127 || :
local arg cmd_name
local args=() quote="'\\''" tilde="~"
for arg in "$@"; do
if [[ -z "${arg}" ]] || [[ "${arg}" =~ [?!%\$\*\|\\[:space:]] || "${arg}" =~ [\`\'\"\(\)=\<\>{}] ]]; then
arg="'${arg//\'/${quote}}'"
else
arg="${arg//${HOME}/${tilde}}"
fi
if [[ -z "${cmd_name}" ]]; then
cmd_name="${arg}"
else
args+=("${arg}")
fi
done
__dcp_log_cmd_name "${cmd_name}"
__dcp_printf '%s' "${args[@]/#/ }"
__dcp_printf '\n'
"$@"
}
unset -f __dcp_contains __dcp_printf_isatty