From c3954cecbef5e41aec15cd605b67c014f0c08710 Mon Sep 17 00:00:00 2001 From: Minggui Cao Date: Mon, 1 Apr 2019 10:48:19 +0800 Subject: [PATCH] DM: add log macro/func and basic data structure. also set default logger as console and enabled. Tracked-On: #3012 Signed-off-by: Minggui Cao Acked-by: Yin Fengwei --- devicemodel/Makefile | 3 ++ devicemodel/include/log.h | 53 ++++++++++++++++++++++++ devicemodel/log/log.c | 86 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 devicemodel/include/log.h create mode 100644 devicemodel/log/log.c diff --git a/devicemodel/Makefile b/devicemodel/Makefile index 9a95d44ac..fd6958c4c 100644 --- a/devicemodel/Makefile +++ b/devicemodel/Makefile @@ -154,6 +154,9 @@ SRCS += arch/x86/pm.c SRCS += vmcfg/vmcfg.c SRCS += vmcfg/apl-mrb/vm1/vm1.c +# log +SRCS += log/log.c + OBJS := $(patsubst %.c,$(DM_OBJDIR)/%.o,$(SRCS)) VERSION_H := $(DM_OBJDIR)/include/version.h diff --git a/devicemodel/include/log.h b/devicemodel/include/log.h new file mode 100644 index 000000000..b34deec85 --- /dev/null +++ b/devicemodel/include/log.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef __LOG_H__ +#define __LOG_H__ + +#include "types.h" + +/* Logging severity levels */ +#define LOG_ERROR 1U +#define LOG_WARNING 2U +#define LOG_NOTICE 3U +#define LOG_INFO 4U +#define LOG_DEBUG 5U + +#define DEFAULT_LOG_LEVEL 4 +#define MAX_ONE_LOG_SIZE 256 + +struct logger_ops { + const char *name; + bool (*is_enabled)(void); + uint8_t (*get_log_level)(void); + int (*init)(bool enable, uint8_t log_level); + void (*deinit)(void); + void (*output)(const char *fmt, va_list args); +}; + +void init_logger_setting(const char *opt); +void output_log(uint8_t level, const char *fmt, ...); + +/* + * Put all logger instances' addresses into one section named logger_dev_ops + * so that DM could enumerate and initialize each of them. + */ +#define DECLARE_LOGGER_SECTION() SET_DECLARE(logger_dev_ops, struct logger_ops) +#define DEFINE_LOGGER_DEVICE(x) DATA_SET(logger_dev_ops, x) +#define FOR_EACH_LOGGER(pp_logger) SET_FOREACH(pp_logger, logger_dev_ops) + + +#ifndef pr_prefix +#define pr_prefix +#endif + +#define pr_err(...) output_log(LOG_ERROR, pr_prefix __VA_ARGS__) +#define pr_warn(...) output_log(LOG_WARNING, pr_prefix __VA_ARGS__) +#define pr_notice(...) output_log(LOG_NOTICE, pr_prefix __VA_ARGS__) +#define pr_info(...) output_log(LOG_INFO, pr_prefix __VA_ARGS__) +#define pr_dbg(...) output_log(LOG_DEBUG, pr_prefix __VA_ARGS__) + +#endif /* __LOG_H__ */ diff --git a/devicemodel/log/log.c b/devicemodel/log/log.c new file mode 100644 index 000000000..c0f1875f3 --- /dev/null +++ b/devicemodel/log/log.c @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" + + +DECLARE_LOGGER_SECTION(); + +/* + * --logger_setting: console,level=4;disk,level=4;kmsg,level=3 + * the setting param is from acrn-dm input, will be parsed here + */ +void init_logger_setting(const char *opt) +{ + +} + +void output_log(uint8_t level, const char *fmt, ...) +{ + va_list args; + struct logger_ops **pp_logger, *logger; + + /* check each logger flag and level, to output */ + FOR_EACH_LOGGER(pp_logger) { + logger = *pp_logger; + if (logger->is_enabled() && (level <= logger->get_log_level()) && (logger->output)) { + va_start(args, fmt); + logger->output(fmt, args); + va_end(args); + } + } +} + +/* console setting and its API interface */ +static uint8_t console_log_level = DEFAULT_LOG_LEVEL; +static bool console_enabled = true; + +static bool is_console_enabled(void) +{ + return console_enabled; +} + +static uint8_t get_console_log_level(void) +{ + return console_log_level; +} + +static int init_console_setting(bool enable, uint8_t log_level) +{ + console_enabled = enable; + console_log_level = log_level; + + return 0; +} + +static void write_to_console(const char *fmt, va_list args) +{ + /* if no need add other info, just output */ + vprintf(fmt, args); +} + +struct logger_ops logger_console = { + .name = "console", + .is_enabled = is_console_enabled, + .get_log_level = get_console_log_level, + .init = init_console_setting, + .output = write_to_console, +}; + + +DEFINE_LOGGER_DEVICE(logger_console);