Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Log Files for CGI Submits

by Anonymous Monk
on Jul 23, 2008 at 19:44 UTC ( [id://699676]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone, I am trying to create a CGI program using Perl that creates a log file with a timestamp as the file name. I have seen the example on how to get it down to the second, however, this will be a fairly busy website and I am afraid of submits happening at the same time. Is there a way that if a file is submitted from two different users at 2007 07 23 14 41 00 that it would create 20070723144100a and 20070723144100b? I hope this makes sense. Thank you for the help in advance! This is the code I have now
use Time::HiRes; # grab the current time my @now = localtime(); # rearrange the following to suit your stamping needs. # it currently generates YYYYMMDDhhmmss my $timeStamp = sprintf("%04d%02d%02d%02d%02d%02d", $now[5]+1900, $now[4]+1, $now[3], $now[2], $now[1], $now[0]); # insert stamp into constant portion of file name. # the constant portion of the name could be included # in the sprintf() above. my $fileName = "simplebr_$timeStamp.txt"; print "$fileName";

Replies are listed 'Best First'.
Re: Log Files for CGI Submits
by AltBlue (Chaplain) on Jul 23, 2008 at 21:06 UTC
    You could be lazy and (ab)use File::Temp:
    #!/usr/bin/perl use strict; use warnings; use POSIX (); use File::Temp (); my $log = File::Temp->new( UNLINK => 0, DIR => '/the/path/to/your/log/folder/', # isn't this too fancy?! you don't need "nice" file names* ;-) TEMPLATE => POSIX::strftime( 'simplebr_%Y%m%d%H%M%S_XXXX', localti +me ), SUFFIX => '.txt', ); print $log 'foo bar'; close $log;
    * I guess your "logs" folder is in fact some kind of (very) poor man's queue. If this is correct, you don't want to *sort* those files on a file name basis because it would be too slow, better use whatever your underlying OS offers you for watching a folder (e.g. Sys::Gamin, Linux::Inotify, SGI::FAM, Win32::ChangeNotify). Anyway, you'll have to take some solid countermeasures in order to avoid trashing your disk. ;-)
Re: Log Files for CGI Submits
by pjotrik (Friar) on Jul 23, 2008 at 20:02 UTC

    Instead of open, you might try sysopen with the O_EXCL flag and retry if opening fails. That would only work on some operating and file systems.

    Another common solution is to include pid ($$) in the filename. That should work for CGI, but you might get into trouble with e.g. mod_perl.

    A complete solution would probably require some kind of locking to enclose the file creation into a critical section

Re: Log Files for CGI Submits
by olus (Curate) on Jul 23, 2008 at 20:33 UTC

    Maybe you could work with environment variables like REMOTE_ADDR that gives you the IP of the client (or some proxy). It will give you some more granularity but not solve your problem entirely. Or maybe some session identifiers?

    One thing puzzles me though. From what you say, there will quite a few files hanging on the same dir. Have you thought of how you will manage them?

Re: Log Files for CGI Submits
by igelkott (Priest) on Jul 26, 2008 at 20:04 UTC

    If you expect the site to be that busy, creating many, many log files may not be practical.

    You night consider using a database instead. Timestamps are easy and even guaranteed unique id's are available through auto_increment or a sequence trigger (depending on the DB engine). Logged data can then be retrieved through simple select statements and you could even generate text files (logs) if really needed.

Re: Log Files for CGI Submits
by Lawliet (Curate) on Jul 23, 2008 at 20:00 UTC

    Perhaps check if the file exists, and if so, do whatever to that file's name.

    if (-e simplebr_$timeStampe.txt) { ... #Rename old file and name new file here }

    Update: For reasons stated below, this is not a solution.

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>

      Nice race condition you've created there.

      Update: Added wp link for the benefit of those unfamiliar with the term.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://699676]
Approved by friedo
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-07-23 03:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.