C语言为printf加上时间戳

#define PRINT(fmt, ...) printf("%s   "fmt, get_cur_time(), ##__VA_ARGS__)
#define ERROR(fmt, ...) printf("%s   "fmt" :%s\n", get_cur_time(), ##__VA_ARGS__, strerror(errno))

char *get_cur_time()
{
    static char s[20];
    time_t t;
    struct tm* ltime;


    time(&t);


    ltime = localtime(&t);


    strftime(s, 20, "%Y-%m-%d %H:%M:%S", ltime);


    return s;
}

你可能感兴趣的:(C)