Index: src/backend/postmaster/Makefile
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/Makefile,v
retrieving revision 1.18
diff -u -r1.18 Makefile
--- src/backend/postmaster/Makefile	21 Jul 2004 20:34:46 -0000	1.18
+++ src/backend/postmaster/Makefile	29 Jul 2004 08:41:52 -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: src/backend/postmaster/postmaster.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v
retrieving revision 1.416
diff -u -r1.416 postmaster.c
--- src/backend/postmaster/postmaster.c	27 Jul 2004 01:46:03 -0000	1.416
+++ src/backend/postmaster/postmaster.c	29 Jul 2004 08:41:57 -0000
@@ -118,7 +118,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
@@ -201,6 +201,7 @@
 			BgWriterPID = 0,
 			PgArchPID = 0,
 			PgStatPID = 0;
+pid_t       SysLoggerPID = 0;
 
 /* Startup/shutdown state */
 #define			NoShutdown		0
@@ -852,6 +853,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
@@ -1231,6 +1238,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.
@@ -1771,6 +1783,9 @@
 			kill(BgWriterPID, SIGHUP);
 		if (PgArchPID != 0)
 			kill(PgArchPID, SIGHUP);
+		if (SysLoggerPID != 0)
+		    SysLogger_SIGHUP();
+
 		/* PgStatPID does not currently need SIGHUP */
 		load_hba();
 		load_ident();
@@ -1836,7 +1851,6 @@
 			if (PgStatPID != 0)
 				kill(PgStatPID, SIGQUIT);
 			break;
-
 		case SIGINT:
 			/*
 			 * Fast Shutdown:
@@ -2065,6 +2079,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.
 		 */
@@ -2968,7 +2991,18 @@
 		PgstatCollectorMain(argc, argv);
 		proc_exit(0);
 	}
+#ifndef WIN32
+	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);
+	}
+#endif
 	return 1;					/* shouldn't get here */
 }
 
@@ -3024,7 +3058,6 @@
 		if (Shutdown <= SmartShutdown)
 			SignalChildren(SIGUSR1);
 	}
- 
 	if (PgArchPID != 0 && Shutdown == NoShutdown)
 	{
 		if (CheckPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER))
@@ -3036,6 +3069,10 @@
             kill(PgArchPID, SIGUSR1);
 		}
     }
+	if (CheckPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE) && SysLoggerPID != 0)
+	{
+	    SysLogger_SIGUSR1();
+	}
 
 	PG_SETMASK(&UnBlockSig);
 
Index: src/backend/utils/error/elog.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/error/elog.c,v
retrieving revision 1.143
diff -u -r1.143 elog.c
--- src/backend/utils/error/elog.c	28 Jul 2004 22:05:46 -0000	1.143
+++ src/backend/utils/error/elog.c	29 Jul 2004 08:42:01 -0000
@@ -82,6 +82,10 @@
 static void write_eventlog(int level, const char *line);
 #endif
 
+/* in syslogger.c */
+extern FILE *syslogFile;
+extern FILE *realStdErr;
+extern pid_t SysLoggerPID;
 /*
  * ErrorData holds the data accumulated during any one ereport() cycle.
  * Any non-NULL pointers must point to palloc'd data in ErrorContext.
@@ -1440,10 +1444,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 && realStdErr != 0)
+		{
+		    /*
+			 * If realStdErr is not null in the SysLogger process,
+			 * there's something really wrong because stderr is probably
+			 * redirected to the pipe. To avoid circular writes, we
+			 * write to realStdErr which is hopefully the stderr the postmaster
+			 * was started with.
+			 */
+		    fprintf(realStdErr, "%s", buf.data);
+		}
+		else
+		    fprintf(stderr, "%s", buf.data) ;
+
+		/* syslogFile is open in SysLogger only */
+		if (syslogFile != 0)
+		    fprintf(syslogFile, "%s", buf.data) ;
 	}
 
 	pfree(buf.data);
Index: src/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
--- src/backend/utils/misc/guc.c	28 Jul 2004 14:23:29 -0000	1.225
+++ src/backend/utils/misc/guc.c	29 Jul 2004 08:42:10 -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
@@ -1623,9 +1641,9 @@
 		&session_authorization_string,
 		NULL, assign_session_authorization, show_session_authorization
 	},
-
+#ifdef WIN32
 	{
-		{"log_destination", PGC_SIGHUP, LOGGING_WHERE,
+		{"log_destination", PGC_POSTMASTER, LOGGING_WHERE,
 		 gettext_noop("Sets the destination for server log output."),
 		 gettext_noop("Valid values are combinations of stderr, syslog "
 					  "and eventlog, depending on platform."),
@@ -1634,6 +1652,37 @@
 		&log_destination_string,
 		"stderr", assign_log_destination, NULL
 	},
+#else
+	{
+		{"log_destination", PGC_POSTMASTER, LOGGING_WHERE,
+		 gettext_noop("Sets the destination for server log output."),
+		 gettext_noop("Valid values are combinations of stderr, file, syslog "
+					  "and eventlog, depending on platform."),
+		 GUC_LIST_INPUT
+		},
+		&log_destination_string,
+		"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 +5128,10 @@
 	
 		if (pg_strcasecmp(tok,"stderr") == 0)
 			newlogdest |= LOG_DESTINATION_STDERR;
+#ifndef WIN32
+		else if (pg_strcasecmp(tok,"file") == 0)
+			newlogdest |= LOG_DESTINATION_FILE;
+#endif
 #ifdef HAVE_SYSLOG
 		else if (pg_strcasecmp(tok,"syslog") == 0)
 			newlogdest |= LOG_DESTINATION_SYSLOG;
Index: src/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
--- src/backend/utils/misc/postgresql.conf.sample	21 Jul 2004 20:34:46 -0000	1.118
+++ src/backend/utils/misc/postgresql.conf.sample	29 Jul 2004 08:42:11 -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: src/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
--- src/include/storage/pmsignal.h	19 Jul 2004 02:47:15 -0000	1.9
+++ src/include/storage/pmsignal.h	29 Jul 2004 08:42:12 -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: src/include/utils/elog.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/utils/elog.h,v
retrieving revision 1.70
diff -u -r1.70 elog.h
--- src/include/utils/elog.h	6 Jul 2004 19:51:59 -0000	1.70
+++ src/include/utils/elog.h	29 Jul 2004 08:42:13 -0000
@@ -185,6 +185,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);
