2 #include <sys/socket.h>
13 static const char *log_ident;
15 static int log_facility = LOG_USER;
16 static int log_mask = 0xff;
19 int setlogmask(int maskpri)
22 if (maskpri) log_mask = maskpri;
37 if (log_f) fclose(log_f);
42 static void __openlog(const char *ident, int opt, int facility)
48 log_facility = facility;
50 if (!(opt & LOG_NDELAY) || log_f) return;
52 fd = socket(AF_UNIX, SOCK_DGRAM, 0);
53 fcntl(fd, F_SETFD, FD_CLOEXEC);
54 if (connect(fd, (void *)&log_addr, sizeof(short) + sizeof "/dev/log") < 0)
56 else log_f = fdopen(fd, "wb");
59 void openlog(const char *ident, int opt, int facility)
62 __openlog(ident, opt, facility);
66 void syslog(int priority, const char *message, ...)
73 //const char *fmt, *ident, *sep;
76 if (!(log_mask & LOG_MASK(priority&7)) || (priority&~0x3ff)) return;
80 if (!log_f) __openlog(log_ident, log_opt | LOG_NDELAY, log_facility);
87 sigaddset(&set, SIGPIPE);
88 pthread_sigmask(SIG_BLOCK, &set, &oldset);
92 strftime(timebuf, sizeof timebuf, "%b %e %T", &tm);
94 fprintf(log_f, "<%d>%s ", priority, timebuf);
95 if (log_ident) fprintf(log_f, "%s", log_ident);
96 if (log_opt & LOG_PID) fprintf(log_f, "[%d]", getpid());
97 if (log_ident) fprintf(log_f, ": ");
99 va_start(ap, message);
100 vfprintf(log_f, message, ap);
105 // Note: LOG_CONS is not supported because it is annoying!!
106 // syslogd will send messages to console if it deems them appropriate!
108 /* Clear any possible SIGPIPE generated by the socket write. */
109 sigtimedwait(&set, 0, (struct timespec [1]){0}) || (perror("x"),1);
110 pthread_sigmask(SIG_SETMASK, &oldset, 0);