| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2009 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2009-Mar-27 11:40 (EDT) | ||
| 6 | # Function: debugging + log msgs | ||
| 7 | # | ||
| 8 | # $Id: Debug.pm,v 1.3 2010/09/07 15:31:29 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::DC::Debug; | ||
| 11 | use AC::Daemon; | ||
| 12 | use strict; | ||
| 13 | |||
| 14 | my $config; | ||
| 15 | my $debugall; | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | shift; | ||
| 19 | $debugall = shift; | ||
| 20 | $config = shift; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub _tagged_debug { | ||
| 24 | my $tag = shift; | ||
| 25 | my $msg = shift; | ||
| 26 | |||
| 27 | if( $config && $config->{config} ){ | ||
| 28 | return unless $config->{config}{debug}{$tag} || $config->{config}{debug}{all} || $debugall; | ||
| 29 | }else{ | ||
| 30 | return unless $debugall; | ||
| 31 | } | ||
| 32 | |||
| 33 | debugmsg( "$tag - $msg" ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub import { | ||
| 37 | my $class = shift; | ||
| 38 | my $tag = shift; # use AC::DC::Debug 'tag'; | ||
| 39 | my $caller = caller; | ||
| 40 | |||
| 41 | no strict; | ||
| 42 | if( $tag ){ | ||
| 43 | # export a curried debug (with the specified tag) to the caller | ||
| 44 | *{$caller . '::debug'} = sub { _tagged_debug($tag, @_) }; | ||
| 45 | } | ||
| 46 | |||
| 47 | for my $f qw(verbose problem fatal){ | ||
| 48 | no strict; | ||
| 49 | *{$caller . '::' . $f} = $class->can($f); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; |