mirror of
https://github.com/postgres/postgres.git
synced 2025-08-18 12:22:09 +03:00
Implement %m in src/port/snprintf.c, and teach elog.c to rely on that.
I started out with the idea that we needed to detect use of %m format specs in contexts other than elog/ereport calls, because we couldn't rely on that working in *printf calls. But a better answer is to fix things so that it does work. Now that we're using snprintf.c all the time, we can implement %m in that and we've fixed the problem. This requires also adjusting our various printf-wrapping functions so that they ensure "errno" is preserved when they call snprintf.c. Remove elog.c's handmade implementation of %m, and let it rely on snprintf to support the feature. That should provide some performance gain, though I've not attempted to measure it. There are a lot of places where we could now simplify 'printf("%s", strerror(errno))' into 'printf("%m")', but I'm not in any big hurry to make that happen. Patch by me, reviewed by Michael Paquier Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
This commit is contained in:
@@ -233,6 +233,7 @@ enlargePQExpBuffer(PQExpBuffer str, size_t needed)
|
||||
void
|
||||
printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
|
||||
{
|
||||
int save_errno = errno;
|
||||
va_list args;
|
||||
bool done;
|
||||
|
||||
@@ -244,6 +245,7 @@ printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
|
||||
/* Loop in case we have to retry after enlarging the buffer. */
|
||||
do
|
||||
{
|
||||
errno = save_errno;
|
||||
va_start(args, fmt);
|
||||
done = appendPQExpBufferVA(str, fmt, args);
|
||||
va_end(args);
|
||||
@@ -261,6 +263,7 @@ printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
|
||||
void
|
||||
appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
|
||||
{
|
||||
int save_errno = errno;
|
||||
va_list args;
|
||||
bool done;
|
||||
|
||||
@@ -270,6 +273,7 @@ appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
|
||||
/* Loop in case we have to retry after enlarging the buffer. */
|
||||
do
|
||||
{
|
||||
errno = save_errno;
|
||||
va_start(args, fmt);
|
||||
done = appendPQExpBufferVA(str, fmt, args);
|
||||
va_end(args);
|
||||
@@ -281,6 +285,9 @@ appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
|
||||
* Shared guts of printfPQExpBuffer/appendPQExpBuffer.
|
||||
* Attempt to format data and append it to str. Returns true if done
|
||||
* (either successful or hard failure), false if need to retry.
|
||||
*
|
||||
* Caution: callers must be sure to preserve their entry-time errno
|
||||
* when looping, in case the fmt contains "%m".
|
||||
*/
|
||||
static bool
|
||||
appendPQExpBufferVA(PQExpBuffer str, const char *fmt, va_list args)
|
||||
|
Reference in New Issue
Block a user