Index: backend/postmaster/Makefile
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/Makefile,v
retrieving revision 1.18
diff -u -r1.18 Makefile
--- backend/postmaster/Makefile	21 Jul 2004 20:34:46 -0000	1.18
+++ backend/postmaster/Makefile	4 Aug 2004 18:40:01 -0000
@@ -12,7 +12,7 @@
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS = postmaster.o bgwriter.o pgstat.o pgarch.o
+OBJS = postmaster.o bgwriter.o pgstat.o pgarch.o syslogger.o
 
 all: SUBSYS.o
 
Index: backend/postmaster/postmaster.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v
retrieving revision 1.418
diff -u -r1.418 postmaster.c
--- backend/postmaster/postmaster.c	1 Aug 2004 17:45:43 -0000	1.418
+++ backend/postmaster/postmaster.c	4 Aug 2004 18:40:06 -0000
@@ -117,7 +117,7 @@
 #include "utils/ps_status.h"
 #include "bootstrap/bootstrap.h"
 #include "pgstat.h"
-
+#include "postmaster/syslogger.h"
 
 /*
  * List of active backends (or child processes anyway; we don't actually
@@ -200,6 +200,7 @@
 			BgWriterPID = 0,
 			PgArchPID = 0,
 			PgStatPID = 0;
+pid_t       SysLoggerPID = 0;
 
 /* Startup/shutdown state */
 #define			NoShutdown		0
@@ -851,6 +852,12 @@
 #endif
 
 	/*
+	 * start logging to file
+	 */ 
+
+    SysLoggerPID = SysLogger_Start();
+
+	/*
 	 * Reset whereToSendOutput from Debug (its starting state) to None.
 	 * This stops ereport from sending log messages to stderr unless
 	 * Log_destination permits.  We don't do this until the postmaster
@@ -1230,6 +1237,11 @@
 			StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
 			PgStatPID = pgstat_start();
 
+		/* If we have lost the system logger, try to start a new one */
+		if (SysLoggerPID == 0 &&
+			StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
+			SysLoggerPID = SysLogger_Start();
+
 		/*
 		 * Touch the socket and lock file at least every ten minutes, to ensure
 		 * that they are not removed by overzealous /tmp-cleaning tasks.
@@ -1770,6 +1782,8 @@
 			kill(BgWriterPID, SIGHUP);
 		if (PgArchPID != 0)
 			kill(PgArchPID, SIGHUP);
+		if (SysLoggerPID != 0)
+			kill(SysLoggerPID, SIGHUP);
 		/* PgStatPID does not currently need SIGHUP */
 		load_hba();
 		load_ident();
@@ -1836,7 +1850,7 @@
 				kill(PgStatPID, SIGQUIT);
 			break;
 
-		case SIGINT:
+                        case SIGINT:
 			/*
 			 * Fast Shutdown:
 			 *
@@ -2064,6 +2078,15 @@
 			continue;
 		}
 
+		/* was it the system logger, try to start a new one */
+		if (SysLoggerPID != 0 && pid == SysLoggerPID)
+		{
+			if (exitstatus != 0)
+				LogChildExit(LOG, gettext("system logger process"),
+							 pid, exitstatus);
+			SysLoggerPID = SysLogger_Start();
+			continue;
+		}
 		/*
 		 * Else do standard backend child cleanup.
 		 */
@@ -2967,7 +2990,16 @@
 		PgstatCollectorMain(argc, argv);
 		proc_exit(0);
 	}
+	if (strcmp(argv[1], "-forklog") == 0)
+	{
+		/* Close the postmaster's sockets */
+		ClosePostmasterPorts();
 
+		/* Do not want to attach to shared memory */
+
+		SysLoggerMain(argc, argv);
+		proc_exit(0);
+	}
 	return 1;					/* shouldn't get here */
 }
 
@@ -3023,7 +3055,7 @@
 		if (Shutdown <= SmartShutdown)
 			SignalChildren(SIGUSR1);
 	}
- 
+
 	if (PgArchPID != 0 && Shutdown == NoShutdown)
 	{
 		if (CheckPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER))
@@ -3036,6 +3068,11 @@
 		}
     }
 
+    if (CheckPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE) && SysLoggerPID != 0)
+    {
+        kill(SysLoggerPID, SIGUSR1);
+    }
+
 	PG_SETMASK(&UnBlockSig);
 
 	errno = save_errno;
