1818 *
1919 *
2020 * IDENTIFICATION
21- * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.3 2004/08/0616:06:59 tgl Exp $
21+ * $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.4 2004/08/0619:17:31 tgl Exp $
2222 *
2323 *-------------------------------------------------------------------------
2424 */
@@ -102,6 +102,7 @@ static volatile sig_atomic_t got_SIGHUP = false;
102102static pid_t syslogger_forkexec (void );
103103static void syslogger_parseArgs (int argc ,char * argv []);
104104#endif
105+ static void write_syslogger_file_binary (const char * buffer ,int count );
105106#ifdef WIN32
106107static unsignedint __stdcallpipeThread (void * arg );
107108#endif
@@ -309,7 +310,7 @@ SysLoggerMain(int argc, char *argv[])
309310}
310311else if (bytesRead > 0 )
311312{
312- write_syslogger_file (logbuffer ,bytesRead );
313+ write_syslogger_file_binary (logbuffer ,bytesRead );
313314continue ;
314315}
315316else
@@ -494,13 +495,16 @@ SysLogger_Start(void)
494495close (syslogPipe [1 ]);
495496syslogPipe [1 ]= -1 ;
496497#else
498+ int fd ;
499+
497500fflush (stderr );
498- if ( dup2 ( _open_osfhandle ((long )syslogPipe [1 ],
499- _O_APPEND |_O_TEXT ),
500- _fileno (stderr ))< 0 )
501+ fd = _open_osfhandle ((long )syslogPipe [1 ],
502+ _O_APPEND |_O_TEXT );
503+ if ( dup2 ( fd , _fileno (stderr ))< 0 )
501504ereport (FATAL ,
502505(errcode_for_file_access (),
503506errmsg ("could not redirect stderr: %m" )));
507+ close (fd );
504508/* Now we are done with the write end of the pipe. */
505509CloseHandle (syslogPipe [1 ]);
506510syslogPipe [1 ]= 0 ;
@@ -590,7 +594,7 @@ syslogger_parseArgs(int argc, char *argv[])
590594if (fd != 0 )
591595{
592596fd = _open_osfhandle (fd ,_O_APPEND );
593- if (fd != 0 )
597+ if (fd > 0 )
594598{
595599syslogFile = fdopen (fd ,"a" );
596600setvbuf (syslogFile ,NULL ,LBF_MODE ,0 );
@@ -609,14 +613,56 @@ syslogger_parseArgs(int argc, char *argv[])
609613 */
610614
611615/*
612- * Write to the currently open logfile
616+ * Writetext to the currently open logfile
613617 *
614618 * This is exported so that elog.c can call it when am_syslogger is true.
615619 * This allows the syslogger process to record elog messages of its own,
616620 * even though its stderr does not point at the syslog pipe.
617621 */
618622void
619623write_syslogger_file (const char * buffer ,int count )
624+ {
625+ #ifdef WIN32
626+ /*
627+ * On Windows we need to do our own newline-to-CRLF translation.
628+ */
629+ char convbuf [256 ];
630+ char * p ;
631+ int n ;
632+
633+ p = convbuf ;
634+ n = 0 ;
635+ while (count -- > 0 )
636+ {
637+ if (* buffer == '\n' )
638+ {
639+ * p ++ = '\r' ;
640+ n ++ ;
641+ }
642+ * p ++ = * buffer ++ ;
643+ n ++ ;
644+ if (n >=sizeof (convbuf )- 1 )
645+ {
646+ write_syslogger_file_binary (convbuf ,n );
647+ p = convbuf ;
648+ n = 0 ;
649+ }
650+ }
651+ if (n > 0 )
652+ write_syslogger_file_binary (convbuf ,n );
653+ #else /* !WIN32 */
654+ write_syslogger_file_binary (buffer ,count );
655+ #endif
656+ }
657+
658+ /*
659+ * Write binary data to the currently open logfile
660+ *
661+ * On Windows the data arriving in the pipe already has CR/LF newlines,
662+ * so we must send it to the file without further translation.
663+ */
664+ static void
665+ write_syslogger_file_binary (const char * buffer ,int count )
620666{
621667int rc ;
622668
@@ -664,7 +710,7 @@ pipeThread(void *arg)
664710errmsg ("could not read from logger pipe: %m" )));
665711}
666712else if (bytesRead > 0 )
667- write_syslogger_file (logbuffer ,bytesRead );
713+ write_syslogger_file_binary (logbuffer ,bytesRead );
668714 }
669715
670716/* We exit the above loop only upon detecting pipe EOF */