Skip to content

Commit 2564e2d

Browse files
committed
Fix pg_current_logfile() to not emit a carriage return on Windows.
Due to not having our signals straight about CRLF vs. LF line termination, the output of pg_current_logfile() included a trailing \r on Windows. To fix, force the file descriptor it uses into text mode. While here, move a couple of local variable declarations to make the function's logic clearer. In v12 and v13, also back-patch the test added by 1c4e88e so that this function has some test coverage. However, the 004_logrotate.pl test script doesn't exist before v12, and it didn't seem worth adding to older branches just for this. Per report from Thomas Kellerer. Back-patch to v10 where this function was added. Discussion: https://postgr.es/m/412ae8da-76bb-640f-039a-f3513499e53d@gmx.net
1 parent 0df3f9c commit 2564e2d

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/backend/utils/adt/misc.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <sys/file.h>
1818
#include <dirent.h>
19+
#include <fcntl.h>
1920
#include <math.h>
2021
#include <unistd.h>
2122

@@ -739,9 +740,6 @@ pg_current_logfile(PG_FUNCTION_ARGS)
739740
FILE *fd;
740741
char lbuffer[MAXPGPATH];
741742
char *logfmt;
742-
char *log_filepath;
743-
char *log_format = lbuffer;
744-
char *nlpos;
745743

746744
/* The log format parameter is optional */
747745
if (PG_NARGS() == 0 || PG_ARGISNULL(0))
@@ -768,16 +766,23 @@ pg_current_logfile(PG_FUNCTION_ARGS)
768766
PG_RETURN_NULL();
769767
}
770768

769+
#ifdef WIN32
770+
/* syslogger.c writes CRLF line endings on Windows */
771+
_setmode(_fileno(fd), _O_TEXT);
772+
#endif
773+
771774
/*
772775
* Read the file to gather current log filename(s) registered by the
773776
* syslogger.
774777
*/
775778
while (fgets(lbuffer, sizeof(lbuffer), fd) != NULL)
776779
{
777-
/*
778-
* Extract log format and log file path from the line; lbuffer ==
779-
* log_format, they share storage.
780-
*/
780+
char *log_format;
781+
char *log_filepath;
782+
char *nlpos;
783+
784+
/* Extract log format and log file path from the line. */
785+
log_format = lbuffer;
781786
log_filepath = strchr(lbuffer, ' ');
782787
if (log_filepath == NULL)
783788
{

src/bin/pg_ctl/t/004_logrotate.pl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 4;
6+
use Test::More tests => 5;
77
use Time::HiRes qw(usleep);
88

99
# Set up node with logging collector
@@ -47,6 +47,10 @@
4747

4848
like($first_logfile, qr/division by zero/, 'found expected log file content');
4949

50+
# While we're at it, test pg_current_logfile() function
51+
is($node->safe_psql('postgres', "SELECT pg_current_logfile('stderr')"),
52+
$lfname, 'pg_current_logfile() gives correct answer');
53+
5054
# Sleep 2 seconds and ask for log rotation; this should result in
5155
# output into a different log file name.
5256
sleep(2);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy