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|SOCK_CLOEXEC, 0);
57 void openlog(const char *ident, int opt, int facility)
60 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
62 __openlog(ident, opt, facility);
64 pthread_setcancelstate(cs, 0);
67 static void _vsyslog(int priority, const char *message, va_list ap)
77 __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
86 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
88 pid = (log_opt & LOG_PID) ? getpid() : 0;
89 l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
91 log_ident ? log_ident : "",
92 "["+!pid, pid, "]"+!pid);
93 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
96 if (buf[l-1] != '\n') buf[l++] = '\n';
97 sendto(log_fd, buf, l, 0, (void *)&log_addr, 11);
103 void __vsyslog(int priority, const char *message, va_list ap)
106 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
107 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
109 _vsyslog(priority, message, ap);
111 pthread_setcancelstate(cs, 0);
114 void syslog(int priority, const char *message, ...)
117 va_start(ap, message);
118 __vsyslog(priority, message, ap);
122 weak_alias(__vsyslog, vsyslog);