# fuxiaoy/uflog architecture

> Architecture diagram of the GitHub repository fuxiaoy/uflog, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fuxiaoy/uflog
- Repository: https://github.com/fuxiaoy/uflog

## Overview

##### 项目类型与目的
- 这是一个用 C 语言实现的轻量级嵌入式日志库，适合资源受限的单片机/固件项目。
- 属于开源工具/组件型项目，核心功能是提供可移植的日志输出、分级、前缀定制、十六进制数据打印和可选缓存存储等。
- README 中明确提到“嵌入式固件工程师与 BSP 移植者”为主要受众，并提供了快速上手、移植指南和示例。

##### 文件结构分析
- 顶层：
- LICENSE、README.en.md、README.md：文档及许可信息
- document/：协议与文档格式规范
- src/：核心实现文件
- src 目录：
- uflog.c：日志功能核心实现，包括初始化、输出、缓存管理、API 实现
- uflog.h：库的对外 API 和宏定义
- uflogCfg.h：编译时配置选项（内存模式切换、缓冲区长度、弱符号端口定义等）
- 没有前端、后端、测试目录，仅为一个嵌入式 C 库。

##### README 关键架构线索
- 说明了组件借鉴 Unix 设备模型，通过弱符号（__weak）实现可移植的端口接口：uflog_get_timestamp、log_printf、log_auto_store。
- 支持两种内存模式：静态实例（UFLOG_STATIC_MEMORY）或映射到项目 malloc/free。
- 提供 API 初始化、级别变更、配置变更、关闭等函数，及多实例创建/删除接口。

##### 系统设计图要素
a. 主要组件
1) 应用层（User Application）
- 调用 UFLOG_* 宏或 uflog_log 函数
2) API 层（uflog.h 接口）
- Initialization/Config/Level Change/Log/Delete
3) 核心引擎（uflog.c）
- 日志筛选（按级别）、前缀拼装、消息格式化
- 缓存管理（可选 UFLOG_AUTO_STORE）
4) 配置模块（uflogCfg.h）
- 内存模式开关、缓冲区长度、弱符号映射
5) 端口接口（Weak Symbols）
- uflog_get_timestamp：时间戳生成
- log_printf：输出函数（printf 或串口/UART）
- log_auto_store：缓存持久化回调
6) 运行时内存管理
- 静态全局实例（UFLOG_STATIC_MEMORY）
- 或映射到项目 malloc/free（uflog_malloc/uflog_free）
b. 组件关系与交互
- 应用层 → API 层：调用日志相关函数/宏
- API 层 → 核心引擎：转发参数，执行日志逻辑
- 核心引擎 ↔ 配置模块：读取编译时常量决定行为
- 核心引擎 → 端口接口：调用 uflog_get_timestamp、log_printf、log_auto_store
- 核心引擎 → 内存管理：根据配置选择静态实例或 malloc/free
c. 架构模式与设计原则
- 模块化：清晰分离 API、核心逻辑、配置和端口层
- 弱符号（weak symbols）+ port 模式：实现高度可移植
- 编译时配置与运行时控制分离（uflogCfg.h vs uflog_config）
d. 关键技术栈与库
- 纯 C 语言，面向嵌入式（无 RTOS 假设）
- 弱符号扩展（GCC __weak attribute）
- 宏封装（UFLOG_INF、xUFLOG_DBG 等）

##### 针对“开源工具/嵌入式库”类型的图示要点
- 强调“核心引擎”与“端口实现”可替换、可自定义
- 演示“静态内存模式” vs “动态分配模式”两种路径
- 展示用户如何在 BSP 或裸机项目中提供时间戳、输出和存储接口

##### 图表构建指南
- 使用方框表示组件：Application、API Layer、Core Engine、Config Module、Port Interfaces、Memory Manager
- 用箭头标明调用方向与数据流：API→Core、Core→Port、Core→Memory
- 采用颜色或形状区分：
- 绿色：用户/应用相关
- 蓝色：核心库模块
- 橙色：可移植接口（端口）
- 灰色：编译时配置
- 每个组件旁标注关键文件：
- Core Engine: uflog.c
- Config: uflogCfg.h
- API: uflog.h

##### 细节与完整性
- 图中务必列出：初始化流程、日志输出路径、缓存存储回调、关闭流程
- 对多实例支持也可用小部件展示 uflog_create/uﬂog_delete
- 保证箭头方向清晰，组件标签准确，必要时为每条连接加注释（如“调用 log_printf 输出”）

通过以上分析与指南，您能绘制一份准确反映 uflog 库架构的系统图，全面展现组件边界、扩展点及运行时行为。

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User/Application
    subgraph "User/Application"
        App["User Application"]:::user
    end

    %% uflog Library Components
    subgraph "uflog Library"
        API["API Layer\n(uflog.h)"]:::core
        Core["Core Engine\n(uflog.c)"]:::core
        Config["Configuration Module\n(uflogCfg.h)"]:::config
        Port["Port Interfaces\n(weak symbols)"]:::port
        Memory["Memory Manager\n(static or malloc/free)"]:::config
    end

    %% Documentation & License
    subgraph "Documentation & License"
        Docs["Log Format Protocol\ndocument/ufLogFormatProtocol.md"]:::doc
        Readme["README.md / README.en.md"]:::doc
        License["LICENSE"]:::doc
    end

    %% Connections
    App -->|"calls UFLOG_* macros\nor uflog_log()"| API
    API -->|"forwards parameters"| Core
    Core -->|"reads compile-time options"| Config
    Core -->|"calls port stubs\nuflog_get_timestamp,\nlog_printf,\nlog_auto_store"| Port
    Core -->|"allocates/releases via"| Memory

    %% Click Events
    click Core "https://github.com/fuxiaoy/uflog/blob/main/src/uflog.c"
    click API "https://github.com/fuxiaoy/uflog/blob/main/src/uflog.h"
    click Config "https://github.com/fuxiaoy/uflog/blob/main/src/uflogCfg.h"
    click Port "https://github.com/fuxiaoy/uflog/blob/main/src/uflog.c"
    click Docs "https://github.com/fuxiaoy/uflog/blob/main/document/ufLogFormatProtocol.md"
    click Readme "https://github.com/fuxiaoy/uflog/blob/main/README.md"
    click License "https://github.com/fuxiaoy/uflog/tree/main/LICENSE"

    %% Styles
    classDef user fill:#a2fca0,stroke:#333,stroke-width:1px;
    classDef core fill:#a0c4ff,stroke:#333,stroke-width:1px;
    classDef config fill:#e0e0e0,stroke:#333,stroke-width:1px;
    classDef port fill:#ffd6a5,stroke:#333,stroke-width:1px;
    classDef doc fill:#f0efeb,stroke:#333,stroke-width:1px;
```
