#include <sys/time.h>
#include <iostream>
#include <cstdio>
#include <cstring>
static constexpr uint32_t YEAR_MONTH_DAY_LEN = 28U;
void PrintTimeStampByNow()
{
struct tm tm;
struct timeval tv = {0};
gettimeofday(&tv, nullptr);
localtime_r(&tv.tv_sec, &tm);
char buf[100] = {0};
const size_t ret = strftime(buf, YEAR_MONTH_DAY_LEN, "%Y-%m-%d-%H:%M:%S", &tm); //19
sprintf(buf + ret, ".%-3.3d.%-3.3d", (int32_t)(tv.tv_usec / 1000), (int32_t)(tv.tv_usec % 1000));
printf("%s", buf);
}
#define PRINT(fmt, ...) \
do { \
PrintTimeStampByNow();\
printf(" [%s:%d] ", __FILE__, __LINE__);\
printf(fmt, ##__VA_ARGS__); \
} while (false)
int32_t main(int32_t argc, char** argv)
{
PRINT("%s\n", "hello");
return 0;
}
带时戳的日志宏
于 2022-11-11 20:29:25 首次发布