Index: backend/utils/error/elog.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/error/elog.c,v
retrieving revision 1.144
diff -u -r1.144 elog.c
--- backend/utils/error/elog.c	31 Jul 2004 00:45:38 -0000	1.144
+++ backend/utils/error/elog.c	4 Aug 2004 18:40:10 -0000
@@ -71,7 +71,11 @@
 /* GUC parameters */
 PGErrorVerbosity Log_error_verbosity = PGERROR_VERBOSE;
 char       *Log_line_prefix = NULL; /* format for extra log line info */
+#ifdef WIN32
+unsigned int Log_destination = LOG_DESTINATION_FILE;
+#else
 unsigned int Log_destination = LOG_DESTINATION_STDERR;
+#endif
 
 #ifdef HAVE_SYSLOG
 char	   *Syslog_facility;	/* openlog() parameters */
@@ -83,6 +87,11 @@
 static void write_eventlog(int level, const char *line);
 #endif
 
+/* in syslogger.c */
+extern FILE *syslogFile;
+extern FILE *realStdErr;
+extern pid_t SysLoggerPID;
+
 /* We provide a small stack of ErrorData records for re-entrant cases */
 #define ERRORDATA_STACK_SIZE  5
 
@@ -1554,7 +1563,8 @@
 	}
 #endif   /* HAVE_SYSLOG */
 #ifdef WIN32
-	if (Log_destination & LOG_DESTINATION_EVENTLOG)
+	if ((Log_destination & LOG_DESTINATION_EVENTLOG)
+            || edata->elevel >= FATAL)
 	{
 		int eventlog_level;
 		switch (edata->elevel) 
@@ -1583,10 +1593,31 @@
 		write_eventlog(eventlog_level, buf.data);
 	}
 #endif   /* WIN32 */
-	/* Write to stderr, if enabled */
-	if ((Log_destination & LOG_DESTINATION_STDERR) || whereToSendOutput == Debug)
+	/*
+	 * Write to stderr. If Log_destination is file or stderr
+	 * if file is target, the logger process will handle this
+	 */
+	if ((Log_destination & (LOG_DESTINATION_STDERR | LOG_DESTINATION_FILE)) 
+		|| whereToSendOutput == Debug)
 	{
-		fprintf(stderr, "%s", buf.data);
+	    if (SysLoggerPID == MyProcPid || syslogFile != 0)
+            {
+                if ((Log_destination & LOG_DESTINATION_STDERR)
+                            || whereToSendOutput == Debug)
+                {
+                     if (realStdErr != 0)
+                        fprintf(realStdErr, "%s", buf.data);
+                    else
+                        fprintf(stderr, "%s", buf.data) ;
+
+                }
+
+                if (syslogFile != 0)
+                    fprintf(syslogFile, "%s", buf.data) ;
+            }
+            else
+		fprintf(stderr, "%s", buf.data) ;
+
 	}
 
 	pfree(buf.data);
Index: backend/utils/misc/guc.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/guc.c,v
retrieving revision 1.225
diff -u -r1.225 guc.c
--- backend/utils/misc/guc.c	28 Jul 2004 14:23:29 -0000	1.225
+++ backend/utils/misc/guc.c	4 Aug 2004 18:40:19 -0000
@@ -44,6 +44,7 @@
 #include "parser/parse_expr.h"
 #include "parser/parse_relation.h"
 #include "postmaster/bgwriter.h"
+#include "postmaster/syslogger.h"
 #include "postmaster/postmaster.h"
 #include "storage/bufmgr.h"
 #include "storage/fd.h"
@@ -1285,6 +1286,23 @@
 		BLCKSZ, BLCKSZ, BLCKSZ, NULL, NULL
 	},
 
