C4::Debug - Centralized debug switches module, test and proof conversion (in Dates.pm)
[koha.git] / C4 / Debug.pm
1 package C4::Debug;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use Exporter;
24
25 use CGI;
26 use vars qw($VERSION @ISA @EXPORT $debug $cgi_debug);
27 # use vars qw(@EXPORT_OK %EXPORT_TAGS);
28
29 BEGIN {
30         $VERSION = 1.00;        # set the version for version checking
31         @ISA       = qw(Exporter);
32         @EXPORT    = qw($debug $cgi_debug);
33         # @EXPOR_OK    = qw();
34         # %EXPORT_TAGS = ( all=>[qw($debug $cgi_debug)], );
35 }
36
37 BEGIN {
38         # this stuff needs a begin block too, since dependencies might alter their compilations
39         # for example, adding DataDumper
40         my $query = CGI->new();
41         $debug = $ENV{KOHA_DEBUG} || $ENV{DEBUG} || 0;
42         $cgi_debug = $ENV{KOHA_CGI_DEBUG} || $query->param('debug') || 0;
43         unless (0 <= $debug and $debug <= 9) {
44                 warn "Invalid \$debug value attempted: $debug";
45                 $debug=1;
46         }
47         unless (0 <= $cgi_debug and $cgi_debug <= 9) {
48                 $debug and 
49                 warn "Invalid \$cgi_debug value attempted: $cgi_debug";
50                 $cgi_debug=1;
51         }
52 }
53
54 # sub import {
55 #       print STDERR __PACKAGE__ . " (Debug) import @_\n";
56 #       C4::Debug->export_to_level(1, @_);
57 # }
58
59 1;
60 __END__
61
62 =head1 NAME 
63         
64         C4::Debug - Standardized, centralized, exported debug switches.
65
66 =head1 SYNOPSIS
67
68         use C4::Debug;
69
70 =head1 DESCRIPTION
71
72 The purpose of this module is to centralize some of the "switches" that turn debugging
73 off and on in Koha.  Most often, this functionality will be provided via C4::Context.
74 C4::Debug is separate to preserve the relatively stable state of Context, and 
75 because other code will use C4::Debug without invoking Context.
76
77 Although centralization is our intention, 
78 for logical and security reasons, several approaches to debugging need to be 
79 kept separate.  Information useful to developers in one area will not necessarily
80 be useful or even available to developers in another area. 
81
82 For example, the designer of template-influenced javascript my want to be able to
83 trigger javascript's alert function to display certain variable values, to verify
84 the template selection is being performed correctly.  For this purpose the presence
85 of a javascript "debug" variable might be a good switch.  
86
87 Meanwhile, where security coders (say, for LDAP Auth) will appreciate low level feedback about
88 Authentication transactions, an environmental system variable might be a good switch.  
89 However, clearly we would not want to expose that same information (e.g., entire LDAP records)
90 to the web interface based on a javascript variable (even if it were possible)!  
91
92 All that is a long way of saying THERE ARE SECURITY IMPLICATIONS to turning on 
93 debugging in various parts of the system, so don't treat them all the same or confuse them.
94
95 =head1 VARIABLES / AREAS
96
97 =head2 $debug - System, general
98 The general purpose debug switch.  
99
100 =head3 How to Set $debug:
101
102 =over
103
104 =item environmental variable DEBUG or KOHA_DEBUG.  In bash, you might do:
105         
106         export KOHA_DEBUG=1;
107         perl t/Auth.t;
108
109 =item Keep in mind that your webserver will not be running in the same environment as your shell.
110 However, for development purposes, the same effect can be had by using Apache's SET_ENV
111 command with ERROR_LOG enabled for your VirtualHost.  Not intended for production systems.
112
113 =item You can force the value from perl directly, like:
114
115         use C4::Debug;
116         use C4::Dates;
117         BEGIN { $C4::Debug::debug = 1; }
118         # now any other dependencies that also use C4::Debug will have debugging ON.
119
120 =back
121
122 =head2 $cgi_debug (CGI params) The web-based debug switch.
123
124 =head3 How to Set $cgi_debug:
125
126 =over
127
128 =item From a web browser, for example by supplying a non-zero debug parameter:
129
130         http://www.mylibrary.org/cgi-bin/koha/opac-search.pl?q=history&debug=1
131
132 =item Or in HTML, add a similar input parameter:
133
134         <input type="hidden" name="debug" value="1" />
135
136 =item Or from shell (or Apache), set KOHA_CGI_DEBUG.
137
138 =back 
139
140 The former methods mean $cgi_debug is exposed.  Do NOT use it to trigger any actions that you would
141 not allow a (potentially anonymous) end user to perform.  
142
143 =head1 OTHER SOURCES of Debug Switches
144
145 =head2 System Preferences
146
147 =head2 Database Debug
148
149 Debugging at the database level might be useful.  Koha does not currently integrate any such 
150 capability.
151
152 =head1 CONVENTIONS
153
154 Debug values range from 0 to 9.  At zero (the default), debugging is off.  
155
156 =head1 AUTHOR
157
158 Joe Atzberger
159 atz AT liblime DOT com
160
161 =head1 SEE ALSO
162
163 CGI(3)
164
165 C4::Context
166
167 =cut
168