格式化输出Log

本文介绍如何在Linux系统中实现自定义的Log格式输出,以达到直观易读的效果。示例代码包括logger.h、logger.cpp,通过logger_set_decor调整格式,并在logger_writer.cpp中定制实际输出目的地。
摘要由CSDN通过智能技术生成

很多系统,都需要标准化输出Log,达到直观阅读Log的目的。

这个是在Linux上测试的,如果需要移植到其他的系统,请根据系统情况实现inner_中的系统相关函数。

自己实现了一个Log输出实现,可以根据自己需要自定义输出格式。完整输出如android的规范输出:

05-16 15:21:18.501   11942   11942 F main:pid:100,tid:200
05-16 15:21:18.501   11942   11942 E main:pid:100,tid:200
05-16 15:21:18.501   11942   11942 W main:pid:100,tid:200
05-16 15:21:18.501   11942   11942 I main:pid:100,tid:200
05-16 15:21:18.501   11942   11942 D main:pid:100,tid:200
05-16 15:21:18.501   11942   11942 V main:pid:100,tid:200

输出格式:

       05-16:日期 

       15:21:18.501:具体的时间 ,精确到ms

       11942:进程ID

       11942:线程ID

       F/E/W/I/D/V:打印级别

        main:TAG

        pid:100,tid:200:具体的打印内容

测试代码如下:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

#include "Logger.h"

int main()
{
    LOGF("main","pid:%d,tid:%d",100,200);
    LOGE("main","pid:%d,tid:%d",100,200);
    LOGW("main","pid:%d,tid:%d",100,200);
    LOGI("main","pid:%d,tid:%d",100,200);
    LOGD("main","pid:%d,tid:%d",100,200);
    LOGV("main","pid:%d,tid:%d",100,200);

    return 0;
}

实现如下:

logger.h

#ifndef __LOGGER_H__
#define __LOGGER_H__

#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Declare maximum logging level/verbosity. Lower number indicates higher
 * importance, with the highest importance has level zero. The least
 * important level is five in this implementation, but this can be extended
 * by supplying the appropriate implementation.
 *
 * The level conventions:
 *  - 0: fatal error
 *  - 1: error
 *  - 2: warning
 *  - 3: info
 *  - 4: debug
 *  - 5: verbosity
 *
 * Default: 4
 */
#ifndef LOGGER_MAX_LEVEL
#define LOGGER_MAX_LEVEL   5
#endif

/**
 * Log decoration flag, to be specified with #tk_log_set_decor().
 */
enum log_decoration
{
    LOG_HAS_YEAR       =    1, /**< Include year digit [no]                */
    LOG_HAS_DAY        =    2, /**< Include day of month [no]              */
    LOG_HAS_TIME       =    4, /**< Include time [yes]                     */
    LOG_HAS_SENDER     =    8, /**< Include sender in the log [yes]        */
    LOG_HAS_COLOR      =   16, /**< Colorize logs [yes on win32]           */
    LOG_HAS_LEVEL_TEXT =   32, /**< Include level text string [no]         */
    LOG_HAS_THREAD_ID  =   64, /**< Include thread identification [no]     */
    LOG_HAS_THREAD_SWC =  128, /**< Add mark when thread has switched [yes]*/
    LOG_HAS_CR         =  256, /**< Include carriage return [no]           */
    LOG_HAS_NEWLINE    =  512 /**< Terminate each call with newline [yes] */
};

/**
 * Signature for function to be registered to the logging subsystem to
 * write the actual log message to some output device.
 *
 * @param level     Log level.
 * @param data      Log message, which will be NULL terminated.
 * @param len       Message length.
 */
typedef void logger_func(int level, const char *data, int len);

vo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值