Project

General

Profile

« Previous | Next » 

Revision 9110

lib/sh/util.sh: verbose output: put commands into subsections

View differences:

util.sh
137 137

  
138 138
#### verbose output
139 139

  
140

  
140 141
# usage: (stdout2stderr; cmd...) || return
141 142
# `|| return` needed on Mac because of bug where -e doesn't apply to()
142 143
stdout2stderr() { exec >&2; }
143 144

  
145
usage() { echo "Usage: $1" >&2; (exit 2); }
146

  
147

  
148
### logging
149

  
144 150
# set verbosity
145 151
if isset verbose; then : "${verbosity:=$(bool2int "$verbose")}"; fi
146 152
: "${verbosity=3}" # default
......
180 186
fi # load new aliases
181 187
if self_being_included; then
182 188

  
189

  
190
### command echoing
191

  
183 192
echo_cmd()
184 193
{
185 194
	case "$1" in command) # command implied by the log_level
......
189 198
	log "$*"
190 199
}
191 200

  
201
## external commands
202

  
192 203
alias echo_run_prep='cmd2rel_path; echo_cmd "$@"; indent'
193 204

  
194 205
fi # load new aliases
......
196 207

  
197 208
echo_run() { echo_run_prep; "$@"; }
198 209

  
210
# auto-echo common external commands
211
for cmd in rm; do alias "$cmd=echo_run $cmd"; done; unset cmd
212

  
213
# echo all external commands
214
alias command="echo_run command -- " # last space alias-expands next word
215

  
216
# commands that are always external
217
for cmd in env; do alias "$cmd=command $cmd"; done; unset cmd
218

  
219
## functions
220

  
221
function echo_func()
222
{
223
	inc_log_level
224
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
225
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
226
}
227
alias echo_func='echo_func "$@"'
228

  
229
## vars
230

  
231
echo_vars() # usage: echo_vars var...
232
{
233
	inc_log_level; inc_log_level
234
	if can_log; then
235
		local var
236
		for var in "${@%%=*}"; do { echo -n "$PS4"; declare -p "$var";} >&2;done
237
	fi
238
}
239

  
240
echo_export() { builtin export "$@"; echo_vars "$@"; }
241

  
242
if test "$verbosity" -ge 2; then
243
	alias export="echo_export" # automatically echo env vars when they are set
244
fi
245

  
246

  
247
### external command verbose output
248

  
199 249
# usage: ([stdout2stderr=1] limit_stderr; cmd...) || return
200 250
# `|| return` needed on Mac because of bug where -e doesn't apply to ()
201 251
function limit_stderr()
......
212 262
}
213 263
alias limit_stderr_cmd='limit_stderr_cmd ' # last space alias-expands next word
214 264

  
215
# auto-echo common external commands
216
for cmd in rm; do alias "$cmd=echo_run $cmd"; done; unset cmd
217 265

  
218
# echo all external commands
219
alias command="echo_run command -- " # last space alias-expands next word
266
### external command input/output
220 267

  
221
# commands that are always external
222
for cmd in env; do alias "$cmd=command $cmd"; done; unset cmd
223

  
224
function echo_func()
225
{
226
	inc_log_level
227
	local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
228
	echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
229
}
230
alias echo_func='echo_func "$@"'
231

  
232 268
# usage: cmd1 | { pipe_delay; cmd2; }
233 269
alias pipe_delay='sleep 0.1' # s; display after leading output of cmd1
234 270

  
......
249 285

  
250 286
alias echo_stdout='echo_stdin' # usage: cmd|echo_stdout
251 287

  
252
echo_vars() # usage: echo_vars var...
253
{
254
	inc_log_level; inc_log_level
255
	if can_log; then
256
		local var
257
		for var in "${@%%=*}"; do { echo -n "$PS4"; declare -p "$var";} >&2;done
258
	fi
259
}
260

  
261
echo_export() { builtin export "$@"; echo_vars "$@"; }
262

  
263
if test "$verbosity" -ge 2; then
264
	alias export="echo_export" # automatically echo env vars when they are set
265
fi
266

  
267
usage() { echo "Usage: $1" >&2; (exit 2); }
268

  
269 288
fi # load new aliases
270 289
if self_being_included; then
271 290

  

Also available in: Unified diff