basic Perl log function

I believe any good Perl utility should have a log to which pertinent information should be written. I’m not even going to go over the merits of having a log file, but all good scripts should have one.

Setup – Define the Function and Log Filename

Here’s the log function which I use in my Perl scripts. This could easily be exported to a log module. Yes, I realize there are probably a lot of good logging modules out there already, I’m just offering this for the lazy sods who don’t currently log anything in their Perl scripts, but realize that it’s a good idea to start.

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon  += 1; $wday += 1; $year += 1900;
my @months = qw { Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec };
unshift @months , '';


my $date = sprintf "%04d%03s%02d" , $year , $months[$mon] , $mday ;
my $logfile = "/tmp/$progname.$date.log";

open( LOGFILE , ">> $logfile" )
    or die "Can't open file '$logfile'. $!\n";
select((select(LOGFILE), $| = 1)[0]); # autoflush LOGFILE

sub logh() {
    my $msg = shift(@_);
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $mon  += 1; $wday += 1; $year += 1900;

    my @months = qw { Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec };
    my $dts;
    $dts = sprintf("%04d%3s%02d %02d:%02d:%02d",
                   $year,$months[$mon],$mday,$hour,$min,$sec);
    my $pid = $$;
    printf LOGFILE ("$dts:$pid: $msg\n" , @_);
}

I’ve actually included a bit more than just the log function – a few more lines to show how I set up the log filename. I like to create a daily log and assume the script I’m writing will eventually be run from a cron job or something.

Logging Strings – Calls to the logh() subroutine

This is the reason for all the setup — the actual log strings. Because of the way the arguments to the logh() function are utilized,
printf-style strings & arguments can be sent to the function. Here are some example calls to the logh function (pulled directly from a live production script):

I generally start out with the first line in the examples below (I always have a script global called “$progname”).

&logh("$progname started");
&logh("site '$store_nbr' not found in DB. adding site.");
&logh("all data matches for site '$store_nbr': nothing to do.");
&logh("adding site $store_nbr to db");
&logh("updating '$k' to '$v' for store $store_nbr in db");

Close It Out

Though it’s not really necessary these days, it’s good practice to actually close the file at the end of your script. I like to include the block below to close out the log file.

&logh("$progname ended");
my $line = '='x72;
&logh("$line");
close( LOGFILE );

How to be Physically Healthy

I don’t usually like list posts. I know, everyone adds that nowadays just before writing one. I just wrote it here because I’m really trying to like to be like everyone else. Anyway, here is the list:

  1. Stop eating animals. No meat, no dairy, no honey.
  2. Don’t eat out. This is probably quite difficult for most Americans, because we’ve styled our lives around being dependent on restaurants. Also, this means you’ll have to learn to cook.
  3. Eat healthy foods. Purchase & cook quinoa. Look up quinoa. (No, seriously, click the wikipedia link & check it out.) Eat raw brazil nuts (one daily is fine) and learn to cook steel cut oats. Look up steel cut oats so you know what they are. Also, buy & eat Ezekiel 4:9 cereal. It’s like Grape Nuts but better. (I know it’s like $6 a box, but your health is worth it. And you’re saving a ton of money by not eating out anyway.) These things can be purchased from Whole Foods or any good health food store.
  4. Daily physical activity. Also, you need to be doing stuff that makes you sweat at least 3-4 days a week, for at least 30 minutes each session.
  5. Get enough sleep.

Notice that the majority of these revolve around your diet, and most likely changing it if you’re not already hardcore. I am firmly convinced that nutrition, not physical activity, is the most important factor in being healthy and fit. If you don’t agree with this then you’re wrong.

photo credit

American Rendezvous 2k10

I just returned home yesterday afternoon from 3 days of intense Parkour training event in Columbus, Ohio. The event was American Rendezvous 2010, presented by the Parkour Horizons group.

The instructors came from both Parkour Generations and three of the Yamakasi, also current members of Majestic Force.

Just a side note, as a result, my DCI tracker will be missing entries for a few days, due to the fact that it was a bit difficult to get them in during the event.

Honestly, this is some of the best training one could possibly receive and I highly recommend to anyone interested in Parkour that they train with members of Parkour Generations and Majestic Force if possible.

More than just physical training, I feel like I now know how to train myself and understand much more about Parkour as an art form and a lifestyle as opposed to simply a sport.

currently listening to: El Corazón del Guerrero por Tierra Santa