~ubuntu-branches/ubuntu/precise/padre/precise

« back to all changes in this revision

Viewing changes to lib/Padre/Logger.pm

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2010-05-08 09:17:22 UTC
  • mfrom: (1.2.1 upstream) (10.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100508091722-y6008jtk0ap6znyn
Tags: 0.60.ds1-3
rules: run tests with HOME=$fake_home to avoud failing when $HOME points
to a non-existent location. Closes: #579289

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Padre::Logger;
 
2
 
 
3
=pod
 
4
 
 
5
=head1 NAME
 
6
 
 
7
Padre::Logger - Compile-time logging library for Padre
 
8
 
 
9
=head1 SYNOPSIS
 
10
 
 
11
  # In the launch/dev.pl script
 
12
  BEGIN {
 
13
      $Padre::Logger::DEBUG = 1;
 
14
  }
 
15
 
 
16
  use Padre;
 
17
 
 
18
  # In each Padre::Foo class
 
19
  use Padre::Logger;
 
20
 
 
21
  sub method {
 
22
      TRACE('->method') if DEBUG;
 
23
 
 
24
      # Your code as normal
 
25
  }
 
26
 
 
27
=head1 DESCRIPTION
 
28
 
 
29
This is a logging utility class for Padre. It provides a basic set of
 
30
simple functionality that allows for logging/debugging/tracing statements to be
 
31
used in Padre that will compile out of the application when not in use.
 
32
 
 
33
=cut
 
34
 
 
35
use 5.008;
 
36
use strict;
 
37
use warnings;
 
38
use Padre::Constant ();
 
39
 
 
40
our $VERSION = '0.60';
 
41
 
 
42
sub import {
 
43
        my $pkg = ( caller() )[0];
 
44
        eval <<"END_PERL";
 
45
package $pkg;
 
46
use constant DEBUG => !! (
 
47
        defined(\$${pkg}::DEBUG)     ? \$${pkg}::DEBUG :
 
48
        defined(\$Padre::Logger::DEBUG) ? \$Padre::Logger::DEBUG :
 
49
        \$ENV{PADRE_DEBUG}
 
50
);
 
51
BEGIN {
 
52
        *TRACE = *Padre::Logger::TRACE;
 
53
        TRACE('::DEBUG enabled') if DEBUG;
 
54
}
 
55
1;
 
56
END_PERL
 
57
        die("Failed to enable debugging for $pkg") if $@;
 
58
        return;
 
59
}
 
60
 
 
61
# Global trace function
 
62
sub TRACE {
 
63
        my $time    = scalar localtime time;
 
64
        my $package = ( caller() )[0];
 
65
        my $logfile = Padre::Constant::LOG_FILE;
 
66
        open my $fh, '>>', $logfile or return;
 
67
        foreach my $message (@_) {
 
68
                print $fh sprintf(
 
69
                        "%s %s%s\n",
 
70
                        $time,
 
71
                        $package,
 
72
                        $message,
 
73
                );
 
74
        }
 
75
        close $fh;
 
76
        return;
 
77
}
 
78
 
 
79
1;
 
80
 
 
81
# Copyright 2008-2010 The Padre development team as listed in Padre.pm.
 
82
# LICENSE
 
83
# This program is free software; you can redistribute it and/or
 
84
# modify it under the same terms as Perl 5 itself.