88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.59 2000/05/31 00:28:32 petere Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.60 2000/06/04 15:06:29 petere Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -57,15 +57,14 @@ static void write_syslog(int level, const char *line);
5757# define Use_syslog 0
5858#endif
5959
60+ bool Log_timestamp ;
61+ bool Log_pid ;
6062
61- #ifdef ELOG_TIMESTAMPS
62- static const char * print_timestamp (void );
63- # define TIMESTAMP_SIZE 28
64- #else
65- # define TIMESTAMP_SIZE 0
66- #endif
67-
63+ #define TIMESTAMP_SIZE 20/* format `YYYY-MM-DD HH:MM:SS ' */
64+ #define PID_SIZE 9/* format `[123456] ' */
6865
66+ static const char * print_timestamp (void );
67+ static const char * print_pid (void );
6968
7069static int Debugfile = -1 ;
7170static int Err_file = -1 ;
@@ -117,11 +116,9 @@ elog(int lev, const char *fmt,...)
117116int indent = 0 ;
118117int space_needed ;
119118
120- #ifdef USE_SYSLOG
121- int log_level ;
122-
123- #endif
124119int len ;
120+ /* size of the prefix needed for timestamp and pid, if enabled */
121+ size_t timestamp_size ;
125122
126123if (lev <=DEBUG && Debugfile < 0 )
127124return ;/* ignore debug msgs if noplace to send */
@@ -174,13 +171,19 @@ elog(int lev, const char *fmt,...)
174171errorstr = errorstr_buf ;
175172}
176173
174+ timestamp_size = 0 ;
175+ if (Log_timestamp )
176+ timestamp_size += TIMESTAMP_SIZE ;
177+ if (Log_pid )
178+ timestamp_size += PID_SIZE ;
179+
177180/*
178181 * Set up the expanded format, consisting of the prefix string plus
179182 * input format, with any %m replaced by strerror() string (since
180183 * vsnprintf won't know what to do with %m). To keep space
181184 * calculation simple, we only allow one %m.
182185 */
183- space_needed = TIMESTAMP_SIZE + strlen (prefix )+ indent + (lineno ?24 :0 )
186+ space_needed = timestamp_size + strlen (prefix )+ indent + (lineno ?24 :0 )
184187+ strlen (fmt )+ strlen (errorstr )+ 1 ;
185188if (space_needed > (int )sizeof (fmt_fixedbuf ))
186189{
@@ -194,12 +197,16 @@ elog(int lev, const char *fmt,...)
194197 * fmt_fixedbuf! */
195198}
196199}
197- #ifdef ELOG_TIMESTAMPS
198- strcpy (fmt_buf ,print_timestamp ());
200+
201+ fmt_buf [0 ]= '\0' ;
202+
203+ if (Log_timestamp )
204+ strcat (fmt_buf ,print_timestamp ());
205+ if (Log_pid )
206+ strcat (fmt_buf ,print_pid ());
207+
199208strcat (fmt_buf ,prefix );
200- #else
201- strcpy (fmt_buf ,prefix );
202- #endif
209+
203210bp = fmt_buf + strlen (fmt_buf );
204211while (indent -- > 0 )
205212* bp ++ = ' ' ;
@@ -277,12 +284,12 @@ elog(int lev, const char *fmt,...)
277284/* We're up against it, convert to fatal out-of-memory error */
278285msg_buf = msg_fixedbuf ;
279286lev = REALLYFATAL ;
280- #ifdef ELOG_TIMESTAMPS
281- strcpy (msg_buf ,print_timestamp ());
287+ msg_buf [0 ]= '\0' ;
288+ if (Log_timestamp )
289+ strcat (msg_buf ,print_timestamp ());
290+ if (Log_pid )
291+ strcat (msg_buf ,print_pid ());
282292strcat (msg_buf ,"FATAL: elog: out of memory" );
283- #else
284- strcpy (msg_buf ,"FATAL: elog: out of memory" );
285- #endif
286293break ;
287294}
288295}
@@ -318,7 +325,7 @@ elog(int lev, const char *fmt,...)
318325syslog_level = LOG_CRIT ;
319326}
320327
321- write_syslog (syslog_level ,msg_buf + TIMESTAMP_SIZE );
328+ write_syslog (syslog_level ,msg_buf + timestamp_size );
322329}
323330#endif /* ENABLE_SYSLOG */
324331
@@ -373,7 +380,7 @@ elog(int lev, const char *fmt,...)
373380msgtype = 'E' ;
374381}
375382/* exclude the timestamp from msg sent to frontend */
376- pq_puttextmessage (msgtype ,msg_buf + TIMESTAMP_SIZE );
383+ pq_puttextmessage (msgtype ,msg_buf + timestamp_size );
377384
378385/*
379386 * This flush is normally not necessary, since postgres.c will
@@ -525,33 +532,45 @@ DebugFileOpen(void)
525532#endif
526533
527534
528- #ifdef ELOG_TIMESTAMPS
535+
529536/*
530- * Return a timestamp string like "980119.17:25:59.902 [21974] "
537+ * Return a timestamp string like
538+ *
539+ * "2000-06-04 13:12:03 "
531540 */
532541static const char *
533- print_timestamp ()
542+ print_timestamp (void )
534543{
535- struct timeval tv ;
536- struct timezone tz = {0 ,0 };
537- struct tm * time ;
538- time_t tm ;
539- static char timestamp [32 ],
540- pid [8 ];
541-
542- gettimeofday (& tv ,& tz );
543- tm = tv .tv_sec ;
544- time = localtime (& tm );
545-
546- sprintf (pid ,"[%d]" ,MyProcPid );
547- sprintf (timestamp ,"%02d%02d%02d.%02d:%02d:%02d.%03d %7s " ,
548- time -> tm_year %100 ,time -> tm_mon + 1 ,time -> tm_mday ,
549- time -> tm_hour ,time -> tm_min ,time -> tm_sec ,
550- (int ) (tv .tv_usec /1000 ),pid );
551-
552- return timestamp ;
544+ time_t curtime ;
545+ static char buf [TIMESTAMP_SIZE + 1 ];
546+
547+ curtime = time (NULL );
548+
549+ strftime (buf ,sizeof (buf ),
550+ "%Y-%m-%d %H:%M:%S " ,
551+ localtime (& curtime ));
552+
553+ return buf ;
553554}
554- #endif
555+
556+
557+
558+ /*
559+ * Return a string like
560+ *
561+ * "[123456] "
562+ *
563+ * with the current pid.
564+ */
565+ static const char *
566+ print_pid (void )
567+ {
568+ static char buf [PID_SIZE + 1 ];
569+
570+ snprintf (buf ,PID_SIZE + 1 ,"[%d] " , (int )MyProcPid );
571+ return buf ;
572+ }
573+
555574
556575
557576#ifdef ENABLE_SYSLOG