X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=klibc%2Fklibc%2Fsyslog.c;h=0cd296ab8580753e89e19c8e754c178f3cfad70a;hb=6b493a20e1dbf90a4e54d3be37027fa906220c31;hp=b031d4f0e7946ac45356220445ca9df7df3f7153;hpb=a41a0e28c2ba0abf99b5e7ea17645ae0e4f05758;p=elogind.git diff --git a/klibc/klibc/syslog.c b/klibc/klibc/syslog.c index b031d4f0e..0cd296ab8 100644 --- a/klibc/klibc/syslog.c +++ b/klibc/klibc/syslog.c @@ -18,10 +18,11 @@ #define LOGDEV "/dev/kmsg" /* Max length of ID string */ -#define MAXID 31 +#define MAXID 31 /* MAXID+5 must be < BUFLEN */ int __syslog_fd = -1; static char id[MAXID+1]; +static int syslog_flags = 0; void openlog(const char *ident, int option, int facility) { @@ -36,39 +37,51 @@ void openlog(const char *ident, int option, int facility) fcntl(fd, F_SETFD, (long)FD_CLOEXEC); } + syslog_flags = option; + strncpy(id, ident?ident:"", MAXID); - id[MAXID] = '\0'; /* Make sure it's null-terminated */ } -void syslog(int prio, const char *format, ...) +void vsyslog(int prio, const char *format, va_list ap) { - va_list ap; char buf[BUFLEN]; - int rv, len; + int len; int fd; if ( __syslog_fd == -1 ) openlog(NULL, 0, 0); - fd = __syslog_fd; - if ( fd == -1 ) - fd = 2; /* Failed to open log, write to stderr */ - buf[0] = '<'; buf[1] = LOG_PRI(prio)+'0'; buf[2] = '>'; len = 3; - if ( *id ) + if ( syslog_flags & LOG_PID ) + len += sprintf(buf+3, "%s[%u]: ", id, getpid()); + else if ( *id ) len += sprintf(buf+3, "%s: ", id); - - va_start(ap, format); - rv = vsnprintf(buf+len, BUFLEN-len, format, ap); - va_end(ap); - len += rv; + len += vsnprintf(buf+len, BUFLEN-len, format, ap); + if ( len > BUFLEN-1 ) len = BUFLEN-1; - buf[len] = '\n'; + if (buf[len-1] != '\n') + buf[len++] = '\n'; + + fd = __syslog_fd; + if ( fd == -1 ) + fd = 2; /* Failed to open log, write to stderr */ + + write(fd, buf, len); + + if ( syslog_flags & LOG_PERROR ) + _fwrite(buf+3, len-3, stderr); +} - write(fd, buf, len+1); +void syslog(int prio, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + vsyslog(prio, format, ap); + va_end(ap); }