log.c 8.58 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
/*
2
3
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2008, 2013
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <errno.h>
#include <semaphore.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
35
#include <limits.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
36
37
#include <stdarg.h>
#include <stdbool.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
38

39
40
#include <tcp_connect.h>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
41
42
#include "log.h"

43

Jeroen Vreeken's avatar
Jeroen Vreeken committed
44
static int listen_port = 0;
45
46
47
48
static int server_port = 0;
static char *server_host = NULL;
static char *client_name = NULL;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
49
50
51
52
53
54
55

struct log_client {
	int used;
	int fd;
};

#define LOG_MAX_CLIENTS	10
56
#define LOG_MAX_QUEUE	2048
Jeroen Vreeken's avatar
Jeroen Vreeken committed
57
#define LOG_MAX_LEN	400
Jeroen Vreeken's avatar
Jeroen Vreeken committed
58
59
60
61
62
63
64
65
66
67
68
69
70

struct log_msg {
	int used;
	time_t t;
	int type;
	char msg[LOG_MAX_LEN];
};

static struct log_client clients[LOG_MAX_CLIENTS];

static struct log_msg msgs[LOG_MAX_QUEUE];
static int msgs_wr = 0;
static int msgs_rd = 0;
71
static int lost_counter = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
72
73
74

static sem_t queue_wait;

75
76
77
static enum log_type log_level_console;
static enum log_type log_level_remote;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
78
static pthread_mutex_t log_mutex;
79
80

void log_send(enum log_type type, char *fmt, ...)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
81
82
{
	int val;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
83
84
	va_list ap;

85
86
87
	if (log_level_console < type && log_level_remote < type)
		return;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
88
89
  	va_start(ap, fmt); 

Jeroen Vreeken's avatar
Jeroen Vreeken committed
90
91
	pthread_mutex_lock(&log_mutex);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
92
  	if (msgs[msgs_wr].used == 0) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
93
94
		msgs[msgs_wr].t = time(NULL);
		msgs[msgs_wr].type = type;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
95
96
		vsnprintf(msgs[msgs_wr].msg, LOG_MAX_LEN, fmt, ap);
		msgs[msgs_wr].used = 1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
97
98
		msgs_wr++;
		msgs_wr %= LOG_MAX_QUEUE;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	} else {
		lost_counter++;
	}

	/* If we lost some in the past check if we can log now... */
  	if (lost_counter && msgs[msgs_wr].used == 0) {
		msgs[msgs_wr].t = time(NULL);
		msgs[msgs_wr].type = LOG_T_WARNING;
		snprintf(msgs[msgs_wr].msg, LOG_MAX_LEN, 
		    "Lost %d messages",  lost_counter);
		msgs[msgs_wr].used = 1;
		msgs_wr++;
		msgs_wr %= LOG_MAX_QUEUE;
		lost_counter = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
113
	}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
114
	pthread_mutex_unlock(&log_mutex);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
115
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
116
117
	va_end(ap);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