+	{
+	  {"log_rotation_age", PGC_SIGHUP, LOGGING_WHERE,
+	   gettext_noop("Automatic logfile rotation will occur after n minutes"),
+	   NULL
+	  },
+	  &Log_RotationAge,
+	  24*60, 0, INT_MAX, NULL, NULL
+	},
+	{
+	  {"log_rotation_size", PGC_SIGHUP, LOGGING_WHERE,
+	   gettext_noop("Automatic logfile rotation will occur if this size is reached (in kb)"),
+	   NULL
+	  },
+	  &Log_RotationSize,
+	  10*1024, 0, INT_MAX, NULL, NULL
+	},
+
 	/* End-of-list marker */
 	{
 		{NULL, 0, 0, NULL, NULL}, NULL, 0, 0, 0, NULL, NULL
@@ -1625,14 +1643,37 @@
 	},
 
 	{
-		{"log_destination", PGC_SIGHUP, LOGGING_WHERE,
-		 gettext_noop("Sets the destination for server log output."),
-		 gettext_noop("Valid values are combinations of stderr, syslog "
+		{"log_destination", PGC_POSTMASTER, LOGGING_WHERE,
+		 gettext_noop("Sets the target for log output."),
+		 gettext_noop("Valid values are combinations of stderr, file, syslog "
 					  "and eventlog, depending on platform."),
 		 GUC_LIST_INPUT
 		},
 		&log_destination_string,
+#ifdef WIN32
+		"file", assign_log_destination, NULL
+#else
 		"stderr", assign_log_destination, NULL
+#endif
+        },
+	{
+		{"log_directory", PGC_SIGHUP, LOGGING_WHERE,
+		 gettext_noop("Sets the target directory for log output."),
+		 gettext_noop("May be specified as relative to the cluster directory "
+					  "or as absolute path."),
+		 GUC_LIST_INPUT | GUC_REPORT
+		},
+		&Log_directory,
+		"pg_log", NULL, NULL
+	},
+	{
+		{"log_filename_prefix", PGC_SIGHUP, LOGGING_WHERE,
+		 gettext_noop("prefix for logfile names created in the log_directory."),
+		 NULL,
+		 GUC_LIST_INPUT | GUC_REPORT
+		},
+		&Log_filename_prefix,
+		"postgresql-", NULL, NULL
 	},
 
 #ifdef HAVE_SYSLOG
@@ -5079,6 +5120,8 @@
 	
 		if (pg_strcasecmp(tok,"stderr") == 0)
 			newlogdest |= LOG_DESTINATION_STDERR;
+		else if (pg_strcasecmp(tok,"file") == 0)
+			newlogdest |= LOG_DESTINATION_FILE;
 #ifdef HAVE_SYSLOG
 		else if (pg_strcasecmp(tok,"syslog") == 0)
 			newlogdest |= LOG_DESTINATION_SYSLOG;
Index: backend/utils/misc/postgresql.conf.sample
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/postgresql.conf.sample,v
retrieving revision 1.118
diff -u -r1.118 postgresql.conf.sample
--- backend/utils/misc/postgresql.conf.sample	21 Jul 2004 20:34:46 -0000	1.118
+++ backend/utils/misc/postgresql.conf.sample	4 Aug 2004 18:40:20 -0000
@@ -167,9 +167,17 @@
 
 # - Where to Log -
 
-#log_destination = 'stderr'	# Valid values are combinations of stderr,
-                                # syslog and eventlog, depending on
-                                # platform.
+#log_destination = 'stderr' # Valid values are combinations of stderr, file,
+                            # syslog and eventlog, depending on platform.
+#log_directory = 'pg_log'   # subdirectory where logfiles are written 
+                            # if 'file' log_destination is used.
+                            # May be specified absolute or relative to PGDATA
+#log_filename_prefix = 'postgresql_' # prefix for logfile names
+#log_rotation_age = 1440    # Automatic rotation of logfiles will happen if 
+                            # specified age in minutes is reached. 0 to disable.
+#log_rotation_size = 10240  # Automatic rotation of logfiles will happen if 
+                            # specified size in kb is reached. 0 to disable.
+
 #syslog_facility = 'LOCAL0'
 #syslog_ident = 'postgres'
 
Index: include/storage/pmsignal.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/storage/pmsignal.h,v
retrieving revision 1.9
diff -u -r1.9 pmsignal.h
--- include/storage/pmsignal.h	19 Jul 2004 02:47:15 -0000	1.9
+++ include/storage/pmsignal.h	4 Aug 2004 18:40:21 -0000
@@ -25,7 +25,7 @@
 	PMSIGNAL_PASSWORD_CHANGE,	/* pg_pwd file has changed */
 	PMSIGNAL_WAKEN_CHILDREN,	/* send a SIGUSR1 signal to all backends */
 	PMSIGNAL_WAKEN_ARCHIVER,	/* send a NOTIFY signal to xlog archiver */
-
+	PMSIGNAL_ROTATE_LOGFILE,	/* send SIGUSR1 to syslogger to rotate logfile */
 	NUM_PMSIGNALS				/* Must be last value of enum! */
 } PMSignalReason;
 
Index: elog.h===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/utils/elog.h,v
retrieving revision 1.72
diff -u -r1.72 elog.h
--- elog.h	31 Jul 2004 23:04:55 -0000	1.72
+++ elog.h	4 Aug 2004 18:45:49 -0000
@@ -278,6 +278,7 @@
 #define LOG_DESTINATION_STDERR   1
 #define LOG_DESTINATION_SYSLOG   2
 #define LOG_DESTINATION_EVENTLOG 4
+#define LOG_DESTINATION_FILE     8
 
 /* Other exported functions */
 extern void DebugFileOpen(void);



