summaryrefslogtreecommitdiff
path: root/src/tools/fe_port.c
blob: 3704c2730776bbabf1e26208718137fffa244090 (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
/*
 * pgpool: a language independent connection pool server for PostgreSQL
 * written by Tatsuo Ishii
 *
 * Copyright (c) 2003-2024	PgPool Global Development Group
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of the
 * author not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. The author makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * fe_port.c
 *
 */
#ifndef POOL_PRIVATE
#error "This file is not expected to be compiled for pgpool utilities only"
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include "utils/fe_ports.h"


int			_fe_error_level = 0;
const char *_filename;
const char *_funcname;
int			_lineno;
int			_print_timestamp = 0;

static const char *error_severity(int elevel);
static char *nowsec(void);

#define MAXSTRFTIME 128

/*
 * ignore errdetail() in frontend
 */
int
errdetail(const char *fmt,...)
{
	return 0;
}

/*
 * ignore errhint() in frontend
 */
int
errhint(const char *fmt,...)
{
	return 0;
}

void
errfinish(int dummy,...)
{
}

void
errmsg(const char *fmt,...)
{
	va_list		ap;
#ifdef HAVE_ASPRINTF
	char	   *fmt2;
	int			len;
#endif

#ifdef HAVE_ASPRINTF
	if (_print_timestamp)
		len = asprintf(&fmt2, "%s %s: pid %d: %s\n", nowsec(), error_severity(_fe_error_level), (int) getpid(), fmt);
	else
		len = asprintf(&fmt2, "%s: pid %d: %s\n", error_severity(_fe_error_level), (int) getpid(), fmt);

	if (len >= 0 && fmt2)
	{
		va_start(ap, fmt);
		vfprintf(stderr, fmt2, ap);
		va_end(ap);
		fflush(stderr);
		free(fmt2);
	}
#else
	if (_print_timestamp)
		fprintf(stderr, "%s %s: pid %d: ", nowsec(), error_severity(_fe_error_level), (int) getpid());
	else
		fprintf(stderr, "%s: pid %d: ", error_severity(_fe_error_level), (int) getpid());

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fprintf(stderr, "\n");
#endif

}

/*
 * error_severity --- get localized string representing elevel
 */
static const char *
error_severity(int elevel)
{
	const char *prefix;

	switch (elevel)
	{
		case DEBUG1:
		case DEBUG2:
		case DEBUG3:
		case DEBUG4:
		case DEBUG5:
		case FRONTEND_DEBUG:
			prefix = "DEBUG";
			break;
		case LOG:
		case COMMERROR:
		case FRONTEND_LOG:
			prefix = "LOG";
			break;
		case INFO:
			prefix = "INFO";
			break;
		case NOTICE:
			prefix = "NOTICE";
			break;
		case WARNING:
			prefix = "WARNING";
			break;
		case ERROR:
			prefix = "ERROR";
			break;
		case FATAL:
			prefix = "FATAL";
			break;
		case PANIC:
			prefix = "PANIC";
			break;
		default:
			prefix = "???";
			break;
	}

	return prefix;
}

static char *
nowsec(void)
{
	static char strbuf[MAXSTRFTIME];
	time_t		now = time(NULL);

	strftime(strbuf, MAXSTRFTIME, "%Y-%m-%d %H:%M:%S", localtime(&now));
	return strbuf;
}

bool errstart(int elevel, const char *filename, int lineno,
		 const char *funcname, const char *domain)
{
	_fe_error_level = elevel;

	/*
	 * This is a basic version and for now we just suppress all messages below
	 * WARNING for frontend
	 */
	if (_fe_error_level < WARNING)
		return 0;
	_filename = filename;
	_lineno = lineno;
	_funcname = funcname;
	return 1;
}