C Snippet

2019年5月10日 · 106 字 · 1 分钟

C 代码片段罗列,方便查找

#include <time.h>
#include <string.h>

#define LOG(format, ...) \
do { \
    char demo_log_ts[64] = ""; \
    char demo_log_src[256] = ""; \
    char *ptr;\
    struct tm demotmresult; \
    time_t demoltime = time(NULL); \
    localtime_r(&demoltime, &demotmresult); \
    strftime(demo_log_ts, sizeof(demo_log_ts), \
    "%Y-%m-%d %X ", &demotmresult); \
    snprintf(demo_log_src, sizeof(demo_log_src), \
    "[%s:%d", __FILE__, __LINE__); \
    ptr = strrchr(demo_log_src, '/'); \
    char fun[256]={0}; \
    strcpy(fun, __FUNCTION__); \
    char *f = strtok(fun,":"); \
    while(f){ \
        if(sizeof(f) > 0 && !strchr(f, '[')) break; \
        f=strtok(NULL,":"); \
    } \
    printf("%s%s[%s:%s] " format "\n", demo_log_ts, "[DEBUG] ", ptr+1, f, ##__VA_ARGS__); \
} while (0)