Revision 9119
Added by Aaron Marcuse-Kubitza over 11 years ago
lib/sh/util.sh | ||
---|---|---|
152 | 152 |
: "${verbosity=3}" # default |
153 | 153 |
: "${verbosity:=0}" # ensure non-empty |
154 | 154 |
declare -i verbosity # ensure integer |
155 |
export PS4 verbosity # propagate indent and verbosity to invoked commands
|
|
155 |
export verbosity # propagate to invoked commands
|
|
156 | 156 |
|
157 | 157 |
can_log() { test "$verbosity" -gt 0; } # verbosity=0 turns off all logging |
158 | 158 |
|
159 |
log() { if can_log; then echo "$PS4$1" >&2; fi; } |
|
159 |
log() { if can_log; then echo "$log_indent$PS4$1" >&2; fi; }
|
|
160 | 160 |
|
161 | 161 |
# usage: symbol=... log_custom msg |
162 | 162 |
log_custom() { local PS4="${PS4%[^ ] }$symbol "; log "$@"; } |
... | ... | |
171 | 171 |
# usage: cmd || [type=...] die msg |
172 | 172 |
die() { save_e; "log_${type:-err}" "$1"; rethrow; } |
173 | 173 |
|
174 |
: "${log_level_indent= }" |
|
175 |
export log_level_indent # propagate to invoked commands |
|
174 |
: "${log_level_indent= }" "${log_indent=}" |
|
175 |
export log_level_indent log_indent # propagate to invoked commands |
|
176 |
alias cmd_indent='declare log_indent="$log_indent$log_level_indent"' |
|
176 | 177 |
|
177 | 178 |
# usage: in func: indent; ... |
178 | 179 |
# outside func: indent; ...; outdent |
... | ... | |
201 | 202 |
|
202 | 203 |
## external commands |
203 | 204 |
|
204 |
alias echo_run_prep='cmd2rel_path; echo_cmd "$@"; indent' |
|
205 |
alias echo_run_prep='cmd2rel_path; echo_cmd "$@"; cmd_indent'
|
|
205 | 206 |
|
206 | 207 |
fi # load new aliases |
207 | 208 |
if self_being_included; then |
... | ... | |
234 | 235 |
inc_log_level; inc_log_level |
235 | 236 |
if can_log; then |
236 | 237 |
local var |
237 |
for var in "${@%%=*}"; do { echo -n "$PS4"; declare -p "$var";} >&2;done |
|
238 |
for var in "${@%%=*}"; do |
|
239 |
{ echo -n "$log_indent$PS4"; declare -p "$var";} >&2 |
|
240 |
done |
|
238 | 241 |
fi |
239 | 242 |
} |
240 | 243 |
|
Also available in: Unified diff
lib/sh/util.sh: logging: separate the log_level-based indenting from the external command indenting so that the log_level-based indenting can use a different prefix. this involves propagating a new var, $log_indent, to invoked commands instead of $PS4, and always prepending $log_indent to $PS4 when printing log messages.