封装不同的打印

Posted doitjust

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装不同的打印相关的知识,希望对你有一定的参考价值。

 

封装打印,区分颜色,显示文件名,函数名和行号

01_test_printf.c

#include <stdio.h>
#define ANSI_COLOR_RED     "x1b[31m"
#define ANSI_COLOR_GREEN   "x1b[32m"
#define ANSI_COLOR_YELLOW  "x1b[33m"
#define ANSI_COLOR_BLUE    "x1b[34m"
#define ANSI_COLOR_MAGENTA "x1b[35m"
#define ANSI_COLOR_CYAN    "x1b[36m"
#define ANSI_COLOR_RESET   "x1b[0m"

#define ERROR(x, args...)  printf(ANSI_COLOR_RED    "ERROR, %s,%s:%d  "x ANSI_COLOR_RESET"
", __FILE__, __func__, __LINE__, ##args)
#define WARN(x, args...)   printf(ANSI_COLOR_YELLOW "WARN , %s,%s:%d  "x ANSI_COLOR_RESET"
", __FILE__, __func__, __LINE__, ##args)
#define INFO(x, args...)   printf(ANSI_COLOR_CYAN   "INFO , %s,%s:%d  "x ANSI_COLOR_RESET"
", __FILE__, __func__, __LINE__, ##args)
#define DEBUG(x, args...)  printf(ANSI_COLOR_GREEN  "DEBUG, %s,%s:%d  "x ANSI_COLOR_RESET"
", __FILE__, __func__, __LINE__, ##args)
int main()
{
    printf("hello 
");
    INFO("hello world");
    ERROR("hello world");
    DEBUG("hello world");
    WARN("hello world");
    return 0;
}

运行结果

技术图片

 

以上是关于封装不同的打印的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段14——Vue的axios网络请求封装

argparse 代码片段只打印部分日志

封装不同的打印

我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情

回归 | js实用代码片段的封装与总结(持续更新中...)

LogUtil实现自由的控制日志的打印的封装类