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 <minggui.cao@intel.com>
Acked-by: Yin Fengwei <fengwei.yin@intel.com>
This commit is contained in:
Minggui Cao 2019-04-01 10:48:19 +08:00 committed by wenlingz
parent 31efa2b522
commit c3954cecbe
3 changed files with 142 additions and 0 deletions

View File

@ -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

53
devicemodel/include/log.h Normal file
View File

@ -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__ */

86
devicemodel/log/log.c Normal file
View File

@ -0,0 +1,86 @@
/*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <paths.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#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);