diff --git a/autotest.sh b/autotest.sh index 077b8d2b5..803f37191 100755 --- a/autotest.sh +++ b/autotest.sh @@ -48,6 +48,7 @@ time test/lib/nvme/nvme.sh time test/lib/memory/memory.sh time test/lib/ioat/ioat.sh time test/lib/json/json.sh +time test/lib/log/log.sh timing_exit lib diff --git a/include/spdk/log.h b/include/spdk/log.h new file mode 100644 index 000000000..8b01b52d9 --- /dev/null +++ b/include/spdk/log.h @@ -0,0 +1,114 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * Logging interfaces + */ + +#ifndef SPDK_LOG_H +#define SPDK_LOG_H + +#include +#include +#include +#include + +/* + * Default: 1 - noticelog messages will print to stderr and syslog. + * Can be set to 0 to print noticelog messages to syslog only. + */ +extern unsigned int spdk_g_notice_stderr_flag; + +#define SPDK_NOTICELOG(...) \ + spdk_noticelog(NULL, 0, NULL, __VA_ARGS__) +#define SPDK_WARNLOG(...) \ + spdk_warnlog(NULL, 0, NULL, __VA_ARGS__) +#define SPDK_ERRLOG(...) \ + spdk_errlog(__FILE__, __LINE__, __func__, __VA_ARGS__) + + +#ifdef DEBUG +#define SPDK_LOG_REGISTER_TRACE_FLAG(str, flag) \ +bool flag = false; \ +__attribute__((constructor)) static void register_trace_flag_##flag(void) \ +{ \ + spdk_log_register_trace_flag(str, &flag); \ +} + +#define SPDK_TRACELOG(FLAG, ...) \ + do { \ + extern bool FLAG; \ + if (FLAG) { \ + spdk_tracelog(__FILE__, __LINE__, __func__, __VA_ARGS__); \ + } \ + } while (0) + +#define SPDK_TRACEDUMP(FLAG, LABEL, BUF, LEN) \ + do { \ + extern bool FLAG; \ + if (FLAG) { \ + spdk_trace_dump((LABEL), (BUF), (LEN)); \ + } \ + } while (0) + +#else +#define SPDK_LOG_REGISTER_TRACE_FLAG(str, flag) +#define SPDK_TRACELOG(...) do { } while (0) +#define SPDK_TRACEDUMP(...) do { } while (0) +#endif + +int spdk_set_log_facility(const char *facility); +int spdk_set_log_priority(const char *priority); +void spdk_noticelog(const char *file, const int line, const char *func, + const char *format, ...) __attribute__((__format__(__printf__, 4, 5))); +void spdk_warnlog(const char *file, const int line, const char *func, + const char *format, ...) __attribute__((__format__(__printf__, 4, 5))); +void spdk_tracelog(const char *file, const int line, + const char *func, const char *format, ...) __attribute__((__format__(__printf__, 4, 5))); +void spdk_errlog(const char *file, const int line, const char *func, + const char *format, ...) __attribute__((__format__(__printf__, 4, 5))); +void spdk_trace_dump(const char *label, const uint8_t *buf, size_t len); + +void spdk_log_register_trace_flag(const char *name, bool *enabled); +bool spdk_log_get_trace_flag(const char *flag); +int spdk_log_set_trace_flag(const char *flag); +int spdk_log_clear_trace_flag(const char *flag); + +size_t spdk_log_get_num_trace_flags(void); +const char *spdk_log_get_trace_flag_name(size_t idx); + +void spdk_open_log(void); +void spdk_close_log(void); + +#endif /* SPDK_LOG_H */ diff --git a/lib/Makefile b/lib/Makefile index e6b03aa07..8ef20e210 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -34,7 +34,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -DIRS-y += conf json memory util nvme ioat +DIRS-y += conf json log memory util nvme ioat .PHONY: all clean $(DIRS-y) diff --git a/lib/log/Makefile b/lib/log/Makefile new file mode 100644 index 000000000..aaf087058 --- /dev/null +++ b/lib/log/Makefile @@ -0,0 +1,39 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) + +C_SRCS = log.c +LIBNAME = log + +include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/lib/log/log.c b/lib/log/log.c new file mode 100644 index 000000000..0525ac514 --- /dev/null +++ b/lib/log/log.c @@ -0,0 +1,368 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/log.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +struct spdk_trace_flag { + const char *name; + bool *enabled; +}; + +static size_t g_num_trace_flags = 0; +static struct spdk_trace_flag *g_trace_flags = NULL; + +unsigned int spdk_g_notice_stderr_flag = 1; +unsigned int spdk_g_log_facility = LOG_DAEMON; +unsigned int spdk_g_log_priority = LOG_NOTICE; + +SPDK_LOG_REGISTER_TRACE_FLAG("debug", SPDK_TRACE_DEBUG) + +#define MAX_TMPBUF 1024 + +int +spdk_set_log_facility(const char *facility) +{ + if (strcasecmp(facility, "daemon") == 0) { + spdk_g_log_facility = LOG_DAEMON; + } else if (strcasecmp(facility, "auth") == 0) { + spdk_g_log_facility = LOG_AUTH; + } else if (strcasecmp(facility, "authpriv") == 0) { + spdk_g_log_facility = LOG_AUTHPRIV; + } else if (strcasecmp(facility, "local1") == 0) { + spdk_g_log_facility = LOG_LOCAL1; + } else if (strcasecmp(facility, "local2") == 0) { + spdk_g_log_facility = LOG_LOCAL2; + } else if (strcasecmp(facility, "local3") == 0) { + spdk_g_log_facility = LOG_LOCAL3; + } else if (strcasecmp(facility, "local4") == 0) { + spdk_g_log_facility = LOG_LOCAL4; + } else if (strcasecmp(facility, "local5") == 0) { + spdk_g_log_facility = LOG_LOCAL5; + } else if (strcasecmp(facility, "local6") == 0) { + spdk_g_log_facility = LOG_LOCAL6; + } else if (strcasecmp(facility, "local7") == 0) { + spdk_g_log_facility = LOG_LOCAL7; + } else { + spdk_g_log_facility = LOG_DAEMON; + return -1; + } + return 0; +} + +int +spdk_set_log_priority(const char *priority) +{ + if (strcasecmp(priority, "emerg") == 0) { + spdk_g_log_priority = LOG_EMERG; + } else if (strcasecmp(priority, "alert") == 0) { + spdk_g_log_priority = LOG_ALERT; + } else if (strcasecmp(priority, "crit") == 0) { + spdk_g_log_priority = LOG_CRIT; + } else if (strcasecmp(priority, "err") == 0) { + spdk_g_log_priority = LOG_ERR; + } else if (strcasecmp(priority, "warning") == 0) { + spdk_g_log_priority = LOG_WARNING; + } else if (strcasecmp(priority, "notice") == 0) { + spdk_g_log_priority = LOG_NOTICE; + } else if (strcasecmp(priority, "info") == 0) { + spdk_g_log_priority = LOG_INFO; + } else if (strcasecmp(priority, "debug") == 0) { + spdk_g_log_priority = LOG_DEBUG; + } else { + spdk_g_log_priority = LOG_NOTICE; + return -1; + } + return 0; +} + +void +spdk_noticelog(const char *file, const int line, const char *func, + const char *format, ...) +{ + char buf[MAX_TMPBUF]; + va_list ap; + + va_start(ap, format); + vsnprintf(buf, sizeof buf, format, ap); + if (file != NULL) { + if (func != NULL) { + if (spdk_g_notice_stderr_flag) { + fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); + } + syslog(LOG_NOTICE, "%s:%4d:%s: %s", file, line, func, buf); + } else { + if (spdk_g_notice_stderr_flag) { + fprintf(stderr, "%s:%4d: %s", file, line, buf); + } + syslog(LOG_NOTICE, "%s:%4d: %s", file, line, buf); + } + } else { + if (spdk_g_notice_stderr_flag) { + fprintf(stderr, "%s", buf); + } + syslog(LOG_NOTICE, "%s", buf); + } + va_end(ap); +} + +void +spdk_warnlog(const char *file, const int line, const char *func, + const char *format, ...) +{ + char buf[MAX_TMPBUF]; + va_list ap; + + va_start(ap, format); + vsnprintf(buf, sizeof buf, format, ap); + if (file != NULL) { + if (func != NULL) { + fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); + syslog(LOG_WARNING, "%s:%4d:%s: %s", + file, line, func, buf); + } else { + fprintf(stderr, "%s:%4d: %s", file, line, buf); + syslog(LOG_WARNING, "%s:%4d: %s", file, line, buf); + } + } else { + fprintf(stderr, "%s", buf); + syslog(LOG_WARNING, "%s", buf); + } + + va_end(ap); +} + +void +spdk_tracelog(const char *file, const int line, const char *func, + const char *format, ...) +{ + char buf[MAX_TMPBUF]; + va_list ap; + + va_start(ap, format); + vsnprintf(buf, sizeof buf, format, ap); + if (func != NULL) { + fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); + //syslog(LOG_INFO, "%s:%4d:%s: %s", file, line, func, buf); + } else { + fprintf(stderr, "%s:%4d: %s", file, line, buf); + //syslog(LOG_INFO, "%s:%4d: %s", file, line, buf); + } + va_end(ap); +} + +void +spdk_errlog(const char *file, const int line, const char *func, + const char *format, ...) +{ + char buf[MAX_TMPBUF]; + va_list ap; + + va_start(ap, format); + vsnprintf(buf, sizeof buf, format, ap); + if (func != NULL) { + fprintf(stderr, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf); + syslog(LOG_ERR, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf); + } else { + fprintf(stderr, "%s:%4d: ***ERROR*** %s", file, line, buf); + syslog(LOG_ERR, "%s:%4d: ***ERROR*** %s", file, line, buf); + } + va_end(ap); +} + +static void +fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len) +{ + char tmpbuf[MAX_TMPBUF]; + char buf8[8 + 1]; + size_t total; + size_t idx; + + fprintf(fp, "%s\n", label); + + memset(buf8, 0, sizeof buf8); + total = 0; + for (idx = 0; idx < len; idx++) { + if (idx != 0 && idx % 8 == 0) { + snprintf(tmpbuf + total, sizeof tmpbuf - total, + "%s", buf8); + fprintf(fp, "%s\n", tmpbuf); + total = 0; + } + total += snprintf(tmpbuf + total, sizeof tmpbuf - total, + "%2.2x ", buf[idx] & 0xff); + buf8[idx % 8] = isprint(buf[idx]) ? buf[idx] : '.'; + } + for (; idx % 8 != 0; idx++) { + total += snprintf(tmpbuf + total, sizeof tmpbuf - total, " "); + buf8[idx % 8] = ' '; + } + snprintf(tmpbuf + total, sizeof tmpbuf - total, "%s", buf8); + fprintf(fp, "%s\n", tmpbuf); + fflush(fp); +} + +void +spdk_trace_dump(const char *label, const uint8_t *buf, size_t len) +{ + fdump(stderr, label, buf, len); +} + +static int compare_trace_flags(const void *key, const void *p) +{ + const struct spdk_trace_flag *flag = p; + + return strcasecmp(key, flag->name); +} + +static struct spdk_trace_flag * +get_trace_flag(const char *name) +{ + return bsearch(name, g_trace_flags, g_num_trace_flags, sizeof(struct spdk_trace_flag), + compare_trace_flags); +} + +void +spdk_log_register_trace_flag(const char *name, bool *enabled) +{ + struct spdk_trace_flag *flag, *new_flags; + + if (name == NULL || enabled == NULL) { + fprintf(stderr, "missing spdk_trace_flag parameters\n"); + abort(); + } + + if (get_trace_flag(name)) { + fprintf(stderr, "duplicate spdk_trace_flag '%s'\n", name); + abort(); + } + + new_flags = realloc(g_trace_flags, (g_num_trace_flags + 1) * sizeof(struct spdk_trace_flag)); + if (new_flags == NULL) { + fprintf(stderr, "spdk_trace_flag allocation error\n"); + abort(); + } + + g_trace_flags = new_flags; + + /* Find slot so that new flag is inserted in sorted order */ + for (flag = g_trace_flags; flag != g_trace_flags + g_num_trace_flags; flag++) { + if (strcasecmp(name, flag->name) < 0) { + size_t to_move = g_num_trace_flags - (flag - g_trace_flags); + + memmove(flag + 1, flag, to_move * sizeof(struct spdk_trace_flag)); + break; + } + } + + flag->name = name; + flag->enabled = enabled; + g_num_trace_flags++; +} + +bool +spdk_log_get_trace_flag(const char *name) +{ + struct spdk_trace_flag *flag = get_trace_flag(name); + + if (flag && *flag->enabled) { + return true; + } + + return false; +} + +static int +set_trace_flag(const char *name, bool value) +{ + struct spdk_trace_flag *flag = get_trace_flag(name); + + if (flag == NULL) { + return -1; + } + + *flag->enabled = value; + + return 0; +} + +int +spdk_log_set_trace_flag(const char *name) +{ + return set_trace_flag(name, true); +} + +int +spdk_log_clear_trace_flag(const char *name) +{ + return set_trace_flag(name, false); +} + + +size_t spdk_log_get_num_trace_flags(void) +{ + return g_num_trace_flags; +} + +const char *spdk_log_get_trace_flag_name(size_t idx) +{ + if (idx >= g_num_trace_flags) { + return NULL; + } + + return g_trace_flags[idx].name; +} + +void +spdk_open_log(void) +{ + if (spdk_g_log_facility != 0) { + openlog("spdk", LOG_PID, spdk_g_log_facility); + } else { + openlog("spdk", LOG_PID, LOG_DAEMON); + } +} + +void +spdk_close_log(void) +{ + closelog(); +} diff --git a/test/lib/Makefile b/test/lib/Makefile index 546a43349..c1183ec99 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -34,7 +34,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -DIRS-y = json nvme memory ioat +DIRS-y = log json nvme memory ioat .PHONY: all clean $(DIRS-y) diff --git a/test/lib/log/.gitignore b/test/lib/log/.gitignore new file mode 100644 index 000000000..60261c07b --- /dev/null +++ b/test/lib/log/.gitignore @@ -0,0 +1 @@ +log_ut diff --git a/test/lib/log/Makefile b/test/lib/log/Makefile new file mode 100644 index 000000000..30b244540 --- /dev/null +++ b/test/lib/log/Makefile @@ -0,0 +1,52 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + +APP = log_ut + +C_SRCS := log_ut.c +CFLAGS += -I$(SPDK_ROOT_DIR)/lib/log + +LIBS += $(SPDK_LIBS) -lcunit + +all : $(APP) + +$(APP) : $(OBJS) $(SPDK_LIBS) + $(LINK_C) + +clean : + $(CLEAN_C) $(APP) + +include $(SPDK_ROOT_DIR)/mk/spdk.deps.mk diff --git a/test/lib/log/log.sh b/test/lib/log/log.sh new file mode 100755 index 000000000..41e06b503 --- /dev/null +++ b/test/lib/log/log.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -xe + +testdir=$(readlink -f $(dirname $0)) +rootdir=$testdir/../../.. +source $rootdir/scripts/autotest_common.sh + +timing_enter log + +$testdir/log_ut + +timing_exit log diff --git a/test/lib/log/log_ut.c b/test/lib/log/log_ut.c new file mode 100644 index 000000000..5f245cec2 --- /dev/null +++ b/test/lib/log/log_ut.c @@ -0,0 +1,109 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "CUnit/Basic.h" +#include "spdk/log.h" + +#include "log.c" + +static void +log_test(void) +{ + int rc = 0; + + rc = spdk_set_log_facility("test"); + CU_ASSERT(rc == -1); + rc = spdk_set_log_facility("local7"); + CU_ASSERT(rc == 0); + rc = spdk_set_log_priority("test"); + CU_ASSERT(rc == -1); + rc = spdk_set_log_priority("debug"); + CU_ASSERT(rc == 0); + +#ifdef DEBUG + rc = spdk_log_get_num_trace_flags(); + CU_ASSERT(rc == 1); + CU_ASSERT(strcasecmp(spdk_log_get_trace_flag_name(0), "debug") == 0); + + CU_ASSERT(spdk_log_get_trace_flag("debug") == false); + + spdk_log_set_trace_flag("debug"); + CU_ASSERT(spdk_log_get_trace_flag("debug") == true); + + spdk_log_clear_trace_flag("debug"); + CU_ASSERT(spdk_log_get_trace_flag("debug") == false); +#endif + + spdk_open_log(); + spdk_log_set_trace_flag("debug"); + SPDK_WARNLOG("log warnning unit test\n"); + SPDK_TRACELOG(SPDK_TRACE_DEBUG, "log trace test\n"); + SPDK_TRACEDUMP(SPDK_TRACE_DEBUG, "log trace dump test:", "trace dump", 10); + spdk_trace_dump("spdk dump test:", "spdk dump", 9); + + spdk_close_log(); +} + +int main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + if (CU_initialize_registry() != CUE_SUCCESS) { + return CU_get_error(); + } + + suite = CU_add_suite("log", NULL, NULL); + if (suite == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + if ( + CU_add_test(suite, "log_ut", log_test) == NULL) { + CU_cleanup_registry(); + return CU_get_error(); + } + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + return num_failures; +} + diff --git a/unittest.sh b/unittest.sh index df928b581..478223daa 100755 --- a/unittest.sh +++ b/unittest.sh @@ -19,3 +19,7 @@ make -C test/lib/json CONFIG_WERROR=y test/lib/json/parse/json_parse_ut test/lib/json/util/json_util_ut test/lib/json/write/json_write_ut + +make -C test/lib/log CONFIG_WERROR=y + +test/lib/log/log_ut