# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#   &Vopt("5myname.log");                                                     #
# Set  the  verbosity from various environment variables.  The value may be a #
# verbose level (1 digit), plus an optional output file name.  The file V  is #
# opened to the file, if any, or STDERR by default. The default value for the #
# verbosity level is 1, which generally means to produce only  serious  error #
# messages.                                                                   #
#                                                                             #
# Here's how this routine is typically called:                                #
#   ($P = $0) =~ s'.*/''; # Program name without directories                  #
#   &Vopt($ENV{"V_$P"} || $ENV{"D_$P"} || $ENV{"T_$P"} || '1');               #
# That's for when you want to call this a "verbose" and "debug"  and  "trace" #
# facility.  I mostly just use the V_$P environment variable.                 #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

sub Vopt {
	$Vopt = shift || '1';			# Make sure we have a verbosity string
	$V = 1 unless defined $V;
	($P = $0) =~ s'.*/'' unless defined($P);
	print "<br>Vopt: Vopt=\"$Vopt\"<br>\n" if $V>2;
	if ($Vopt =~ /^(\d+)(.+)$/) {
		$V = int($1);				# Verbose level
		$Vfil = $2;					# Verbose output file
		if (open(V,">>$Vfil")) {	# Try to append to the file
			open(STDERR,">>&V");	# Switch STDERR over to V
		} else {
			print V "$P: Can't write \"$Vfil\" ($!)\n" if $V>0;
			open(V,">>&STDERR");	# STDERR is the default 
		}
	} elsif ($Vopt) {				# File name only?
		if (open(V,">>$Vopt")) {	# Try to append to the file
			open(STDERR,">>&V");	# Switch STDERR over to V
		} else {
			print V "$P: Can't write \"$Vopt\" ($!)\n" if $V>0;
			open(V,">>&STDERR");	# STDERR is the default 
		}
	} else {
		$V ++;					# Null arg, just increment the verbose level
		open(V,">>&STDERR");	# And write to STDERR
	}
	select V; $| = 1;			# Make V unbuffered
	select STDOUT; $| = 1;		# Make sure STDERR is unbuffered
	$esep = '=' x 70;			# Create a couple of global spacers
	$hsep = '-' x 70;
	print V "\n$P $esep\n" if $V>1;	# Tell the world if we're verbose
	print V "$P started with V=$V [pid=$$] ", `date` if $V>1;
}

1;	# Report success in loading this file.
