Log all debug messages through log callback.

This cahnge allow to redirect all output of library
to a log processor.
This commit is contained in:
Milan Broz
2018-10-21 11:25:13 +02:00
parent bce567db46
commit 493e8580d6
4 changed files with 30 additions and 36 deletions

View File

@@ -141,12 +141,17 @@ void crypt_log(struct crypt_device *cd, int level, const char *msg)
{ {
if (!msg) if (!msg)
return; return;
if (level < _debug_level)
return;
if (cd && cd->log) if (cd && cd->log)
cd->log(level, msg, cd->log_usrptr); cd->log(level, msg, cd->log_usrptr);
else if (_default_log) else if (_default_log)
_default_log(level, msg, NULL); _default_log(level, msg, NULL);
else if (_debug_level) /* Default to stdout/stderr if there is no callback. */
printf("%s", msg); else
fprintf(level == CRYPT_LOG_ERROR ? stderr : stdout, "%s", msg);
} }
__attribute__((format(printf, 5, 6))) __attribute__((format(printf, 5, 6)))
@@ -159,19 +164,11 @@ void logger(struct crypt_device *cd, int level, const char *file,
va_start(argp, format); va_start(argp, format);
if (vsnprintf(&target[0], LOG_MAX_LEN, format, argp) > 0 ) { if (vsnprintf(&target[0], LOG_MAX_LEN, format, argp) > 0 ) {
if (level >= 0) { /* All verbose and error messages in tools end with EOL. */
/* All verbose and error messages in tools end with EOL. */ if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR)
if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR) strncat(target, "\n", LOG_MAX_LEN);
strncat(target, "\n", LOG_MAX_LEN);
crypt_log(cd, level, target); 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
} }
va_end(argp); va_end(argp);

View File

@@ -2738,7 +2738,7 @@ int main(int argc, const char **argv)
if (opt_debug) { if (opt_debug) {
opt_verbose = 1; opt_verbose = 1;
crypt_set_debug_level(-1); crypt_set_debug_level(CRYPT_DEBUG_ALL);
dbg_version_and_cmd(argc, argv); dbg_version_and_cmd(argc, argv);
} }

View File

@@ -88,19 +88,11 @@ void clogger(struct crypt_device *cd, int level, const char *file, int line,
va_start(argp, format); va_start(argp, format);
if (vsnprintf(&target[0], LOG_MAX_LEN, format, argp) > 0) { if (vsnprintf(&target[0], LOG_MAX_LEN, format, argp) > 0) {
if (level >= 0) { /* All verbose and error messages in tools end with EOL. */
/* All verbose and error messages in tools end with EOL. */ if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR)
if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR) strncat(target, "\n", LOG_MAX_LEN);
strncat(target, "\n", LOG_MAX_LEN);
crypt_log(cd, level, target); 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
} }
va_end(argp); va_end(argp);
@@ -111,21 +103,18 @@ void tool_log(int level, const char *msg, void *usrptr __attribute__((unused)))
switch(level) { switch(level) {
case CRYPT_LOG_NORMAL: case CRYPT_LOG_NORMAL:
fputs(msg, stdout); fprintf(stdout, "%s", msg);
break; break;
case CRYPT_LOG_VERBOSE: case CRYPT_LOG_VERBOSE:
if (opt_verbose) if (opt_verbose)
fputs(msg, stdout); fprintf(stdout, "%s", msg);
break; break;
case CRYPT_LOG_ERROR: case CRYPT_LOG_ERROR:
fputs(msg, stderr); fprintf(stderr, "%s", msg);
break; break;
case CRYPT_LOG_DEBUG: case CRYPT_LOG_DEBUG:
if (opt_debug) if (opt_debug)
printf("# %s\n", msg); fprintf(stdout, "# %s\n", msg);
break;
default:
fprintf(stderr, "Internal error on logging class for msg: %s", msg);
break; break;
} }
} }

View File

@@ -256,8 +256,16 @@ void global_log_callback(int level, const char *msg, void *usrptr)
{ {
int len; int len;
if (_debug) if (_debug) {
printf("LOG: %s", msg); 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)); strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
global_lines++; global_lines++;
if (level == CRYPT_LOG_ERROR) { if (level == CRYPT_LOG_ERROR) {