From: Roy Marples Date: Tue, 11 Apr 2017 19:18:41 +0000 (+0100) Subject: Over engineer the logging some more. X-Git-Tag: v7.0.0-beta3~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=196992be51f2ca438ecbd247d5b59ec4505e6025;p=thirdparty%2Fdhcpcd.git Over engineer the logging some more. --- diff --git a/src/dhcpcd.c b/src/dhcpcd.c index e9fbcc50..4b504bd7 100644 --- a/src/dhcpcd.c +++ b/src/dhcpcd.c @@ -1461,8 +1461,7 @@ main(int argc, char **argv) #ifdef INET ctx.udp_fd = -1; #endif - /* syslog style logging */ - logopts = LOGERR_LOG|LOGERR_LOG_DATE|LOGERR_LOG_TAG|LOGERR_LOG_PID; + logopts = LOGERR_ERR|LOGERR_LOG|LOGERR_LOG_DATE|LOGERR_LOG_PID; i = 0; while ((opt = getopt_long(argc, argv, ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS, diff --git a/src/logerr.c b/src/logerr.c index 2ae7ce37..73b477a7 100644 --- a/src/logerr.c +++ b/src/logerr.c @@ -42,23 +42,28 @@ #define LOGERR_SYSLOG_FACILITY LOG_DAEMON #endif +#ifdef SMALL +#undef LOGERR_TAG +#endif + #define UNUSED(a) (void)(a) struct logctx { unsigned int log_opts; #ifndef SMALL - const char *log_tag; FILE *log_file; +#ifdef LOGERR_TAG + const char *log_tag; +#endif #endif }; static struct logctx _logctx = { - /* syslog style by default */ - .log_opts = LOGERR_LOG | LOGERR_LOG_DATE | - LOGERR_LOG_TAG | LOGERR_LOG_PID, + /* syslog style, but without the hostname or tag. */ + .log_opts = LOGERR_LOG | LOGERR_LOG_DATE | LOGERR_LOG_PID, }; -#if !defined(SMALL) && defined(__linux__) +#if defined(LOGERR_TAG) && defined(__linux__) /* Poor man's getprogname(3). */ static char *_logprog; static const char * @@ -84,13 +89,42 @@ getprogname(void) } #endif +#ifndef SMALL +/* Write the time, syslog style. month day time - */ +static void +logprintdate(FILE *stream) +{ + struct timeval tv; + time_t now; + struct tm tmnow; + char buf[32]; + + if (gettimeofday(&tv, NULL) == -1) + return; + + now = tv.tv_sec; + tzset(); + localtime_r(&now, &tmnow); + strftime(buf, sizeof(buf), "%b %d %T ", &tmnow); + fprintf(stream, "%s", buf); +} +#endif + __printflike(3, 0) static void vlogprintf_r(struct logctx *ctx, FILE *stream, const char *fmt, va_list args) { va_list a; #ifndef SMALL - bool log_tag, log_pid; + bool log_pid; +#ifdef LOGERR_TAG + bool log_tag; +#endif + + if ((stream == stderr && ctx->log_opts & LOGERR_ERR_DATE) || + (stream != stderr && ctx->log_opts & LOGERR_LOG_DATE)) + logprintdate(stream); +#ifdef LOGERR_TAG log_tag = ((stream == stderr && ctx->log_opts & LOGERR_ERR_TAG) || (stream != stderr && ctx->log_opts & LOGERR_LOG_TAG)); if (log_tag) { @@ -98,13 +132,18 @@ vlogprintf_r(struct logctx *ctx, FILE *stream, const char *fmt, va_list args) ctx->log_tag = getprogname(); fprintf(stream, "%s", ctx->log_tag); } +#endif log_pid = ((stream == stderr && ctx->log_opts & LOGERR_ERR_PID) || (stream != stderr && ctx->log_opts & LOGERR_LOG_PID)); if (log_pid) fprintf(stream, "[%d]", getpid()); +#ifdef LOGERR_TAG if (log_tag || log_pid) +#else + if (log_pid) +#endif fprintf(stream, ": "); #else UNUSED(ctx); @@ -134,14 +173,12 @@ vlogprintf_r(struct logctx *ctx, FILE *stream, const char *fmt, va_list args) __printflike(2, 0) static void vlogmessage(int pri, const char *fmt, va_list args) { -#ifndef SMALL - struct timeval tv; -#endif struct logctx *ctx = &_logctx; - if (pri <= LOG_ERR || + if (ctx->log_opts & LOGERR_ERR && + (pri <= LOG_ERR || (!(ctx->log_opts & LOGERR_QUIET) && pri <= LOG_INFO) || - (ctx->log_opts & LOGERR_DEBUG && pri <= LOG_DEBUG)) + (ctx->log_opts & LOGERR_DEBUG && pri <= LOG_DEBUG))) vlogprintf_r(ctx, stderr, fmt, args); if (!(ctx->log_opts & LOGERR_LOG)) @@ -154,23 +191,8 @@ vlogmessage(int pri, const char *fmt, va_list args) vsyslog(pri, fmt, args); return; } - if (pri == LOG_DEBUG && !(ctx->log_opts & LOGERR_DEBUG)) return; - - /* Write the time, syslog style. month day time - */ - if (ctx->log_opts & LOGERR_LOG_DATE && gettimeofday(&tv, NULL) != -1) { - time_t now; - struct tm tmnow; - char buf[32]; - - now = tv.tv_sec; - tzset(); - localtime_r(&now, &tmnow); - strftime(buf, sizeof(buf), "%b %d %T ", &tmnow); - fprintf(ctx->log_file, "%s", buf); - } - vlogprintf_r(ctx, ctx->log_file, fmt, args); #endif } @@ -267,10 +289,11 @@ logsetopts(unsigned int opts) setlogmask(LOG_UPTO(opts & LOGERR_DEBUG ? LOG_DEBUG : LOG_INFO)); } +#ifdef LOGERR_TAG void logsettag(const char *tag) { -#ifndef SMALL +#if !defined(SMALL) struct logctx *ctx = &_logctx; ctx->log_tag = tag; @@ -278,6 +301,7 @@ logsettag(const char *tag) UNUSED(tag); #endif } +#endif int logopen(const char *path) @@ -294,7 +318,7 @@ logopen(const char *path) } #ifndef SMALL - if ((ctx->log_file = fopen(path, "w")) == NULL) + if ((ctx->log_file = fopen(path, "a")) == NULL) return -1; setlinebuf(ctx->log_file); return fileno(ctx->log_file); @@ -318,7 +342,7 @@ logclose(void) fclose(ctx->log_file); ctx->log_file = NULL; #endif -#if !defined(SMALL) && defined(__linux__) +#if defined(LOGERR_TAG) && defined(__linux__) free(_logprog); #endif } diff --git a/src/logerr.h b/src/logerr.h index b9a8fbc5..ebff3de2 100644 --- a/src/logerr.h +++ b/src/logerr.h @@ -49,15 +49,24 @@ __printflike(1, 2) void logerr(const char *, ...); __printflike(1, 2) void logerrx(const char *, ...); void logsetopts(unsigned int); -#define LOGERR_LOG (1U << 1) -#define LOGERR_LOG_TAG (1U << 2) -#define LOGERR_LOG_PID (1U << 3) -#define LOGERR_LOG_DATE (1U << 4) -#define LOGERR_ERR_TAG (1U << 11) -#define LOGERR_ERR_PID (1U << 12) -#define LOGERR_DEBUG (1U << 21) -#define LOGERR_QUIET (1U << 22) +#define LOGERR_DEBUG (1U << 6) +#define LOGERR_QUIET (1U << 7) +#define LOGERR_LOG (1U << 11) +#define LOGERR_LOG_DATE (1U << 12) +#define LOGERR_LOG_HOST (1U << 13) +#define LOGERR_LOG_TAG (1U << 14) +#define LOGERR_LOG_PID (1U << 15) +#define LOGERR_ERR (1U << 21) +#define LOGERR_ERR_DATE (1U << 22) +#define LOGERR_ERR_HOST (1U << 23) +#define LOGERR_ERR_TAG (1U << 24) +#define LOGERR_ERR_PID (1U << 25) + +/* To build tag support or not. */ +//#define LOGERR_TAG +#if defined(LOGERR_TAG) void logsettag(const char *); +#endif int logopen(const char *); void logclose(void);