这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» 综合技术» 基础知识» 分享一个自用的、极简的log模块

共13条 1/2 1 2 跳转至

分享一个自用的、极简的log模块

高工
2023-02-23 09:47:37 打赏
在平时的开发中,log打印必不可少。如果仅仅使用printf,则log信息不好定位。一些开源稳定、成熟的log模块功能往往比较强大,而我们可能又不需要那么多的功能。

今天,分享一个我自己用的、极简的log模块:log颜色可设置,带时间戳、文件、行号、函数。这个log模块仅包含两个文件:log.h 和 log.c。

一、log模块代码

log.h:

#ifndef LOG_H #define LOG_H #ifdef __cplusplus extern "C" { #endif #define LOG_BUF_SIZE 1024 typedef long long (*get_sys_time_ms_def)(void); enum log_color { COLOR_NULL = 0, RED = 1, GREEN = 2, YELLOW = 3, BLUE = 4 }; void log_print(enum log_color color, const char *file, int line, const char *func, const char* fmt, ...); void log_time_register(get_sys_time_ms_def p_get_sys_time_ms); #define LOG_D(...) log_print(BLUE, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) #ifdef __cplusplus } #endif #endif

log.c:

#include  #include  #include "log.h" static long long default_get_sys_time_ms(void) { return (long long)0; } static get_sys_time_ms_def s_get_sys_time_ms = default_get_sys_time_ms; void log_time_register(get_sys_time_ms_def p_get_sys_time_ms) { s_get_sys_time_ms = p_get_sys_time_ms; } void log_print(enum log_color color, const char *file, int line, const char *func, const char* fmt, ...) { va_list ap; char buf[LOG_BUF_SIZE] = {0}; long long time = s_get_sys_time_ms(); va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); switch(color) { case COLOR_NULL: printf("<%lld ms>[%s:%d %s] %s ", time, file, line, func, buf); break; case RED: printf("\033[31m<%lld ms>[%s:%d %s] %s\033[0m", time, file, line, func, buf); break; case GREEN: printf("\033[32m<%lld ms>[%s:%d %s] %s\033[0m", time, file, line, func, buf); break; case YELLOW: printf("\033[33m<%lld ms>[%s:%d %s] %s\033[0m", time, file, line, func, buf); break; case BLUE: printf("\033[34m<%lld ms>[%s:%d %s] %s\033[0m", time, file, line, func, buf); break; default: break; } }

其中,默认打印ms级的系统时间。因为不同的平台(Linux、Windows、STM32等),获取系统时间的方式都不一样。

使用时,再根据不同的平台自己定义一个获取系统时间的函数,以注册的方式进行绑定。不注册获取时间函数也不影响使用,时间戳打印为0。

二、log模块测试1、Linux平台

log_test.c:

#include  #include  #include "log.h" static long long linux_get_sys_time_ms(void) { long long time_ms = 0; struct timeval tv; gettimeofday(&tv, NULL); time_ms = (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000; return (long long)time_ms; } int main(void) { log_time_register(linux_get_sys_time_ms); char ch = 'a'; char str[10] = "ZhengN"; float float_val = 10.10; int num = 88; double double_val = 10.123456; LOG_D("字符为 %c \n", ch); LOG_D("字符串为 %s \n" , str); LOG_D("浮点数为 %f \n", float_val); LOG_D("整数为 %d\n" , num); LOG_D("双精度值为 %lf \n", double_val); LOG_D("八进制值为 %o \n", num); LOG_D("十六进制值为 %x \n", num); return 0; }

编译,运行:

image.png

第18行屏蔽掉:

image.png

2、Windows平台

log_test.c:

#include  #include  #include "log.h" static long long win_get_sys_time_ms(void) { long long time_ms = 0; time_ms = GetTickCount(); return time_ms; } void test(void) { LOG_D("Hello world\n"); } int main(void) { log_time_register(win_get_sys_time_ms); char ch = 'a'; char str[10] = "ZhengN"; float float_val = 10.10; int num = 88; double double_val = 10.123456; LOG_D("字符为 %c \n", ch); LOG_D("字符串为 %s \n" , str); LOG_D("浮点数为 %f \n", float_val); LOG_D("整数为 %d\n" , num); LOG_D("双精度值为 %lf \n", double_val); LOG_D("八进制值为 %o \n", num); LOG_D("十六进制值为 %x \n", num); test(); return 0; }

编译,运行:

image.png





关键词: 极简 log 模块

高工
2023-02-23 10:04:46 打赏
2楼

学习了!!!


高工
2023-02-23 10:04:52 打赏
3楼

学习了!!!学习了!!!


高工
2023-02-23 11:00:03 打赏
4楼

感谢分享


高工
2023-02-25 12:41:41 打赏
5楼

感谢分享


院士
2023-02-25 12:57:59 打赏
6楼

谢谢分享


工程师
2023-02-25 16:35:20 打赏
7楼

感谢分享


专家
2023-02-28 07:31:07 打赏
8楼
感谢分享

专家
2023-02-28 08:11:10 打赏
9楼

感谢分享


专家
2023-02-28 08:27:59 打赏
10楼

感谢分享


共13条 1/2 1 2 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]