trafgen: Move checking of dynamic packet elements to header
[netsniff-ng-new.git] / cookie.c
blob9ee1ac6aebc5cc2f9a46482f095c249ee2dbfb8a
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <string.h>
4 #include <syslog.h>
6 #include "cookie.h"
8 static char const *priov[] = {
9 [LOG_EMERG] = "EMERG:",
10 [LOG_ALERT] = "ALERT:",
11 [LOG_CRIT] = "CRIT:",
12 [LOG_ERR] = "ERR:",
13 [LOG_WARNING] = "WARNING:",
14 [LOG_NOTICE] = "NOTICE:",
15 [LOG_INFO] = "INFO:",
16 [LOG_DEBUG] = "DEBUG:",
19 static ssize_t cookie_writer(void *cookie, char const *data, size_t leng)
21 int prio = LOG_DEBUG, len;
23 do {
24 len = strlen(priov[prio]);
25 } while (memcmp(data, priov[prio], len) && --prio >= 0);
27 if (prio < 0) {
28 prio = LOG_INFO;
29 } else {
30 data += len;
31 leng -= len;
34 while (*data == ' ') {
35 ++data;
36 --leng;
39 syslog(prio, "%.*s", (int) leng, data);
41 return leng;
44 static cookie_io_functions_t cookie_log = {
45 .write = cookie_writer,
48 void to_std_log(FILE **fp)
50 setvbuf(*fp = fopencookie(NULL, "w", cookie_log), NULL, _IOLBF, 0);