@@ -106,8 +106,8 @@ class Base
106106 # Format of input, one of `:abnf`, `:ebnf`, `:isoebnf`, `:isoebnf`, `:native`, or `:sxp`.
107107 # Use `:native` for the native EBNF parser, rather than the PEG parser.
108108 # @param [Hash{Symbol => Object}] options
109- # @option options [Boolean, Array ] :debug
110- # Output debug information to an array or $stdout .
109+ # @option options [Boolean] :level
110+ # Trace level. 0( debug), 1(info), 2(warn), 3(error) .
111111 # @option options [Boolean, Array] :validate
112112 # Validate resulting grammar.
113113 def initialize ( input , format : :ebnf , **options )
@@ -311,24 +311,17 @@ def depth
311311
312312 # Progress output, less than debugging
313313 def progress ( *args , **options )
314- return unless @options [ :progress ] || @options [ :debug ]
315- depth = options [ :depth ] || @depth
316- args << yield if block_given?
317- message = "#{ args . join ( ': ' ) } "
318- str = "[#{ @lineno } ]#{ ' ' * depth } #{ message } "
319- @options [ :debug ] << str if @options [ :debug ] . is_a? ( Array )
320- $stderr. puts ( str ) if @options [ :progress ] || @options [ :debug ] == true
314+ debug ( *args , **options . merge ( level : Logger ::INFO ) )
321315 end
322316
323317 # Error output
324318 def error ( *args , **options )
325319 depth = options [ :depth ] || @depth
326320 args << yield if block_given?
327321 message = "#{ args . join ( ': ' ) } "
322+ debug ( message , **options . merge ( level : Logger ::ERROR ) )
328323 @errors << message
329- str = "[#{ @lineno } ]#{ ' ' * depth } #{ message } "
330- @options [ :debug ] << str if @options [ :debug ] . is_a? ( Array )
331- $stderr. puts ( str )
324+ $stderr. puts ( message )
332325 end
333326
334327 ##
@@ -343,13 +336,17 @@ def error(*args, **options)
343336 #
344337 # @yieldreturn [String] added to message
345338 def debug ( *args , **options )
346- return unless @options [ :debug ]
339+ return unless @options . key? ( :logger )
340+ level = @options [ :level ] || Logger ::DEBUG
347341 depth = options [ :depth ] || @depth
348342 args << yield if block_given?
349343 message = "#{ args . join ( ': ' ) } "
350344 str = "[#{ @lineno } ]#{ ' ' * depth } #{ message } "
351- @options [ :debug ] << str if @options [ :debug ] . is_a? ( Array )
352- $stderr. puts ( str ) if @options [ :debug ] == true
345+ if @options [ :logger ] . respond_to? ( :add )
346+ @options [ :logger ] . add ( level , str )
347+ elsif @options [ :logger ] . respond_to? ( :<< )
348+ @options [ :logger ] << "[#{ lineno } ] " + str
349+ end
353350 end
354351 end
355352end
0 commit comments