8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -57,15 +57,14 @@ static void write_syslog(int level, const char *line);
57
57
# define Use_syslog 0
58
58
#endif
59
59
60
+ bool Log_timestamp ;
61
+ bool Log_pid ;
60
62
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] ' */
68
65
66
+ static const char * print_timestamp (void );
67
+ static const char * print_pid (void );
69
68
70
69
static int Debugfile = -1 ;
71
70
static int Err_file = -1 ;
@@ -117,11 +116,9 @@ elog(int lev, const char *fmt,...)
117
116
int indent = 0 ;
118
117
int space_needed ;
119
118
120
- #ifdef USE_SYSLOG
121
- int log_level ;
122
-
123
- #endif
124
119
int len ;
120
+ /* size of the prefix needed for timestamp and pid, if enabled */
121
+ size_t timestamp_size ;
125
122
126
123
if (lev <= DEBUG && Debugfile < 0 )
127
124
return ; /* ignore debug msgs if noplace to send */
@@ -174,13 +171,19 @@ elog(int lev, const char *fmt,...)
174
171
errorstr = errorstr_buf ;
175
172
}
176
173
174
+ timestamp_size = 0 ;
175
+ if (Log_timestamp )
176
+ timestamp_size += TIMESTAMP_SIZE ;
177
+ if (Log_pid )
178
+ timestamp_size += PID_SIZE ;
179
+
177
180
/*
178
181
* Set up the expanded format, consisting of the prefix string plus
179
182
* input format, with any %m replaced by strerror() string (since
180
183
* vsnprintf won't know what to do with %m). To keep space
181
184
* calculation simple, we only allow one %m.
182
185
*/
183
- space_needed = TIMESTAMP_SIZE + strlen (prefix ) + indent + (lineno ? 24 : 0 )
186
+ space_needed = timestamp_size + strlen (prefix ) + indent + (lineno ? 24 : 0 )
184
187
+ strlen (fmt ) + strlen (errorstr ) + 1 ;
185
188
if (space_needed > (int ) sizeof (fmt_fixedbuf ))
186
189
{
@@ -194,12 +197,16 @@ elog(int lev, const char *fmt,...)
194
197
* fmt_fixedbuf! */
195
198
}
196
199
}
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
+
199
208
strcat (fmt_buf , prefix );
200
- #else
201
- strcpy (fmt_buf , prefix );
202
- #endif
209
+
203
210
bp = fmt_buf + strlen (fmt_buf );
204
211
while (indent -- > 0 )
205
212
* bp ++ = ' ' ;
@@ -277,12 +284,12 @@ elog(int lev, const char *fmt,...)
277
284
/* We're up against it, convert to fatal out-of-memory error */
278
285
msg_buf = msg_fixedbuf ;
279
286
lev = 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 ());
282
292
strcat (msg_buf , "FATAL: elog: out of memory" );
283
- #else
284
- strcpy (msg_buf , "FATAL: elog: out of memory" );
285
- #endif
286
293
break ;
287
294
}
288
295
}
@@ -318,7 +325,7 @@ elog(int lev, const char *fmt,...)
318
325
syslog_level = LOG_CRIT ;
319
326
}
320
327
321
- write_syslog (syslog_level , msg_buf + TIMESTAMP_SIZE );
328
+ write_syslog (syslog_level , msg_buf + timestamp_size );
322
329
}
323
330
#endif /* ENABLE_SYSLOG */
324
331
@@ -373,7 +380,7 @@ elog(int lev, const char *fmt,...)
373
380
msgtype = 'E' ;
374
381
}
375
382
/* exclude the timestamp from msg sent to frontend */
376
- pq_puttextmessage (msgtype , msg_buf + TIMESTAMP_SIZE );
383
+ pq_puttextmessage (msgtype , msg_buf + timestamp_size );
377
384
378
385
/*
379
386
* This flush is normally not necessary, since postgres.c will
@@ -525,33 +532,45 @@ DebugFileOpen(void)
525
532
#endif
526
533
527
534
528
- #ifdef ELOG_TIMESTAMPS
535
+
529
536
/*
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 "
531
540
*/
532
541
static const char *
533
- print_timestamp ()
542
+ print_timestamp (void )
534
543
{
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 ;
553
554
}
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
+
555
574
556
575
557
576
#ifdef ENABLE_SYSLOG
0 commit comments