os_kernel_log.c 源码
2026/7/20大约 2 分钟源码参考内核日志调试
os_kernel_log.c — 内核日志打印
源文件路径: kernel/source/os_kernel_log.c
引用章节: 6.1 日志系统
核心机制
内核日志系统 (os_kernel_print) 提供:
- 四级日志:E(0) / W(1) / I(2) / D(3)
- 全局级别过滤:
KLOG_GLOBAL_LEVEL控制 - Tag 级别过滤:支持按模块标签单独设置日志级别
- 颜色输出:ANSI 转义序列支持(红/黄/绿/蓝)
- 时间戳:自动附加
[tick]前缀
完整源码(关键部分)
/**
***********************************************************************************************************************
* Copyright (c) 2020, China Mobile Communications Group Co.,Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @file os_kernel_log.c
*
* @brief Implementation of kernel printing function.
*
* @revision
* Date Author Notes
* 2020-03-05 OneOS Team First version.
***********************************************************************************************************************
*/
#include <oneos_config.h>
#include <os_list.h>
#include <os_clock.h>
#include <string.h>
#include <board.h>
#include <os_util.h>
#include <os_assert.h>
#ifdef OS_USING_KERNEL_DEBUG
#define KLOG_NEWLINE_SIGN "\r\n"
#ifdef KLOG_USING_COLOR
/* CSI(Control Sequence Introducer/Initiator) sign
* more information on https://en.wikipedia.org/wiki/ANSI_escape_code */
#define CSI_START "\033["
#define CSI_END "\033[0m"
/* Output log front color */
#define F_BLACK "30m"
#define F_RED "31m"
#define F_GREEN "32m"
#define F_YELLOW "33m"
#define F_BLUE "34m"
#define F_MAGENTA "35m"
#define F_CYAN "36m"
#define F_WHITE "37m"
#define KLOG_COLOR_DEBUG (OS_NULL)
#define KLOG_COLOR_INFO (F_GREEN)
#define KLOG_COLOR_WARN (F_YELLOW)
#define KLOG_COLOR_ERROR (F_RED)
static const char *gs_klog_color_output_info[] = {
KLOG_COLOR_ERROR, /* level 0 */
KLOG_COLOR_WARN, /* level 1 */
KLOG_COLOR_INFO, /* level 2 */
KLOG_COLOR_DEBUG, /* level 3 */
};
#endif /* KLOG_USING_COLOR */
#define KLOG_FILTER_TAG_MAX_LEN 15
/* Tag 级别过滤器 */
struct os_klog_tag_lvl_filter
{
os_list_node_t list;
char tag[KLOG_FILTER_TAG_MAX_LEN + 1];
uint16_t level;
};
/* 日志控制信息 */
struct os_klog_ctrl_info
{
os_list_node_t tag_lvl_list; /* Tag 过滤器链表 */
uint16_t global_level; /* 全局日志级别 */
char log_buff[OS_LOG_BUFF_SIZE]; /* 日志缓冲区 */
};
static os_klog_ctrl_info_t gs_klog_ctrl_info = {
.tag_lvl_list = OS_LIST_INIT(gs_klog_ctrl_info.tag_lvl_list),
.global_level = KLOG_GLOBAL_LEVEL
};
static const char *gs_klog_level_output_info[] = {"E/", "W/", "I/", "D/"};
/**
* @brief 内核日志打印函数
* @param level 日志级别 (0=E, 1=W, 2=I, 3=D)
* @param tag 模块标签
* @param newline 是否换行
* @param fmt 格式化字符串
*/
void os_kernel_print(uint16_t level, const char *tag, os_bool_t newline,
const char *fmt, ...)
{
va_list args;
static int32_t s_cnt, s_offset, s_newline_len;
static os_bool_t s_found;
static uint16_t s_tag_level;
/* 级别过滤:先检查 Tag 级别,再检查全局级别 */
if (os_list_empty(&gs_klog_ctrl_info.tag_lvl_list))
{
if (level > gs_klog_ctrl_info.global_level) return;
}
else
{
s_found = os_get_klog_tag_lvl(tag, &s_tag_level);
if (s_found)
{
if (level > s_tag_level) return;
}
else
{
if (level > gs_klog_ctrl_info.global_level) return;
}
}
/* 格式化日志:[tick] 级别/TAG: 消息 */
s_offset = 0;
memset(gs_klog_ctrl_info.log_buff, 0, OS_LOG_BUFF_SIZE);
/* 颜色前缀 */
#ifdef KLOG_USING_COLOR
if (gs_klog_color_output_info[level])
{
s_cnt = os_snprintf(gs_klog_ctrl_info.log_buff + s_offset,
OS_LOG_BUFF_SIZE - s_offset,
"%s%s", CSI_START, gs_klog_color_output_info[level]);
s_offset += s_cnt;
}
#endif
s_cnt = os_snprintf(gs_klog_ctrl_info.log_buff + s_offset,
OS_LOG_BUFF_SIZE - s_offset,
"[%lu] %s%s: ",
os_tick_get_value(),
gs_klog_level_output_info[level], tag);
va_start(args, fmt);
s_offset += s_cnt;
s_cnt = os_vsnprintf(gs_klog_ctrl_info.log_buff + s_offset,
OS_LOG_BUFF_SIZE - s_offset, fmt, args);
va_end(args);
s_offset += s_cnt;
/* 换行 + 颜色结束 */
if (newline) strcpy(gs_klog_ctrl_info.log_buff + s_offset, KLOG_NEWLINE_SIGN);
#ifdef KLOG_USING_COLOR
if (gs_klog_color_output_info[level] != OS_NULL)
strcpy(gs_klog_ctrl_info.log_buff + s_offset + strlen(KLOG_NEWLINE_SIGN), CSI_END);
#endif
os_hw_console_output(gs_klog_ctrl_info.log_buff);
}
#endif /* OS_USING_KERNEL_DEBUG */输出示例
[12345] I/TASK: task led_blink created successfully
[12346] W/SENSOR: aht10 read timeout
[12347] E/MQTT: connection failed, rc=-2