CGI/Perl Guide | Learning Center | Forums | Advertise | Login Site Search: in Perl Guide PerlGuru Forums Learning Ctr

Home: Perl Programming Help: Beginner:
Opening a log file that is in use by another program

chaotic
Novice

May 16, 2008, 5:02 PM

Post #1 of 2 (242 views)
 Opening a log file that is in use by another program Can't Post
Hello,

I'm trying to write a script that will analyze the content of a log file (not apache or anything that's available with many google searches). It cannot use tie::file or tail, and I'm running this with Activestate on Win32.

If I run the script below when the application that generates the log entries is NOT running, everything works fine. When the application is running, the script will only parse up until the time I start the script.

For example: The program has added 100 entries to the log. Then I start the script. It gets to line 100, then continues in the while loop, not reading any lines. Any new lines added by the program are not seen.

I realize that the problem may be with the $linecount portion, but I am unaware of any way to fix this. This is a very small log (rarely over 1MB), so I don't care about any unnecessary memory utilization. Any help is greatly appreciated.  Code $logfile = 'C:\path\to\log\file.txt'; #replace this with the file location.  This is case sensitive. $batchfile = 'C:\path\to\batch\file.bat' #Replace this with the location of the batch file. This is case sensitive.$found = 0; $linecount = 0; while ($found == 0) { 	$pid = open (LOGFILE, "<$logfile"); 	@loglines = <LOGFILE>; 	linecheck (@loglines[$linecount]);$linecount++; 	close (LOGFILE); 	sleep (20); }  if ($found == 1) { sleep (120); unlink$logfile; }  sub linecheck { #Replace the text between // with the string to be matched.  if (@_[0] =~ /some string I'm searching for/i) {   	system ($batchfile);$found = 1; 	} }

KevinR
Veteran

May 18, 2008, 8:03 AM

Post #2 of 2 (223 views)
 Re: [chaotic] Opening a log file that is in use by another program [In reply to] Can't Post
Not sure if it will help you are not, but look into the seek() function.
-------------------------------------------------

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0