This patch makes a couple small changes to pg_ctl:

- refactor a bunch of code to call a separate function print_msg() which
checks whether "silent mode" is enabled before printing an error
message. I could have made print_msg() take a varargs list, but I didn't
bother ("worse is better"). Also, we do fflush() whether it is needed or
not, on the grounds performance here is irrelevant.

- rename "silence_mode" to "silent_mode", which IMHO makes more sense

- make the error messages we emit in "waiting" mode more consistent (I
believe this is a recent regression introduced by Peter's error message
commit). The patch changes the output to:

% pg_ctl -l /tmp/foo -w start   
waiting for postmaster to start.... done
postmaster started
% pg_ctl -l /tmp/foo -w stop 
waiting for postmaster to shut down.... done
postmaster stopped

Rather than:

% pg_ctl -l /tmp/foo -w start              
waiting for postmaster to start....done
postmaster started
% pg_ctl -l /tmp/foo -w stop               
waiting for postmaster to shut down... .done
postmaster stopped

Barring any objections, I intend to apply to HEAD tomorrow.

-Neil

Index: src/bin/pg_ctl/pg_ctl.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/bin/pg_ctl/pg_ctl.c,v
retrieving revision 1.35
diff -c -r1.35 pg_ctl.c
*** src/bin/pg_ctl/pg_ctl.c	13 Oct 2004 10:35:05 -0000	1.35
--- src/bin/pg_ctl/pg_ctl.c	14 Oct 2004 04:58:21 -0000
***************
*** 66,72 ****
  static bool do_wait = false;
  static bool wait_set = false;
  static int	wait_seconds = 60;
! static bool silence_echo = false;
  static ShutdownMode shutdown_mode = SMART_MODE;
  static int	sig = SIGTERM;		/* default */
  static CtlCommand ctl_command = NO_COMMAND;
--- 66,72 ----
  static bool do_wait = false;
  static bool wait_set = false;
  static int	wait_seconds = 60;
! static bool silent_mode = false;
  static ShutdownMode shutdown_mode = SMART_MODE;
  static int	sig = SIGTERM;		/* default */
  static CtlCommand ctl_command = NO_COMMAND;
***************
*** 98,103 ****
--- 98,104 ----
  static void do_reload(void);
  static void do_status(void);
  static void do_kill(pgpid_t pid);
+ static void print_msg(const char *msg);
  
  #if defined(WIN32) || defined(__CYGWIN__)
  static bool pgwin32_IsInstalled(SC_HANDLE);
***************
*** 201,207 ****
  }
  
  
- 
  static char *
  xstrdup(const char *s)
  {
--- 202,207 ----
***************
*** 216,222 ****
  	return result;
  }
  
! 
  
  static pgpid_t
  get_pgpid(void)
--- 216,234 ----
  	return result;
  }
  
! /*
!  * Given an already-localized string, print it to stdout unless the
!  * user has specified that no messages should be printed.
!  */
! static void
! print_msg(const char *msg)
! {
! 	if (!silent_mode)
! 	{
! 		fputs(msg, stdout);
! 		fflush(stdout);
! 	}
! }
  
  static pgpid_t
  get_pgpid(void)
***************
*** 429,439 ****
  		}
  		else
  		{
! 			if (!silence_echo)
! 			{
! 				printf(".");
! 				fflush(stdout);
! 			}
  			pg_usleep(1000000); /* 1 sec */
  		}
  	}
--- 441,447 ----
  		}
  		else
  		{
! 			print_msg(".");
  			pg_usleep(1000000); /* 1 sec */
  		}
  	}
***************
*** 563,585 ****
  
  	if (do_wait)
  	{
! 		if (!silence_echo)
! 		{
! 			printf(_("waiting for postmaster to start..."));
! 			fflush(stdout);
! 		}
  
  		if (test_postmaster_connection() == false)
! 			printf(_("could not start postmaster\n"));
! 		else if (!silence_echo)
! 			printf(_("done\npostmaster started\n"));
  	}
! 	else if (!silence_echo)
! 		printf(_("postmaster starting\n"));
  }
  
  
