Skip to content

Commit 0a54de8

Browse files
committed
Removed ELOG_TIMESTAMPS #define in favor of two run-time
configuration options `Log_timestamp' and `Log_pid'.
1 parent 209aa77 commit 0a54de8

File tree

4 files changed

+75
-56
lines changed

4 files changed

+75
-56
lines changed

src/backend/utils/error/elog.c

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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

7069
static int Debugfile = -1;
7170
static int Err_file = -1;
@@ -117,11 +116,9 @@ elog(int lev, const char *fmt,...)
117116
int indent = 0;
118117
int space_needed;
119118

120-
#ifdef USE_SYSLOG
121-
int log_level;
122-
123-
#endif
124119
int len;
120+
/* size of the prefix needed for timestamp and pid, if enabled */
121+
size_t timestamp_size;
125122

126123
if (lev <= DEBUG && Debugfile < 0)
127124
return; /* ignore debug msgs if noplace to send */
@@ -174,13 +171,19 @@ elog(int lev, const char *fmt,...)
174171
errorstr = 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;
185188
if (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+
199208
strcat(fmt_buf, prefix);
200-
#else
201-
strcpy(fmt_buf, prefix);
202-
#endif
209+
203210
bp = fmt_buf + strlen(fmt_buf);
204211
while (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 */
278285
msg_buf = msg_fixedbuf;
279286
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());
282292
strcat(msg_buf, "FATAL: elog: out of memory");
283-
#else
284-
strcpy(msg_buf, "FATAL: elog: out of memory");
285-
#endif
286293
break;
287294
}
288295
}
@@ -318,7 +325,7 @@ elog(int lev, const char *fmt,...)
318325
syslog_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,...)
373380
msgtype = '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
*/
532541
static 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

src/backend/utils/misc/guc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Support for grand unified configuration scheme, including SET
55
* command, configuration file, and command line options.
66
*
7-
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.1 2000/05/31 00:28:34 petere Exp $
7+
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.2 2000/06/04 15:06:30 petere Exp $
88
*
99
* Copyright 2000 by PostgreSQL Global Development Group
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
@@ -139,9 +139,11 @@ ConfigureNamesBool[] =
139139
{"geqo", PGC_USERSET, &enable_geqo, true},
140140

141141
{"net_server", PGC_POSTMASTER, &NetServer, false},
142-
{"fsync", PGC_POSTMASTER, &enableFsync, true},
142+
{"fsync", PGC_BACKEND, &enableFsync, true},
143143

144144
{"log_connections", PGC_POSTMASTER, &Log_connections, false},
145+
{"log_timestamp", PGC_BACKEND, &Log_timestamp, false},
146+
{"log_pid", PGC_BACKEND, &Log_pid, false},
145147

146148
{"debug_print_query", PGC_SUSET, &Debug_print_query, false},
147149
{"debug_print_parse", PGC_SUSET, &Debug_print_parse, false},

src/include/config.h.in

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* or in config.h afterwards. Of course, if you edit config.h, then your
99
* changes will be overwritten the next time you run configure.
1010
*
11-
* $Id: config.h.in,v 1.115 2000/06/04 01:44:36 petere Exp $
11+
* $Id: config.h.in,v 1.116 2000/06/04 15:06:32 petere Exp $
1212
*/
1313

1414
#ifndef CONFIG_H
@@ -140,11 +140,6 @@
140140
*/
141141
#define TBL_FREE_CMD_MEMORY
142142

143-
/*
144-
* ELOG_TIMESTAMPS: adds a timestamp with the following format to elog
145-
* messages: yymmdd.hh:mm:ss.mmm [pid] message
146-
*/
147-
/* #define ELOG_TIMESTAMPS */
148143

149144
#undef ENABLE_SYSLOG
150145

src/include/utils/elog.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: elog.h,v 1.17 2000/05/31 00:28:40 petere Exp $
10+
* $Id: elog.h,v 1.18 2000/06/04 15:06:34 petere Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -27,6 +27,9 @@
2727
extern int Use_syslog;
2828
#endif
2929

30+
extern bool Log_timestamp;
31+
extern bool Log_pid;
32+
3033
#ifndef __GNUC__
3134
extern void elog(int lev, const char *fmt,...);
3235

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