Commit ab5c7751 authored by Tom Lane's avatar Tom Lane

Change pqInternalNotice to accept a format string and args instead of

just a preformatted message; per suggestion by Sean Chittenden.
parent 40862532
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.251 2003/06/23 17:03:19 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.252 2003/06/23 19:20:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -2843,8 +2843,9 @@ static void ...@@ -2843,8 +2843,9 @@ static void
defaultNoticeReceiver(void *arg, const PGresult *res) defaultNoticeReceiver(void *arg, const PGresult *res)
{ {
(void) arg; /* not used */ (void) arg; /* not used */
(*res->noticeHooks.noticeProc) (res->noticeHooks.noticeProcArg, if (res->noticeHooks.noticeProc != NULL)
PQresultErrorMessage(res)); (*res->noticeHooks.noticeProc) (res->noticeHooks.noticeProcArg,
PQresultErrorMessage(res));
} }
/* /*
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.139 2003/06/21 21:51:34 tgl Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.140 2003/06/23 19:20:24 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -440,18 +440,29 @@ pqPrepareAsyncResult(PGconn *conn) ...@@ -440,18 +440,29 @@ pqPrepareAsyncResult(PGconn *conn)
} }
/* /*
* pqInternalNotice - helper routine for internally-generated notices * pqInternalNotice - produce an internally-generated notice message
*
* A format string and optional arguments can be passed. Note that we do
* libpq_gettext() here, so callers need not.
* *
* The supplied text is taken as primary message (ie., it should not include * The supplied text is taken as primary message (ie., it should not include
* a trailing newline, and should not be more than one line). * a trailing newline, and should not be more than one line).
*/ */
void void
pqInternalNotice(const PGNoticeHooks *hooks, const char *msgtext) pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt, ...)
{ {
char msgBuf[1024];
va_list args;
PGresult *res; PGresult *res;
if (hooks->noticeRec == NULL) if (hooks->noticeRec == NULL)
return; /* nobody home? */ return; /* nobody home to receive notice? */
/* Format the message */
va_start(args, fmt);
vsnprintf(msgBuf, sizeof(msgBuf), libpq_gettext(fmt), args);
va_end(args);
msgBuf[sizeof(msgBuf)-1] = '\0'; /* make real sure it's terminated */
/* Make a PGresult to pass to the notice receiver */ /* Make a PGresult to pass to the notice receiver */
res = PQmakeEmptyPGresult(NULL, PGRES_NONFATAL_ERROR); res = PQmakeEmptyPGresult(NULL, PGRES_NONFATAL_ERROR);
...@@ -459,14 +470,14 @@ pqInternalNotice(const PGNoticeHooks *hooks, const char *msgtext) ...@@ -459,14 +470,14 @@ pqInternalNotice(const PGNoticeHooks *hooks, const char *msgtext)
/* /*
* Set up fields of notice. * Set up fields of notice.
*/ */
pqSaveMessageField(res, 'M', msgtext); pqSaveMessageField(res, 'M', msgBuf);
pqSaveMessageField(res, 'S', libpq_gettext("NOTICE")); pqSaveMessageField(res, 'S', libpq_gettext("NOTICE"));
/* XXX should provide a SQLSTATE too? */ /* XXX should provide a SQLSTATE too? */
/* /*
* Result text is always just the primary message + newline. * Result text is always just the primary message + newline.
*/ */
res->errMsg = (char *) pqResultAlloc(res, strlen(msgtext) + 2, FALSE); res->errMsg = (char *) pqResultAlloc(res, strlen(msgBuf) + 2, FALSE);
sprintf(res->errMsg, "%s\n", msgtext); sprintf(res->errMsg, "%s\n", msgBuf);
/* /*
* Pass to receiver, then free it. * Pass to receiver, then free it.
*/ */
...@@ -1585,16 +1596,13 @@ PQbinaryTuples(const PGresult *res) ...@@ -1585,16 +1596,13 @@ PQbinaryTuples(const PGresult *res)
static int static int
check_field_number(const PGresult *res, int field_num) check_field_number(const PGresult *res, int field_num)
{ {
char noticeBuf[128];
if (!res) if (!res)
return FALSE; /* no way to display error message... */ return FALSE; /* no way to display error message... */
if (field_num < 0 || field_num >= res->numAttributes) if (field_num < 0 || field_num >= res->numAttributes)
{ {
snprintf(noticeBuf, sizeof(noticeBuf), pqInternalNotice(&res->noticeHooks,
libpq_gettext("column number %d is out of range 0..%d"), "column number %d is out of range 0..%d",
field_num, res->numAttributes - 1); field_num, res->numAttributes - 1);
PGDONOTICE(res, noticeBuf);
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
...@@ -1604,24 +1612,20 @@ static int ...@@ -1604,24 +1612,20 @@ static int
check_tuple_field_number(const PGresult *res, check_tuple_field_number(const PGresult *res,
int tup_num, int field_num) int tup_num, int field_num)
{ {
char noticeBuf[128];
if (!res) if (!res)
return FALSE; /* no way to display error message... */ return FALSE; /* no way to display error message... */
if (tup_num < 0 || tup_num >= res->ntups) if (tup_num < 0 || tup_num >= res->ntups)
{ {
snprintf(noticeBuf, sizeof(noticeBuf), pqInternalNotice(&res->noticeHooks,
libpq_gettext("row number %d is out of range 0..%d"), "row number %d is out of range 0..%d",
tup_num, res->ntups - 1); tup_num, res->ntups - 1);
PGDONOTICE(res, noticeBuf);
return FALSE; return FALSE;
} }
if (field_num < 0 || field_num >= res->numAttributes) if (field_num < 0 || field_num >= res->numAttributes)
{ {
snprintf(noticeBuf, sizeof(noticeBuf), pqInternalNotice(&res->noticeHooks,
libpq_gettext("column number %d is out of range 0..%d"), "column number %d is out of range 0..%d",
field_num, res->numAttributes - 1); field_num, res->numAttributes - 1);
PGDONOTICE(res, noticeBuf);
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
...@@ -1822,7 +1826,6 @@ PQoidValue(const PGresult *res) ...@@ -1822,7 +1826,6 @@ PQoidValue(const PGresult *res)
char * char *
PQcmdTuples(PGresult *res) PQcmdTuples(PGresult *res)
{ {
char noticeBuf[128];
char *p; char *p;
if (!res) if (!res)
...@@ -1850,10 +1853,9 @@ PQcmdTuples(PGresult *res) ...@@ -1850,10 +1853,9 @@ PQcmdTuples(PGresult *res)
if (*p == 0) if (*p == 0)
{ {
snprintf(noticeBuf, sizeof(noticeBuf), pqInternalNotice(&res->noticeHooks,
libpq_gettext("could not interpret result from server: %s"), "could not interpret result from server: %s",
res->cmdStatus); res->cmdStatus);
PGDONOTICE(res, noticeBuf);
return ""; return "";
} }
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.97 2003/06/21 21:51:34 tgl Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.98 2003/06/23 19:20:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -206,7 +206,6 @@ pqGetInt(int *result, size_t bytes, PGconn *conn) ...@@ -206,7 +206,6 @@ pqGetInt(int *result, size_t bytes, PGconn *conn)
{ {
uint16 tmp2; uint16 tmp2;
uint32 tmp4; uint32 tmp4;
char noticeBuf[64];
switch (bytes) switch (bytes)
{ {
...@@ -225,10 +224,9 @@ pqGetInt(int *result, size_t bytes, PGconn *conn) ...@@ -225,10 +224,9 @@ pqGetInt(int *result, size_t bytes, PGconn *conn)
*result = (int) ntohl(tmp4); *result = (int) ntohl(tmp4);
break; break;
default: default:
snprintf(noticeBuf, sizeof(noticeBuf), pqInternalNotice(&conn->noticeHooks,
libpq_gettext("integer of size %lu not supported by pqGetInt"), "integer of size %lu not supported by pqGetInt",
(unsigned long) bytes); (unsigned long) bytes);
PGDONOTICE(conn, noticeBuf);
return EOF; return EOF;
} }
...@@ -248,7 +246,6 @@ pqPutInt(int value, size_t bytes, PGconn *conn) ...@@ -248,7 +246,6 @@ pqPutInt(int value, size_t bytes, PGconn *conn)
{ {
uint16 tmp2; uint16 tmp2;
uint32 tmp4; uint32 tmp4;
char noticeBuf[64];
switch (bytes) switch (bytes)
{ {
...@@ -263,10 +260,9 @@ pqPutInt(int value, size_t bytes, PGconn *conn) ...@@ -263,10 +260,9 @@ pqPutInt(int value, size_t bytes, PGconn *conn)
return EOF; return EOF;
break; break;
default: default:
snprintf(noticeBuf, sizeof(noticeBuf), pqInternalNotice(&conn->noticeHooks,
libpq_gettext("integer of size %lu not supported by pqPutInt"), "integer of size %lu not supported by pqPutInt",
(unsigned long) bytes); (unsigned long) bytes);
PGDONOTICE(conn, noticeBuf);
return EOF; return EOF;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-protocol2.c,v 1.3 2003/06/21 23:25:38 tgl Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-protocol2.c,v 1.4 2003/06/23 19:20:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -358,7 +358,6 @@ void ...@@ -358,7 +358,6 @@ void
pqParseInput2(PGconn *conn) pqParseInput2(PGconn *conn)
{ {
char id; char id;
char noticeWorkspace[128];
/* /*
* Loop to parse successive complete messages available in the buffer. * Loop to parse successive complete messages available in the buffer.
...@@ -424,10 +423,9 @@ pqParseInput2(PGconn *conn) ...@@ -424,10 +423,9 @@ pqParseInput2(PGconn *conn)
} }
else else
{ {
snprintf(noticeWorkspace, sizeof(noticeWorkspace), pqInternalNotice(&conn->noticeHooks,
libpq_gettext("message type 0x%02x arrived from server while idle"), "message type 0x%02x arrived from server while idle",
id); id);
PGDONOTICE(conn, noticeWorkspace);
/* Discard the unexpected message; good idea?? */ /* Discard the unexpected message; good idea?? */
conn->inStart = conn->inEnd; conn->inStart = conn->inEnd;
break; break;
...@@ -464,12 +462,9 @@ pqParseInput2(PGconn *conn) ...@@ -464,12 +462,9 @@ pqParseInput2(PGconn *conn)
if (pqGetc(&id, conn)) if (pqGetc(&id, conn))
return; return;
if (id != '\0') if (id != '\0')
{ pqInternalNotice(&conn->noticeHooks,
snprintf(noticeWorkspace, sizeof(noticeWorkspace), "unexpected character %c following empty query response (\"I\" message)",
libpq_gettext("unexpected character %c following empty query response (\"I\" message)"), id);
id);
PGDONOTICE(conn, noticeWorkspace);
}
if (conn->result == NULL) if (conn->result == NULL)
conn->result = PQmakeEmptyPGresult(conn, conn->result = PQmakeEmptyPGresult(conn,
PGRES_EMPTY_QUERY); PGRES_EMPTY_QUERY);
...@@ -522,9 +517,8 @@ pqParseInput2(PGconn *conn) ...@@ -522,9 +517,8 @@ pqParseInput2(PGconn *conn)
} }
else else
{ {
snprintf(noticeWorkspace, sizeof(noticeWorkspace), pqInternalNotice(&conn->noticeHooks,
libpq_gettext("server sent data (\"D\" message) without prior row description (\"T\" message)")); "server sent data (\"D\" message) without prior row description (\"T\" message)");
PGDONOTICE(conn, noticeWorkspace);
/* Discard the unexpected message; good idea?? */ /* Discard the unexpected message; good idea?? */
conn->inStart = conn->inEnd; conn->inStart = conn->inEnd;
return; return;
...@@ -539,9 +533,8 @@ pqParseInput2(PGconn *conn) ...@@ -539,9 +533,8 @@ pqParseInput2(PGconn *conn)
} }
else else
{ {
snprintf(noticeWorkspace, sizeof(noticeWorkspace), pqInternalNotice(&conn->noticeHooks,
libpq_gettext("server sent binary data (\"B\" message) without prior row description (\"T\" message)")); "server sent binary data (\"B\" message) without prior row description (\"T\" message)");
PGDONOTICE(conn, noticeWorkspace);
/* Discard the unexpected message; good idea?? */ /* Discard the unexpected message; good idea?? */
conn->inStart = conn->inEnd; conn->inStart = conn->inEnd;
return; return;
...@@ -872,7 +865,8 @@ pqGetErrorNotice2(PGconn *conn, bool isError) ...@@ -872,7 +865,8 @@ pqGetErrorNotice2(PGconn *conn, bool isError)
} }
else else
{ {
(*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); if (res->noticeHooks.noticeRec != NULL)
(*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res);
PQclear(res); PQclear(res);
} }
...@@ -1196,7 +1190,7 @@ pqEndcopy2(PGconn *conn) ...@@ -1196,7 +1190,7 @@ pqEndcopy2(PGconn *conn)
if (svLast == '\n') if (svLast == '\n')
conn->errorMessage.data[conn->errorMessage.len-1] = '\0'; conn->errorMessage.data[conn->errorMessage.len-1] = '\0';
PGDONOTICE(conn, conn->errorMessage.data); pqInternalNotice(&conn->noticeHooks, "%s", conn->errorMessage.data);
conn->errorMessage.data[conn->errorMessage.len-1] = svLast; conn->errorMessage.data[conn->errorMessage.len-1] = svLast;
} }
...@@ -1207,7 +1201,8 @@ pqEndcopy2(PGconn *conn) ...@@ -1207,7 +1201,8 @@ pqEndcopy2(PGconn *conn)
* entirely due to application screwup of the copy in/out protocol. To * entirely due to application screwup of the copy in/out protocol. To
* recover, reset the connection (talk about using a sledgehammer...) * recover, reset the connection (talk about using a sledgehammer...)
*/ */
PGDONOTICE(conn, libpq_gettext("lost synchronization with server, resetting connection")); pqInternalNotice(&conn->noticeHooks,
"lost synchronization with server, resetting connection");
/* /*
* Users doing non-blocking connections need to handle the reset * Users doing non-blocking connections need to handle the reset
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-protocol3.c,v 1.3 2003/06/21 23:25:38 tgl Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-protocol3.c,v 1.4 2003/06/23 19:20:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -57,7 +57,6 @@ pqParseInput3(PGconn *conn) ...@@ -57,7 +57,6 @@ pqParseInput3(PGconn *conn)
char id; char id;
int msgLength; int msgLength;
int avail; int avail;
char noticeWorkspace[128];
/* /*
* Loop to parse successive complete messages available in the buffer. * Loop to parse successive complete messages available in the buffer.
...@@ -172,10 +171,9 @@ pqParseInput3(PGconn *conn) ...@@ -172,10 +171,9 @@ pqParseInput3(PGconn *conn)
} }
else else
{ {
snprintf(noticeWorkspace, sizeof(noticeWorkspace), pqInternalNotice(&conn->noticeHooks,
libpq_gettext("message type 0x%02x arrived from server while idle"), "message type 0x%02x arrived from server while idle",
id); id);
PGDONOTICE(conn, noticeWorkspace);
/* Discard the unexpected message */ /* Discard the unexpected message */
conn->inCursor += msgLength; conn->inCursor += msgLength;
} }
...@@ -667,7 +665,8 @@ pqGetErrorNotice3(PGconn *conn, bool isError) ...@@ -667,7 +665,8 @@ pqGetErrorNotice3(PGconn *conn, bool isError)
{ {
/* We can cheat a little here and not copy the message. */ /* We can cheat a little here and not copy the message. */
res->errMsg = workBuf.data; res->errMsg = workBuf.data;
(*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); if (res->noticeHooks.noticeRec != NULL)
(*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res);
PQclear(res); PQclear(res);
} }
...@@ -1119,7 +1118,7 @@ pqEndcopy3(PGconn *conn) ...@@ -1119,7 +1118,7 @@ pqEndcopy3(PGconn *conn)
if (svLast == '\n') if (svLast == '\n')
conn->errorMessage.data[conn->errorMessage.len-1] = '\0'; conn->errorMessage.data[conn->errorMessage.len-1] = '\0';
PGDONOTICE(conn, conn->errorMessage.data); pqInternalNotice(&conn->noticeHooks, "%s", conn->errorMessage.data);
conn->errorMessage.data[conn->errorMessage.len-1] = svLast; conn->errorMessage.data[conn->errorMessage.len-1] = svLast;
} }
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: libpq-int.h,v 1.75 2003/06/21 21:51:34 tgl Exp $ * $Id: libpq-int.h,v 1.76 2003/06/23 19:20:25 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -360,7 +360,9 @@ extern char *pqResultStrdup(PGresult *res, const char *str); ...@@ -360,7 +360,9 @@ extern char *pqResultStrdup(PGresult *res, const char *str);
extern void pqClearAsyncResult(PGconn *conn); extern void pqClearAsyncResult(PGconn *conn);
extern void pqSaveErrorResult(PGconn *conn); extern void pqSaveErrorResult(PGconn *conn);
extern PGresult *pqPrepareAsyncResult(PGconn *conn); extern PGresult *pqPrepareAsyncResult(PGconn *conn);
extern void pqInternalNotice(const PGNoticeHooks *hooks, const char *msgtext); extern void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt, ...)
/* This lets gcc check the format string for consistency. */
__attribute__((format(printf, 2, 3)));
extern int pqAddTuple(PGresult *res, PGresAttValue *tup); extern int pqAddTuple(PGresult *res, PGresAttValue *tup);
extern void pqSaveMessageField(PGresult *res, char code, extern void pqSaveMessageField(PGresult *res, char code,
const char *value); const char *value);
...@@ -435,10 +437,6 @@ extern void pqsecure_close(PGconn *); ...@@ -435,10 +437,6 @@ extern void pqsecure_close(PGconn *);
extern ssize_t pqsecure_read(PGconn *, void *ptr, size_t len); extern ssize_t pqsecure_read(PGconn *, void *ptr, size_t len);
extern ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len); extern ssize_t pqsecure_write(PGconn *, const void *ptr, size_t len);
/* Note: PGDONOTICE macro will work if applied to either PGconn or PGresult */
#define PGDONOTICE(conn,message) \
pqInternalNotice(&(conn)->noticeHooks, (message))
/* /*
* this is so that we can check if a connection is non-blocking internally * this is so that we can check if a connection is non-blocking internally
* without the overhead of a function call * without the overhead of a function call
......
# $Header: /cvsroot/pgsql/src/interfaces/libpq/nls.mk,v 1.8 2002/09/22 20:57:21 petere Exp $ # $Header: /cvsroot/pgsql/src/interfaces/libpq/nls.mk,v 1.9 2003/06/23 19:20:25 tgl Exp $
CATALOG_NAME := libpq CATALOG_NAME := libpq
AVAIL_LANGUAGES := cs de es fr pt_BR ru sv zh_CN zh_TW AVAIL_LANGUAGES := cs de es fr pt_BR ru sv zh_CN zh_TW
GETTEXT_FILES := fe-auth.c fe-connect.c fe-exec.c fe-lobj.c fe-misc.c fe-secure.c GETTEXT_FILES := fe-auth.c fe-connect.c fe-exec.c fe-lobj.c fe-misc.c fe-secure.c
GETTEXT_TRIGGERS:= libpq_gettext GETTEXT_TRIGGERS:= libpq_gettext pqInternalNotice:2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment