LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2005, 09:59 AM   #1
ttumelty
LQ Newbie
 
Registered: Apr 2004
Distribution: Slackware & Gentoo & Ubuntu
Posts: 22

Rep: Reputation: 15
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
 
Old 12-13-2005, 11:06 AM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
try http://this. Third one down seems to be good.
 
Old 12-13-2005, 12:30 PM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a log file to post to corfrm all components for dvd plaback cjae SUSE / openSUSE 1 07-12-2005 05:36 PM
Creating the log-file dr_sad Programming 5 04-12-2005 05:01 PM
Creating a Log file nguye103 Linux - Newbie 1 10-21-2004 06:16 PM
Creating a log file in unix?? linux_dejan Programming 3 09-03-2004 10:56 PM
Cron - not creating log file t3___ Linux - Newbie 3 02-26-2004 06:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration