Skip to content

Commit

Permalink
[RFC]: add a logging handler callback
Browse files Browse the repository at this point in the history
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 <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Jun 8, 2021
1 parent 05beb08 commit 8e8bd9c
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 11 deletions.
4 changes: 4 additions & 0 deletions include/tss2/tss2_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stddef.h>
typedef void (*TSS2_LOG_HANDLER)(const char *msg, size_t size);

/*
* Return Codes
*/
Expand Down
4 changes: 4 additions & 0 deletions include/tss2/tss2_esys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions include/tss2/tss2_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/tss2-esys.def
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,4 @@ EXPORTS
Esys_ZGen_2Phase
Esys_ZGen_2Phase_Async
Esys_ZGen_2Phase_Finish
Esys_SetLogHandler
1 change: 1 addition & 0 deletions lib/tss2-esys.map
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@
Esys_Initialize;
Esys_GetPollHandles;
Esys_Finalize;
Esys_SetLogHandler;
local:
*;
};
1 change: 1 addition & 0 deletions lib/tss2-sys.def
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,4 @@ EXPORTS
Tss2_Sys_ZGen_2Phase_Prepare
Tss2_Sys_ZGen_2Phase_Complete
Tss2_Sys_ZGen_2Phase
Tss2_Sys_SetLogHandler
1 change: 1 addition & 0 deletions lib/tss2-sys.map
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
Tss2_Sys_ZGen_2Phase_Prepare;
Tss2_Sys_ZGen_2Phase_Complete;
Tss2_Sys_ZGen_2Phase;
Tss2_Sys_SetLogHandler;
local:
*;
};
16 changes: 16 additions & 0 deletions src/tss2-esys/api/Esys_SetLogHandler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* SPDX-License-Identifier: BSD-2-Clause */
#ifdef HAVE_CONFIG_H
#include <config.h>
#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);
}
18 changes: 18 additions & 0 deletions src/tss2-sys/api/Tss2_Sys_SetLogHandler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/***********************************************************************;
* Copyright (c) 2015 - 2018, Intel Corporation
* All rights reserved.
***********************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#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);
}
46 changes: 35 additions & 11 deletions src/util/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <config.h>
#endif

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/util/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8e8bd9c

Please sign in to comment.