Creating a log on daily basis

Discussion in 'C Programming' started by abhi147@gmail.com, Sep 8, 2006.

  1. Guest

    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 !
     
    , Sep 8, 2006
    #1
    1. Advertising

  2. mdler Guest

    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

    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 !
     
    mdler, Sep 8, 2006
    #2
    1. Advertising

  3. Guest

    mdler wrote:
    > 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
    >
    > 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 :- )
     
    , Sep 11, 2006
    #3
  4. On 8 Sep 2006 02:37:20 -0700, "mdler" <>
    wrote:
    (topposting fixed)
    > schreef:


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


    > 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
     
    Dave Thompson, Sep 21, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NoSf3RaTu
    Replies:
    0
    Views:
    483
    NoSf3RaTu
    Jan 21, 2004
  2. =?Utf-8?B?VGVycnk=?=

    Changing styles sheets on a per user basis

    =?Utf-8?B?VGVycnk=?=, Feb 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    370
    =?Utf-8?B?VGVycnk=?=
    Feb 4, 2004
  3. Steve Franks
    Replies:
    2
    Views:
    1,258
    Steve Franks
    Jun 10, 2004
  4. Replies:
    14
    Views:
    539
    bijeshn
    Apr 8, 2008
  5. Replies:
    4
    Views:
    182
Loading...

Share This Page