saving old versions of file

Discussion in 'Perl Misc' started by Jarmo, Feb 16, 2010.

  1. Jarmo

    Jarmo Guest

    I have a problem and I was hoping someone could help me. I have a program
    that every time it runs it saves a log file with same name about the changes
    it did on that particular run. Result is that old file gets overwritten and
    lost. I would like to create "virtual file" so that every time foobar.log is
    written I actually end up with a file that has date and time added to it.

    In other words:
    I run application that writes to "foobar.log" and I want the file actually
    go to "~/foobarlogs/foobarYYYY-MM-DD-HH-MM-SS.log" instead.

    I know I did something similar years ago with perl but my
    programming/scripting skills are too rusty to accomplish it anymore on my
    own. I would greatly appreciate the help.

    Jarmo
     
    Jarmo, Feb 16, 2010
    #1
    1. Advertising

  2. On 16/02/2010 15:29, Jarmo wrote:
    > I have a problem and I was hoping someone could help me. I have a program
    > that every time it runs it saves a log file with same name about the changes
    > it did on that particular run. Result is that old file gets overwritten and
    > lost. I would like to create "virtual file" so that every time foobar.log is
    > written I actually end up with a file that has date and time added to it.
    >
    > In other words:
    > I run application that writes to "foobar.log" and I want the file actually
    > go to "~/foobarlogs/foobarYYYY-MM-DD-HH-MM-SS.log" instead.
    >
    > I know I did something similar years ago with perl but my
    > programming/scripting skills are too rusty to accomplish it anymore on my
    > own. I would greatly appreciate the help.


    The obvious (to me) answer is not to run the program directly but run it
    from a script. The script can rename the logfile after the program exits.

    For a Perl solution, the following might be a useful starting point.
    perldoc -f system
    perldoc -f localtime
    perldoc -f rename

    You could also do this quite easily in any of the popular Unix shells.
    man date
     
    RedGrittyBrick, Feb 16, 2010
    #2
    1. Advertising

  3. Jarmo

    ccc31807 Guest

    On Feb 16, 10:29 am, Jarmo <> wrote:
    > lost. I would like to create "virtual file" so that every time foobar.logis
    > written I actually end up with a file that has date and time added to it.


    #make log file name with time string
    my ($s, $m, $h, $d, $m, $y, @r) = localtime();
    my $timestring = sprintf ("%d-%02d-%02d-%02d-%02d-%02s", $y + 1900, $m
    + 1, $d, $h, $m, $s);
    my $logfile = "log_${timestring}.txt";
    #open log file
    open LOG, '>', $logfile;
    #write to log
    close LOG;

    Better, in my opinion, is to use the append operator ('>>') and write
    to the same file daily, but this depends on the quantity of logging
    you do.

    CC
     
    ccc31807, Feb 16, 2010
    #3
  4. Jarmo

    Uri Guttman Guest

    >>>>> "c" == ccc31807 <> writes:

    c> On Feb 16, 10:29 am, Jarmo <> wrote:
    >> lost. I would like to create "virtual file" so that every time foobar.log is
    >> written I actually end up with a file that has date and time added to it.


    c> #make log file name with time string
    c> my ($s, $m, $h, $d, $m, $y, @r) = localtime();
    c> my $timestring = sprintf ("%d-%02d-%02d-%02d-%02d-%02s", $y + 1900, $m
    c> + 1, $d, $h, $m, $s);

    GACK!! please use POSIX::strftime. i hate seeing hand made date
    formatting. all you need is in that one sub and with less cruft and
    noise and chance for errors.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Feb 16, 2010
    #4
  5. Jarmo <> wrote:
    >I have a problem and I was hoping someone could help me. I have a program
    >that every time it runs it saves a log file with same name about the changes
    >it did on that particular run. Result is that old file gets overwritten and
    >lost. I would like to create "virtual file" so that every time foobar.log is
    >written I actually end up with a file that has date and time added to it.
    >
    >In other words:
    >I run application that writes to "foobar.log" and I want the file actually
    >go to "~/foobarlogs/foobarYYYY-MM-DD-HH-MM-SS.log" instead.


    Not a Perl solution but what about running a cron job at midnight which
    does a
    ln -s foobar.log foobar[whateverthenewdayis]

    jue
     
    Jürgen Exner, Feb 16, 2010
    #5
  6. Jarmo

    sreservoir Guest

    On 2/16/2010 10:29 AM, Jarmo wrote:
    > I have a problem and I was hoping someone could help me. I have a program
    > that every time it runs it saves a log file with same name about the changes
    > it did on that particular run. Result is that old file gets overwritten and
    > lost. I would like to create "virtual file" so that every time foobar.log is
    > written I actually end up with a file that has date and time added to it.
    >
    > In other words:
    > I run application that writes to "foobar.log" and I want the file actually
    > go to "~/foobarlogs/foobarYYYY-MM-DD-HH-MM-SS.log" instead.
    >
    > I know I did something similar years ago with perl but my
    > programming/scripting skills are too rusty to accomplish it anymore on my
    > own. I would greatly appreciate the help.


    strftime("~/foobarlogs/foobar%Y-%m-%d-%H-%M-%S.log", gmtime);

    though I doubt what you want is a directory named ~.

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
     
    sreservoir, Feb 16, 2010
    #6
  7. Jarmo

    Frank Seitz Guest

    Jürgen Exner wrote:
    > Jarmo <> wrote:
    >> I have a problem and I was hoping someone could help me. I have a program
    >> that every time it runs it saves a log file with same name about the changes
    >> it did on that particular run. Result is that old file gets overwritten and
    >> lost. I would like to create "virtual file" so that every time foobar.log is
    >> written I actually end up with a file that has date and time added to it.
    >>
    >> In other words:
    >> I run application that writes to "foobar.log" and I want the file actually
    >> go to "~/foobarlogs/foobarYYYY-MM-DD-HH-MM-SS.log" instead.

    >
    > Not a Perl solution but what about running a cron job at midnight which
    > does a
    > ln -s foobar.log foobar[whateverthenewdayis]


    A symlink is obviously not a solution, because the file
    gets overwritten with every run.

    Frank
    --
    Dipl.-Inform. Frank Seitz
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

    Blog: http://www.fseitz.de/blog
    XING-Profil: http://www.xing.com/profile/Frank_Seitz2
     
    Frank Seitz, Feb 16, 2010
    #7
  8. On Tue, 16 Feb 2010, Frank Seitz wrote:

    > > ln -s foobar.log foobar[whateverthenewdayis]

    >
    > A symlink is obviously not a solution, because the file
    > gets overwritten with every run.


    The other way would work:

    Every night:
    rm foobar.log
    ln -s foobar[whateverthenewdayis] foobar.log

    --
    Helmut Richter
     
    Helmut Richter, Feb 16, 2010
    #8
  9. Jarmo

    ccc31807 Guest

    On Feb 16, 1:27 pm, "Uri Guttman" <> wrote:
    >
    > GACK!! please use POSIX::strftime. i hate seeing hand made date
    > formatting. all you need is in that one sub and with less cruft and
    > noise and chance for errors.


    I have to create these kinds of file names and directory names in just
    about every script I write, so I wrote a utility library to make it
    easy. It's not any cruft, at least not in the executable script, but I
    wasn't aware that POSIX::strftime existed. I'll try it and see if it's
    any easier. Thanks for the pointer.

    CC.
     
    ccc31807, Feb 16, 2010
    #9
  10. Jarmo

    Uri Guttman Guest

    >>>>> "c" == ccc31807 <> writes:

    c> On Feb 16, 1:27 pm, "Uri Guttman" <> wrote:
    >>
    >> GACK!! please use POSIX::strftime. i hate seeing hand made date
    >> formatting. all you need is in that one sub and with less cruft and
    >> noise and chance for errors.


    c> I have to create these kinds of file names and directory names in just
    c> about every script I write, so I wrote a utility library to make it
    c> easy. It's not any cruft, at least not in the executable script, but I
    c> wasn't aware that POSIX::strftime existed. I'll try it and see if it's
    c> any easier. Thanks for the pointer.

    it has to be easier as it already supports all the common date/time
    formats and parts you want. it requires no temp variables, no need to
    mung the values (+1900, etc), and it is stable. you can change the
    format easily unlike a hand written version.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Feb 16, 2010
    #10
  11. Jarmo

    Jarmo Guest

    sreservoir <> wrote:

    > strftime("~/foobarlogs/foobar%Y-%m-%d-%H-%M-%S.log", gmtime);


    That is a nice and simple way to get the date and time compared to monster
    I was working on...

    > though I doubt what you want is a directory named ~.


    No, I don't. Not sure how that got there in the first place as ~ isn't even
    close to " or / on my keyboard.

    --
    Jampe
    ----------------------------------------------------------------
    Things are getting worse. Please send chocolate.
    ----------------------------------------------------------------
     
    Jarmo, Feb 16, 2010
    #11
  12. Jarmo

    Frank Seitz Guest

    Helmut Richter wrote:
    > On Tue, 16 Feb 2010, Frank Seitz wrote:


    >>> ln -s foobar.log foobar[whateverthenewdayis]

    >>
    >> A symlink is obviously not a solution, because the file
    >> gets overwritten with every run.

    >
    > The other way would work:
    >
    > Every night:
    > rm foobar.log
    > ln -s foobar[whateverthenewdayis] foobar.log


    No, but a hardlink (ln without -s) or cp would do it.

    Frank
    --
    Dipl.-Inform. Frank Seitz
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

    Blog: http://www.fseitz.de/blog
    XING-Profil: http://www.xing.com/profile/Frank_Seitz2
     
    Frank Seitz, Feb 16, 2010
    #12
  13. On 2010-02-16 19:56, Jarmo <> wrote:
    > sreservoir <> wrote:
    >
    >> strftime("~/foobarlogs/foobar%Y-%m-%d-%H-%M-%S.log", gmtime);

    >
    > That is a nice and simple way to get the date and time compared to monster
    > I was working on...
    >
    >> though I doubt what you want is a directory named ~.

    >
    > No, I don't. Not sure how that got there in the first place as ~ isn't even
    > close to " or / on my keyboard.


    ~ is an abbreviation for the home directory in most shells. So in Perl
    that would be:

    $ENV{HOME} . strftime("/foobarlogs/foobar%Y-%m-%d-%H-%M-%S.log", gmtime);

    hp
     
    Peter J. Holzer, Feb 16, 2010
    #13
  14. RedGrittyBrick <> wrote:

    > The obvious (to me) answer is not to run the program directly but run it
    > from a script. The script can rename the logfile after the program exits.


    I didn't consider that an option at first because I have several different
    people running the program at random times and I didn't think I could count
    on them remembering to start using a new command to start it. Now reading
    this I had an idea that I could rename the original executable and make the
    script with the original name. I think that is what I will do.

    --
    Jampe
    ----------------------------------------------------------------
    Things are getting worse. Please send chocolate.
    ----------------------------------------------------------------
     
    Jarmo Sillanpaa, Feb 16, 2010
    #14
  15. Thanks for answers. I went with a different implementation although I am
    still curious if it is possible to do what I was thinking about first.
    --
    Jampe
    ----------------------------------------------------------------
    Things are getting worse. Please send chocolate.
    ----------------------------------------------------------------
     
    Jarmo Sillanpaa, Feb 16, 2010
    #15
  16. On Tue, 16 Feb 2010, Frank Seitz wrote:

    > Helmut Richter wrote:
    > > On Tue, 16 Feb 2010, Frank Seitz wrote:

    >
    > >>> ln -s foobar.log foobar[whateverthenewdayis]
    > >>
    > >> A symlink is obviously not a solution, because the file
    > >> gets overwritten with every run.

    > >
    > > The other way would work:
    > >
    > > Every night:
    > > rm foobar.log
    > > ln -s foobar[whateverthenewdayis] foobar.log

    >
    > No, but a hardlink (ln without -s) or cp would do it.


    The softlink works: when the program tries to open foobar.log, it opens
    the linked-to file instead.

    The hardlink does not work because one cannot create a hardlink to a
    non-existent file. If hardlink, the sequence would have to be:

    touch foobar[whateverthenewdayis]
    rm foobar.log
    ln foobar[whateverthenewdayis] foobar.log

    --
    Helmut Richter
     
    Helmut Richter, Feb 16, 2010
    #16
  17. Jarmo

    Frank Seitz Guest

    Helmut Richter wrote:
    > On Tue, 16 Feb 2010, Frank Seitz wrote:
    >> Helmut Richter wrote:
    >>> On Tue, 16 Feb 2010, Frank Seitz wrote:
    >>>>> ln -s foobar.log foobar[whateverthenewdayis]
    >>>> A symlink is obviously not a solution, because the file
    >>>> gets overwritten with every run.
    >>> The other way would work:
    >>>
    >>> Every night:
    >>> rm foobar.log
    >>> ln -s foobar[whateverthenewdayis] foobar.log

    >> No, but a hardlink (ln without -s) or cp would do it.

    >
    > The softlink works: when the program tries to open foobar.log, it opens
    > the linked-to file instead.


    You are right.

    > The hardlink does not work because one cannot create a hardlink to a
    > non-existent file. If hardlink, the sequence would have to be:
    >
    > touch foobar[whateverthenewdayis]
    > rm foobar.log
    > ln foobar[whateverthenewdayis] foobar.log


    Yes. Maybe a mv would suffice.

    Frank
    --
    Dipl.-Inform. Frank Seitz
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

    Blog: http://www.fseitz.de/blog
    XING-Profil: http://www.xing.com/profile/Frank_Seitz2
     
    Frank Seitz, Feb 17, 2010
    #17
  18. Jarmo

    Ted Zlatanov Guest

    On Tue, 16 Feb 2010 10:29:27 -0500 Jarmo <> wrote:

    J> I have a problem and I was hoping someone could help me. I have a program
    J> that every time it runs it saves a log file with same name about the changes
    J> it did on that particular run. Result is that old file gets overwritten and
    J> lost. I would like to create "virtual file" so that every time foobar.log is
    J> written I actually end up with a file that has date and time added to it.

    J> In other words:
    J> I run application that writes to "foobar.log" and I want the file actually
    J> go to "~/foobarlogs/foobarYYYY-MM-DD-HH-MM-SS.log" instead.

    J> I know I did something similar years ago with perl but my
    J> programming/scripting skills are too rusty to accomplish it anymore on my
    J> own. I would greatly appreciate the help.

    You may want to just use the `logrotate' utility as part of your
    application's startup. Much easier than symlinks and custom code.

    Ted
     
    Ted Zlatanov, Feb 18, 2010
    #18
  19. Frank Seitz <> wrote:
    >Jürgen Exner wrote:
    >> Jarmo <> wrote:
    >>> I have a problem and I was hoping someone could help me. I have a program
    >>> that every time it runs it saves a log file with same name about the changes
    >>> it did on that particular run. Result is that old file gets overwritten and
    >>> lost.[...]

    >> Not a Perl solution but what about running a cron job at midnight which
    >> does a
    >> ln -s foobar.log foobar[whateverthenewdayis]

    >
    >A symlink is obviously not a solution, because the file
    >gets overwritten with every run.


    You are right, I misread the original question.

    If the application is really this misbehaved, then the OP has a whole
    slew of other problems, too, e.g. what happens if two instances of the
    application are running concurrently? Will they both write into the same
    log file, stepping on each other's feet and destroying whatever the
    other instance just wrote?

    I would strongly suggest to fix the application. Even embedding the call
    into a wrapper script which renames the log file immediately as
    suggested by others won't fix all problems.

    jue
     
    Jürgen Exner, Feb 18, 2010
    #19
    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. David Smith
    Replies:
    2
    Views:
    380
    Shane Petroff
    Jul 24, 2003
  2. Greg Schmidt
    Replies:
    10
    Views:
    671
    Toby Inkster
    Oct 21, 2004
  3. Replies:
    1
    Views:
    330
    Simon Watkins
    Mar 15, 2007
  4. Paul Brannan
    Replies:
    2
    Views:
    92
    Paul Brannan
    Jul 28, 2005
  5. Lee Hinman
    Replies:
    1
    Views:
    119
    Ryan Davis
    Jun 25, 2009
Loading...

Share This Page