saving old versions of file

J

Jarmo

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
 
R

RedGrittyBrick

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
 
C

ccc31807

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
 
U

Uri Guttman

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
 
J

Jürgen Exner

Jarmo said:
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
 
S

sreservoir

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 ~.
 
F

Frank Seitz

Jürgen Exner said:
Jarmo said:
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
 
H

Helmut Richter

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
 
C

ccc31807

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.
 
U

Uri Guttman

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
 
J

Jarmo

sreservoir said:
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.
 
F

Frank Seitz

Helmut said:
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
 
P

Peter J. Holzer

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


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
 
J

Jarmo Sillanpaa

RedGrittyBrick said:
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.
 
J

Jarmo Sillanpaa

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.
 
H

Helmut Richter

Helmut said:
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
 
F

Frank Seitz

Helmut said:
Helmut said:
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
 
T

Ted Zlatanov

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
 
J

Jürgen Exner

Frank Seitz said:
Jürgen Exner said:
Jarmo said:
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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top