编码 | 宏定义格式化日志信息

//trace 文件管理
//通过定义宏定义 SORT_OUTPUT_ENABLE_CLOSE 可控制日志文件系统的关闭。

#ifndef MODULES_TRACE_MODULE_TRACE_H
#define MODULES_TRACE_MODULE_TRACE_H

#if defined (__cplusplus)
extern "C" {
#endif

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

//output log's level
#define SORT_LVL_ASSERT                      0
#define SORT_LVL_ERROR                       1
#define SORT_LVL_WARN                        2
#define SORT_LVL_INFO                        3
#define SORT_LVL_DEBUG                       4
#define SORT_LVL_VERBOSE                     5

/** Define macro, used in the PrintMsg function. */
#define PRF_EMERG	LOG_EMERG	/* system is unusable			*/
#define PRF_ALERT	LOG_ALERT	/* action must be taken immediately	*/
#define PRF_CRIT	LOG_CRIT	/* critical conditions			*/
#define PRF_ERR	        LOG_ERR		/* error conditions			*/
#define PRF_WARNING	LOG_WARNING	/* warning conditions			*/
#define PRF_NOTICE	LOG_NOTICE	/* normal but significant condition	*/
#define PRF_INFO	LOG_INFO	/* informational			*/
#define PRF_DEBUG	LOG_DEBUG	/* debug-level messages			*/


static inline void sort_output(dt_u8_t level, dt_sc8_t* tag, dt_uc32_t line, dt_sc8_t* format, ...)
{
	char str_buff[1024] = { 0 }, levelStr[2] = "V";
	unsigned int len = 0;
	va_list args;

	switch(level)
	{
	case SORT_LVL_ASSERT:
	    strcpy(levelStr, "A");
	    break;
	case SORT_LVL_ERROR:
	    strcpy(levelStr, "E");
	    break;
    case SORT_LVL_WARN:
        strcpy(levelStr, "W");
        break;
    case SORT_LVL_INFO:
        strcpy(levelStr, "I");
        break;
    case SORT_LVL_DEBUG:
        strcpy(levelStr, "D");
        break;
    case SORT_LVL_VERBOSE:
        strcpy(levelStr, "V");
        break;
    default:
        strcpy(levelStr, "V");
	}

	memset(str_buff, 0, sizeof(str_buff));
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	dt_u64_t elapsed_msec = ts.tv_sec + ts.tv_nsec * (1e-6);
	len = sprintf(str_buff, "%10llu,%s,%s,%4d,", (dt_u64_t) elapsed_msec, levelStr, tag, line);

	va_start(args, format);
	len += vsnprintf(str_buff + len, 1024, (char const*) format, args); //size 1024
	va_end(args);

	len += sprintf(str_buff + len, "%s", "
");

    // print
    printf("%s", str_buff);
}


#define sort_assert(tag, ...) 
				sort_output(SORT_LVL_ASSERT, tag, __LINE__, __VA_ARGS__)

#define sort_error(tag, ...) 
				sort_output(SORT_LVL_ERROR, tag, __LINE__, __VA_ARGS__)

#define sort_warn(tag, ...) 
				sort_output(SORT_LVL_WARN, tag, __LINE__, __VA_ARGS__)

#define sort_info(tag, ...) 
				sort_output(SORT_LVL_INFO, tag, __LINE__, __VA_ARGS__)

#define sort_debug(tag, ...) 
				sort_output(SORT_LVL_DEBUG, tag, __LINE__, __VA_ARGS__)

#define sort_verbose(tag, ...) 
				sort_output(SORT_LVL_VERBOSE, tag, __LINE__, __VA_ARGS__)

//not use
#define ASSERT() do{log_a("log assert");while (1);}while(0)


#define sort_a(tag, ...)     sort_assert(tag, __VA_ARGS__)
#define sort_e(tag, ...)     sort_error(tag, __VA_ARGS__)
#define sort_w(tag, ...)     sort_warn(tag, __VA_ARGS__)
#define sort_i(tag, ...)     sort_info(tag, __VA_ARGS__)
#define sort_d(tag, ...)     sort_debug(tag, __VA_ARGS__)
#define sort_v(tag, ...)     sort_verbose(tag, __VA_ARGS__)

/**
* log API short definition
* NOTE: The `LOG_TAG` and `LOG_LVL` must defined before including the <sort.h> when you want to use log_x API.
*/

#ifndef SORT_OUTPUT_ENABLE_CLOSE   //close all log
#ifndef SORT_OUTPUT_ENABLE    //switch whether open log
#define log_a(...)       ((void)0)
#define log_e(...)       ((void)0)
#define log_w(...)       ((void)0)
#define log_i(...)       ((void)0)
#define log_d(...)       ((void)0)
#define log_v(...)       ((void)0)

#else

#if !defined(LOG_TAG)
#define LOG_TAG          "N_TAG"              //default NO_TAG
#endif
#if !defined(LOG_LVL)
#define LOG_LVL          SORT_LVL_VERBOSE      //default SORT_LVL_VERBOSE
#endif
#if LOG_LVL >= SORT_LVL_ASSERT
#define log_a(...)       sort_a(LOG_TAG, __VA_ARGS__)
#else
#define log_a(...)       ((void)0);
#endif
#if LOG_LVL >= SORT_LVL_ERROR
#define log_e(...)       sort_e(LOG_TAG, __VA_ARGS__)
#else
#define log_e(...)       ((void)0);
#endif
#if LOG_LVL >= SORT_LVL_WARN
#define log_w(...)       sort_w(LOG_TAG, __VA_ARGS__)
#else
#define log_w(...)       ((void)0);
#endif
#if LOG_LVL >= SORT_LVL_INFO
#define log_i(...)       sort_i(LOG_TAG, __VA_ARGS__)
#else
#define log_i(...)       ((void)0);
#endif
#if LOG_LVL >= SORT_LVL_DEBUG
#define log_d(...)       sort_d(LOG_TAG, __VA_ARGS__)
#else
#define log_d(...)       ((void)0);
#endif
#if LOG_LVL >= SORT_LVL_VERBOSE
#define log_v(...)       sort_v(LOG_TAG, __VA_ARGS__)
#else
#define log_v(...)       ((void)0);
#endif

#endif

#if defined (__cplusplus)
}
#endif /* defined (__cplusplus) */

#endif
原文地址:https://www.cnblogs.com/CristL/p/14988721.html