mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-07-31 22:23:24 +02:00
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 1c4e88e2f
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
This commit is contained in:
parent
331da659be
commit
601d419b2b
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@ -738,9 +739,6 @@ pg_current_logfile(PG_FUNCTION_ARGS)
|
|||||||
FILE *fd;
|
FILE *fd;
|
||||||
char lbuffer[MAXPGPATH];
|
char lbuffer[MAXPGPATH];
|
||||||
char *logfmt;
|
char *logfmt;
|
||||||
char *log_filepath;
|
|
||||||
char *log_format = lbuffer;
|
|
||||||
char *nlpos;
|
|
||||||
|
|
||||||
/* The log format parameter is optional */
|
/* The log format parameter is optional */
|
||||||
if (PG_NARGS() == 0 || PG_ARGISNULL(0))
|
if (PG_NARGS() == 0 || PG_ARGISNULL(0))
|
||||||
@ -767,16 +765,23 @@ pg_current_logfile(PG_FUNCTION_ARGS)
|
|||||||
PG_RETURN_NULL();
|
PG_RETURN_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
/* syslogger.c writes CRLF line endings on Windows */
|
||||||
|
_setmode(_fileno(fd), _O_TEXT);
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read the file to gather current log filename(s) registered by the
|
* Read the file to gather current log filename(s) registered by the
|
||||||
* syslogger.
|
* syslogger.
|
||||||
*/
|
*/
|
||||||
while (fgets(lbuffer, sizeof(lbuffer), fd) != NULL)
|
while (fgets(lbuffer, sizeof(lbuffer), fd) != NULL)
|
||||||
{
|
{
|
||||||
/*
|
char *log_format;
|
||||||
* Extract log format and log file path from the line; lbuffer ==
|
char *log_filepath;
|
||||||
* log_format, they share storage.
|
char *nlpos;
|
||||||
*/
|
|
||||||
|
/* Extract log format and log file path from the line. */
|
||||||
|
log_format = lbuffer;
|
||||||
log_filepath = strchr(lbuffer, ' ');
|
log_filepath = strchr(lbuffer, ' ');
|
||||||
if (log_filepath == NULL)
|
if (log_filepath == NULL)
|
||||||
{
|
{
|
||||||
|
@ -3,7 +3,7 @@ use warnings;
|
|||||||
|
|
||||||
use PostgresNode;
|
use PostgresNode;
|
||||||
use TestLib;
|
use TestLib;
|
||||||
use Test::More tests => 4;
|
use Test::More tests => 5;
|
||||||
use Time::HiRes qw(usleep);
|
use Time::HiRes qw(usleep);
|
||||||
|
|
||||||
# Set up node with logging collector
|
# Set up node with logging collector
|
||||||
@ -47,6 +47,10 @@ for (my $attempts = 0; $attempts < $max_attempts; $attempts++)
|
|||||||
|
|
||||||
like($first_logfile, qr/division by zero/, 'found expected log file content');
|
like($first_logfile, qr/division by zero/, 'found expected log file content');
|
||||||
|
|
||||||
|
# While we're at it, test pg_current_logfile() function
|
||||||
|
is($node->safe_psql('postgres', "SELECT pg_current_logfile('stderr')"),
|
||||||
|
$lfname, 'pg_current_logfile() gives correct answer');
|
||||||
|
|
||||||
# Sleep 2 seconds and ask for log rotation; this should result in
|
# Sleep 2 seconds and ask for log rotation; this should result in
|
||||||
# output into a different log file name.
|
# output into a different log file name.
|
||||||
sleep(2);
|
sleep(2);
|
||||||
|
Loading…
Reference in New Issue
Block a user