From 2f79aa4f53829aa22a19e9d48a085112017e7af9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 9 Aug 2011 13:01:05 +0200
Subject: [PATCH 3/6] New DEBUG facility - conversion

(conversion of the old debug_level format to the new one)

https://fedorahosted.org/sssd/ticket/925

There are several new macros in util/util.h:

- SSSDBG_* to reflect a debug level (same names as in the ticket) - please,
  don't use magic numbers anymore

- DEBUG_MSG(level, function, message) which will format the debug message
  like "(time) [prg_name] [function] (level): message\n"

- DEBUG_IS_SET(level) that you should use to check if the level is allowed
  to be logged
  You can use it like: if (DEBUG_IS_SET(SSSDBG_TRACE_LIBS)) {...}
---
 src/monitor/monitor.c               |    5 +++--
 src/monitor/monitor_sbus.c          |    1 +
 src/providers/child_common.c        |    2 +-
 src/providers/data_provider.h       |    2 +-
 src/providers/data_provider_be.c    |    2 ++
 src/providers/data_provider_fo.c    |    8 ++++----
 src/providers/krb5/krb5_child.c     |    2 ++
 src/providers/ldap/ldap_child.c     |    2 ++
 src/providers/ldap/sdap_async.c     |    4 ++--
 src/providers/ldap/sdap_fd_events.c |    4 ++--
 src/providers/proxy/proxy_auth.c    |    2 +-
 src/providers/proxy/proxy_child.c   |    1 +
 src/providers/proxy/proxy_id.c      |    2 +-
 src/responder/nss/nsssrv.c          |    2 ++
 src/responder/pam/pamsrv.c          |    2 ++
 src/sbus/sssd_dbus_common.c         |    4 ++--
 src/tests/auth-tests.c              |    2 ++
 src/tests/crypto-tests.c            |    2 +-
 src/tests/fail_over-tests.c         |    2 +-
 src/tests/files-tests.c             |    2 +-
 src/tests/find_uid-tests.c          |    2 +-
 src/tests/krb5_utils-tests.c        |    1 +
 src/tests/refcount-tests.c          |    2 +-
 src/tests/resolv-tests.c            |    2 +-
 src/tests/simple_access-tests.c     |    1 +
 src/tests/strtonum-tests.c          |    2 ++
 src/tests/sysdb-tests.c             |    2 ++
 src/tests/util-tests.c              |    2 ++
 src/tools/selinux.c                 |   14 +-------------
 src/tools/sss_cache.c               |    2 +-
 src/tools/sss_groupadd.c            |    2 +-
 src/tools/sss_groupdel.c            |    2 +-
 src/tools/sss_groupmod.c            |    2 +-
 src/tools/sss_groupshow.c           |    2 +-
 src/tools/sss_useradd.c             |    2 +-
 src/tools/sss_userdel.c             |    2 +-
 src/tools/sss_usermod.c             |    2 +-
 src/util/debug.c                    |   27 ++++++++-------------------
 src/util/server.c                   |    2 ++
 39 files changed, 64 insertions(+), 62 deletions(-)

diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index 8326f38..ae429f8 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -956,7 +956,7 @@ static int get_service_config(struct mt_ctx *ctx, const char *name,
              * make sure to pass it into the children, overriding the
              * config file.
              */
-            svc->command = talloc_asprintf(svc, "%s/sssd_%s -d %d%s%s",
+            svc->command = talloc_asprintf(svc, "%s/sssd_%s -d 0x%.4x%s%s",
                                            SSSD_LIBEXEC_PATH,
                                            svc->name,
                                            cmdline_debug_level,
@@ -1099,7 +1099,7 @@ static int get_provider_config(struct mt_ctx *ctx, const char *name,
                                 debug_to_file ? " --debug-to-files" : "");
         } else {
             svc->command = talloc_asprintf(svc,
-                                "%s/sssd_be --domain %s -d %d%s%s ",
+                                "%s/sssd_be --domain %s -d 0x%.4x%s%s ",
                                 SSSD_LIBEXEC_PATH,
                                 svc->name,
                                 cmdline_debug_level,
@@ -2331,6 +2331,7 @@ int main(int argc, const char *argv[])
             return 1;
         }
     }
+    debug_level = debug_convert_old_level(debug_level);
 
     /* If the level was passed at the command-line, we want
      * to save it and pass it to the children later.
diff --git a/src/monitor/monitor_sbus.c b/src/monitor/monitor_sbus.c
index 632de49..9e20671 100644
--- a/src/monitor/monitor_sbus.c
+++ b/src/monitor/monitor_sbus.c
@@ -205,6 +205,7 @@ errno_t monitor_common_rotate_logs(struct confdb_ctx *confdb,
 
     if (debug_level != old_debug_level) {
         DEBUG(0, ("Debug level changed to %d\n", debug_level));
+        debug_level = debug_convert_old_level(debug_level);
     }
 
     return EOK;
diff --git a/src/providers/child_common.c b/src/providers/child_common.c
index 16618c7..74cfee6 100644
--- a/src/providers/child_common.c
+++ b/src/providers/child_common.c
@@ -400,7 +400,7 @@ static errno_t prepare_child_argv(TALLOC_CTX *mem_ctx,
 
     argv[--argc] = NULL;
 
-    argv[--argc] = talloc_asprintf(argv, "--debug-level=%d",
+    argv[--argc] = talloc_asprintf(argv, "--debug-level=0x%.4x",
                               debug_level);
     if (argv[argc] == NULL) {
         ret = ENOMEM;
diff --git a/src/providers/data_provider.h b/src/providers/data_provider.h
index e699e08..e75cd22 100644
--- a/src/providers/data_provider.h
+++ b/src/providers/data_provider.h
@@ -140,7 +140,7 @@
 /* AUTH related common data and functions */
 
 #define DEBUG_PAM_DATA(level, pd) do { \
-    if (level <= debug_level) pam_print_data(level, pd); \
+    if (DEBUG_IS_SET(level)) pam_print_data(level, pd); \
 } while(0);
 
 
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index 380e6cd..7df82f4 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -1216,6 +1216,8 @@ int main(int argc, const char *argv[])
 
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
 
     /* set up things like debug , signals, daemonization, etc... */
     debug_log_file = talloc_asprintf(NULL, "sssd_%s", be_domain);
diff --git a/src/providers/data_provider_fo.c b/src/providers/data_provider_fo.c
index b5c4173..0b991a9 100644
--- a/src/providers/data_provider_fo.c
+++ b/src/providers/data_provider_fo.c
@@ -432,7 +432,7 @@ static void be_resolve_server_done(struct tevent_req *subreq)
 
     /* all fine we got the server */
 
-    if (debug_level >= 4 && fo_get_server_name(state->srv)) {
+    if (DEBUG_IS_SET(SSSDBG_CONF_SETTINGS) && fo_get_server_name(state->srv)) {
         struct resolv_hostent *srvaddr;
         char ipaddr[128];
         srvaddr = fo_get_server_hostent(state->srv);
@@ -446,9 +446,9 @@ static void be_resolve_server_done(struct tevent_req *subreq)
         inet_ntop(srvaddr->family, srvaddr->addr_list[0]->ipaddr,
                   ipaddr, 128);
 
-        DEBUG(4, ("Found address for server %s: [%s] TTL %d\n",
-                  fo_get_server_str_name(state->srv), ipaddr,
-                  srvaddr->addr_list[0]->ttl));
+        DEBUG(SSSDBG_CONF_SETTINGS, ("Found address for server %s: [%s] TTL %d\n",
+                fo_get_server_str_name(state->srv), ipaddr,
+                srvaddr->addr_list[0]->ttl));
     }
 
     srv_status_change = fo_get_server_hostname_last_change(state->srv);
diff --git a/src/providers/krb5/krb5_child.c b/src/providers/krb5/krb5_child.c
index ffc6c79..48442de 100644
--- a/src/providers/krb5/krb5_child.c
+++ b/src/providers/krb5/krb5_child.c
@@ -1573,6 +1573,8 @@ int main(int argc, const char *argv[])
 
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
     DEBUG(7, ("krb5_child started.\n"));
 
     pd = talloc_zero(NULL, struct pam_data);
diff --git a/src/providers/ldap/ldap_child.c b/src/providers/ldap/ldap_child.c
index fb8dd80..8a1dc49 100644
--- a/src/providers/ldap/ldap_child.c
+++ b/src/providers/ldap/ldap_child.c
@@ -378,6 +378,8 @@ int main(int argc, const char *argv[])
 
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
     DEBUG(7, ("ldap_child started.\n"));
 
     main_ctx = talloc_new(NULL);
diff --git a/src/providers/ldap/sdap_async.c b/src/providers/ldap/sdap_async.c
index 5594dd5..1fa4fa3 100644
--- a/src/providers/ldap/sdap_async.c
+++ b/src/providers/ldap/sdap_async.c
@@ -981,12 +981,12 @@ static errno_t sdap_get_generic_ext_step(struct tevent_req *req)
 
     DEBUG(6, ("calling ldap_search_ext with [%s][%s].\n", state->filter,
                                                           state->search_base));
-    if (debug_level >= 7) {
+    if (DEBUG_IS_SET(SSSDBG_TRACE_LIBS)) {
         int i;
 
         if (state->attrs) {
             for (i = 0; state->attrs[i]; i++) {
-                DEBUG(7, ("Requesting attrs: [%s]\n", state->attrs[i]));
+                DEBUG(SSSDBG_TRACE_LIBS, ("Requesting attrs: [%s]\n", state->attrs[i]));
             }
         }
     }
diff --git a/src/providers/ldap/sdap_fd_events.c b/src/providers/ldap/sdap_fd_events.c
index 4fb67b8..0a51622 100644
--- a/src/providers/ldap/sdap_fd_events.c
+++ b/src/providers/ldap/sdap_fd_events.c
@@ -104,9 +104,9 @@ static int sdap_ldap_connect_callback_add(LDAP *ld, Sockbuf *sb,
         return EINVAL;
     }
 
-    if (debug_level >= 7) {
+    if (DEBUG_IS_SET(SSSDBG_TRACE_LIBS)) {
         char *uri = ldap_url_desc2str(srv);
-        DEBUG(7, ("New LDAP connection to [%s] with fd [%d].\n",
+        DEBUG(SSSDBG_TRACE_LIBS, ("New LDAP connection to [%s] with fd [%d].\n",
                   uri, ber_fd));
         free(uri);
     }
diff --git a/src/providers/proxy/proxy_auth.c b/src/providers/proxy/proxy_auth.c
index b3b878c..25e781b 100644
--- a/src/providers/proxy/proxy_auth.c
+++ b/src/providers/proxy/proxy_auth.c
@@ -240,7 +240,7 @@ static struct tevent_req *proxy_child_init_send(TALLOC_CTX *mem_ctx,
     state->child_ctx = child_ctx;
 
     state->command = talloc_asprintf(req,
-            "%s/proxy_child -d %d%s%s --domain %s --id %d",
+            "%s/proxy_child -d 0x%.4x%s%s --domain %s --id %d",
             SSSD_LIBEXEC_PATH, debug_level,
             (debug_timestamps ? "" : " --debug-timestamps=0"),
             (debug_to_file ? " --debug-to-files" : ""),
diff --git a/src/providers/proxy/proxy_child.c b/src/providers/proxy/proxy_child.c
index 0f58849..d19cc74 100644
--- a/src/providers/proxy/proxy_child.c
+++ b/src/providers/proxy/proxy_child.c
@@ -507,6 +507,7 @@ int main(int argc, const char *argv[])
 
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
 
     /* set up things like debug , signals, daemonization, etc... */
     debug_log_file = talloc_asprintf(NULL, "proxy_child_%s", domain);
diff --git a/src/providers/proxy/proxy_id.c b/src/providers/proxy/proxy_id.c
index 5ce9095..ec13c93 100644
--- a/src/providers/proxy/proxy_id.c
+++ b/src/providers/proxy/proxy_id.c
@@ -395,7 +395,7 @@ done:
 
 #define DEBUG_GR_MEM(level, grp) \
     do { \
-        if (debug_level >= level) { \
+        if (DEBUG_IS_SET(level)) { \
             if (!grp->gr_mem || !grp->gr_mem[0]) { \
                 DEBUG(level, ("Group %s has no members!\n", \
                               grp->gr_name)); \
diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c
index 8fe53a8..7f60b73 100644
--- a/src/responder/nss/nsssrv.c
+++ b/src/responder/nss/nsssrv.c
@@ -353,6 +353,8 @@ int main(int argc, const char *argv[])
 
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
    /* set up things like debug, signals, daemonization, etc... */
     debug_log_file = "sssd_nss";
 
diff --git a/src/responder/pam/pamsrv.c b/src/responder/pam/pamsrv.c
index 86283ea..2308288 100644
--- a/src/responder/pam/pamsrv.c
+++ b/src/responder/pam/pamsrv.c
@@ -209,6 +209,8 @@ int main(int argc, const char *argv[])
 
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
     /* set up things like debug, signals, daemonization, etc... */
     debug_log_file = "sssd_pam";
 
diff --git a/src/sbus/sssd_dbus_common.c b/src/sbus/sssd_dbus_common.c
index d446632..502bd29 100644
--- a/src/sbus/sssd_dbus_common.c
+++ b/src/sbus/sssd_dbus_common.c
@@ -226,14 +226,14 @@ void sbus_toggle_watch(DBusWatch *dbus_watch, void *data)
         }
     }
 
-    if (debug_level >= 8) {
+    if (DEBUG_IS_SET(SSSDBG_TRACE_INTERNAL)) {
 #ifdef HAVE_DBUS_WATCH_GET_UNIX_FD
         fd = dbus_watch_get_unix_fd(dbus_watch);
 #else
         fd = dbus_watch_get_fd(dbus_watch);
 #endif
     }
-    DEBUG(8, ("%p/%p (%d), %s/%s (%s)\n",
+    DEBUG(SSSDBG_TRACE_INTERNAL, ("%p/%p (%d), %s/%s (%s)\n",
               watch, dbus_watch, fd,
               ((flags & DBUS_WATCH_READABLE)?"R":"-"),
               ((flags & DBUS_WATCH_WRITABLE)?"W":"-"),
diff --git a/src/tests/auth-tests.c b/src/tests/auth-tests.c
index abdc874..54d5b29 100644
--- a/src/tests/auth-tests.c
+++ b/src/tests/auth-tests.c
@@ -320,6 +320,8 @@ int main(int argc, const char *argv[])
     }
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
     tests_set_cwd();
 
     ret = clean_db_dir();
diff --git a/src/tests/crypto-tests.c b/src/tests/crypto-tests.c
index 286bc23..0f6f2fe 100644
--- a/src/tests/crypto-tests.c
+++ b/src/tests/crypto-tests.c
@@ -135,7 +135,7 @@ int main(int argc, const char *argv[])
     }
     poptFreeContext(pc);
 
-    debug_level = debug;
+    debug_level = debug_convert_old_level(debug);
     tests_set_cwd();
 
     Suite *s = crypto_suite();
diff --git a/src/tests/fail_over-tests.c b/src/tests/fail_over-tests.c
index 8beaa8a..439e5cf 100644
--- a/src/tests/fail_over-tests.c
+++ b/src/tests/fail_over-tests.c
@@ -307,7 +307,7 @@ main(int argc, const char *argv[])
         }
     }
     poptFreeContext(pc);
-    debug_level = debug;
+    debug_level = debug_convert_old_level(debug);
 
     tests_set_cwd();
 
diff --git a/src/tests/files-tests.c b/src/tests/files-tests.c
index 7f6b41e..15d34c8 100644
--- a/src/tests/files-tests.c
+++ b/src/tests/files-tests.c
@@ -318,7 +318,7 @@ int main(int argc, const char *argv[])
         return 1;
     }
     poptFreeContext(pc);
-    debug_level = debug;
+    debug_level = debug_convert_old_level(debug);
 
     tests_set_cwd();
 
diff --git a/src/tests/find_uid-tests.c b/src/tests/find_uid-tests.c
index 8d1362f..a8f7405 100644
--- a/src/tests/find_uid-tests.c
+++ b/src/tests/find_uid-tests.c
@@ -114,7 +114,7 @@ Suite *find_uid_suite (void)
 
 int main(void)
 {
-    debug_level = 255;
+    debug_level = SSSDBG_MASK_ALL;
     int number_failed;
 
     tests_set_cwd();
diff --git a/src/tests/krb5_utils-tests.c b/src/tests/krb5_utils-tests.c
index d5d1f09..65e4319 100644
--- a/src/tests/krb5_utils-tests.c
+++ b/src/tests/krb5_utils-tests.c
@@ -654,6 +654,7 @@ int main(int argc, const char *argv[])
     }
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
 
     ret = mkdir(TESTS_PATH, 0775);
     if (ret != EOK) {
diff --git a/src/tests/refcount-tests.c b/src/tests/refcount-tests.c
index 5e85b6f..99eedd9 100644
--- a/src/tests/refcount-tests.c
+++ b/src/tests/refcount-tests.c
@@ -218,7 +218,7 @@ int main(int argc, const char *argv[])
         }
     }
     poptFreeContext(pc);
-    debug_level = debug;
+    debug_level = debug_convert_old_level(debug);
 
     tests_set_cwd();
 
diff --git a/src/tests/resolv-tests.c b/src/tests/resolv-tests.c
index 5c1a487..3768fc9 100644
--- a/src/tests/resolv-tests.c
+++ b/src/tests/resolv-tests.c
@@ -790,7 +790,7 @@ int main(int argc, const char *argv[])
         }
     }
     poptFreeContext(pc);
-    debug_level = debug;
+    debug_level = debug_convert_old_level(debug);
 
     if (!use_net_test) {
         printf("Network tests disabled. Rerun with the \"-n\" "
diff --git a/src/tests/simple_access-tests.c b/src/tests/simple_access-tests.c
index fbbc836..6ae2356 100644
--- a/src/tests/simple_access-tests.c
+++ b/src/tests/simple_access-tests.c
@@ -162,6 +162,7 @@ int main(int argc, const char *argv[])
     }
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
 
     Suite *s = access_simple_suite();
     SRunner *sr = srunner_create(s);
diff --git a/src/tests/strtonum-tests.c b/src/tests/strtonum-tests.c
index 2b91d48..8e86c11 100644
--- a/src/tests/strtonum-tests.c
+++ b/src/tests/strtonum-tests.c
@@ -446,6 +446,8 @@ int main(int argc, const char *argv[]) {
     }
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
     tests_set_cwd();
 
     strtonum_suite = create_strtonum_suite();
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index d0f7ab4..274c7fa 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -3173,6 +3173,8 @@ int main(int argc, const char *argv[]) {
     }
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
     tests_set_cwd();
 
     ret = unlink(TESTS_PATH"/"LOCAL_SYSDB_FILE);
diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c
index e6d099b..b347aa7 100644
--- a/src/tests/util-tests.c
+++ b/src/tests/util-tests.c
@@ -355,6 +355,8 @@ int main(int argc, const char *argv[])
     }
     poptFreeContext(pc);
 
+    debug_level = debug_convert_old_level(debug_level);
+
     tests_set_cwd();
 
     srunner_run_all(sr, CK_ENV);
diff --git a/src/tools/selinux.c b/src/tools/selinux.c
index 45672d1..8eb19a2 100644
--- a/src/tools/selinux.c
+++ b/src/tools/selinux.c
@@ -121,19 +121,7 @@ static void sss_semanage_error_callback(void *varg,
         return;
     }
 
-    if (level <= debug_level) {
-        if (debug_timestamps) {
-            time_t rightnow = time(NULL);
-            char stamp[25];
-            memcpy(stamp, ctime(&rightnow), 24);
-            stamp[24] = '\0';
-            debug_fn("(%s) [%s] [libsemanage] (%d): %s\n",
-                     stamp, debug_prg_name, level, message);
-        } else {
-            debug_fn("[%s] [libsemanage] (%d): %s\n",
-                     debug_prg_name, level, message);
-        }
-    }
+    DEBUG_MSG(level, "libsemanage", message);
     free(message);
 }
 
diff --git a/src/tools/sss_cache.c b/src/tools/sss_cache.c
index 0b08652..b0771b9 100644
--- a/src/tools/sss_cache.c
+++ b/src/tools/sss_cache.c
@@ -300,7 +300,7 @@ errno_t init_context(int argc, const char *argv[], struct cache_tool_ctx **tctx)
         BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
-    debug_level = debug;
+    debug_level = debug_convert_old_level(debug);
     debug_prg_name = argv[0];
     CHECK_ROOT(ret, debug_prg_name);
 
diff --git a/src/tools/sss_groupadd.c b/src/tools/sss_groupadd.c
index 8817625..90b1d08 100644
--- a/src/tools/sss_groupadd.c
+++ b/src/tools/sss_groupadd.c
@@ -65,7 +65,7 @@ int main(int argc, const char **argv)
         BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
-    debug_level = pc_debug;
+    debug_level = debug_convert_old_level(pc_debug);
 
     /* groupname is an argument, not option */
     pc_groupname = poptGetArg(pc);
diff --git a/src/tools/sss_groupdel.c b/src/tools/sss_groupdel.c
index 99b6908..b3e741e 100644
--- a/src/tools/sss_groupdel.c
+++ b/src/tools/sss_groupdel.c
@@ -61,7 +61,7 @@ int main(int argc, const char **argv)
         BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
-    debug_level = pc_debug;
+    debug_level = debug_convert_old_level(pc_debug);
 
     pc_groupname = poptGetArg(pc);
     if (pc_groupname == NULL) {
diff --git a/src/tools/sss_groupmod.c b/src/tools/sss_groupmod.c
index 59ec10b..96189ab 100644
--- a/src/tools/sss_groupmod.c
+++ b/src/tools/sss_groupmod.c
@@ -98,7 +98,7 @@ int main(int argc, const char **argv)
         BAD_POPT_PARAMS(pc, _("Specify group to modify\n"), ret, fini);
     }
 
-    debug_level = pc_debug;
+    debug_level = debug_convert_old_level(pc_debug);
 
     CHECK_ROOT(ret, debug_prg_name);
 
diff --git a/src/tools/sss_groupshow.c b/src/tools/sss_groupshow.c
index 1532485..69063fc 100644
--- a/src/tools/sss_groupshow.c
+++ b/src/tools/sss_groupshow.c
@@ -644,7 +644,7 @@ int main(int argc, const char **argv)
         }
     }
 
-    debug_level = pc_debug;
+    debug_level = debug_convert_old_level(pc_debug);
 
     if (ret != -1) {
         BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
diff --git a/src/tools/sss_useradd.c b/src/tools/sss_useradd.c
index edff6e1..0d3a34d 100644
--- a/src/tools/sss_useradd.c
+++ b/src/tools/sss_useradd.c
@@ -96,7 +96,7 @@ int main(int argc, const char **argv)
         }
     }
 
-    debug_level = pc_debug;
+    debug_level = debug_convert_old_level(pc_debug);
 
     if (ret != -1) {
         BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
diff --git a/src/tools/sss_userdel.c b/src/tools/sss_userdel.c
index 1d0cb12..29ecfba 100644
--- a/src/tools/sss_userdel.c
+++ b/src/tools/sss_userdel.c
@@ -173,7 +173,7 @@ int main(int argc, const char **argv)
         }
     }
 
-    debug_level = pc_debug;
+    debug_level = debug_convert_old_level(pc_debug);
 
     if (ret != -1) {
         BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
diff --git a/src/tools/sss_usermod.c b/src/tools/sss_usermod.c
index bcf2b3a..d2b6782 100644
--- a/src/tools/sss_usermod.c
+++ b/src/tools/sss_usermod.c
@@ -109,7 +109,7 @@ int main(int argc, const char **argv)
         BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
-    debug_level = pc_debug;
+    debug_level = debug_convert_old_level(pc_debug);
 
     /* username is an argument without --option */
     pc_username = poptGetArg(pc);
diff --git a/src/util/debug.c b/src/util/debug.c
index caafbf9..22efd7c 100644
--- a/src/util/debug.c
+++ b/src/util/debug.c
@@ -33,7 +33,7 @@
 
 const char *debug_prg_name = "sssd";
 
-int debug_level = SSS_UNRESOLVED_DEBUG_LEVEL;
+int debug_level = SSSDBG_UNRESOLVED;
 int debug_timestamps = SSSDBG_TIMESTAMP_UNRESOLVED;
 
 int debug_to_file = 0;
@@ -140,22 +140,22 @@ int debug_get_level(int old_level)
 void ldb_debug_messages(void *context, enum ldb_debug_level level,
                         const char *fmt, va_list ap)
 {
-    int loglevel = -1;
+    int loglevel = SSSDBG_UNRESOLVED;
     int ret;
     char * message = NULL;
 
     switch(level) {
     case LDB_DEBUG_FATAL:
-        loglevel = 0;
+        loglevel = SSSDBG_FATAL_FAILURE;
         break;
     case LDB_DEBUG_ERROR:
-        loglevel = 1;
+        loglevel = SSSDBG_CRIT_FAILURE;
         break;
     case LDB_DEBUG_WARNING:
-        loglevel = 6;
+        loglevel = SSSDBG_TRACE_FUNC;
         break;
     case LDB_DEBUG_TRACE:
-        loglevel = 9;
+        loglevel = SSSDBG_TRACE_ALL;
         break;
     }
 
@@ -165,19 +165,8 @@ void ldb_debug_messages(void *context, enum ldb_debug_level level,
         return;
     }
 
-    if (loglevel <= debug_level) {
-        if (debug_timestamps) {
-            time_t rightnow = time(NULL);
-            char stamp[25];
-            memcpy(stamp, ctime(&rightnow), 24);
-            stamp[24] = '\0';
-            debug_fn("(%s) [%s] [ldb] (%d): %s\n",
-                     stamp, debug_prg_name, loglevel, message);
-        } else {
-            debug_fn("[%s] [ldb] (%d): %s\n",
-                     debug_prg_name, loglevel, message);
-        }
-    }
+    DEBUG_MSG(loglevel, "ldb", message);
+
     free(message);
 }
 
diff --git a/src/util/server.c b/src/util/server.c
index 7d16d04..1d48d84 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -470,6 +470,8 @@ int server_setup(const char *name, int flags,
                       ret, strerror(ret)));
             return ret;
         }
+
+        debug_level = debug_convert_old_level(debug_level);
     }
 
     /* same for debug timestamps */
-- 
1.7.6

