4242 *
4343 *
4444 * IDENTIFICATION
45- * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.162 2005/08/12 21:36:59 momjian Exp $
45+ * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.163 2005/10/14 16:41:02 tgl Exp $
4646 *
4747 *-------------------------------------------------------------------------
4848 */
@@ -85,6 +85,7 @@ char *Syslog_ident;
8585
8686static void write_syslog (int level ,const char * line );
8787#endif
88+
8889#ifdef WIN32
8990static void write_eventlog (int level ,const char * line );
9091#endif
@@ -1152,41 +1153,51 @@ DebugFileOpen(void)
11521153#endif
11531154
11541155/*
1155- * Write a message line to syslog if the syslog option is set.
1156- *
1157- * Our problem here is that many syslog implementations don't handle
1158- * long messages in an acceptable manner. While this function doesn't
1159- * help that fact, it does work around by splitting up messages into
1160- * smaller pieces.
1156+ * Write a message line to syslog
11611157 */
11621158static void
11631159write_syslog (int level ,const char * line )
11641160{
11651161static bool openlog_done = false;
11661162static unsigned long seq = 0 ;
1167- static int syslog_fac = LOG_LOCAL0 ;
11681163
1169- int len = strlen ( line ) ;
1164+ int len ;
11701165
11711166if (!openlog_done )
11721167{
1168+ int syslog_fac ;
1169+ char * syslog_ident ;
1170+
11731171if (pg_strcasecmp (Syslog_facility ,"LOCAL0" )== 0 )
11741172syslog_fac = LOG_LOCAL0 ;
1175- if (pg_strcasecmp (Syslog_facility ,"LOCAL1" )== 0 )
1173+ else if (pg_strcasecmp (Syslog_facility ,"LOCAL1" )== 0 )
11761174syslog_fac = LOG_LOCAL1 ;
1177- if (pg_strcasecmp (Syslog_facility ,"LOCAL2" )== 0 )
1175+ else if (pg_strcasecmp (Syslog_facility ,"LOCAL2" )== 0 )
11781176syslog_fac = LOG_LOCAL2 ;
1179- if (pg_strcasecmp (Syslog_facility ,"LOCAL3" )== 0 )
1177+ else if (pg_strcasecmp (Syslog_facility ,"LOCAL3" )== 0 )
11801178syslog_fac = LOG_LOCAL3 ;
1181- if (pg_strcasecmp (Syslog_facility ,"LOCAL4" )== 0 )
1179+ else if (pg_strcasecmp (Syslog_facility ,"LOCAL4" )== 0 )
11821180syslog_fac = LOG_LOCAL4 ;
1183- if (pg_strcasecmp (Syslog_facility ,"LOCAL5" )== 0 )
1181+ else if (pg_strcasecmp (Syslog_facility ,"LOCAL5" )== 0 )
11841182syslog_fac = LOG_LOCAL5 ;
1185- if (pg_strcasecmp (Syslog_facility ,"LOCAL6" )== 0 )
1183+ else if (pg_strcasecmp (Syslog_facility ,"LOCAL6" )== 0 )
11861184syslog_fac = LOG_LOCAL6 ;
1187- if (pg_strcasecmp (Syslog_facility ,"LOCAL7" )== 0 )
1185+ else if (pg_strcasecmp (Syslog_facility ,"LOCAL7" )== 0 )
11881186syslog_fac = LOG_LOCAL7 ;
1189- openlog (Syslog_ident ,LOG_PID |LOG_NDELAY |LOG_NOWAIT ,syslog_fac );
1187+ else
1188+ syslog_fac = LOG_LOCAL0 ;
1189+ /*
1190+ * openlog() usually just stores the passed char pointer as-is,
1191+ * so we must give it a string that will be unchanged for the life of
1192+ * the process. The Syslog_ident GUC variable does not meet this
1193+ * requirement, so strdup() it. This isn't a memory leak because
1194+ * this code is executed at most once per process.
1195+ */
1196+ syslog_ident = strdup (Syslog_ident );
1197+ if (syslog_ident == NULL )/* out of memory already!? */
1198+ syslog_ident = "postgres" ;
1199+
1200+ openlog (syslog_ident ,LOG_PID |LOG_NDELAY |LOG_NOWAIT ,syslog_fac );
11901201openlog_done = true;
11911202}
11921203
@@ -1196,8 +1207,16 @@ write_syslog(int level, const char *line)
11961207 */
11971208seq ++ ;
11981209
1199- /* divide into multiple syslog() calls if message is too long */
1200- /* or if the message contains embedded NewLine(s) '\n' */
1210+ /*
1211+ * Our problem here is that many syslog implementations don't handle
1212+ * long messages in an acceptable manner. While this function doesn't
1213+ * help that fact, it does work around by splitting up messages into
1214+ * smaller pieces.
1215+ *
1216+ * We divide into multiple syslog() calls if message is too long
1217+ * or if the message contains embedded NewLine(s) '\n'.
1218+ */
1219+ len = strlen (line );
12011220if (len > PG_SYSLOG_LIMIT || strchr (line ,'\n' )!= NULL )
12021221{
12031222int chunk_nr = 0 ;
@@ -1230,8 +1249,8 @@ write_syslog(int level, const char *line)
12301249buf [buflen ]= '\0' ;
12311250
12321251/* already word boundary? */
1233- if (! isspace (( unsigned char ) line [buflen ]) &&
1234- line [buflen ]!= '\0' )
1252+ if (line [buflen ]!= '\0' &&
1253+ ! isspace (( unsigned char ) line [buflen ]) )
12351254{
12361255/* try to divide at word boundary */
12371256i = buflen - 1 ;
@@ -1259,6 +1278,7 @@ write_syslog(int level, const char *line)
12591278}
12601279}
12611280#endif /* HAVE_SYSLOG */
1281+
12621282#ifdef WIN32
12631283/*
12641284 * Write a message line to the windows event log