Creating a log on daily basis

A

abhi147

Hi ,

I need to know that is it possible to create new log daily on
a running system ?

I am using these basic file write statements to print the logs :

char log_file[80];
FILE *fp_stdout;
FILE *fp_stderr;

sprintf(log_file, "%s.log", "sample");
fp_stdout = fopen(log_file, "a");

Thanks for your help !
 
M

mdler

Hello

t is posible, make something from the date in the logfilename

I use mostly yyyymmdd as subplement on the filename

long today(void)
{
char tijda[11];
time_t tijd;
struct tm *tijd2;

tijd = time(NULL);
tijd2 = localtime(&tijd);
tijd2->tm_mon++; /* heeft 0=jan tot 11=dec */

sprintf(tijda,"%04d%02d%02d",tijd2->tm_year+1900,tijd2->tm_mon,tijd2->tm_mday);
return (ULong)atol(tijda);
}

{
char log_file[80];
FILE *fp_stdout=NULL;
sprintf(log_file, "%s_%08d.log", "sample",today());
fp_stdout = fopen(log_file, "a");
}


now every day there is a new logfile name created

today it should be 'sample_20060908.log'

Greetings Olaf

(e-mail address removed) schreef:
 
A

abhi147

mdler said:
Hello

t is posible, make something from the date in the logfilename

I use mostly yyyymmdd as subplement on the filename

long today(void)
{
char tijda[11];
time_t tijd;
struct tm *tijd2;

tijd = time(NULL);
tijd2 = localtime(&tijd);
tijd2->tm_mon++; /* heeft 0=jan tot 11=dec */

sprintf(tijda,"%04d%02d%02d",tijd2->tm_year+1900,tijd2->tm_mon,tijd2->tm_mday);
return (ULong)atol(tijda);
}

{
char log_file[80];
FILE *fp_stdout=NULL;
sprintf(log_file, "%s_%08d.log", "sample",today());
fp_stdout = fopen(log_file, "a");
}


now every day there is a new logfile name created

today it should be 'sample_20060908.log'

Greetings Olaf

(e-mail address removed) schreef:
Hi ,

I need to know that is it possible to create new log daily on
a running system ?

I am using these basic file write statements to print the logs :

char log_file[80];
FILE *fp_stdout;
FILE *fp_stderr;

sprintf(log_file, "%s.log", "sample");
fp_stdout = fopen(log_file, "a");

Thanks for your help !


Thanks a lot :- )
 
D

Dave Thompson

On 8 Sep 2006 02:37:20 -0700, "mdler" <[email protected]>
wrote:
(topposting fixed)
(e-mail address removed) schreef:
t is posible, make something from the date in the logfilename

I use mostly yyyymmdd as subplement on the filename

long today(void)
{
char tijda[11];
time_t tijd;
struct tm *tijd2;

tijd = time(NULL);
tijd2 = localtime(&tijd);
tijd2->tm_mon++; /* heeft 0=jan tot 11=dec */

sprintf(tijda,"%04d%02d%02d",tijd2->tm_year+1900,tijd2->tm_mon,tijd2->tm_mday);
return (ULong)atol(tijda);

Or to avoid the useless conversions, just
return (long)(t->tm_year+1900)*10000L + t->mon*100 + t->tm_mday;
/* or t->tm_year*10000L + 19000000 + ...
}

{
char log_file[80];
FILE *fp_stdout=NULL;
sprintf(log_file, "%s_%08d.log", "sample",today());
fp_stdout = fopen(log_file, "a");
}


now every day there is a new logfile name created
Every time _this code is executed_ on a different day.

In a 'running system' or more specifically program, if you only open
the logfile at startup, it won't automatically switch. You must either
(close and) reopen it on _every_ output, which can be inefficient (but
also robust); or check on every output whether the date has changed
and reopen if so; or <not standard C> set yourself to receive some
kind of signal or alarm when the date changes and reopen then. </>


- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top