#include <ac/stdarg.h>
#include "slap.h"
+#include "lutil.h"
static int LogType = SOCK_DGRAM; /* type of socket connection */
static int LogFile = -1; /* fd for log */
static int LogStat; /* status bits, set by openlog() */
static const char *LogTag; /* string to tag the entry with */
static int LogFacility = LOG_USER; /* default facility code */
-static int LogMask = 0xff; /* mask of priorities to be logged */
static void disconnectlog(void);
static void connectlog(void);
static void my_localtime(const time_t *t, struct tm *tm);
+static struct berval LogPfx;
+
/*
* syslog
* print message on log file; output is intended for syslogd(8).
syslog(int pri, const char *fmt, ...)
{
va_list ap;
- char *p, *t;
+ char *p, *pend;
#define TBUF_LEN 2048
#define FMT_LEN 1024
- char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
- time_t now;
+ char tbuf[TBUF_LEN];
int cnt;
- int fd, saved_errno, error;
- char ch;
- int tbuf_left, fmt_left, prlen;
- struct tm tm;
+ int tbuf_left, prlen;
va_start(ap, fmt);
+#if 0
/* Check for invalid bits. */
if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
if (LogTest(LOG_ERR))
pri &= LOG_PRIMASK|LOG_FACMASK;
}
- saved_errno = errno;
-
/* Set default facility if none specified. */
if ((pri & LOG_FACMASK) == 0)
pri |= LogFacility;
- (void)time(&now);
-
p = tbuf;
- tbuf_left = TBUF_LEN;
+ pend = p + TBUF_LEN;
-#define DEC() \
- do { \
- if (prlen < 0) \
- prlen = 0; \
- if (prlen >= tbuf_left) \
- prlen = tbuf_left - 1; \
- p += prlen; \
- tbuf_left -= prlen; \
- } while (0)
-
- prlen = snprintf(p, tbuf_left, "<%d>", pri);
- DEC();
+ *p++ = '<';
+ p += sprintf(p, "%d", pri);
+ *p++ = '>';
+#if 0
+ (void)time(&now);
my_localtime(&now, &tm);
- prlen = strftime(p, tbuf_left, "%h %e %T ", &tm);
- DEC();
+ p += strftime(p, tbuf_left, "%h %e %T ", &tm);
+#endif
if (LogTag != NULL) {
- prlen = snprintf(p, tbuf_left, "%s", LogTag);
- DEC();
+ p = lutil_strcopy(p, LogTag);
}
if (LogStat & LOG_PID) {
- prlen = snprintf(p, tbuf_left, "[%ld]", (long)getpid());
- DEC();
+ *p++ = '[';
+ p += sprintf(p, "%ld", (long)getpid());
+ *p++ = ']';
}
if (LogTag != NULL) {
- if (tbuf_left > 1) {
- *p++ = ':';
- tbuf_left--;
- }
- if (tbuf_left > 1) {
- *p++ = ' ';
- tbuf_left--;
- }
- }
-
- /* strerror() is not reentrant */
-
- for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) {
- if (ch == '%' && fmt[1] == 'm') {
- ++fmt;
- prlen = snprintf(t, fmt_left, "%s",
- strerror(saved_errno));
- if (prlen < 0)
- prlen = 0;
- if (prlen >= fmt_left)
- prlen = fmt_left - 1;
- t += prlen;
- fmt_left -= prlen;
- } else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
- *t++ = '%';
- *t++ = '%';
- fmt++;
- fmt_left -= 2;
- } else {
- if (fmt_left > 1) {
- *t++ = ch;
- fmt_left--;
- }
- }
+ *p++ = ':';
+ *p++ = ' ';
}
- *t = '\0';
+#endif
+ pend = tbuf + sizeof(tbuf);
+ p = preplog(tbuf);
- prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
- DEC();
- cnt = p - tbuf;
+ tbuf_left = pend - p;
+ prlen = vsnprintf(p, tbuf_left, fmt, ap);
va_end(ap);
+ if (prlen < 0)
+ prlen = 0;
+ else if (prlen >= tbuf_left)
+ prlen = tbuf_left - 1;
+ p += prlen;
+ cnt = p - tbuf;
+
+ sendlog(tbuf, cnt);
+}
+
+/* Copy the syslog header into tbuf and return a pointer
+ * to end of header, where caller can begin writing the
+ * actual message.
+ */
+char *
+preplog(char *tbuf)
+{
+ memcpy(tbuf, LogPfx.bv_val, LogPfx.bv_len);
+ return tbuf + LogPfx.bv_len;
+}
+
+void
+sendlog(char *tbuf, int cnt)
+{
+ int error;
/* Get connected, output the message to the local logger. */
if (LogFile == -1)
openlog(LogTag, LogStat, 0);
connectlog();
+#if 1
/*
* If the send() failed, there are two likely scenarios:
* 1) syslogd was restarted
break;
} while (errno == ENOBUFS);
}
+#endif
}
static void
struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
if (LogFile == -1) {
- if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
+ if ((LogFile = socket(AF_UNIX, LogType, 0)) == -1)
return;
(void)fcntl(LogFile, F_SETFD, FD_CLOEXEC);
}
void
openlog(const char *ident, int logstat, int logfac)
{
+ char buf[512], *p = buf;
+
+
if (ident != NULL)
LogTag = ident;
LogStat = logstat;
if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
LogFacility = logfac;
+ logfac |= LOG_DEBUG; /* we currently hardcode severity */
+
+ /* Our facility, pid, and ident never change so
+ * just build the message header now. Avoiding
+ * sprintf() and multiple calls to getpid()
+ * saves a lot of time.
+ */
+ *p++ = '<';
+ p += sprintf(p, "%d", logfac);
+ *p++ = '>';
+ /* timestamp goes here but rsyslog ignores it, so skip it */
+ p = lutil_strcopy(p, ident);
+ *p++ = '[';
+ p += sprintf(p, "%ld", (long)getpid());
+ *p++ = ']';
+ *p++ = ':';
+ *p++ = ' ';
+ LogPfx.bv_len = p - buf;
+ LogPfx.bv_val = ch_malloc(LogPfx.bv_len);
+ memcpy(LogPfx.bv_val, buf, LogPfx.bv_len);
+
if (LogStat & LOG_NDELAY) /* open immediately */
connectlog();
}
LogFile = -1;
connected = 0;
LogTag = NULL;
+ ch_free(LogPfx.bv_val);
}
+#if 0
#define SECS_PER_HOUR (60 * 60)
#define SECS_PER_DAY (SECS_PER_HOUR * 24)
tm->tm_mon = y;
tm->tm_mday = days + 1;
}
+#endif