18
18
*
19
19
*
20
20
* 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 $
22
22
*
23
23
*-------------------------------------------------------------------------
24
24
*/
@@ -102,6 +102,7 @@ static volatile sig_atomic_t got_SIGHUP = false;
102
102
static pid_t syslogger_forkexec (void );
103
103
static void syslogger_parseArgs (int argc ,char * argv []);
104
104
#endif
105
+ static void write_syslogger_file_binary (const char * buffer ,int count );
105
106
#ifdef WIN32
106
107
static unsignedint __stdcallpipeThread (void * arg );
107
108
#endif
@@ -309,7 +310,7 @@ SysLoggerMain(int argc, char *argv[])
309
310
}
310
311
else if (bytesRead > 0 )
311
312
{
312
- write_syslogger_file (logbuffer ,bytesRead );
313
+ write_syslogger_file_binary (logbuffer ,bytesRead );
313
314
continue ;
314
315
}
315
316
else
@@ -494,13 +495,16 @@ SysLogger_Start(void)
494
495
close (syslogPipe [1 ]);
495
496
syslogPipe [1 ]= -1 ;
496
497
#else
498
+ int fd ;
499
+
497
500
fflush (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 )
501
504
ereport (FATAL ,
502
505
(errcode_for_file_access (),
503
506
errmsg ("could not redirect stderr: %m" )));
507
+ close (fd );
504
508
/* Now we are done with the write end of the pipe. */
505
509
CloseHandle (syslogPipe [1 ]);
506
510
syslogPipe [1 ]= 0 ;
@@ -590,7 +594,7 @@ syslogger_parseArgs(int argc, char *argv[])
590
594
if (fd != 0 )
591
595
{
592
596
fd = _open_osfhandle (fd ,_O_APPEND );
593
- if (fd != 0 )
597
+ if (fd > 0 )
594
598
{
595
599
syslogFile = fdopen (fd ,"a" );
596
600
setvbuf (syslogFile ,NULL ,LBF_MODE ,0 );
@@ -609,14 +613,56 @@ syslogger_parseArgs(int argc, char *argv[])
609
613
*/
610
614
611
615
/*
612
- * Write to the currently open logfile
616
+ * Writetext to the currently open logfile
613
617
*
614
618
* This is exported so that elog.c can call it when am_syslogger is true.
615
619
* This allows the syslogger process to record elog messages of its own,
616
620
* even though its stderr does not point at the syslog pipe.
617
621
*/
618
622
void
619
623
write_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 )
620
666
{
621
667
int rc ;
622
668
@@ -664,7 +710,7 @@ pipeThread(void *arg)
664
710
errmsg ("could not read from logger pipe: %m" )));
665
711
}
666
712
else if (bytesRead > 0 )
667
- write_syslogger_file (logbuffer ,bytesRead );
713
+ write_syslogger_file_binary (logbuffer ,bytesRead );
668
714
}
669
715
670
716
/* We exit the above loop only upon detecting pipe EOF */