LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   creating a log file (https://www.linuxquestions.org/questions/programming-9/creating-a-log-file-392353/)

ttumelty 12-13-2005 09:59 AM

creating a log file
 
I am creating a log file for an application I am writing. The text file has each line representing a log message. Each line needs a time stamp so that we can see when that event happened. What would be the correct time related functions to use with g++ to get these time values ?

Thanks in advance,
Tom

sirclif 12-13-2005 11:06 AM

try http://this. Third one down seems to be good.

kshkid 12-13-2005 12:30 PM

hope this helps,

Code:

void logStatus(char *logMsg,...)
{
  char info[200];
  char eventTime[200];
  va_list alist;
  time_t  logtime;
  struct tm *now = NULL;

  memset(info, '\0', 200);
  memset(eventTime, '\0', 200);

  va_start(alist, logMsg);
  vsprintf(info, logMsg, alist);
  va_end(alist);

  time(&logtime);
  now=localtime(&logtime);

        fprintf(<your_logfile_pointer>, "%04d-%02d-%02d %02d:%02d:%02d %s\n",
        1900 + now->tm_year, now->tm_mon + 1, now->tm_mday,
        now->tm_hour, now->tm_min, now->tm_sec, info);
}

any part of the code to log a message in to the log file
can make a call to this function

log file would have the appropriate message with time stamp

the log file can be opened , logged with message and closed
upon each time a message can is logged

else can be opened only once and the file pointer can be made available as a global pointer


All times are GMT -5. The time now is 07:25 AM.