Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC]: add a logging handler callback #2087

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions include/tss2/tss2_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ 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>
#define LOGL_NONE 0
#define LOGL_ERROR 2
#define LOGL_WARNING 3
#define LOGL_INFO 4
#define LOGL_DEBUG 5
#define LOGL_TRACE 6
#define LOGL_UNDEF 0xFF

typedef enum {
LOGLEVEL_NONE = LOGL_NONE,
LOGLEVEL_ERROR = LOGL_ERROR,
LOGLEVEL_WARNING = LOGL_WARNING,
LOGLEVEL_INFO = LOGL_INFO,
LOGLEVEL_DEBUG = LOGL_DEBUG,
LOGLEVEL_TRACE = LOGL_TRACE,
LOGLEVEL_UNDEFINED = LOGL_UNDEF
} log_level;

/* TODO ENUMs can change size based on compiler settings, so just use int or unisgned here */
typedef void (*TSS2_LOG_HANDLER)(log_level loglevel, const char *module,
const char *file, const char *func, int line,
const char *msg);

/*
* 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:
*;
};
15 changes: 15 additions & 0 deletions src/tss2-esys/api/Esys_SetLogHandler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* 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);
}
1 change: 1 addition & 0 deletions src/tss2-esys/tss2-esys.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
<ClCompile Include="api\Esys_SequenceUpdate.c" />
<ClCompile Include="api\Esys_SetAlgorithmSet.c" />
<ClCompile Include="api\Esys_SetCommandCodeAuditStatus.c" />
<ClCompile Include="api/Esys_SetLogHandler.c" />
<ClCompile Include="api\Esys_SetPrimaryPolicy.c" />
<ClCompile Include="api\Esys_Shutdown.c" />
<ClCompile Include="api\Esys_Sign.c" />
Expand Down
19 changes: 19 additions & 0 deletions src/tss2-sys/api/Tss2_Sys_SetLogHandler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* 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"
#define LOGMODULE sys
#include "util/log.h"

TSS2_LOG_HANDLER
Tss2_Sys_SetLogHandler(
TSS2_LOG_HANDLER new_handler)
{
return set_log_handler(new_handler);
}
1 change: 1 addition & 0 deletions src/tss2-sys/tss2-sys.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<ClCompile Include="api\Tss2_Sys_RSA_Decrypt.c" />
<ClCompile Include="api\Tss2_Sys_RSA_Encrypt.c" />
<ClCompile Include="api\Tss2_Sys_SelfTest.c" />
<ClCompile Include="api/Tss2_Sys_SetLogHandler.c" />
<ClCompile Include="api\Tss2_Sys_SequenceComplete.c" />
<ClCompile Include="api\Tss2_Sys_SequenceUpdate.c" />
<ClCompile Include="api\Tss2_Sys_SetAlgorithmSet.c" />
Expand Down
52 changes: 37 additions & 15 deletions src/util/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ getLogFile(void)
#endif
}

static void do_default_log(log_level loglevel, const char *module,
const char *file, const char *func, int line,
const char *msg) {

char buf[4096];
int size = snprintf(buf, sizeof(buf), "%s:%s:%s:%d:%s() %s \n",
log_strings[loglevel], module, file, line, func, msg);

FILE *logfile = getLogFile();
fwrite (buf, size, 1, logfile);
fflush(logfile);
}

static TSS2_LOG_HANDLER log_handler = do_default_log;

TSS2_LOG_HANDLER set_log_handler(TSS2_LOG_HANDLER new_handler) {
TSS2_LOG_HANDLER old = log_handler;
log_handler = new_handler;
return old;
}

/* TODO update logblob */
void
doLogBlob(log_level loglevel, const char *module, log_level logdefault,
log_level *status,
Expand Down Expand Up @@ -188,27 +210,27 @@ doLog(log_level loglevel, const char *module, log_level logdefault,
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 log handler, skip message */
if (!log_handler) {
return;
/* If the default is registered, short circuit if loglevel is not high enough */
} else if (log_handler == do_default_log) {
if (unlikely(*status == LOGLEVEL_UNDEFINED))
*status = getLogLevel(module, logdefault);

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);
if (loglevel > *status)
return;
}

/* Either the message needs to be logged or needs to defer to registered handler */
char usermsgbuf[1024];

va_list vaargs;
va_start(vaargs, msg);
logfile = getLogFile();
vfprintf (logfile, fmt,
/* log_strings[loglevel], module, file, func, line, */
vaargs);
fflush(logfile);
vsnprintf(usermsgbuf, sizeof(usermsgbuf), msg, vaargs);
va_end(vaargs);

log_handler(loglevel, module, file, func, line, usermsgbuf);
}

static log_level
Expand Down
20 changes: 2 additions & 18 deletions src/util/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,6 @@
#define LOGDEFAULT LOGLEVEL_WARNING
#endif

#define LOGL_NONE 0
#define LOGL_ERROR 2
#define LOGL_WARNING 3
#define LOGL_INFO 4
#define LOGL_DEBUG 5
#define LOGL_TRACE 6
#define LOGL_UNDEF 0xFF

typedef enum {
LOGLEVEL_NONE = LOGL_NONE,
LOGLEVEL_ERROR = LOGL_ERROR,
LOGLEVEL_WARNING = LOGL_WARNING,
LOGLEVEL_INFO = LOGL_INFO,
LOGLEVEL_DEBUG = LOGL_DEBUG,
LOGLEVEL_TRACE = LOGL_TRACE,
LOGLEVEL_UNDEFINED = LOGL_UNDEF
} log_level;

static const char *log_strings[] COMPILER_ATTR(unused) = {
"none",
"(unused)",
Expand Down Expand Up @@ -146,6 +128,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