File: liblogthread.c

package info (click to toggle)
redhat-cluster 3.0.12-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,620 kB
  • ctags: 14,431
  • sloc: ansic: 150,556; sh: 10,974; perl: 4,383; python: 3,022; makefile: 2,253; xml: 61
file content (334 lines) | stat: -rw-r--r-- 7,254 bytes parent folder | download | duplicates (3)
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
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <syslog.h>
#include <pthread.h>
#include <sys/param.h>

#include "liblogthread.h"

#define DEFAULT_ENTRIES 4096
#define ENTRY_STR_LEN 128

struct entry {
	int level;
	char str[ENTRY_STR_LEN];
	time_t time;
};

static struct entry *ents;
static unsigned int num_ents = DEFAULT_ENTRIES;
static unsigned int head_ent, tail_ent; /* add at head, remove from tail */
static unsigned int dropped;
static unsigned int pending_ents;
static unsigned int init;
static unsigned int done;
static pthread_t thread_handle;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

static int logt_mode; /* LOG_MODE_ */
static int logt_syslog_facility;
static int logt_syslog_priority;
static int logt_logfile_priority;
static char logt_name[PATH_MAX];
static char logt_logfile[PATH_MAX];
static FILE *logt_logfile_fp;

static char *_time(time_t *t)
{
	static char buf[64];

	strftime(buf, sizeof(buf), "%b %d %T", localtime(t));
	return buf;
}

static void write_entry(int level, time_t *t, char *str)
{
	if ((logt_mode & LOG_MODE_OUTPUT_FILE) &&
	    (level <= logt_logfile_priority) && logt_logfile_fp) {
		fprintf(logt_logfile_fp, "%s %s %s", _time(t), logt_name, str);
		fflush(logt_logfile_fp);
	}
	if ((logt_mode & LOG_MODE_OUTPUT_SYSLOG) &&
	    (level <= logt_syslog_priority))
		syslog(level, "%s", str);
}

static void write_dropped(int level, time_t *t, int num)
{
	char str[ENTRY_STR_LEN];
	sprintf(str, "dropped %d entries", num);
	write_entry(level, t, str);
}

static void *thread_fn(void *arg)
{
	char str[ENTRY_STR_LEN];
	struct entry *e;
	time_t logtime;
	int level, prev_dropped = 0;

	while (1) {
		pthread_mutex_lock(&mutex);
		while (head_ent == tail_ent) {
			if (done) {
				pthread_mutex_unlock(&mutex);
				goto out;
			}
			pthread_cond_wait(&cond, &mutex);
		}

		e = &ents[tail_ent++];
		tail_ent = tail_ent % num_ents;
		pending_ents--;

		memcpy(str, e->str, ENTRY_STR_LEN);
		level = e->level;
		logtime = e->time;

		prev_dropped = dropped;
		dropped = 0;
		pthread_mutex_unlock(&mutex);

		if (prev_dropped) {
			write_dropped(level, &logtime, prev_dropped);
			prev_dropped = 0;
		}

		write_entry(level, &logtime, str);
	}
 out:
	pthread_exit(NULL);
}

static void _logt_print(int level, char *buf)
{
	struct entry *e;

	pthread_mutex_lock(&mutex);

	if (pending_ents == num_ents) {
		dropped++;
		goto out;
	}

	e = &ents[head_ent++];
	head_ent = head_ent % num_ents;
	pending_ents++;

	strncpy(e->str, buf, ENTRY_STR_LEN);
	e->level = level;
	e->time = time(NULL);
 out:
	pthread_cond_signal(&cond);
	pthread_mutex_unlock(&mutex);
}

void logt_print(int level, const char *fmt, ...)
{
	va_list ap;
	char buf[ENTRY_STR_LEN];

	if (!init)
		return;

	buf[sizeof(buf) - 1] = 0;

	va_start(ap, fmt);
	vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
	va_end(ap);

	if (level > logt_syslog_priority && level > logt_logfile_priority)
		return;

	/* this stderr crap really doesn't belong in this lib, please
	   feel free to not use it */
	if (logt_mode & LOG_MODE_OUTPUT_STDERR)
		fputs(buf, stderr);

	_logt_print(level, buf);
}

