Log all debug messages through log callback.

This cahnge allow to redirect all output of library
to a log processor.
diff --git a/lib/setup.c b/lib/setup.c
index abc2087..d799148 100644
--- a/lib/setup.c
+++ b/lib/setup.c
@@ -141,12 +141,17 @@
 {
 	if (!msg)
 		return;
+
+	if (level < _debug_level)
+		return;
+
 	if (cd && cd->log)
 		cd->log(level, msg, cd->log_usrptr);
 	else if (_default_log)
 		_default_log(level, msg, NULL);
-	else if (_debug_level)
-		printf("%s", msg);
+	/* Default to stdout/stderr if there is no callback. */
+	else
+		fprintf(level == CRYPT_LOG_ERROR ? stderr : stdout, "%s", msg);
 }
 
 __attribute__((format(printf, 5, 6)))
@@ -159,19 +164,11 @@
 	va_start(argp, format);
 
 	if (vsnprintf(&target[0], LOG_MAX_LEN, format, argp) > 0 ) {
-		if (level >= 0) {
-			/* All verbose and error messages in tools end with EOL. */
-			if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR)
-				strncat(target, "\n", LOG_MAX_LEN);
+		/* All verbose and error messages in tools end with EOL. */
+		if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR)
+			strncat(target, "\n", LOG_MAX_LEN);
 
-			crypt_log(cd, level, target);
-#ifdef CRYPT_DEBUG
-		} else if (_debug_level)
-			printf("# %s:%d %s\n", file ?: "?", line, target);
-#else
-		} else if (_debug_level)
-			printf("# %s\n", target);
-#endif
+		crypt_log(cd, level, target);
 	}
 
 	va_end(argp);
diff --git a/src/cryptsetup.c b/src/cryptsetup.c
index 158a270..4741c87 100644
--- a/src/cryptsetup.c
+++ b/src/cryptsetup.c
@@ -2738,7 +2738,7 @@
 
 	if (opt_debug) {
 		opt_verbose = 1;
-		crypt_set_debug_level(-1);
+		crypt_set_debug_level(CRYPT_DEBUG_ALL);
 		dbg_version_and_cmd(argc, argv);
 	}
 
diff --git a/src/utils_tools.c b/src/utils_tools.c
index f0ba6f3..63817b7 100644
--- a/src/utils_tools.c
+++ b/src/utils_tools.c
@@ -88,19 +88,11 @@
 	va_start(argp, format);
 
 	if (vsnprintf(&target[0], LOG_MAX_LEN, format, argp) > 0) {
-		if (level >= 0) {
-			/* All verbose and error messages in tools end with EOL. */
-			if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR)
-				strncat(target, "\n", LOG_MAX_LEN);
+		/* All verbose and error messages in tools end with EOL. */
+		if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR)
+			strncat(target, "\n", LOG_MAX_LEN);
 
-			crypt_log(cd, level, target);
-#ifdef CRYPT_DEBUG
-		} else if (opt_debug)
-			printf("# %s:%d %s\n", file ?: "?", line, target);
-#else
-		} else if (opt_debug)
-			printf("# %s\n", target);
-#endif
+		crypt_log(cd, level, target);
 	}
 
 	va_end(argp);
@@ -111,21 +103,18 @@
 	switch(level) {
 
 	case CRYPT_LOG_NORMAL:
-		fputs(msg, stdout);
+		fprintf(stdout, "%s", msg);
 		break;
 	case CRYPT_LOG_VERBOSE:
 		if (opt_verbose)
-			fputs(msg, stdout);
+			fprintf(stdout, "%s", msg);
 		break;
 	case CRYPT_LOG_ERROR:
-		fputs(msg, stderr);
+		fprintf(stderr, "%s", msg);
 		break;
 	case CRYPT_LOG_DEBUG:
 		if (opt_debug)
-			printf("# %s\n", msg);
-		break;
-	default:
-		fprintf(stderr, "Internal error on logging class for msg: %s", msg);
+			fprintf(stdout, "# %s\n", msg);
 		break;
 	}
 }
diff --git a/tests/test_utils.c b/tests/test_utils.c
index 181435d..982ec65 100644
--- a/tests/test_utils.c
+++ b/tests/test_utils.c
@@ -256,8 +256,16 @@
 {
 	int len;
 
-	if (_debug)
-		printf("LOG: %s", msg);
+	if (_debug) {
+		if (level == CRYPT_LOG_DEBUG)
+			fprintf(stdout, "# %s\n", msg);
+		else
+			fprintf(stdout, "%s", msg);
+	}
+
+	if (level == CRYPT_LOG_DEBUG)
+		return;
+
 	strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
 	global_lines++;
 	if (level == CRYPT_LOG_ERROR) {