Project

General

Profile

« Previous | Next » 

Revision 9214

lib/sh/util.sh: command(): use just one control var $cmd_log_fd instead of three flags (limit_log_fd, limit_stdout, stderr_is_errors) that indicated various common fd configurations. this is much clearer (you state which fd the common uses as its logging fd), more configurable (the logging fd can be any fd, not just 1 or 2), and more automatic (redirecting fd 2 to err_fd happens automatically if it isn't used for logging).

View differences:

util.sh
230 230
## external commands
231 231

  
232 232
function command()
233
# usage: [limit_log_fd=1] [limit_stdout=1] [stderr_is_errors=1] command \
234
# extern_cmd...
233
# usage: [cmd_log_fd=|1|2|#] command extern_cmd...
235 234
# to view only explicitly-displayed errors: explicit_errors_only=1 script...
236 235
{
237
	if test "$limit_stdout"; then local stdout2log_fd=1 limit_log_fd=1; fi
238
	local stderr_is_errors="${stderr_is_errors-$stdout2log_fd}"
239
	
240 236
	cmd2rel_path; (echo_params; can_log) && indent || true
241 237
	(
242 238
		# the following redirections must happen in exactly this order
243
		if test "$limit_log_fd"; then limit_log_fd; fi
244
		if test "$stdout2log_fd"; then stdout2log_fd; fi
245
		if test "$stderr_is_errors"; then exec 2>&"$err_fd"; fi
239
		limit_log_fd
240
		if test "$cmd_log_fd"; then echo_eval exec "$cmd_log_fd>&$log_fd"; fi
241
		if test "$cmd_log_fd" != 2; then # fd 2 not used for logging
242
			exec 2>&"$err_fd" # assume fd 2 used for errors
243
		fi
246 244
		
247 245
		exec -- "$@" # -- so cmd name not treated as `exec` option
248 246
	) || return

Also available in: Unified diff