static void _conf(const char *name, int mode, int syslog_facility,
		  int syslog_priority, int logfile_priority, const char *logfile)
{
	int fd;

	pthread_mutex_lock(&mutex);
	logt_mode = mode;
	logt_syslog_facility = syslog_facility;
	logt_syslog_priority = syslog_priority;
	logt_logfile_priority = logfile_priority;
	if (name)
		strncpy(logt_name, name, PATH_MAX);
	if (logfile)
		strncpy(logt_logfile, logfile, PATH_MAX);

	if (logt_mode & LOG_MODE_OUTPUT_FILE && logt_logfile[0]) {
		if (logt_logfile_fp) {
			fclose(logt_logfile_fp);
			logt_logfile_fp = NULL;
		}
		logt_logfile_fp = fopen(logt_logfile, "a+");
		if (logt_logfile_fp != NULL) {
			fd = fileno(logt_logfile_fp);
			fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
		}
	} else {
		if (logt_logfile_fp) {
			fclose(logt_logfile_fp);
			logt_logfile_fp = NULL;
		}
	}

	if (logt_mode & LOG_MODE_OUTPUT_SYSLOG) {
		closelog();
		openlog(logt_name, LOG_CONS | LOG_PID, logt_syslog_facility);
	}
	pthread_mutex_unlock(&mutex);
}

void logt_conf(const char *name, int mode, int syslog_facility, int syslog_priority,
	       int logfile_priority, const char *logfile)
{
	if (!init)
		return;

	_conf(name, mode, syslog_facility, syslog_priority, logfile_priority,
	      logfile);
}

int logt_init(const char *name, int mode, int syslog_facility, int syslog_priority,
	      int logfile_priority, const char *logfile)
{
	int rv;

	if (init)
		return -1;

	_conf(name, mode, syslog_facility, syslog_priority, logfile_priority,
	      logfile);

	ents = malloc(num_ents * sizeof(struct entry));
	if (!ents)
		return -1;
	memset(ents, 0, num_ents * sizeof(struct entry));

	rv = pthread_create(&thread_handle, NULL, thread_fn, NULL);
	if (rv) {
		free(ents);
		return -1;
	}
	done = 0;
	init = 1;
	return 0;
}


/*
 * Reinitialize logt w/ previous values (e.g. use after
 * a call to fork())
 *
 * Only works after you call logt_init and logt_exit
 */
int logt_reinit(void)
{
	char name_tmp[PATH_MAX];
	char file_tmp[PATH_MAX];

	if (!done || init)
		return -1;

	/* Use copies on the stack for these */
	memset(name_tmp, 0, sizeof(name_tmp));
	memset(file_tmp, 0, sizeof(file_tmp));

	strncpy(name_tmp, logt_name, sizeof(name_tmp));
	if (!strlen(name_tmp))
		return -1;
	if (strlen(logt_logfile))
		strncpy(file_tmp, logt_logfile, sizeof(file_tmp));

	return logt_init(name_tmp, logt_mode, logt_syslog_facility,
			 logt_syslog_priority, logt_logfile_priority,
			 file_tmp);
}


void logt_exit(void)
{
	pthread_mutex_lock(&mutex);
	done = 1;
	init = 0;
	pthread_cond_signal(&cond);
	pthread_mutex_unlock(&mutex);
	pthread_join(thread_handle, NULL);

	pthread_mutex_lock(&mutex);
	/* close syslog + log file */
	closelog();
	if (logt_logfile_fp) {
		fclose(logt_logfile_fp);
		logt_logfile_fp = NULL;
	}

	/* clean up any pending log messages */
	dropped = 0;
	pending_ents = 0;
	head_ent = tail_ent = 0;
	free(ents);
	ents = NULL;

	pthread_mutex_unlock(&mutex);
}

#ifdef TEST
int main(int argc, char **argv)
{
	int pid;

	logt_init("test", LOG_MODE_OUTPUT_FILE|LOG_MODE_OUTPUT_SYSLOG,
		  LOG_DAEMON, LOG_DEBUG, LOG_DEBUG, "/tmp/logthread");
	logt_print(LOG_DEBUG, "debugging message %d\n", argc);
	logt_print(LOG_ERR, "error message %d\n", argc);
	sleep(1);
	logt_print(LOG_DEBUG, "second debug message\n");
	logt_exit();

	logt_print(LOG_ERR, "If you see this, it's a bug\n");

	logt_init("test2", LOG_MODE_OUTPUT_FILE|LOG_MODE_OUTPUT_SYSLOG,
		  LOG_DAEMON, LOG_DEBUG, LOG_DEBUG, "/tmp/logthread");
	logt_print(LOG_DEBUG, "after 2nd init %d\n", argc);
	logt_print(LOG_ERR, "error message %d\n", argc);
	logt_print(LOG_DEBUG, "third debug message\n");
	logt_exit();

	logt_print(LOG_ERR, "If you see this, it's a bug\n");

	logt_reinit();
	logt_print(LOG_DEBUG, "after reinit\n");
	logt_print(LOG_DEBUG, "<-- should say test2\n");

	logt_exit();

	if ((pid = fork()) < 0)
		return -1;

	if (pid) 
		exit(0);

	/* child process */
	logt_reinit();
	logt_print(LOG_DEBUG, "HELLO from child process\n");
	logt_exit();

	return 0;
}
#endif