How to log time in Linux? (C Programming)

 

#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

/* time, gmtime, asctime */
static void log_time()
{
        struct tm *current_time = NULL;
        char *time_string = NULL;
        time_t t = time(NULL);
        current_time = gmtime(&t); /* the return value of gmtime is statically allocated, no need for free */
        assert(current_time != NULL);
        time_string = asctime(current_time);
        assert(time_string != NULL);
        printf("%s\tsome event here ...\n", time_string);
}

int main(int argc, char *argv[])
{
        for (;;)
        {
                log_time();
                sleep(10);
        }
        exit(EXIT_SUCCESS);
}

 

root@localhost :/home/James/mypro/Linux-Pro/daemon# ./log_time
Wed Jun 13 06:27:48 2012
        some event here ...
Wed Jun 13 06:27:58 2012
        some event here ...
Wed Jun 13 06:28:08 2012
        some event here ...

 

 

 

 

 

 

 

你可能感兴趣的:(How to log time in Linux? (C Programming))