Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Problem with switching log files

by Sewi (Friar)
on Dec 27, 2011 at 20:58 UTC ( [id://945242]=note: print w/replies, xml ) Need Help??


in reply to Problem with switching log files

It's hard to find all possible problems without the complete source file, but let's go with the snippets you pasted:

First of all: Debugging. Never ever think that you know the value of a variable. If something goes wrong - add print or warn statements to your source and look at the values while the script is running. This is the same for Perl gurus and beginners. You already started that by your print "Change file\n";

Let's only look at $NEXT_TIME. It's not initialized, so it's initial numeric value is 0. This is less than time, so your script is going to change the file the first time it's going to that if at the first line of your pasted source. The file is switched (a new file is opened) and the next timestamp is set to $NEXT_TIME + 60. Remember, $NEXT_TIME was 0 and 0 + 60 = 60. Current time is 1325018738 and your requested next file is due at 60 (which should be something like 1970-01-01 00:01:00). $NEXT_TIME = time + 60 might be better, but it's less accurate because you likely won't reach this line as soon as $NEXT_TIME is reached: You'ld get "60 seconds after setting $NEXT_TIME to the new value" instead of "60 seconds after estimated start time of last file", but this should/might be good enough.

Add some debug statements to see the value of $LOG_FILE. It will be some kind of reference but the address (hexadecimal number) will show you if a file has been rotated. You should at least check the value right after your open call and just before writing into this filehandle.

You already added a "or die" error handler to your open call, so if the file isn't cycled and you don't get an error (where your $! should help you to get the right reason), I assume that your code never reaches the sub thread_log for the second time.

Your sub is called thread_log and I hope that doesn't mean that you're using threads. Don't do this for starting with Perl, threads have some hard-to-handle effects. Get your script running without threads and then add threads if you feel that you need them. If you just used this prefix without any threading stuff, ignore this :-).

If you could feed only very very very few packets into your logger, install Devel::Trace and start your script using perl -d:Trace my_packet_logger.pl. This will show you any Perl source line as it's processed, but will heavily slow down your script (because screen output is slow). You might also use the Perl debugger but it's not that easy to use for beginners.

There are some lines that I'ld write another way, but Perl is TIMTOWTDI and it's ok. There are many "bad style" things you didn't use, so your code quality is very good for a beginner (I think).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://945242]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found