File: loglib.c

package info (click to toggle)
rtpengine 13.5.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,676 kB
  • sloc: ansic: 86,764; perl: 59,422; python: 3,193; sh: 1,030; makefile: 693; asm: 211
file content (289 lines) | stat: -rw-r--r-- 6,105 bytes parent folder | download
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
#include "loglib.h"
#include <stdarg.h>
#include <syslog.h>
#include <stdio.h>
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include <glib/gprintf.h>
#include "auxlib.h"
#include "bencode.h"


struct log_limiter_entry {
	char *prefix;
	char *msg;
	time_t when;
};

typedef struct _fac_code {
	char	*c_name;
	int	c_val;
} _fac_code_t;



static write_log_t log_both;

unsigned int max_log_line_length = 500;
write_log_t *write_log = (write_log_t *) log_both;



#define ll(system, descr) #system,
const char * const log_level_names[] = {
#include "loglevels.h"
NULL
};
#undef ll
#define ll(system, descr) descr,
const char * const log_level_descriptions[] = {
#include "loglevels.h"
NULL
};
#undef ll



static const _fac_code_t _facilitynames[] =
	{
		{ "auth", LOG_AUTH },
		{ "authpriv", LOG_AUTHPRIV },
		{ "cron", LOG_CRON },
		{ "daemon", LOG_DAEMON },
		{ "ftp", LOG_FTP },
		{ "kern", LOG_KERN },
		{ "lpr", LOG_LPR },
		{ "mail", LOG_MAIL },
		{ "news", LOG_NEWS },
		{ "syslog", LOG_SYSLOG },
		{ "user", LOG_USER },
		{ "uucp", LOG_UUCP },
		{ "local0", LOG_LOCAL0 },
		{ "local1", LOG_LOCAL1 },
		{ "local2", LOG_LOCAL2 },
		{ "local3", LOG_LOCAL3 },
		{ "local4", LOG_LOCAL4 },
		{ "local5", LOG_LOCAL5 },
		{ "local6", LOG_LOCAL6 },
		{ "local7", LOG_LOCAL7 },
		{ NULL, -1 }
	};

static const char* const prio_str[] = {
		"EMERG",
		"ALERT",
		"CRIT",
		"ERR",
		"WARNING",
		"NOTICE",
		"INFO",
		"DEBUG"
	};

int ilog_facility = LOG_DAEMON;



static GHashTable *__log_limiter;
static mutex_t __log_limiter_lock = MUTEX_STATIC_INIT;
static unsigned int __log_limiter_count;
static bencode_buffer_t __log_limiter_buffer;




static void vlog_to_stderr(int facility_priority, const char *format, va_list ap) {
	char *msg;
	int ret;
	struct timeval tv_now;

	ret = vasprintf(&msg, format, ap);

	if (ret < 0) {
		fprintf(stderr,"ERR: Failed to print log message - message dropped\n");
		return;
	}

	if (rtpe_common_config_ptr->no_log_timestamps)
		fprintf(stderr, "%s\n", msg);
	else {
		gettimeofday(&tv_now, NULL);

		fprintf(stderr, "[%lu.%06lu] %s\n", (unsigned long) tv_now.tv_sec,
				(unsigned long) tv_now.tv_usec, msg);
	}

	free(msg);
}

void log_to_stderr(int facility_priority, const char *format, ...) {
	va_list ap;

	va_start(ap, format);
	vlog_to_stderr(facility_priority, format, ap);
	va_end(ap);
}

static void log_both(int facility_priority, const char *format, ...) {
	va_list ap;

	va_start(ap, format);
	vsyslog(facility_priority, format, ap);
	va_end(ap);

	if (LOG_LEVEL_MASK(facility_priority) <= LOG_WARN) {
		va_start(ap, format);
		vlog_to_stderr(facility_priority, format, ap);
		va_end(ap);
	}
}



