From: David Härdeman Date: Wed, 8 Oct 2025 05:44:56 +0000 (+0200) Subject: odhcpd: support stderr logging X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=e2ecf7ba6d72a9b66d4a3b59fdabef9885d43949;p=project%2Fodhcpd.git odhcpd: support stderr logging This is just for debugging purposes. Also, make sure that a cmdline loglevel doesn't get overwritten by what's in the cfg file (it's customary that command-line options take precedence over configuration file settings). Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/273 Signed-off-by: Álvaro Fernández Rojas --- diff --git a/src/config.c b/src/config.c index 0bcae10..d6573e2 100644 --- a/src/config.c +++ b/src/config.c @@ -41,6 +41,8 @@ struct config config = { .ra_piofolder_fd = -1, .uci_cfgfile = "dhcp", .log_level = LOG_WARNING, + .log_level_cmdline = false, + .log_syslog = true, }; #define START_DEFAULT 100 @@ -423,9 +425,11 @@ static void set_config(struct uci_section *s) if ((c = tb[ODHCPD_ATTR_LOGLEVEL])) { int log_level = (blobmsg_get_u32(c) & LOG_PRIMASK); - if (config.log_level != log_level) { + if (config.log_level != log_level && config.log_level_cmdline) { config.log_level = log_level; - setlogmask(LOG_UPTO(config.log_level)); + if (config.log_syslog) + setlogmask(LOG_UPTO(config.log_level)); + notice("Log level set to %d\n", config.log_level); } } } diff --git a/src/odhcpd.c b/src/odhcpd.c index 6b07f0e..1f3cd1b 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -74,6 +74,7 @@ static void print_usage(const char *app) "\n" " -c Use an alternative configuration file\n" " -l Specify log level 0..7 (default %d)\n" + " -f Log to stderr instead of syslog\n" " -h Print this help text and exit\n", app, config.log_level); } @@ -92,10 +93,9 @@ static bool ipv6_enabled(void) int main(int argc, char **argv) { - openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON); int opt; - while ((opt = getopt(argc, argv, "c:l:h")) != -1) { + while ((opt = getopt(argc, argv, "c:l:fh")) != -1) { switch (opt) { case 'c': config.uci_cfgfile = realpath(optarg, NULL); @@ -103,14 +103,24 @@ int main(int argc, char **argv) break; case 'l': config.log_level = (atoi(optarg) & LOG_PRIMASK); + config.log_level_cmdline = true; fprintf(stderr, "Log level set to %d\n", config.log_level); break; + case 'f': + config.log_syslog = false; + fprintf(stderr, "Logging to stderr\n"); + break; case 'h': print_usage(argv[0]); return 0; } } - setlogmask(LOG_UPTO(config.log_level)); + + if (config.log_syslog) { + openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON); + setlogmask(LOG_UPTO(config.log_level)); + } + uloop_init(); if (getuid() != 0) { diff --git a/src/odhcpd.h b/src/odhcpd.h index 0cda056..9a41a59 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -69,10 +69,14 @@ struct nl_sock; extern struct vlist_tree leases; extern struct config config; -#define __iflog(lvl, fmt, ...) \ - do { \ - if (lvl <= config.log_level) \ - syslog(lvl, fmt __VA_OPT__(, ) __VA_ARGS__); \ +#define __iflog(lvl, fmt, ...) \ + do { \ + if (lvl > config.log_level) \ + break; \ + if (config.log_syslog) \ + syslog(lvl, fmt __VA_OPT__(, ) __VA_ARGS__); \ + else \ + fprintf(stderr, fmt "\n" __VA_OPT__(, ) __VA_ARGS__); \ } while(0) #define debug(fmt, ...) __iflog(LOG_DEBUG, fmt __VA_OPT__(, ) __VA_ARGS__) @@ -194,6 +198,8 @@ struct config { char *uci_cfgfile; int log_level; + bool log_level_cmdline; + bool log_syslog; }; /* 2-byte type + 128-byte DUID, RFC8415, §11.1 */