2 #include <sys/socket.h>
16 static char log_ident[32];
18 static int log_facility = LOG_USER;
19 static int log_mask = 0xff;
20 static int log_fd = -1;
22 int setlogmask(int maskpri)
24 if (maskpri) return a_swap(&log_mask, maskpri);
39 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
44 pthread_setcancelstate(cs, 0);
47 static void __openlog()
49 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
50 if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
53 void openlog(const char *ident, int opt, int facility)
56 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
60 size_t n = strnlen(ident, sizeof log_ident - 1);
61 memcpy(log_ident, ident, n);
67 log_facility = facility;
69 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
72 pthread_setcancelstate(cs, 0);
75 static void _vsyslog(int priority, const char *message, va_list ap)
81 int errno_save = errno;
87 if (log_fd < 0) __openlog();
89 if (!(priority & LOG_FACMASK)) priority |= log_facility;
93 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
95 pid = (log_opt & LOG_PID) ? getpid() : 0;
96 l = snprintf(buf, sizeof buf, "<%d>%s %n%s%s%.0d%s: ",
97 priority, timebuf, &hlen, log_ident, "["+!pid, pid, "]"+!pid);
99 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
101 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
103 if (buf[l-1] != '\n') buf[l++] = '\n';
104 if (send(log_fd, buf, l, 0) < 0 && (log_opt & LOG_CONS)) {
105 fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
107 dprintf(fd, "%.*s", l-hlen, buf+hlen);
111 if (log_opt & LOG_PERROR) dprintf(2, "%.*s", l-hlen, buf+hlen);
115 void __vsyslog(int priority, const char *message, va_list ap)
118 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
119 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
121 _vsyslog(priority, message, ap);
123 pthread_setcancelstate(cs, 0);
126 void syslog(int priority, const char *message, ...)
129 va_start(ap, message);
130 __vsyslog(priority, message, ap);
134 weak_alias(__vsyslog, vsyslog);