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 7, 2021
1 parent 05beb08 commit 6babaf2
Show file tree
Hide file tree
Showing 3 changed files with 41 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
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 6babaf2

Please sign in to comment.