diff --git a/lib/setup.c b/lib/setup.c index abc2087e..d7991486 100644 --- a/lib/setup.c +++ b/lib/setup.c @@ -141,12 +141,17 @@ void crypt_log(struct crypt_device *cd, int level, const char *msg) { 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 @@ void logger(struct crypt_device *cd, int level, const char *file, 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 158a270d..4741c87e 100644 --- a/src/cryptsetup.c +++ b/src/cryptsetup.c @@ -2738,7 +2738,7 @@ int main(int argc, const char **argv) 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 f0ba6f3e..63817b7a 100644 --- a/src/utils_tools.c +++ b/src/utils_tools.c @@ -88,19 +88,11 @@ void clogger(struct crypt_device *cd, int level, const char *file, int line, 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 @@ void tool_log(int level, const char *msg, void *usrptr __attribute__((unused))) 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 181435d4..982ec657 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -256,8 +256,16 @@ void global_log_callback(int level, const char *msg, void *usrptr) { 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) {