How to log time in C/C++


 
Thread Tools Search this Thread
Top Forums Programming How to log time in C/C++
# 1  
Old 04-28-2009
How to log time in C/C++

Hi,

Actually I want to create a log file in c, which should log the details in to log file.
Here am facing one problem..like if I am doing loging am not able to log the exact time..Its printing the same time..Can anybody help me out on this..??

Sample code:
===========
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
struct tm *ptr;
time_t lt;

FILE * pFile = fopen("myfile.txt","a");
int main ()
{
int n;
char name [100];
lt = time(NULL);

fprintf(pFile,"\n==========starting===========\n");
for (n=0 ; n<2 ; n++)
{
puts ("please, enter a name: ");
gets (name);
printf("%s %d",__FILE__,__LINE__);
fprintf (pFile,"\nName %d [%-10.10s] : %s : Line %d: %s\n",n,name,__FILE__,__LINE__,asctime(localtime(&lt)));
}
fprintf(pFile,"Hello : %s : Line %d: %s\n",__FILE__,__LINE__,asctime(localtime(&lt)));
fprintf(pFile,"Error : %s : Line %d: %s\n",__FILE__,__LINE__,asctime(localtime(&lt)));

fprintf(pFile,"=======COMPLETED========");
return 0;
}

Here I could print the log time..but always its same..I need it in a sequence order..
Could you please help me out on this..?

Thanks in advance..!
# 2  
Old 04-28-2009
This line actually gets the time:
Code:
lt = time(NULL);

You need to keep updating it.
# 3  
Old 04-29-2009
Quote:
Originally Posted by Corona688
This line actually gets the time:
Code:
lt = time(NULL);

You need to keep updating it.

Could you please explain it..?

What I have to give ther?
# 4  
Old 04-29-2009
You are reading the current time into the variable lt only once. Never again afterwards. That way it will never change during the runtime of your program.
# 5  
Old 04-29-2009
Quote:
Originally Posted by Kattoor
Could you please explain it..?
Time changes over... well... time. Wait 10 minutes and lt becomes 10 minutes out of date.
Quote:
What do I have to give there?
The exact same thing.
# 6  
Old 04-30-2009
Quote:
Originally Posted by Kattoor
Hi,

Actually I want to create a log file in c, which should log the details in to log file.
Here am facing one problem..like if I am doing loging am not able to log the exact time..Its printing the same time..Can anybody help me out on this..??

Sample code:
===========
#include <stdio.h>
#include <time.h>
#include<stdlib.h>
struct tm *ptr;
time_t lt;

FILE * pFile = fopen("myfile.txt","a");
int main ()
{
int n;
char name [100];
lt = time(NULL);

fprintf(pFile,"\n==========starting===========\n");
for (n=0 ; n<2 ; n++)
{
puts ("please, enter a name: ");
gets (name);
printf("%s %d",__FILE__,__LINE__);
fprintf (pFile,"\nName %d [%-10.10s] : %s : Line %d: %s\n",n,name,__FILE__,__LINE__,asctime(localtime(&lt)));
}
fprintf(pFile,"Hello : %s : Line %d: %s\n",__FILE__,__LINE__,asctime(localtime(&lt)));
fprintf(pFile,"Error : %s : Line %d: %s\n",__FILE__,__LINE__,asctime(localtime(&lt)));

fprintf(pFile,"=======COMPLETED========");
return 0;
}

Here I could print the log time..but always its same..I need it in a sequence order..
Could you please help me out on this..?

Thanks in advance..!

I tried with multiple initialization of lt = time(NULL);

But the time varies only once...Afterwards again its printing as constant time...

Can anybody help me out..?

Thanks in advance...!
# 7  
Old 04-30-2009
Where did you put the additional invocations? How do you call your program?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Log all the commands input by user at real time in /var/log/messages

Below is my script to log all the command input by any user to /var/log/messages. But I cant achieve the desired output that i want. PLease see below. function log2syslog { declare COMMAND COMMAND=$(fc -ln -0) logger -p local1.notice -t bash -i -- "$USER:$COMMAND" } trap... (12 Replies)
Discussion started by: invinzin21
12 Replies

2. Shell Programming and Scripting

Log search and mail it if the log is updated before 24 hours from the current time

Hi , We have around 22 logs , each has different entries. I have to automate this using shell script. The ideas which am sharing is given below 1) We use only TAIL -100 <location and name of the log> Command to check the logs. 2) We want to check whether the log was updated before 24... (13 Replies)
Discussion started by: Kalaihari
13 Replies

3. Solaris

logadm rotates log every time

I'm running logadm manually to test and it seems to be rotating my /var/log/oracle/oracle_audit.log file every single time it's ran instead of rotating once it gets passed 10 gigs, any ideas? Here's the logadm.conf for reference. (0 Replies)
Discussion started by: thmnetwork
0 Replies

4. Shell Programming and Scripting

Unix Log out time

Hi, Is there a way to find out the logout date for a user who has been logged in to the system for more than 1 day. For example, the below user as indicated by the "last" command had logged into the system on Mar 8 and he has been online for 2 days and 6:53 hour:min. So basically, he logged out... (2 Replies)
Discussion started by: devtakh
2 Replies

5. Shell Programming and Scripting

Specified log in time for users

I have this task : Check the logintime.txt every minute to only allow user to log in at the specified time. logintime.txt has the following content: USER TIME_START TIME_STOP Example: john 17:00 18:00 My idea is locking the user at the TIME_STOP and unlocking at the TIME_START while... (4 Replies)
Discussion started by: muffle
4 Replies

6. Shell Programming and Scripting

How can view log messages between two time frame from /var/log/message or any type of log files

How can view log messages between two time frame from /var/log/message or any type of log files. when logfiles are very big and especially many messages with in few minutes, I would like to display log messages between 5 minute interval. Could you pls give me the command? (1 Reply)
Discussion started by: johnveslin
1 Replies

7. Shell Programming and Scripting

Monitoring a users log in time?

how do i start with this guys? Sample run: $ LOGTIME it2015678 <enter> User it2015678 is CRUZ Tommy H And has logged on to the system for: 8 hours 12 minutes from the time this script was run. (1 Reply)
Discussion started by: skypigeon
1 Replies

8. Shell Programming and Scripting

Calculate response time from log

I have a running log file (jboss_server.log) which rotates at midnight . I need to constantly check and calculate the time for each thread and alert if it doesnt complete within 60 minute. For example my log file has following printed . I want to run a script in cron every 30 minutes and... (2 Replies)
Discussion started by: gubbu
2 Replies

9. Shell Programming and Scripting

Processing a log file based on date/time input and the date/time on the log file

Hi, I'm trying to accomplish the following and would like some suggestions or possible bash script examples that may work I have a directory that has a list of log files that's periodically dumped from a script that is crontab that are rotated 4 generations. There will be a time stamp that is... (4 Replies)
Discussion started by: primp
4 Replies

10. Shell Programming and Scripting

Date and time log file

Hi, I wrote a small perl script in unix that searches in a file and saves some information in a separate file. Since this is a log file, I would like to have the date added to file name. I have no idea where to start. output: log_010907.txt thanks ken (8 Replies)
Discussion started by: captoro
8 Replies
Login or Register to Ask a Question