~ubuntu-branches/ubuntu/trusty/zonecheck/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# $Id: dbg.rb,v 1.30 2010/06/23 12:03:59 chabannf Exp $

# 
# CONTACT     : zonecheck@nic.fr
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
#
# CREATED     : 2002/09/16 13:31:29
# REVISION    : $Revision: 1.30 $ 
# DATE        : $Date: 2010/06/23 12:03:59 $
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
# LICENSE     : GPL v3
# COPYRIGHT   : AFNIC (c) 2003
#
# This file is part of ZoneCheck.
#
# ZoneCheck is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# ZoneCheck is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZoneCheck; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

module ZoneCheck
require 'thread'


##
## Debugging
##
class DBG
    #
    # Debugging types
    #
    INIT	= 0x0001	# Initialisation
    LOCALE	= 0x0002	# Localization / Internationalisation
    CONFIG	= 0x0004	# Configuration
    AUTOCONF	= 0x0008	# Autoconf
    LOADING	= 0x0010	# Loading tests
    TESTS	= 0x0020	# Tests performed
    TESTDBG	= 0x0040	# Debugging messages from tests
    CACHE_INFO	= 0x0400	# Information about cached object
    DBG		= 0x0800	# Debugger itself

    CRAZYDEBUG	= 0x1000	# Crazy Debug, don't try it...
    DNSRUBY	= 0x2000	# Dnsruby debugging messages
    NOCACHE	= 0x4000	# Disable caching
    DONT_RESCUE	= 0x8000	# Don't try to rescue exceptions

    #
    # Tag associated with some types
    #
    Tag = { 
	INIT		=> 'init',
	LOCALE		=> 'locale',
	CONFIG		=> 'config',
	AUTOCONF	=> 'autoconf',
	LOADING		=> 'loading',
	TESTS		=> 'tests',
	TESTDBG		=> 'testdbg',
	CACHE_INFO	=> 'cache',
	DBG		=> 'dbg'
    }


    #
    # Initializer
    #
    def initialize(lvl=0, output=$stderr)
	@output = output
	@lvl    = lvl
	@mutex	= Mutex::new
  @old_messages = []
	msg(DBG) { "Debugger initialized at level %0x" % @lvl }
    end


    #
    # Test if debug is enabled for that type
    #
    def enabled?(type) 
	@lvl & type != 0
    end
    alias [] enabled?


    #
    # Enable debugging for the specified type
    #
    def []=(type, enable)
	self.level = enable ? @lvl | type : @lvl & ~type
    end


    #
    # Change debugging level
    #
    def level=(lvl)
	old_crazydebug	= enabled?(CRAZYDEBUG)
	old_dnsruby	= enabled?(DNSRUBY)

	# parsing
	case lvl
	when String then @lvl = lvl =~ /^0x/ ? lvl.hex : lvl.to_i
	when Fixnum then @lvl = lvl
	else raise ArgumentError, "unable to interprete: #{lvl}"
	end
	
	# message
	msg(DBG) { "Setting level to 0x%0x" % lvl }


	# enable/disable Dnsruby
	if enabled?(DNSRUBY) ^ old_dnsruby
    Dnsruby::TheLog.level = Logger::DEBUG if enabled?(DNSRUBY)
	end

	# enable/disable CrazyDebug
	if enabled?(CRAZYDEBUG) ^ old_crazydebug
	    dbgfunc = if enabled?(CRAZYDEBUG)
			  proc { |event, file, line, id, binding, classname|
				@output.printf "%8s %s:%-2d %10s %8s\n", 
    				event, file, line, id, classname }
		      else
			  nil
		      end
	    set_trace_func(dbgfunc)
	end
	    failed_messages = @old_messages
      
      @mutex.synchronize {
        @old_messages = []
      }
      failed_messages.each {|type,msg|
        msg(type,msg) unless msg.nil?
      }
    end


    #
    # Print debugging message
    # WARN: It is adviced to use a block instead of the string 
    #       second argument, as this will provide a lazy evaluation
    #
    def msg(type, arg=nil)

	unless block_given? ^ !arg.nil?
	    raise ArgumentError, 'either string or block should be given'
	end
	arg = yield if block_given?
	
	arg = arg.to_s
  unless enabled?(type)
    @mutex.synchronize {
      @old_messages << [type,arg]
	  }
	  return 
	end
	@mutex.synchronize {
	    case arg
	    when Array
		case arg.size
		when 0
		    raise ArgumentError, 'the array argument must not be empty'
		when 1
		    @output.puts "DBG[#{Tag[type]}]: #{arg[0]}"
		else
		    tag       = "DBG[#{Tag[type]}]"
		    tagfiller = " " * tag.size
		    @output.puts "#{tag}: #{arg[0]}"
		    arg[1..-1].each { |l|
			@output.puts "#{tagfiller}| #{l}" }
		end
	    else
		@output.puts "DBG[#{Tag[type]}]: #{arg}"
	    end
	}
    end


    def self.status2str(status, ok=true)
	case status
	when FalseClass, TrueClass then status == ok ? 'passed' : 'failed'
	when NilClass,   Exception then 'exception'
	else                            'n/a'
	end
    end
end
end