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

	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
41
42
43
44
45
46
47
48

#include "log.h"

static int listen_port = 0;

struct log_client {
	int used;
	int fd;
};

#define LOG_MAX_CLIENTS	10
49
#define LOG_MAX_QUEUE	2048
Jeroen Vreeken's avatar
Jeroen Vreeken committed
50
#define LOG_MAX_LEN	400
Jeroen Vreeken's avatar
Jeroen Vreeken committed
51
52
53
54
55
56
57
58
59
60
61
62
63

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;
64
static int lost_counter = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
65
66
67

static sem_t queue_wait;

68
69
70
71
72
static enum log_type log_level_console;
static enum log_type log_level_remote;


void log_send(enum log_type type, char *fmt, ...)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
73
74
{
	int val;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
75
76
	va_list ap;

77
78
79
	if (log_level_console < type && log_level_remote < type)
		return;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
80
81
82
  	va_start(ap, fmt); 

  	if (msgs[msgs_wr].used == 0) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
83
84
		msgs[msgs_wr].t = time(NULL);
		msgs[msgs_wr].type = type;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
85
86
		vsnprintf(msgs[msgs_wr].msg, LOG_MAX_LEN, fmt, ap);
		msgs[msgs_wr].used = 1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
87
88
		msgs_wr++;
		msgs_wr %= LOG_MAX_QUEUE;
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	} 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
103
104
	}
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
105
106
	va_end(ap);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
107
108
109
110
111
	sem_getvalue(&queue_wait, &val);
	if (!val)
		sem_post(&queue_wait);
}

Jeroen Vreeken's avatar
Jeroen Vreeken committed
112
113
114
115
116
117
118
static void timestamp(struct tm *tm, char *time)
{
	sprintf(time, "%04d%02d%02d.%02d%02d%02d",
	    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
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
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
157
		bool newclient;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
158
159
160
161
162
		
		if (!msgs[msgs_rd].used) {
			sem_wait(&queue_wait);
		}
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
163
164
165
166
167
		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
168
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
169
170
			select(fd_accept + 1, &fdset_r, NULL, NULL, &tv);
			newclient = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
171
		
Jeroen Vreeken's avatar
Jeroen Vreeken committed
172
173
174
175
176
177
178
179
			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
180
				
Jeroen Vreeken's avatar
Jeroen Vreeken committed
181
182
183
184
185
186
187
188
189
190
191
					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
192
193
194
					}
				}
			}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
195
196
		} while (newclient);
				
Jeroen Vreeken's avatar
Jeroen Vreeken committed
197
198
199
200
201
202
203
		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
204
			timestamp(&gmt, time);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
205
206
207

			switch(msgs[msgs_rd].type) {
				case LOG_T_ERROR:
208
					header = "  ERROR: ";
Jeroen Vreeken's avatar
Jeroen Vreeken committed
209
210
211
212
213
					break;
				case LOG_T_WARNING:
					header = "WARNING: ";
					break;
				case LOG_T_INFO:
214
					header = "   INFO: ";
Jeroen Vreeken's avatar
Jeroen Vreeken committed
215
					break;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
216
				case LOG_T_DEBUG:
217
					header = "  DEBUG: ";
Jeroen Vreeken's avatar
Jeroen Vreeken committed
218
					break;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
219
220
221
222
223
				default:
					header = "UNKNOWN: ";
					break;
			}
			
224
225
226
			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
227
				
228
229
					if (clients[i].used == 0)
						continue;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
230
				
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
					write(clients[i].fd, time, strlen(time));
					write(clients[i].fd, " ", 1);
					write(clients[i].fd, header, strlen(header));
					
					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
246
247
248
				}
			}
			
249
250
			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
251
252
253
254
255
256
257
258
259
260
			
			msgs[msgs_rd].used = 0;
			msgs_rd++;
			msgs_rd %= LOG_MAX_QUEUE;
		}
	}

	return NULL;
}

261
262
int log_server_start(int port, enum log_type console_level,
    enum log_type remote_level)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
263
264
265
266
267
{
	pthread_t thread_id;
	pthread_attr_t attr;
	int i;

268
269
270
	log_level_console = console_level;
	log_level_remote = remote_level;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
	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;
}
293
294
295
296
297
298
299
300

void log_server_flush(void)
{
	sleep(1);
	do {
		sleep(1);
	} while (msgs[msgs_rd].used);
}