2 #include <sys/socket.h>
14 static char log_ident[32];
16 static int log_facility = LOG_USER;
17 static int log_mask = 0xff;
18 static int log_fd = -1;
20 int setlogmask(int maskpri)
22 if (maskpri) return a_swap(&log_mask, maskpri);
37 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
42 pthread_setcancelstate(cs, 0);
45 static void __openlog()
47 log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
48 if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
51 void openlog(const char *ident, int opt, int facility)
54 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
58 size_t n = strnlen(ident, sizeof log_ident - 1);
59 memcpy(log_ident, ident, n);
65 log_facility = facility;
67 if ((opt & LOG_NDELAY) && log_fd<0) __openlog();
70 pthread_setcancelstate(cs, 0);
73 static void _vsyslog(int priority, const char *message, va_list ap)
84 if (log_fd < 0) return;
87 if (!(priority & LOG_FACMASK)) priority |= log_facility;
91 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
93 pid = (log_opt & LOG_PID) ? getpid() : 0;
94 l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ",
95 priority, timebuf, log_ident, "["+!pid, pid, "]"+!pid);
96 l2 = vsnprintf(buf+l, sizeof buf - l, message, ap);
98 if (l2 >= sizeof buf - l) l = sizeof buf - 1;
100 if (buf[l-1] != '\n') buf[l++] = '\n';
101 send(log_fd, buf, l, 0);
105 void __vsyslog(int priority, const char *message, va_list ap)
108 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
109 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
111 _vsyslog(priority, message, ap);
113 pthread_setcancelstate(cs, 0);
116 void syslog(int priority, const char *message, ...)
119 va_start(ap, message);
120 __vsyslog(priority, message, ap);
124 weak_alias(__vsyslog, vsyslog);