2 #include <sys/socket.h>
14 static const char *log_ident;
16 static int log_facility = LOG_USER;
17 static int log_mask = 0xff;
18 static int log_fd = -1;
20 int setlogmask(int maskpri)
23 if (maskpri) log_mask = maskpri;
38 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
43 pthread_setcancelstate(cs, 0);
46 static void __openlog(const char *ident, int opt, int facility)
50 log_facility = facility;
52 if (!(opt & LOG_NDELAY) || log_fd>=0) return;
54 log_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
55 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
58 void openlog(const char *ident, int opt, int facility)
61 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
63 __openlog(ident, opt, facility);
65 pthread_setcancelstate(cs, 0);
68 static void _vsyslog(int priority, const char *message, va_list ap)
78 __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
87 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
89 pid = (log_opt & LOG_PID) ? getpid() : 0;
90 l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
92 log_ident ? log_ident : "",
93 "["+!pid, pid, "]"+!pid);
94 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
97 if (buf[l-1] != '\n') buf[l++] = '\n';
98 sendto(log_fd, buf, l, 0, (void *)&log_addr, 11);
104 void __vsyslog(int priority, const char *message, va_list ap)
107 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
108 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
110 _vsyslog(priority, message, ap);
112 pthread_setcancelstate(cs, 0);
115 void syslog(int priority, const char *message, ...)
118 va_start(ap, message);
119 __vsyslog(priority, message, ap);
123 weak_alias(__vsyslog, vsyslog);