void __vpilog(int prio, const char *prefix, const char *fmt, va_list ap) {
	g_autoptr(char) msg = NULL;
	char *piece;
	const char *infix = "";
	int len, xprio;
	const char *prio_prefix;

	xprio = LOG_LEVEL_MASK(prio);
	prio_prefix = prio_str[prio & LOG_PRIMASK];
	if (!prefix)
		prefix = "";

	len = g_vasprintf(&msg, fmt, ap);

	while (len > 0 && msg[len-1] == '\n')
		len--;

	if ((prio & LOG_FLAG_LIMIT)) {
		struct log_limiter_entry lle, *llep;

		lle.prefix = (char *) prefix;
		lle.msg = msg;

		LOCK(&__log_limiter_lock);

		if (__log_limiter_count > 10000) {
			g_hash_table_remove_all(__log_limiter);
			__log_limiter_count = 0;
			bencode_buffer_free(&__log_limiter_buffer);
			bencode_buffer_init(&__log_limiter_buffer);
		}

		time_t now = time(NULL);

		llep = g_hash_table_lookup(__log_limiter, &lle);
		if (!llep || (now - llep->when) >= 15) {
			llep = bencode_buffer_alloc(&__log_limiter_buffer, sizeof(*llep));
			if (llep) {
				*llep = (__typeof(*llep)) {
					.prefix = bencode_strdup(&__log_limiter_buffer, prefix),
					.msg = bencode_strdup(&__log_limiter_buffer, msg),
					.when = now,
				};
				g_hash_table_insert(__log_limiter, llep, llep);
				__log_limiter_count++;
				llep = NULL;
			}
		}

		if (llep)
			return;
	}

	piece = msg;

	while (1) {
		unsigned int max_line_len = rtpe_common_config_ptr->max_log_line_length;
		unsigned int skip_len = max_line_len;
		if (rtpe_common_config_ptr->split_logs) {
			char *newline = strchr(piece, '\n');
			if (newline) {
				unsigned int nl_pos = newline - piece;
				if (!max_line_len || nl_pos < max_line_len) {
					max_line_len = nl_pos;
					skip_len = nl_pos + 1;
					if (nl_pos >= 1 && piece[nl_pos-1] == '\r')
						max_line_len--;
				}
			}
		}

		if (!max_line_len)
			break;
		if (len <= max_line_len)
			break;

		write_log(xprio, "%s: %s%s%.*s ...", prio_prefix, prefix, infix, max_line_len, piece);
		len -= skip_len;
		piece += skip_len;
		infix = "... ";
	}

	write_log(xprio, "%s: %s%s%.*s", prio_prefix, prefix, infix, len, piece);
}


void __ilog_np(int prio, const char *fmt, ...) {
	va_list ap;

	va_start(ap, fmt);
	__vpilog(prio, NULL, fmt, ap);
	va_end(ap);
}



static unsigned int log_limiter_entry_hash(const void *p) {
	const struct log_limiter_entry *lle = p;
	return g_str_hash(lle->msg) ^ g_str_hash(lle->prefix);
}

static int log_limiter_entry_equal(const void *a, const void *b) {
	const struct log_limiter_entry *A = a, *B = b;
	if (!g_str_equal(A->msg, B->msg))
		return 0;
	if (!g_str_equal(A->prefix, B->prefix))
		return 0;
	return 1;
}

void log_init(const char *handle) {
	__log_limiter = g_hash_table_new(log_limiter_entry_hash, log_limiter_entry_equal);
	bencode_buffer_init(&__log_limiter_buffer);

	if (!rtpe_common_config_ptr->log_stderr)
		openlog(handle, LOG_PID | LOG_NDELAY, ilog_facility);
}

void log_free(void) {
	g_hash_table_destroy(__log_limiter);
	bencode_buffer_free(&__log_limiter_buffer);
}

int parse_log_facility(const char *name, int *dst) {
	int i;
	for (i = 0 ; _facilitynames[i].c_name; i++) {
		if (strcmp(_facilitynames[i].c_name, name) == 0) {
			*dst = _facilitynames[i].c_val;
			return 1;
		}
	}
	return 0;
}

void print_available_log_facilities(void) {
	int i;

	fprintf(stderr, "available facilities:");
	for (i = 0 ; _facilitynames[i].c_name; i++) {
		fprintf(stderr, " %s",  _facilitynames[i].c_name);
	}
	fprintf(stderr, "\n");
}