From 8e8bd9cf470c8aedf243928ae91c57fd9b2f970b Mon Sep 17 00:00:00 2001 From: William Roberts Date: Mon, 7 Jun 2021 12:43:08 -0500 Subject: [PATCH] [RFC]: add a logging handler callback Just some code to guide a discussion on how to add a logging callback routine to make applications and bindings uses the various apis easier to integrate into different logging mechanisms. This would be nice for the Python bindings. TODO: 1. Add ESAPI support 2. Add FAPI support 3. Test 4. Fiddle with names, style and locations in the code base Signed-off-by: William Roberts --- include/tss2/tss2_common.h | 4 ++ include/tss2/tss2_esys.h | 4 ++ include/tss2/tss2_sys.h | 4 ++ lib/tss2-esys.def | 1 + lib/tss2-esys.map | 1 + lib/tss2-sys.def | 1 + lib/tss2-sys.map | 1 + src/tss2-esys/api/Esys_SetLogHandler.c | 16 ++++++++ src/tss2-sys/api/Tss2_Sys_SetLogHandler.c | 18 +++++++++ src/util/log.c | 46 +++++++++++++++++------ src/util/log.h | 2 + 11 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/tss2-esys/api/Esys_SetLogHandler.c create mode 100644 src/tss2-sys/api/Tss2_Sys_SetLogHandler.c diff --git a/include/tss2/tss2_common.h b/include/tss2/tss2_common.h index 082883f52a..ef95578278 100644 --- a/include/tss2/tss2_common.h +++ b/include/tss2/tss2_common.h @@ -39,6 +39,10 @@ struct TSS2_ABI_VERSION { #define TSS2_ABI_VERSION_CURRENT {1, 2, 1, 108} +/* TODO: Maybe this is better in tss2_log.h? */ +#include +typedef void (*TSS2_LOG_HANDLER)(const char *msg, size_t size); + /* * Return Codes */ diff --git a/include/tss2/tss2_esys.h b/include/tss2/tss2_esys.h index b3fa9ca717..09a1bf68ed 100644 --- a/include/tss2/tss2_esys.h +++ b/include/tss2/tss2_esys.h @@ -3302,6 +3302,10 @@ Esys_GetSysContext( ESYS_CONTEXT *esys_context, TSS2_SYS_CONTEXT **sys_context); +TSS2_LOG_HANDLER +Esys_SetLogHandler( + TSS2_LOG_HANDLER new_handler); + #ifdef __cplusplus } #endif diff --git a/include/tss2/tss2_sys.h b/include/tss2/tss2_sys.h index c73df5fc29..b9b732d849 100644 --- a/include/tss2/tss2_sys.h +++ b/include/tss2/tss2_sys.h @@ -2281,6 +2281,10 @@ TSS2_RC Tss2_Sys_PolicyAuthorizeNV( TSS2L_SYS_AUTH_COMMAND const *cmdAuthsArray, TSS2L_SYS_AUTH_RESPONSE *rspAuthsArray); +TSS2_LOG_HANDLER +Tss2_Sys_SetLogHandler( + TSS2_LOG_HANDLER new_handler); + #ifdef __cplusplus } #endif diff --git a/lib/tss2-esys.def b/lib/tss2-esys.def index ffdadebfb3..847439ed7d 100644 --- a/lib/tss2-esys.def +++ b/lib/tss2-esys.def @@ -362,3 +362,4 @@ EXPORTS Esys_ZGen_2Phase Esys_ZGen_2Phase_Async Esys_ZGen_2Phase_Finish + Esys_SetLogHandler diff --git a/lib/tss2-esys.map b/lib/tss2-esys.map index ce6fd2e0ee..2599e53f40 100644 --- a/lib/tss2-esys.map +++ b/lib/tss2-esys.map @@ -365,6 +365,7 @@ Esys_Initialize; Esys_GetPollHandles; Esys_Finalize; + Esys_SetLogHandler; local: *; }; diff --git a/lib/tss2-sys.def b/lib/tss2-sys.def index 1f7fd6747f..9f22b4aae8 100644 --- a/lib/tss2-sys.def +++ b/lib/tss2-sys.def @@ -366,3 +366,4 @@ EXPORTS Tss2_Sys_ZGen_2Phase_Prepare Tss2_Sys_ZGen_2Phase_Complete Tss2_Sys_ZGen_2Phase + Tss2_Sys_SetLogHandler diff --git a/lib/tss2-sys.map b/lib/tss2-sys.map index d55602ec73..a9f268d4b9 100644 --- a/lib/tss2-sys.map +++ b/lib/tss2-sys.map @@ -370,6 +370,7 @@ Tss2_Sys_ZGen_2Phase_Prepare; Tss2_Sys_ZGen_2Phase_Complete; Tss2_Sys_ZGen_2Phase; + Tss2_Sys_SetLogHandler; local: *; }; diff --git a/src/tss2-esys/api/Esys_SetLogHandler.c b/src/tss2-esys/api/Esys_SetLogHandler.c new file mode 100644 index 0000000000..a9eeccf369 --- /dev/null +++ b/src/tss2-esys/api/Esys_SetLogHandler.c @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "tss2_esys.h" + +#define LOGMODULE esys +#include "util/log.h" + +TSS2_LOG_HANDLER +Esys_SetLogHandler( + TSS2_LOG_HANDLER new_handler) +{ + return set_log_handler(new_handler); +} diff --git a/src/tss2-sys/api/Tss2_Sys_SetLogHandler.c b/src/tss2-sys/api/Tss2_Sys_SetLogHandler.c new file mode 100644 index 0000000000..8cc806c449 --- /dev/null +++ b/src/tss2-sys/api/Tss2_Sys_SetLogHandler.c @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/***********************************************************************; + * Copyright (c) 2015 - 2018, Intel Corporation + * All rights reserved. + ***********************************************************************/ +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "tss2_sys.h" +#include "sysapi_util.h" + +TSS2_LOG_HANDLER +Tss2_Sys_SetLogHandler( + TSS2_LOG_HANDLER new_handler) +{ + return set_log_handler(new_handler); +} diff --git a/src/util/log.c b/src/util/log.c index 76899eb19d..e28f451416 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -2,6 +2,7 @@ #include #endif +#include #include #include #include @@ -182,32 +183,55 @@ doLogBlob(log_level loglevel, const char *module, log_level logdefault, } } +static void do_default_log(const char *msg, size_t size) +{ + FILE *logfile = getLogFile(); + fwrite(msg, size, 1, logfile); + fflush(logfile); +} + +static TSS2_LOG_HANDLER log_cb = do_default_log; + +TSS2_LOG_HANDLER set_log_handler(TSS2_LOG_HANDLER new_handler) +{ + TSS2_LOG_HANDLER old = log_cb; + log_cb = new_handler; + return old; +} + void doLog(log_level loglevel, const char *module, log_level logdefault, log_level *status, const char *file, const char *func, int line, const char *msg, ...) { - FILE *logfile; - if (unlikely(*status == LOGLEVEL_UNDEFINED)) - *status = getLogLevel(module, logdefault); - - if (loglevel > *status) + /* No logger set, skip */ + if (!log_cb) { return; + /* Logger is default, so check the level */ + } else if (log_cb == do_default_log) { + + if (unlikely(*status == LOGLEVEL_UNDEFINED)) + *status = getLogLevel(module, logdefault); + + if (loglevel > *status) + return; + } + + /* Any set logger gets called */ + char fmt[1024]; + char buf[4096]; - int size = snprintf(NULL, 0, "%s:%s:%s:%d:%s() %s \n", - log_strings[loglevel], module, file, line, func, msg); - char fmt[size+1]; snprintf(fmt, sizeof(fmt), "%s:%s:%s:%d:%s() %s \n", log_strings[loglevel], module, file, line, func, msg); va_list vaargs; va_start(vaargs, msg); - logfile = getLogFile(); - vfprintf (logfile, fmt, + int rc = vsnprintf (buf, sizeof(buf), fmt, /* log_strings[loglevel], module, file, func, line, */ vaargs); - fflush(logfile); + assert(rc > 0); + log_cb(buf, (size_t)rc); va_end(vaargs); } diff --git a/src/util/log.h b/src/util/log.h index 75ed7191ad..0acd2cbe70 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -146,6 +146,8 @@ static log_level LOGMODULE_status COMPILER_ATTR(unused) = LOGLEVEL_UNDEFINED; #define LOGBLOB_TRACE(FORMAT, ...) {} #endif +TSS2_LOG_HANDLER set_log_handler(TSS2_LOG_HANDLER new_handler); + void doLog(log_level loglevel, const char *module, log_level logdefault, log_level *status,