118
119
120
121
122
	sem_getvalue(&queue_wait, &val);
	if (!val)
		sem_post(&queue_wait);
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
123
124
static void timestamp(struct tm *tm, char *time)
{
125
	sprintf(time, "%04d-%02d-%02dT%02d:%02d:%02dZ",
Jeroen Vreeken's avatar
Jeroen Vreeken committed
126
127
128
129
	    tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
	    tm->tm_hour, tm->tm_min, tm->tm_sec);
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
static void *log_server(void *arg)
{
	int fd_accept;
	struct sockaddr_in sockaddr;
	
	signal(SIGPIPE, SIG_IGN);
	
	fd_accept = socket(AF_INET, SOCK_STREAM, 0);
	if (fd_accept < 0) {
		perror("socket() failed");
		return NULL;
	}
	
	sockaddr.sin_family = AF_INET;
	sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	sockaddr.sin_port = htons(listen_port);
	
	setsockopt(fd_accept, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));

	if (bind(fd_accept,
	    (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
		perror("bind() failed");
		close(fd_accept);
		return NULL;
	}
	
	if (listen(fd_accept, 4) < 0) {
		perror("listen() failed");
		close(fd_accept);
		return NULL;
	}
	
	while (1) {
		int client;
		struct sockaddr_in sockaddr_client;
		socklen_t len;
		struct timeval tv;
		fd_set fdset_r;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
168
		bool newclient;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
169
170
171
172
173
		
		if (!msgs[msgs_rd].used) {
			sem_wait(&queue_wait);
		}
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
174
175
176
177
178
		do {
			FD_ZERO(&fdset_r);
			FD_SET(fd_accept, &fdset_r);
			tv.tv_sec = 0;
			tv.tv_usec = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
179
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
180
181
			select(fd_accept + 1, &fdset_r, NULL, NULL, &tv);
			newclient = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
182
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
183
184
185
186
187
188
189
190
			if (FD_ISSET(fd_accept, &fdset_r)) {
				len = sizeof(sockaddr_client);
				client = accept(fd_accept,
				    (struct sockaddr *)&sockaddr_client, &len);
				newclient = true;
			
				if (client >= 0) {
					int i;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
191
				
Jeroen Vreeken's avatar
Jeroen Vreeken committed
192
193
194
195
196
197
198
199
200
201
202
					ioctl(client, FIONBIO, &(int){1}, sizeof(int));
					
					for (i = 0; i < LOG_MAX_CLIENTS; i++) {
						if (!clients[i].used) {
							clients[i].used = 1;
							clients[i].fd = client;
							break;
						}
					}
					if (i == LOG_MAX_CLIENTS) {
						close(client);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
203
204
205
					}
				}
			}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
206
207
		} while (newclient);
				
Jeroen Vreeken's avatar
Jeroen Vreeken committed
208
209
210
211
212
213
214
		if (msgs[msgs_rd].used) {
			int i;
			char time[100];
			char *header;
			struct tm gmt;
			
			gmtime_r(&msgs[msgs_rd].t, &gmt);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
215
			timestamp(&gmt, time);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
216
217
218

			switch(msgs[msgs_rd].type) {
				case LOG_T_ERROR:
219
					header = "  ERROR: ";
Jeroen Vreeken's avatar
Jeroen Vreeken committed
220
221
222
223
224
					break;
				case LOG_T_WARNING:
					header = "WARNING: ";
					break;
				case LOG_T_INFO:
225
					header = "   INFO: ";
Jeroen Vreeken's avatar
Jeroen Vreeken committed
226
					break;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
227
				case LOG_T_DEBUG:
228
					header = "  DEBUG: ";
Jeroen Vreeken's avatar
Jeroen Vreeken committed
229
					break;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
230
231
232
233
234
				default:
					header = "UNKNOWN: ";
					break;
			}
			
235
236
237
			if (msgs[msgs_rd].type <= log_level_remote) {
				for (i = 0; i < LOG_MAX_CLIENTS; i++) {
					int r;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
238
				
239
240
					if (clients[i].used == 0)
						continue;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
241
				
242
243
244
					write(clients[i].fd, time, strlen(time));
					write(clients[i].fd, " ", 1);
					write(clients[i].fd, header, strlen(header));
245

246
247
248
249
250
251
252
253
254
255
256
					write(clients[i].fd, msgs[msgs_rd].msg,
					    strlen(msgs[msgs_rd].msg));
					
					r = write(clients[i].fd, "\n", 1);
					fsync(clients[i].fd);
					
					if (r <= 0) {
						close(clients[i].fd);
						clients[i].fd = -1;
						clients[i].used = 0;
					}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
257
258
259
				}
			}
			
260
261
			if (msgs[msgs_rd].type <= log_level_console)
				printf("%s %s%s\n", time, header, msgs[msgs_rd].msg);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
262
263
264
265
266
267
268
269
270
271
			
			msgs[msgs_rd].used = 0;
			msgs_rd++;
			msgs_rd %= LOG_MAX_QUEUE;
		}
	}

	return NULL;
}

272
273
int log_server_start(int port, enum log_type console_level,
    enum log_type remote_level)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
274
275
276
277
278
{
	pthread_t thread_id;
	pthread_attr_t attr;
	int i;

279
280
281
	log_level_console = console_level;
	log_level_remote = remote_level;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	listen_port = port;

	for (i = 0; i < LOG_MAX_QUEUE; i++) {
		msgs[i].used = 0;
	}
	
	for (i = 0; i < LOG_MAX_CLIENTS; i++) {
		clients[i].fd = -1;
		clients[i].used = 0;
	}

	sem_init(&queue_wait, 0, 1);

	printf("Starting log server on port %d\n", port);

	pthread_attr_init(&attr);
	pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN * 2);

	pthread_create(&thread_id, &attr, log_server, NULL);

	return 0;
}
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
410
411
412
413
414
415
416
417
418
419
420
421

static void *log_client(void *arg)
{
	int fd = -1;
	
	signal(SIGPIPE, SIG_IGN);
	
	while (1) {
		if (!msgs[msgs_rd].used) {
			sem_wait(&queue_wait);
		}
						
		if (msgs[msgs_rd].used) {
			char time[100];
			char *header;
			struct tm gmt;
			
			gmtime_r(&msgs[msgs_rd].t, &gmt);
			timestamp(&gmt, time);

			switch(msgs[msgs_rd].type) {
				case LOG_T_ERROR:
					header = "  ERROR: ";
					break;
				case LOG_T_WARNING:
					header = "WARNING: ";
					break;
				case LOG_T_INFO:
					header = "   INFO: ";
					break;
				case LOG_T_DEBUG:
					header = "  DEBUG: ";
					break;
				default:
					header = "UNKNOWN: ";
					break;
			}
			
			if (fd < 0) {
				fd = tcp_connect(server_host, server_port);
			}
			
			if (msgs[msgs_rd].type <= log_level_remote && fd >= 0) {
				int r;
				
				write(fd, time, strlen(time));
				write(fd, " ", 1);
				write(fd, header, strlen(header));
					
				if (client_name) {
					write(fd, client_name, strlen(client_name));
					write(fd, ": ", 2);
				}
					
				write(fd, msgs[msgs_rd].msg,
				    strlen(msgs[msgs_rd].msg));
					
				r = write(fd, "\n", 1);
				fsync(fd);
					
				if (r <= 0) {
					close(fd);
					fd = -1;
				}
			}
			
			if (msgs[msgs_rd].type <= log_level_console)
				printf("%s %s%s\n", time, header, msgs[msgs_rd].msg);
			
			msgs[msgs_rd].used = 0;
			msgs_rd++;
			msgs_rd %= LOG_MAX_QUEUE;
		}
	}

	return NULL;
}

int log_client_start(char *host, int port, enum log_type console_level,
    enum log_type remote_level, char *name)
{
	pthread_t thread_id;
	pthread_attr_t attr;
	int i;

	log_level_console = console_level;
	log_level_remote = remote_level;

	server_port = port;
	if (server_host)
		free(server_host);
	server_host = strdup(host);
	if (client_name)
		free(client_name);
	client_name = strdup(name);
	
	for (i = 0; i < LOG_MAX_QUEUE; i++) {
		msgs[i].used = 0;
	}
	
	for (i = 0; i < LOG_MAX_CLIENTS; i++) {
		clients[i].fd = -1;
		clients[i].used = 0;
	}

	sem_init(&queue_wait, 0, 1);

	printf("Starting log client %s:%d for %s\n", host, port, name);

	pthread_attr_init(&attr);
	pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN * 2);

	pthread_create(&thread_id, &attr, log_client, NULL);

	return 0;
}

422
423
424
425
426
427
428
void log_server_flush(void)
{
	sleep(1);
	do {
		sleep(1);
	} while (msgs[msgs_rd].used);
}