Parent Directory
|
Revision Log
Revision 4050 - (view) (download) (as text)
| 1 : | sh002i | 2367 | ################################################################################ |
| 2 : | # WeBWorK Online Homework Delivery System | ||
| 3 : | sh002i | 3973 | # Copyright © 2000-2006 The WeBWorK Project, http://openwebwork.sf.net/ |
| 4 : | sh002i | 4050 | # $CVSHeader: webwork2/lib/WeBWorK/Debug.pm,v 1.7 2006/01/25 23:13:51 sh002i Exp $ |
| 5 : | sh002i | 2367 | # |
| 6 : | # This program is free software; you can redistribute it and/or modify it under | ||
| 7 : | # the terms of either: (a) the GNU General Public License as published by the | ||
| 8 : | # Free Software Foundation; either version 2, or (at your option) any later | ||
| 9 : | # version, or (b) the "Artistic License" which comes with this package. | ||
| 10 : | # | ||
| 11 : | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 : | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 13 : | # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the | ||
| 14 : | # Artistic License for more details. | ||
| 15 : | ################################################################################ | ||
| 16 : | |||
| 17 : | package WeBWorK::Debug; | ||
| 18 : | use base qw(Exporter); | ||
| 19 : | jj | 2871 | use Date::Format; |
| 20 : | sh002i | 2367 | our @EXPORT = qw(debug); |
| 21 : | |||
| 22 : | =head1 NAME | ||
| 23 : | |||
| 24 : | WeBWorK::Debug - Print (or don't print) debugging output. | ||
| 25 : | |||
| 26 : | head1 SYNOPSIS | ||
| 27 : | |||
| 28 : | use WeBWorK::Debug; | ||
| 29 : | |||
| 30 : | # Enable debugging | ||
| 31 : | $WeBWorK::Debug::Enabled = 1; | ||
| 32 : | |||
| 33 : | # Log to a file instead of STDERR | ||
| 34 : | $WeBWorK::Debug::Logfile = "/path/to/debug.log"; | ||
| 35 : | |||
| 36 : | # log some debugging output | ||
| 37 : | debug("Generated 5 widgets."); | ||
| 38 : | |||
| 39 : | =cut | ||
| 40 : | |||
| 41 : | use strict; | ||
| 42 : | use warnings; | ||
| 43 : | sh002i | 3485 | use Time::HiRes qw/gettimeofday/; |
| 44 : | sh002i | 4050 | use WeBWorK::Utils qw/undefstr/; |
| 45 : | sh002i | 2367 | |
| 46 : | ################################################################################ | ||
| 47 : | |||
| 48 : | =head1 CONFIGURATION VARIABLES | ||
| 49 : | |||
| 50 : | =over | ||
| 51 : | |||
| 52 : | =item $Enabled | ||
| 53 : | |||
| 54 : | If true, debugging messages will be output. If false, they will be ignored. | ||
| 55 : | |||
| 56 : | =cut | ||
| 57 : | |||
| 58 : | our $Enabled = 0 unless defined $Enabled; | ||
| 59 : | |||
| 60 : | =item $Logfile | ||
| 61 : | |||
| 62 : | If non-empty, debugging output will be sent to the file named rather than STDERR. | ||
| 63 : | |||
| 64 : | =cut | ||
| 65 : | |||
| 66 : | our $Logfile = "" unless defined $Logfile; | ||
| 67 : | |||
| 68 : | sh002i | 3486 | =item $DenySubroutineOutput |
| 69 : | sh002i | 2372 | |
| 70 : | sh002i | 3485 | If defined, prevent subroutines matching the following regular expression from |
| 71 : | logging. | ||
| 72 : | sh002i | 2372 | |
| 73 : | =cut | ||
| 74 : | |||
| 75 : | sh002i | 3486 | our $DenySubroutineOutput; |
| 76 : | sh002i | 2372 | |
| 77 : | sh002i | 3485 | =item $AllowSubroutineOutput |
| 78 : | |||
| 79 : | If defined, allow only subroutines matching the following regular expression to | ||
| 80 : | log. | ||
| 81 : | |||
| 82 : | =cut | ||
| 83 : | |||
| 84 : | our $AllowSubroutineOutput; | ||
| 85 : | |||
| 86 : | sh002i | 2367 | =back |
| 87 : | |||
| 88 : | =cut | ||
| 89 : | |||
| 90 : | ################################################################################ | ||
| 91 : | |||
| 92 : | =head1 FUNCTIONS | ||
| 93 : | |||
| 94 : | =over | ||
| 95 : | |||
| 96 : | =item debug(@messages) | ||
| 97 : | |||
| 98 : | Write @messages to the debugging log. | ||
| 99 : | |||
| 100 : | =cut | ||
| 101 : | |||
| 102 : | sub debug { | ||
| 103 : | sh002i | 4050 | my (@message) = undefstr("###UNDEF###", @_); |
| 104 : | sh002i | 2367 | |
| 105 : | if ($Enabled) { | ||
| 106 : | my ($package, $filename, $line, $subroutine) = caller(1); | ||
| 107 : | sh002i | 3485 | return if defined $AllowSubroutineOutput and not $subroutine =~ m/$AllowSubroutineOutput/; |
| 108 : | sh002i | 3486 | return if defined $DenySubroutineOutput and $subroutine =~ m/$DenySubroutineOutput/; |
| 109 : | sh002i | 2372 | |
| 110 : | sh002i | 3485 | my ($sec, $msec) = gettimeofday; |
| 111 : | my $date = time2str("%a %b %d %H:%M:%S.$msec %Y", $sec); | ||
| 112 : | my $finalMessage = "[$date] $subroutine: " . join("", @message); | ||
| 113 : | sh002i | 2367 | $finalMessage .= "\n" unless $finalMessage =~ m/\n$/; |
| 114 : | sh002i | 3485 | |
| 115 : | sh002i | 2367 | if ($WeBWorK::Debug::Logfile ne "") { |
| 116 : | if (open my $fh, ">>", $Logfile) { | ||
| 117 : | print $fh $finalMessage; | ||
| 118 : | close $fh; | ||
| 119 : | } else { | ||
| 120 : | warn "Failed to open debug log '$Logfile' in append mode: $!"; | ||
| 121 : | print STDERR $finalMessage; | ||
| 122 : | } | ||
| 123 : | } else { | ||
| 124 : | print STDERR $finalMessage; | ||
| 125 : | } | ||
| 126 : | } | ||
| 127 : | } | ||
| 128 : | |||
| 129 : | =back | ||
| 130 : | |||
| 131 : | =cut | ||
| 132 : | |||
| 133 : | ################################################################################ | ||
| 134 : | |||
| 135 : | =head1 AUTHOR | ||
| 136 : | |||
| 137 : | Written by Sam Hathaway, sh002i (at) math.rochester.edu. | ||
| 138 : | |||
| 139 : | =cut | ||
| 140 : | |||
| 141 : | 1; |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |