commit 7875ac663280307a92fbaaf25d4b84f42d272f71
parent 33fe2b8e4f759ca35e1139f7ff918b5214ece595
Author: Kyle Milz <kyle@getaddrinfo.net>
Date: Thu, 12 Mar 2015 22:49:16 -0600
PriceChart: tee log output to stdout and file
Diffstat:
3 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/DEPS b/DEPS
@@ -5,6 +5,7 @@ p5-Email-Simple
p5-FCGI
p5-Getopt-Std
p5-HTML-Grabber
+p5-IO-Tee
p5-SVG
p5-Template
p5-Term-ReadKey
diff --git a/PriceChart.pm b/PriceChart.pm
@@ -28,6 +28,7 @@ sub get_config
"db_dir",
"htdocs",
"templates",
+ "logs",
],
},
vendors => {
@@ -109,20 +110,22 @@ sub new_ua
sub get_log
{
- my $file = shift;
+ my $cfg = shift || return undef;
+ my $file = shift || return undef;
my $verbose = shift || 0;
- my $log_dir = "/var/www/logs/pricechart";
+ my $path = $cfg->{"chroot"} . $cfg->{"logs"} . "/" . $file;
- return undef unless defined $file;
+ mkdir $log_dir;
+ open my $log, ">>", $path or die "can't open $path: $!";
if ($verbose) {
- open my $log, '>&', STDOUT or die "$!";
- return $log;
+ print "info: get_log: outputting to tee\n";
+ open my $std_out, '>&', STDOUT or die "$!";
+
+ return IO::Tee->new($log, $std_out);
}
- mkdir $log_dir;
- open my $log, ">>", "$log_dir/$file.log" or die "$!";
- return $log;
+ return IO::Tee->new($log);
}
#
diff --git a/price_scraper.pl b/price_scraper.pl
@@ -6,6 +6,7 @@ use warnings;
use Config::Grammar;
use Getopt::Std;
use HTML::Grabber;
+use IO::Tee;
use List::Util qw(min);
use LWP::Simple;
use PriceChart;
@@ -19,8 +20,8 @@ getopts("m:np:v", \%args);
$| = 1 if ($args{v});
-my $log = get_log("scrapes", $args{v});
my $cfg = get_config();
+my $log = get_log($cfg->{"http"}, "price_scrapes.txt", $args{v});
my $ua = new_ua($cfg->{"general"}, $args{v});
my $dbh = get_dbh($cfg->{"general"}, undef, $args{v});
@@ -124,7 +125,9 @@ for my $vendor (sort keys %{$cfg->{"vendors"}}) {
printf $log "%s %-10s %-20s [%s] (%i s)\n", $timestamp, $manufacturer,
$part_num, join("", @status), time - $start;
-close $log;
+$log->close();
+$prices_sth = undef;
+$products_sth = undef;
$dbh->disconnect();
exit 0;