From 10c8c889986046e7b28bb2215895f9669d7104a8 Mon Sep 17 00:00:00 2001 From: Marius Tomaschewski Date: Wed, 12 Feb 2014 21:59:01 +0100 Subject: [PATCH] debug: init debug and log level from environment The environment variable WICKED_DEBUG= initialize the debug mask and WICKED_LOG_LEVEL= the level in similar way as the --debug and --log-level option do. For compatibility, the DEBUG variable is used if WICKED_DEBUG is empty and is allowed to contain also the yes/no values. --- autoip4/main.c | 1 + client/main.c | 2 ++ client/suse/scripts/ifup.in | 11 +++-------- dhcp4/main.c | 1 + dhcp6/main.c | 1 + etc/systemd/wicked.service.in | 6 +++--- etc/systemd/wicked@.service.in | 4 ++-- etc/systemd/wickedd-auto4.service.in | 2 +- etc/systemd/wickedd-dhcp4.service.in | 2 +- etc/systemd/wickedd-dhcp6.service.in | 2 +- etc/systemd/wickedd-nanny.service.in | 2 +- etc/systemd/wickedd.service.in | 2 +- etc/systemv/network.in | 14 +++++--------- etc/systemv/wickedd.in | 5 +---- include/wicked/logging.h | 1 + nanny/main.c | 1 + server/main.c | 1 + src/logging.c | 23 +++++++++++++++++++++-- 18 files changed, 48 insertions(+), 33 deletions(-) diff --git a/autoip4/main.c b/autoip4/main.c index ecc51200c..c307fd9ab 100644 --- a/autoip4/main.c +++ b/autoip4/main.c @@ -79,6 +79,7 @@ main(int argc, char **argv) { int c; + ni_log_init(); program_name = ni_basename(argv[0]); while ((c = getopt_long(argc, argv, "+", options, NULL)) != EOF) { diff --git a/client/main.c b/client/main.c index 2d9a12c08..88a246d18 100644 --- a/client/main.c +++ b/client/main.c @@ -111,7 +111,9 @@ main(int argc, char **argv) mtrace(); + ni_log_init(); program_name = ni_basename(argv[0]); + while ((c = getopt_long(argc, argv, "+", options, NULL)) != EOF) { switch (c) { case OPT_HELP: diff --git a/client/suse/scripts/ifup.in b/client/suse/scripts/ifup.in index d1965217e..397bed4e5 100644 --- a/client/suse/scripts/ifup.in +++ b/client/suse/scripts/ifup.in @@ -80,7 +80,7 @@ mesg() debug() { - test "$DEBUG" = "yes" && mesg -p debug "$*" + test "X${DEBUG:-no}" != "Xno" && mesg -p debug "$*" } systemd_running() @@ -141,13 +141,12 @@ case $1 in -o) shift ;; esac OPTIONS=$@ -opt_debug="" opt_quiet="" opt_force="" while [ $# -gt 0 ]; do case $1 in - debug) DEBUG="yes" ; opt_debug="most" ;; - debug=*) DEBUG="yes" ; opt_debug="${1#debug=}" ;; + debug) DEBUG="yes" ;; + debug=*) DEBUG="${1#debug=}" ;; quiet) opt_quiet="--quiet" ;; force) opt_force="--force device-exists" ;; *) debug "unknown option \"$1\"" ;; @@ -231,13 +230,11 @@ wicked_client="@wicked_sbindir@/wicked" case $SCRIPTNAME in ifup) $wicked_client \ - ${opt_debug:+--debug $opt_debug} \ ifup "$INTERFACE" rc=$(rc_map_return "$?") ;; ifdown) $wicked_client \ - ${opt_debug:+--debug $opt_debug} \ ifdown \ ${opt_force} \ "$INTERFACE" @@ -245,7 +242,6 @@ case $SCRIPTNAME in ;; ifstatus) $wicked_client \ - ${opt_debug:+--debug $opt_debug} \ ifstatus \ ${opt_quiet} \ "$INTERFACE" @@ -253,7 +249,6 @@ case $SCRIPTNAME in ;; ifprobe) $wicked_client \ - ${opt_debug:+--debug $opt_debug} \ ifcheck \ --changed \ ${opt_quiet} \ diff --git a/dhcp4/main.c b/dhcp4/main.c index e869812ee..43fac95a8 100644 --- a/dhcp4/main.c +++ b/dhcp4/main.c @@ -108,6 +108,7 @@ main(int argc, char **argv) const char * opt_test_ifname = NULL; int c; + ni_log_init(); program_name = ni_basename(argv[0]); while ((c = getopt_long(argc, argv, "+", options, NULL)) != EOF) { diff --git a/dhcp6/main.c b/dhcp6/main.c index 3023fe901..876c86cf5 100644 --- a/dhcp6/main.c +++ b/dhcp6/main.c @@ -115,6 +115,7 @@ main(int argc, char **argv) const char * opt_test_ifname = NULL; int c; + ni_log_init(); program_name = ni_basename(argv[0]); while ((c = getopt_long(argc, argv, "+", options, NULL)) != EOF) { diff --git a/etc/systemd/wicked.service.in b/etc/systemd/wicked.service.in index 01b14e0e1..9052e9864 100644 --- a/etc/systemd/wicked.service.in +++ b/etc/systemd/wicked.service.in @@ -9,9 +9,9 @@ Type=oneshot RemainAfterExit=yes LimitCORE=infinity EnvironmentFile=-/etc/sysconfig/network/config -ExecStart=@wicked_sbindir@/wicked $WICKED_DEBUG_PARAM --systemd ifup all -ExecStop=@wicked_sbindir@/wicked $WICKED_DEBUG_PARAM --systemd ifdown all -ExecReload=@wicked_sbindir@/wicked $WICKED_DEBUG_PARAM --systemd ifreload all +ExecStart=@wicked_sbindir@/wicked --systemd ifup all +ExecStop=@wicked_sbindir@/wicked --systemd ifdown all +ExecReload=@wicked_sbindir@/wicked --systemd ifreload all [Install] WantedBy=multi-user.target diff --git a/etc/systemd/wicked@.service.in b/etc/systemd/wicked@.service.in index 93a9dd5ae..3a1ace50f 100644 --- a/etc/systemd/wicked@.service.in +++ b/etc/systemd/wicked@.service.in @@ -13,8 +13,8 @@ Type=oneshot RemainAfterExit=yes LimitCORE=infinity EnvironmentFile=-/etc/sysconfig/network/config -ExecStart=@wicked_sbindir@/wicked $WICKED_DEBUG_PARAM ifup --boot-stage %i -- all -ExecStop=@wicked_sbindir@/wicked $WICKED_DEBUG_PARAM ifdown --boot-stage %i -- all +ExecStart=@wicked_sbindir@/wicked ifup --boot-stage %i -- all +ExecStop=@wicked_sbindir@/wicked ifdown --boot-stage %i -- all [Install] Also=wickedd.service diff --git a/etc/systemd/wickedd-auto4.service.in b/etc/systemd/wickedd-auto4.service.in index 58e78baf0..869a37a5a 100644 --- a/etc/systemd/wickedd-auto4.service.in +++ b/etc/systemd/wickedd-auto4.service.in @@ -9,7 +9,7 @@ Type=dbus BusName=org.opensuse.Network.AUTO4 LimitCORE=infinity EnvironmentFile=-/etc/sysconfig/network/config -ExecStart=@wicked_supplicantdir@/wickedd-auto4 $WICKED_DEBUG_PARAM --systemd --foreground +ExecStart=@wicked_supplicantdir@/wickedd-auto4 --systemd --foreground StandardError=null Restart=on-abort diff --git a/etc/systemd/wickedd-dhcp4.service.in b/etc/systemd/wickedd-dhcp4.service.in index 1ee808fb0..951e89575 100644 --- a/etc/systemd/wickedd-dhcp4.service.in +++ b/etc/systemd/wickedd-dhcp4.service.in @@ -9,7 +9,7 @@ Type=dbus BusName=org.opensuse.Network.DHCP4 LimitCORE=infinity EnvironmentFile=-/etc/sysconfig/network/config -ExecStart=@wicked_supplicantdir@/wickedd-dhcp4 $WICKED_DEBUG_PARAM --systemd --foreground +ExecStart=@wicked_supplicantdir@/wickedd-dhcp4 --systemd --foreground StandardError=null Restart=on-abort diff --git a/etc/systemd/wickedd-dhcp6.service.in b/etc/systemd/wickedd-dhcp6.service.in index 82084845d..01252bcbe 100644 --- a/etc/systemd/wickedd-dhcp6.service.in +++ b/etc/systemd/wickedd-dhcp6.service.in @@ -9,7 +9,7 @@ Type=dbus BusName=org.opensuse.Network.DHCP6 LimitCORE=infinity EnvironmentFile=-/etc/sysconfig/network/config -ExecStart=@wicked_supplicantdir@/wickedd-dhcp6 $WICKED_DEBUG_PARAM --systemd --foreground +ExecStart=@wicked_supplicantdir@/wickedd-dhcp6 --systemd --foreground StandardError=null Restart=on-abort diff --git a/etc/systemd/wickedd-nanny.service.in b/etc/systemd/wickedd-nanny.service.in index a9b7b98f5..02c24a27c 100644 --- a/etc/systemd/wickedd-nanny.service.in +++ b/etc/systemd/wickedd-nanny.service.in @@ -9,7 +9,7 @@ Type=dbus BusName=org.opensuse.Network.Nanny LimitCORE=infinity EnvironmentFile=-/etc/sysconfig/network/config -ExecStart=@wicked_sbindir@/wickedd-nanny $WICKED_DEBUG_PARAM --systemd --foreground +ExecStart=@wicked_sbindir@/wickedd-nanny --systemd --foreground StandardError=null Restart=on-abort diff --git a/etc/systemd/wickedd.service.in b/etc/systemd/wickedd.service.in index f67f80d15..2816d9161 100644 --- a/etc/systemd/wickedd.service.in +++ b/etc/systemd/wickedd.service.in @@ -8,7 +8,7 @@ Type=dbus BusName=org.opensuse.Network LimitCORE=infinity EnvironmentFile=-/etc/sysconfig/network/config -ExecStart=@wicked_sbindir@/wickedd $WICKED_DEBUG_PARAM --systemd --foreground +ExecStart=@wicked_sbindir@/wickedd --systemd --foreground StandardError=null Restart=on-abort diff --git a/etc/systemv/network.in b/etc/systemv/network.in index 91fa277f8..91b29d2e4 100644 --- a/etc/systemv/network.in +++ b/etc/systemv/network.in @@ -110,10 +110,6 @@ while [ $# -gt 0 ]; do shift done -if [ "$DEBUG" = "yes" ]; then - WICKED_DEBUG="--debug ${WICKED_DEBUG_LEVEL:-most}" -fi - case $ACTION in start) IFUP_ARGS="" @@ -127,13 +123,13 @@ start) # FIXME: remove it ulimit -c unlimited - $FAKE $WICKED_CLIENT_BIN $WICKED_DEBUG ifup $IFUP_ARGS all + $FAKE $WICKED_CLIENT_BIN ifup $IFUP_ARGS all rc_status -v ;; stop) - $FAKE $WICKED_CLIENT_BIN $WICKED_DEBUG ifdown all + $FAKE $WICKED_CLIENT_BIN ifdown all rc_status -v ;; @@ -160,13 +156,13 @@ force-reload) ;; reload) - $FAKE $WICKED_CLIENT_BIN $WICKED_DEBUG ifreload all + $FAKE $WICKED_CLIENT_BIN ifreload all rc_status -v ;; status) echo "Checking for service network:" - $FAKE $WICKED_CLIENT_BIN $WICKED_DEBUG ifstatus --brief all + $FAKE $WICKED_CLIENT_BIN ifstatus --brief all echo -n "Status of service network " rc_status -v ;; @@ -175,7 +171,7 @@ probe) ## Optional / not part of LSB: Probe for the necessity of a reload, ## print out the argument to this init script which is required for ## a reload (or restart when reload not supported). - $FAKE $WICKED_CLIENT_BIN $WICKED_DEBUG ifcheck --changed --quiet all \ + $FAKE $WICKED_CLIENT_BIN ifcheck --changed --quiet all \ &>/dev/null || echo reload ;; diff --git a/etc/systemv/wickedd.in b/etc/systemv/wickedd.in index 0740851cb..191db6418 100644 --- a/etc/systemv/wickedd.in +++ b/etc/systemv/wickedd.in @@ -36,7 +36,7 @@ wicked_CONFIG=@wicked_configdir@/server.xml wicked_DAEMON_BIN=@wicked_sbindir@/wickedd wicked_PIDFILE=@wicked_piddir@/wicked.pid -# for WICKED_DEBUG_LEVEL only +# for WICKED_DEBUG and WICKED_LOG_LEVEL and DEBUG (sysconfig compat) test -f /etc/sysconfig/network/config && \ . /etc/sysconfig/network/config @@ -115,9 +115,6 @@ esac case $ACTION in start) COMMON_ARGS="" - if [ -n "$WICKED_DEBUG_LEVEL" ]; then - COMMON_ARGS+=" --debug $WICKED_DEBUG_LEVEL" - fi echo -n "Starting" diff --git a/include/wicked/logging.h b/include/wicked/logging.h index 06a3005b4..54fdc514e 100644 --- a/include/wicked/logging.h +++ b/include/wicked/logging.h @@ -31,6 +31,7 @@ extern const char * ni_debug_facility_to_name(unsigned int); extern int ni_debug_name_to_facility(const char *, unsigned int *); extern const char * ni_debug_facility_to_description(unsigned int); +extern void ni_log_init(void); extern ni_bool_t ni_log_level_set(const char *); extern unsigned int ni_log_level_get(void); diff --git a/nanny/main.c b/nanny/main.c index 742811f43..a15ddd735 100644 --- a/nanny/main.c +++ b/nanny/main.c @@ -81,6 +81,7 @@ main(int argc, char **argv) { int c; + ni_log_init(); program_name = ni_basename(argv[0]); while ((c = getopt_long(argc, argv, "+", options, NULL)) != EOF) { diff --git a/server/main.c b/server/main.c index bea36e99d..009829e04 100644 --- a/server/main.c +++ b/server/main.c @@ -86,6 +86,7 @@ main(int argc, char **argv) { int c; + ni_log_init(); program_name = ni_basename(argv[0]); while ((c = getopt_long(argc, argv, "+", options, NULL)) != EOF) { diff --git a/src/logging.c b/src/logging.c index df3127a6d..2eed47682 100644 --- a/src/logging.c +++ b/src/logging.c @@ -27,7 +27,7 @@ #define NI_TRACE_MOST ~(NI_TRACE_XPATH | NI_TRACE_WICKED_XML | NI_TRACE_DBUS) #define NI_TRACE_ALL ~0U -unsigned int ni_debug; +unsigned int ni_debug = 0; unsigned int ni_log_level = NI_LOG_NOTICE; static unsigned int ni_log_syslog; static const char * ni_log_ident; @@ -167,7 +167,7 @@ ni_enable_debug(const char *fac) char *copy, *s; int rv = 0; - copy = xstrdup(fac); + copy = xstrdup(fac ? fac : ""); for (s = strtok(copy, ","); s; s = strtok(NULL, ",")) { unsigned int flags = 0; int not = 0; @@ -208,6 +208,25 @@ ni_debug_help(void) } } +void +ni_log_init(void) +{ + const char *var = getenv("WICKED_DEBUG"); + + if (ni_string_empty(var) && (var = getenv("DEBUG"))) { + if (ni_string_eq(var, "no")) + var = ""; + else + if (ni_string_eq(var, "yes")) + var = "most"; + } + ni_enable_debug(var); + + if ((var = getenv("WICKED_LOG_LEVEL"))) { + ni_log_level_set(var); + } +} + unsigned int ni_log_level_get(void) {