Last active
November 29, 2022 02:22
-
-
Save spiritedRunning/6aadc355b114458ac1873b9f2987c072 to your computer and use it in GitHub Desktop.
print timestamp in log using C/C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define PRINT(tag, fmt, ...) printf("%s %s " fmt"", get_cur_time(), tag, ##__VA_ARGS__) | |
inline char* get_cur_time() { | |
static char s[32] = {0}; | |
time_t t; | |
struct tm* ltime; | |
struct timeval stamp; | |
gettimeofday(&stamp, NULL); | |
ltime = localtime(&stamp.tv_sec); | |
s[0] = '['; | |
strftime(&s[1], 20, "%Y-%m-%d %H:%M:%S", ltime); | |
sprintf(&s[strlen(s)], ".%03ld]", stamp.tv_usec / 1000); | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment