-
-
Save mandre00/01d21fb5dce4baae1ad3e9822b9ff3b8 to your computer and use it in GitHub Desktop.
C printf macro to add timestamp at each print
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
#include <stdio.h> | |
#include <time.h> | |
#include <string.h> | |
char * timestamp(); | |
#define print_log(f_, ...) printf("%s ", timestamp()), printf((f_), ##__VA_ARGS__), printf("\n") | |
int main(int argc, char* argv[]) { | |
print_log("Hello"); | |
print_log("%s%d","mokumus",1996); | |
return 0; | |
} | |
char * timestamp(){ | |
time_t now = time(NULL); | |
char * time = asctime(gmtime(&now)); | |
time[strlen(time)-1] = '\0'; // Remove \n | |
return time; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment