summaryrefslogtreecommitdiff
path: root/src/backend/utils/misc/trace.c
blob: baf8ce6121868bc050835f4322c372e701ea7067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*-------------------------------------------------------------------------
 *
 * trace.c
 *
 *	  Conditional trace ans logging functions.
 *
 *	  Massimo Dal Zotto <dz@cs.unitn.it>
 *
 *-------------------------------------------------------------------------
 */

#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "postgres.h"

#ifdef USE_SYSLOG
#include <syslog.h>
#endif

#include "miscadmin.h"
#include "utils/trace.h"

#ifdef USE_SYSLOG
/*
 * Global option to control the use of syslog(3) for logging:
 *
 *		0	stdout/stderr only
 *		1	stdout/stderr + syslog
 *		2	syslog only
 */
#define UseSyslog		pg_options[OPT_SYSLOG]
#define PG_LOG_FACILITY LOG_LOCAL0
#define PG_LOG_IDENT	"postgres"
#else
#define UseSyslog 0
#endif

/*
 * Trace option names, must match the constants in trace_opts[].
 */
static char *opt_names[] = {
	"all",						/* 0=trace some, 1=trace all, -1=trace none */
	"verbose",
	"query",
	"plan",
	"parse",
	"rewritten",
	"pretty_plan",
	"pretty_parse",
	"pretty_rewritten",
	"parserstats",
	"plannerstats",
	"executorstats",
	"shortlocks",				/* currently unused but needed, see lock.c */
	"locks",
	"userlocks",
	"spinlocks",
	"notify",
	"malloc",
	"palloc",
	"lock_debug_oidmin",
	"lock_debug_relid",
	"lock_read_priority",		/* lock priority, see lock.c */
	"deadlock_timeout",			/* deadlock timeout, see proc.c */
	"syslog",					/* use syslog for error messages */
	"hostlookup",				/* enable hostname lookup in ps_status */
	"showportnumber",			/* show port number in ps_status */

	/* NUM_PG_OPTIONS */		/* must be the last item of enum */
};

/*
 * Array of trace flags which can be set or reset independently.
 */
int			pg_options[NUM_PG_OPTIONS] = {0};

/*
 * Print a timestamp and a message to stdout if the trace flag
 * indexed by the flag value is set.
 */
int
tprintf(int flag, const char *fmt,...)
{
	va_list		ap;
	char		line[ELOG_MAXLEN + TIMESTAMP_SIZE + 1];

#ifdef USE_SYSLOG
	int			log_level;
#endif

	if ((flag == TRACE_ALL) || (pg_options[TRACE_ALL] > 0))
	{
		/* uconditional trace or trace all option set */
	}
	else if (pg_options[TRACE_ALL] == 0)
	{
		if ((flag < 0) || (flag >= NUM_PG_OPTIONS) || (!pg_options[flag]))
			return 0;
	}
	else if (pg_options[TRACE_ALL] < 0)
		return 0;

	va_start(ap, fmt);
#ifdef ELOG_TIMESTAMPS
	strcpy(line, tprintf_timestamp());
#endif
	vsnprintf(line + TIMESTAMP_SIZE, ELOG_MAXLEN, fmt, ap);
	va_end(ap);

#ifdef USE_SYSLOG
	log_level = ((flag == TRACE_ALL) ? LOG_INFO : LOG_DEBUG);
	write_syslog(log_level, line + TIMESTAMP_SIZE);
#endif

	if (UseSyslog <= 1)
	{
		puts(line);
		fflush(stdout);
	}

	return 1;
}

/*
 * Print a timestamp and a message to stdout or to syslog.
 */
#ifdef NOT_USED
int
tprintf1(const char *fmt,...)
{
	va_list		ap;
	char		line[ELOG_MAXLEN + TIMESTAMP_SIZE + 1];

	va_start(ap, fmt);
#ifdef ELOG_TIMESTAMPS
	strcpy(line, tprintf_timestamp());
#endif
	vsnprintf(line + TIMESTAMP_SIZE, ELOG_MAXLEN, fmt, ap);
	va_end(ap);

#ifdef USE_SYSLOG
	write_syslog(LOG_INFO, line + TIMESTAMP_SIZE);
#endif

	if (UseSyslog <= 1)
	{
		puts(line);
		fflush(stdout);
	}

	return 1;
}
#endif

/*
 * Print a timestamp and a message to stderr.
 */
int
eprintf(const char *fmt,...)
{
	va_list		ap;
	char		line[ELOG_MAXLEN + TIMESTAMP_SIZE + 1];

	va_start(ap, fmt);
#ifdef ELOG_TIMESTAMPS
	strcpy(line, tprintf_timestamp());
#endif
	vsnprintf(line + TIMESTAMP_SIZE, ELOG_MAXLEN, fmt, ap);
	va_end(ap);

#ifdef USE_SYSLOG
	write_syslog(LOG_ERR, line + TIMESTAMP_SIZE);
#endif

	if (UseSyslog <= 1)
	{
		fputs(line, stderr);
		fputc('\n', stderr);
		fflush(stderr);
	}

	return 1;
}

#ifdef USE_SYSLOG
/*
 * Write a message line to syslog if the syslog option is set.
 */
void
write_syslog(int level, char *line)
{
	static int	openlog_done = 0;

	if (UseSyslog >= 1)
	{
		if (!openlog_done)
		{
			openlog_done = 1;
			openlog(PG_LOG_IDENT, LOG_PID | LOG_NDELAY, PG_LOG_FACILITY);
		}
		syslog(level, "%s", line);
	}
}
#endif

#ifdef ELOG_TIMESTAMPS
/*
 * Return a timestamp string like "980119.17:25:59.902 [21974] "
 */
char *
tprintf_timestamp()
{
	struct timeval tv;
	struct timezone tz = { 0, 0 };
	struct tm  *time;
	time_t		tm;
	static char timestamp[32],
				pid[8];

	gettimeofday(&tv, &tz);
	tm = tv.tv_sec;
	time = localtime(&tm);

	sprintf(pid, "[%d]", MyProcPid);
	sprintf(timestamp, "%02d%02d%02d.%02d:%02d:%02d.%03d %7s ",
			time->tm_year, time->tm_mon + 1, time->tm_mday,
			time->tm_hour, time->tm_min, time->tm_sec,
			(int) (tv.tv_usec/1000), pid);

	return timestamp;
}
#endif

#ifdef NOT_USED
static int
option_flag(int flag)
{
	if ((flag < 0) || (flag >= NUM_PG_OPTIONS))
		return 0;
	return pg_options[flag];
}

int
set_option_flag(int flag, int value)
{
	if ((flag < 0) || (flag >= NUM_PG_OPTIONS))
		return -1;

	pg_options[flag] = value;
	return value;
}
#endif

/*
 * Parse an option string like "name,name+,name-,name=value".
 * Single options are delimited by ',',space,tab,newline or cr.
 *
 * If 'secure' is false, the option string came from a remote client via
 * connection "debug options" field --- do not obey any requests that
 * might potentially be security loopholes.
 */
void
parse_options(char *str, bool secure)
{
	char	   *s,
			   *name;
	int			i,
				len,
				val,
				is_comment;

	Assert((sizeof(opt_names) / sizeof(char *)) == NUM_PG_OPTIONS);

	str = strdup(str);
	for (s = str; *s;)
	{
		is_comment = 0;
		name = s;
		val = 1;
		for (; *s; s++)
		{
			switch (*s)
			{
				case '#':
					is_comment = 1;
					break;
				case '=':
					*s++ = '\0';
					val = strtol(s, &s, 10);
					goto setval;
				case '-':
					*s++ = '\0';
					val = 0;
					goto setval;
				case '+':
					*s++ = '\0';
					val = 1;
					goto setval;
				case ' ':
				case ',':
				case '\t':
				case '\n':
				case '\r':
					*s = ',';
					val = 1;
					goto setval;
			}
		}
setval:
		for (; *s; s++)
		{
			if (*s == ',')
			{
				*s++ = '\0';
				break;
			}
		}
		len = strlen(name);
		if (len == 0)
			continue;
		for (i = 0; i < NUM_PG_OPTIONS; i++)
		{
			if (strncmp(name, opt_names[i], len) == 0)
			{
				pg_options[i] = val;
				break;
			}
		}
		if (!is_comment && (i >= NUM_PG_OPTIONS))
			fprintf(stderr, "invalid option: %s\n", name);
	}
	free(str);
}

#define BUF_SIZE 4096

void
read_pg_options(SIGNAL_ARGS)
{
	int			fd;
	int			n;
	int			verbose;
	char		buffer[BUF_SIZE];
	char		c;
	char	   *s,
			   *p;

	if (!DataDir)
	{
		fprintf(stderr, "read_pg_options: DataDir not defined\n");
		return;
	}

	snprintf(buffer, BUF_SIZE - 1, "%s/%s", DataDir, "pg_options");
#ifndef __CYGWIN32__
	if ((fd = open(buffer, O_RDONLY)) < 0)
#else
	if ((fd = open(buffer, O_RDONLY | O_BINARY)) < 0)
#endif
		return;

	if ((n = read(fd, buffer, BUF_SIZE - 1)) > 0)
	{
		/* collpse buffer in place removing comments and spaces */
		for (s = buffer, p = buffer, c = '\0'; s < (buffer + n);)
		{
			switch (*s)
			{
				case '#':
					while ((s < (buffer + n)) && (*s++ != '\n'));
					break;
				case ' ':
				case '\t':
				case '\n':
				case '\r':
					if (c != ',')
						c = *p++ = ',';
					s++;
					break;
				default:
					c = *p++ = *s++;
					break;
			}
		}
		if (c == ',')
			p--;
		*p = '\0';
		verbose = pg_options[TRACE_VERBOSE];
		parse_options(buffer, true);
		verbose |= pg_options[TRACE_VERBOSE];
		if (verbose || postgres_signal_arg == SIGHUP)
			tprintf(TRACE_ALL, "read_pg_options: %s", buffer);
	}

	close(fd);
}

/*
 * Local variables:
 *	tab-width: 4
 *	c-indent-level: 4
 *	c-basic-offset: 4
 * End:
 */