PERL Logger

March 1, 2010


Tech Tips, March 2010

A Perl of a logger

When it comes to logging a station's stream, there are many products available with a variety of features. But if you're handy with writing scripts, you can create your own. Rob Landry, a contract engineer in Boston, created such a script for one of his clients.

His Perl script records each hour as an MP3 file by capturing the station's Internet stream. Each hour is labeled and placed in a directory for each day (e.g. "wed_dec_16_2009").

#!/usr/bin/perl # ==> AIRCHECK SCRIPT sub log { my $a = shift; my $time = localtime(time); if (open (LOG,">>$LOG")) { print (LOG "$time: $a\n"); close LOG; } } sub purge { my $a = ''; my @stat = (); my $ptime = time - 86400 * $RETAIN; if (opendir (ROOT,$ROOT)) { while ($a = readdir ROOT) { @stat = stat("$ROOT/$a"); if ($stat[9] < $ptime) { if (system("rm -rf $ROOT/$a")) { &log("FAILED: rm -rf $ROOT/$a"); } else { &log("$ROOT/$a purged"); } } } closedir ROOT; } } # ==> MAIN PROGRAM @MON = ('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'); @DAY = ('sun','mon','tue','wed','thu','fri','sat','sun'); @HOUR = ('12mid','1am','2am','3am','4am','5am','6am','7am','8am','9am','10am','11am','12noon','1pm','2pm','3pm','4pm','5pm','6pm','7pm','8pm','9pm','10pm','11pm'); $ROOT = '/var/aircheck'; $RETAIN = 60; $LOG = '/var/aircheck/aircheck.log'; $PERIOD = 3600; # 1 hour $URL = "http://192.168.0.101:8001/xstream"; if ($a = fork) { $count = 0; while ($count < $PERIOD) { $count++; sleep 1; } kill 9,$a; wait(); print "exiting\n"; } else { &purge; @time = localtime(time); $wday = $DAY[$time[6]]; $year = 1900 + $time[5]; $mon = $MON[$time[4]]; $day = $time[3]; $hour = $HOUR[$time[2]]; $dir = "$ROOT/$wday\_$mon\_$day\_$year"; if (! (-d $dir)) {system("mkdir $dir")} exec("wget $URL -O $dir/$hour.mp3"); }

What custom scripts have you developed to handle routine tasks? Share them with us to be posted online.


We need your tips!

Ideas submitted to Tech Tips may be suitable to earn SBE recertification credits.

Do you have a tech tip? Send it to us at radio@RadioMagOnline.com



Receive regular news and technology updates. Sign up for our free newsletter here.

Comments