2 #include <sys/socket.h>
15 static char log_ident[32];
17 static int log_facility = LOG_USER;
18 static int log_mask = 0xff;
19 static int log_fd = -1;
21 int setlogmask(int maskpri)
23 if (maskpri) return a_swap(&log_mask, maskpri);
38 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
43 pthread_setcancelstate(cs, 0);
46 static void __openlog()
48 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
49 if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
52 void openlog(const char *ident, int opt, int facility)
55 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
59 size_t n = strnlen(ident, sizeof log_ident - 1);
60 memcpy(log_ident, ident, n);
66 log_facility = facility;
68 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
71 pthread_setcancelstate(cs, 0);
74 static void _vsyslog(int priority, const char *message, va_list ap)
80 int errno_save = errno;
86 if (log_fd < 0) return;
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 %s%s%.0d%s: ",
97 priority, timebuf, 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 send(log_fd, buf, l, 0);
108 void __vsyslog(int priority, const char *message, va_list ap)
111 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
112 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
114 _vsyslog(priority, message, ap);
116 pthread_setcancelstate(cs, 0);
119 void syslog(int priority, const char *message, ...)
122 va_start(ap, message);
123 __vsyslog(priority, message, ap);
127 weak_alias(__vsyslog, vsyslog);