- 
  static void
  do_stop(void)
  {
--- 571,588 ----
  
  	if (do_wait)
  	{
! 		print_msg(_("waiting for postmaster to start..."));
  
  		if (test_postmaster_connection() == false)
! 			printf(_(" failed\ncould not start postmaster\n"));
! 		else
! 			print_msg(_(" done\npostmaster started\n"));
  	}
! 	else
! 		print_msg(_("postmaster starting\n"));
  }
  
  
  static void
  do_stop(void)
  {
***************
*** 612,638 ****
  
  	if (!do_wait)
  	{
! 		if (!silence_echo)
! 			printf(_("postmaster shutting down\n"));
  		return;
  	}
  	else
  	{
! 		if (!silence_echo)
! 		{
! 			printf(_("waiting for postmaster to shut down... "));
! 			fflush(stdout);
! 		}
  
  		for (cnt = 0; cnt < wait_seconds; cnt++)
  		{
  			if ((pid = get_pgpid()) != 0)
  			{
! 				if (!silence_echo)
! 				{
! 					printf(".");
! 					fflush(stdout);
! 				}
  				pg_usleep(1000000);		/* 1 sec */
  			}
  			else
--- 615,632 ----
  
  	if (!do_wait)
  	{
! 		print_msg(_("postmaster shutting down\n"));
  		return;
  	}
  	else
  	{
! 		print_msg(_("waiting for postmaster to shut down..."));
  
  		for (cnt = 0; cnt < wait_seconds; cnt++)
  		{
  			if ((pid = get_pgpid()) != 0)
  			{
! 				print_msg(".");
  				pg_usleep(1000000);		/* 1 sec */
  			}
  			else
***************
*** 641,654 ****
  
  		if (pid != 0)			/* pid file still exists */
  		{
! 			if (!silence_echo)
! 				printf(_("failed\n"));
  
  			write_stderr(_("%s: postmaster does not shut down\n"), progname);
  			exit(1);
  		}
! 		if (!silence_echo)
! 			printf(_("done\n"));
  
  		printf(_("postmaster stopped\n"));
  	}
--- 635,646 ----
  
  		if (pid != 0)			/* pid file still exists */
  		{
! 			print_msg(_(" failed\n"));
  
  			write_stderr(_("%s: postmaster does not shut down\n"), progname);
  			exit(1);
  		}
! 		print_msg(_(" done\n"));
  
  		printf(_("postmaster stopped\n"));
  	}
***************
*** 691,701 ****
  		exit(1);
  	}
  
! 	if (!silence_echo)
! 	{
! 		printf(_("waiting for postmaster to shut down..."));
! 		fflush(stdout);
! 	}
  
  	/* always wait for restart */
  
--- 683,689 ----
  		exit(1);
  	}
  
! 	print_msg(_("waiting for postmaster to shut down..."));
  
  	/* always wait for restart */
  
***************
*** 703,713 ****
  	{
  		if ((pid = get_pgpid()) != 0)
  		{
! 			if (!silence_echo)
! 			{
! 				printf(".");
! 				fflush(stdout);
! 			}
  			pg_usleep(1000000); /* 1 sec */
  		}
  		else
--- 691,697 ----
  	{
  		if ((pid = get_pgpid()) != 0)
  		{
! 			print_msg(".");
  			pg_usleep(1000000); /* 1 sec */
  		}
  		else
***************
*** 716,731 ****
  
  	if (pid != 0)				/* pid file still exists */
  	{
! 		if (!silence_echo)
! 			printf(_(" failed\n"));
  
  		write_stderr(_("%s: postmaster does not shut down\n"), progname);
  		exit(1);
  	}
  
! 	if (!silence_echo)
! 		printf(_("done\n"));
! 
  	printf(_("postmaster stopped\n"));
  	do_start();
  }
--- 700,712 ----
  
  	if (pid != 0)				/* pid file still exists */
  	{
! 		print_msg(_(" failed\n"));
  
  		write_stderr(_("%s: postmaster does not shut down\n"), progname);
  		exit(1);
  	}
  
! 	print_msg(_(" done\n"));
  	printf(_("postmaster stopped\n"));
  	do_start();
  }
***************
*** 760,767 ****
  		exit(1);
  	}
  
! 	if (!silence_echo)
! 		fprintf(stdout, _("postmaster signaled\n"));
  }
  
  /*
--- 741,747 ----
  		exit(1);
  	}
  
! 	print_msg(_("postmaster signaled\n"));
  }
  
  /*
***************
*** 1287,1293 ****
  					register_password = xstrdup(optarg);
  					break;
  				case 's':
! 					silence_echo = true;
  					break;
  				case 'U':
  					if (strchr(optarg, '\\'))
--- 1267,1273 ----
  					register_password = xstrdup(optarg);
  					break;
  				case 's':
! 					silent_mode = true;
  					break;
  				case 'U':
  					if (strchr(optarg, '\\'))
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to [EMAIL PROTECTED] so that your
      message can get through to the mailing list cleanly

Reply via email to