summaryrefslogtreecommitdiff
path: root/contrib/pgxc_ctl/pgxc_ctl_log.c
blob: 8934dbfa3fac6097842783166137e06aca8eb28d (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
/*-------------------------------------------------------------------------
 *
 * pgxc_ctl_log.c
 *
 *    Logging module of Postgres-XC configuration and operation tool.
 *
 *
 * Portions Copyright (c) 2013 Postgres-XC Development Group
 *
 *-------------------------------------------------------------------------
 */
/*
 * To allow mutiple pgxc_ctl to run in parallel and write a log to the same file,
 * this module uses fctl to lock log I/O.  You can lock/unlock in stack.   Anyway
 * actual lock will be captured/released at the bottom level of this stack.
 * If you'd like to have a block of the logs to be in a single block, not interrupted
 * bo other pgxc_ctl log, you should be careful to acquire the lock and release it
 * reasonablly.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>

#include "pgxc_ctl.h"
#include "pgxc_ctl_log.h"
#include "varnames.h"
#include "variables.h"
#include "config.h"
#include "utils.h"

FILE *logFile = NULL;
char logFileName[MAXPATH+1];
static char *pgxcCtlGetTime(void);
static int lockStack = 0;
#define lockStackLimit 8

int logMsgLevel = INFO;
int printMsgLevel = WARNING;
int	printLocation = FALSE;
int logLocation = FALSE;


/*
 * Path is NULL if name is effective.
 * Path is valid if name is NULL
 */
static void set_msgLogLevel(void)
{
	if (sval(VAR_logMessage) == NULL)
		logMsgLevel = WARNING;
	else if (strcasecmp(sval(VAR_logMessage), "panic") == 0)
		logMsgLevel = PANIC;
	else if (strcasecmp(sval(VAR_logMessage), "error") == 0)
		logMsgLevel = ERROR;
	else if (strcasecmp(sval(VAR_logMessage), "warning") == 0)
		logMsgLevel = WARNING;
	else if (strcasecmp(sval(VAR_logMessage), "notice") == 0)
		logMsgLevel = NOTICE;
	else if (strcasecmp(sval(VAR_logMessage), "info") == 0)
		logMsgLevel = INFO;
	else if (strcasecmp(sval(VAR_logMessage), "debug1") == 0)
		logMsgLevel = DEBUG1;
	else if (strcasecmp(sval(VAR_logMessage), "debug2") == 0)
		logMsgLevel = DEBUG2;
	else if (strcasecmp(sval(VAR_logMessage), "debug3") == 0)
		logMsgLevel = DEBUG3;
	else
		logMsgLevel = INFO;
}

static void set_printLogLevel(void)
{
	if (sval(VAR_printMessage) == NULL)
		printMsgLevel = ERROR;
	else if (strcasecmp(sval(VAR_printMessage), "panic") == 0)
		printMsgLevel = PANIC;
	else if (strcasecmp(sval(VAR_printMessage), "error") == 0)
		printMsgLevel = ERROR;
	else if (strcasecmp(sval(VAR_printMessage), "warning") == 0)
		printMsgLevel = WARNING;
	else if (strcasecmp(sval(VAR_printMessage), "notice") == 0)
		printMsgLevel = NOTICE;
	else if (strcasecmp(sval(VAR_printMessage), "info") == 0)
		printMsgLevel = INFO;
	else if (strcasecmp(sval(VAR_printMessage), "debug1") == 0)
		printMsgLevel = DEBUG1;
	else if (strcasecmp(sval(VAR_printMessage), "debug2") == 0)
		printMsgLevel = DEBUG2;
	else if (strcasecmp(sval(VAR_printMessage), "debug3") == 0)
		printMsgLevel = DEBUG3;
	else
		printMsgLevel = WARNING;
}

void initLog(char *path, char *name)
{
	if(logFile)
		return;
	if(name)
		strncat(logFileName, name, MAXPATH);
	else
		snprintf(logFileName, MAXPATH, "%s/%d_pgxc_ctl.log", path, getpid());
	if ((logFile = fopen(logFileName, "a")) == NULL)
		fprintf(stderr, "Could not open log file %s, %s\n", logFileName, strerror(errno));
	/* Setup log/print message level */
	set_msgLogLevel();
	set_printLogLevel();
	printLocation = (isVarYes(VAR_printLocation)) ? TRUE : FALSE;
	logLocation = (isVarYes(VAR_logLocation)) ? TRUE : FALSE;
	lockStack = 0;
}

void closeLog()
{
	fclose(logFile);
	logFile = NULL;
}

static char *fname;
static char *funcname;
static int lineno;

void elog_start(const char *file, const char *func, int line)
{
	fname = Strdup(file);
	funcname = Strdup(func);
	lineno = line;
}

static void clean_location(void)
{
	freeAndReset(fname);
	freeAndReset(funcname);
	lineno = -1;
}


static void elogMsgRaw0(int level, const char *msg, int flag)
{
	if (logFile && level >= logMsgLevel)
	{
		if (logLocation && flag)
			fprintf(logFile, "%s(%d):%s %s:%s(%d) %s", progname, getpid(), pgxcCtlGetTime(), 
					fname, funcname, lineno, msg); 
		else
			fprintf(logFile, "%s(%d):%s %s", progname, getpid(), pgxcCtlGetTime(), msg); 
		fflush(logFile);
	}
	if (level >= printMsgLevel)
	{
		if (printLocation && flag)
			fprintf(((outF) ? outF : stderr), "%s:%s(%d) %s", fname, funcname, lineno, msg); 
		else
			fputs(msg, (outF) ? outF : stderr);
		fflush((outF) ? outF : stderr);
	}
	clean_location();
}

void elogMsgRaw(int level, const char *msg)
{
	lockLogFile();
	elogMsgRaw0(level, msg, TRUE);
	unlockLogFile();
}

void elogFinish(int level, const char *fmt, ...)
{
	char msg[MAXLINE+1];
	va_list arg;

	lockLogFile();
	if ((level >= logMsgLevel) || (level >= printMsgLevel))
	{
		va_start(arg, fmt);
		vsnprintf(msg, MAXLINE, fmt, arg);
		va_end(arg);
		elogMsgRaw(level, msg);
	}
	unlockLogFile();
}

void elogFileRaw(int level, char *path)
{
	FILE *f;
	char s[MAXLINE+1];

	lockLogFile();
	if ((f = fopen(path, "r")))
	{
		while(fgets(s, MAXLINE, f))
			elogMsgRaw0(level, s, FALSE);
		fclose(f);
	}
	else
		elog(ERROR, "ERROR: Cannot open \"%s\" for read, %s\n", path, strerror(errno));
	unlockLogFile();
}

static char timebuf[MAXTOKEN+1];

/*
 * Please note that this routine is not reentrant
 */
static char *pgxcCtlGetTime(void)
{
	struct tm *tm_s;
	time_t now;

	now = time(NULL);
	tm_s = localtime(&now);
/*	tm_s = gmtime(&now); */

	snprintf(timebuf, MAXTOKEN, "%02d%02d%02d%02d%02d_%02d", 
			 ((tm_s->tm_year+1900) >= 2000) ? (tm_s->tm_year + (1900 - 2000)) : tm_s->tm_year, 
			 tm_s->tm_mon+1, tm_s->tm_mday, tm_s->tm_hour, tm_s->tm_min, tm_s->tm_sec);
	return timebuf;
}

void writeLogRaw(const char *fmt, ...)
{
	char msg[MAXLINE+1];
	va_list arg;

	va_start(arg, fmt);
	vsnprintf(msg, MAXLINE, fmt, arg);
	va_end(arg);
	if (logFile)
	{
		lockLogFile();
		fprintf(logFile, "%s(%d):%s %s", progname, getpid(), pgxcCtlGetTime(), msg); 
		fflush(logFile);
		unlockLogFile();
	}
	fputs(msg, logFile ? logFile : stderr);
	fflush(outF ? outF : stderr);
}

void writeLogOnly(const char *fmt, ...)
{
	char msg[MAXLINE+1];
	va_list arg;

	if (logFile)
	{
		va_start(arg, fmt);
		vsnprintf(msg, MAXLINE, fmt, arg);
		va_end(arg);
		lockLogFile();
		fprintf(logFile, "%s(%d):%s %s", progname, getpid(), pgxcCtlGetTime(), msg); 
		fflush(logFile);
		unlockLogFile();
	}
}

int setLogMsgLevel(int newLevel)
{
	int rc;

	rc = logMsgLevel;
	logMsgLevel = newLevel;
	return rc;
}

int getLogMsgLevel(void)
{
	return logMsgLevel;
}

int setPrintMsgLevel(int newLevel)
{
	int rc;

	rc = printMsgLevel;
	printMsgLevel = newLevel;
	return rc;
}

int getPrintMsgLevel(void)
{
	return printMsgLevel;
}

void lockLogFile(void)
{
	struct flock lock1;

	if (logFile == NULL)
		return;
	if (lockStack > lockStackLimit)
	{
		fprintf(stderr, "Log file lock stack exceeded the limit %d. Something must be wrong.\n", lockStackLimit);
		return;
	}
	if (lockStack == 0)
	{
		lock1.l_type = F_WRLCK;
		lock1.l_start = 0;
		lock1.l_len = 0;
		lock1.l_whence = SEEK_SET;
		fcntl(fileno(logFile), F_SETLKW, &lock1);
	}
	lockStack++;
}


void unlockLogFile(void)
{
	struct flock lock1;

	if (logFile == NULL)
		return;
	lockStack--;
	if (lockStack < 0)
	{
		fprintf(stderr, "Log file stack is below zero.  Something must be wrong.\n");
		return;
	}
	if (lockStack == 0)
	{
		lock1.l_type = F_UNLCK;
		lock1.l_start = 0;
		lock1.l_len = 0;
		lock1.l_whence = SEEK_SET;
		fcntl(fileno(logFile), F_SETLKW, &lock1);
	}
}