Skip to content

Instantly share code, notes, and snippets.

@spiritedRunning
Last active November 29, 2022 02:22
Show Gist options
  • Save spiritedRunning/6aadc355b114458ac1873b9f2987c072 to your computer and use it in GitHub Desktop.
Save spiritedRunning/6aadc355b114458ac1873b9f2987c072 to your computer and use it in GitHub Desktop.
print timestamp in log using C/C++
#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