Skip to content

Commit 23b0387

Browse files
committed
Fix too long syslog message problem
1 parent 8bba4b4 commit 23b0387

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

src/backend/utils/misc/trace.c

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
#include "miscadmin.h"
2525
#include "utils/trace.h"
2626

27+
#include <ctype.h>
28+
29+
#ifdef MULTIBYTE
30+
#include "mb/pg_wchar.h"
31+
#endif
32+
2733
/*
2834
* We could support trace messages of indefinite length, as elog() does,
2935
* but it's probably not worth the trouble. Instead limit trace message
@@ -204,16 +210,90 @@ eprintf(const char *fmt,...)
204210
void
205211
write_syslog(int level, char *line)
206212
{
213+
#ifndef PG_SYSLOG_LIMIT
214+
#define PG_SYSLOG_LIMIT 128
215+
#endif /* PG_SYSLOG_LIMIT */
216+
207217
static int openlog_done = 0;
218+
static char buf[PG_SYSLOG_LIMIT+1];
219+
static int logid = 0;
208220

209221
if (UseSyslog >= 1)
210222
{
223+
int len = strlen(line);
224+
int buflen;
225+
int seq = 0;
226+
int l;
227+
int i;
228+
211229
if (!openlog_done)
212230
{
213231
openlog_done = 1;
214232
openlog(PG_LOG_IDENT, LOG_PID | LOG_NDELAY, PG_LOG_FACILITY);
215233
}
216-
syslog(level, "%s", line);
234+
235+
/* divide into multiple syslog() calls if message is
236+
* too long
237+
*/
238+
if (len > PG_SYSLOG_LIMIT)
239+
{
240+
logid++;
241+
242+
while (len > 0)
243+
{
244+
strncpy(buf, line, PG_SYSLOG_LIMIT);
245+
buf[PG_SYSLOG_LIMIT] = '\0';
246+
247+
l = strlen(buf);
248+
#ifdef MULTIBYTE
249+
/* trim to multibyte letter boundary */
250+
buflen = pg_mbcliplen(buf, l, l);
251+
buf[buflen] = '\0';
252+
l = strlen(buf);
253+
#endif
254+
/* already word boundary? */
255+
if (isspace(line[l]) || line[l] == '\0')
256+
{
257+
buflen = l;
258+
}
259+
else
260+
{
261+
/* try to divide in word boundary */
262+
i = l - 1;
263+
while(i > 0 && !isspace(buf[i]))
264+
{
265+
i--;
266+
}
267+
if (i <= 0) /* couldn't divide word boundary */
268+
{
269+
buflen = l;
270+
}
271+
else
272+
{
273+
buflen = i;
274+
buf[i] = '\0';
275+
}
276+
}
277+
278+
seq++;
279+
/*
280+
* Here we actually call syslog().
281+
* For segmented messages, we add logid
282+
* (incremented at each write_syslog call),
283+
* and seq (incremented at each syslog call
284+
* within a write_syslog call).
285+
* This will prevent syslog to surpress
286+
* "same" messages...
287+
*/
288+
syslog(level, "[%d-%d] %s", logid, seq, buf);
289+
line += buflen;
290+
len -= buflen;
291+
}
292+
}
293+
else
294+
{
295+
syslog(level, "%s", line);
296+
}
217297
}
218298
}
219299

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