Commit 0ce363be authored by Vasilii Ivanov's avatar Vasilii Ivanov Committed by Konrad Sztyber
Browse files

spdk_log: introduce spdk_log_ext API



Current spdk_log implementation allows to provide only log function.
In some cases it might not be enough because user have to take care
of managing resources associated with logger.
Extended API supports providing open and close functions
with set context which will be called by SPDK in corresponded places.

Fixes #3480

Change-Id: Ia1d41fe3b414e2f603c2ae6612b7f9eb8d29bbb7
Signed-off-by: default avatarVasilii Ivanov <iwanovvvasilij@gmail.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/24847


Community-CI: Mellanox Build Bot
Reviewed-by: default avatarShuhei Matsumoto <smatsumoto@nvidia.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Reviewed-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
parent 412fced1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -122,7 +122,7 @@ struct spdk_app_opts {
	/**
	 * for passing user-provided log call
	 */
	logfunc         *log;
	spdk_log_cb		*log;

	uint64_t		base_virtaddr;

+41 −3
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
#ifndef SPDK_LOG_H
#define SPDK_LOG_H

#include "spdk/assert.h"
#include "spdk/stdinc.h"
#include "spdk/queue.h"

@@ -29,14 +30,51 @@ extern "C" {
 * \param format Format string to the message.
 * \param args Additional arguments for format string.
 */
typedef void logfunc(int level, const char *file, const int line,
typedef void spdk_log_cb(int level, const char *file, const int line,
			 const char *func, const char *format, va_list args);

/**
 * for opening user-provided logger
 *
 * \param ctx User-defined context for log open/close
 */
typedef void spdk_log_open_cb(void *ctx);

/**
 * for closing user-provided logger
 *
 * \param ctx User-defined context for log open/close
 */
typedef void spdk_log_close_cb(void *ctx);

struct spdk_log_opts {
	/**
	 * The size of spdk_log_opts according to the caller of this library is used for ABI
	 * compatibility.  The library uses this field to know how many fields in this
	 * structure are valid. And the library will populate any remaining fields with default values.
	 * New added fields should be put at the end of the struct.
	 */
	size_t			size;
	spdk_log_cb		*log;
	spdk_log_open_cb	*open;
	spdk_log_close_cb	*close;
	void			*user_ctx;
};
SPDK_STATIC_ASSERT(sizeof(struct spdk_log_opts) == 40, "Incorrect size");

/**
 * Initialize the logging module. Messages prior
 * to this call will be dropped.
 */
void spdk_log_open(logfunc *logf);
void spdk_log_open(spdk_log_cb *log);

/**
 * Extended API to initialize the logging module. Messages prior
 * to this call will be dropped.
 *
 * \param opts Options to provide log related functions with user-defined context for open/close
 */
void spdk_log_open_ext(struct spdk_log_opts *opts);

/**
 * Close the currently active log. Messages after this call
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

SO_VER := 7
SO_MINOR := 0
SO_MINOR := 1
SO_SUFFIX := $(SO_VER).$(SO_MINOR)

C_SRCS = log.c log_flags.c log_deprecated.c
+50 −9
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
 */

#include "spdk/stdinc.h"
#include "spdk/util.h"

#include "spdk/log.h"

@@ -17,7 +18,12 @@ static const char *const spdk_level_names[] = {

#define MAX_TMPBUF 1024

static logfunc *g_log = NULL;
static struct spdk_log_opts g_log_opts = {
	.log = NULL,
	.open = NULL,
	.close = NULL,
	.user_ctx = NULL,
};
static bool g_log_timestamps = true;

enum spdk_log_level g_spdk_log_level;
@@ -49,22 +55,57 @@ spdk_log_get_print_level(void) {
	return g_spdk_log_print_level;
}

static void
log_open(void *ctx)
{
	openlog("spdk", LOG_PID, LOG_LOCAL7);
}

static void
log_close(void *ctx)
{
	closelog();
}

void
spdk_log_open(logfunc *logf)
spdk_log_open(spdk_log_cb *log)
{
	if (logf) {
		g_log = logf;
	if (log) {
		struct spdk_log_opts opts = {.log = log};
		opts.size = SPDK_SIZEOF(&opts, log);
		spdk_log_open_ext(&opts);
	} else {
		openlog("spdk", LOG_PID, LOG_LOCAL7);
		spdk_log_open_ext(NULL);
	}
}

void
spdk_log_open_ext(struct spdk_log_opts *opts)
{
	if (!opts) {
		g_log_opts.open = log_open;
		g_log_opts.close = log_close;
		goto out;
	}

	g_log_opts.log = SPDK_GET_FIELD(opts, log, NULL);
	g_log_opts.open = SPDK_GET_FIELD(opts, open, NULL);
	g_log_opts.close = SPDK_GET_FIELD(opts, close, NULL);
	g_log_opts.user_ctx = SPDK_GET_FIELD(opts, user_ctx, NULL);

out:
	if (g_log_opts.open) {
		g_log_opts.open(g_log_opts.user_ctx);
	}
}

void
spdk_log_close(void)
{
	if (!g_log) {
		closelog();
	if (g_log_opts.close) {
		g_log_opts.close(g_log_opts.user_ctx);
	}
	memset(&g_log_opts, 0, sizeof(g_log_opts));
}

void
@@ -141,8 +182,8 @@ spdk_vlog(enum spdk_log_level level, const char *file, const int line, const cha
	va_list ap_copy;
	int rc;

	if (g_log) {
		g_log(level, file, line, func, format, ap);
	if (g_log_opts.log) {
		g_log_opts.log(level, file, line, func, format, ap);
		return;
	}

+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@

	# public functions
	spdk_log_open;
	spdk_log_open_ext;
	spdk_log_close;
	spdk_log_set_level;
	spdk_log_get_level;
Loading