Skip to content

Instantly share code, notes, and snippets.

@mokumus
Created June 17, 2020 16:05
Show Gist options
  • Save mokumus/bdd9d4fa837345f01b35e0cd03d67f35 to your computer and use it in GitHub Desktop.
Save mokumus/bdd9d4fa837345f01b35e0cd03d67f35 to your computer and use it in GitHub Desktop.
C printf macro to add timestamp at each print
#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