debugtrace 0.3.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,8 +10,8 @@ class Config
10
10
  attr_reader :config
11
11
  attr_reader :logger_name
12
12
  attr_reader :log_path
13
- attr_reader :logging_format
14
- attr_reader :logging_datetime_format
13
+ attr_reader :rubylogger_format
14
+ attr_reader :log_datetime_format
15
15
  attr_reader :enter_format
16
16
  attr_reader :leave_format
17
17
  attr_reader :thread_boundary_format
@@ -19,8 +19,7 @@ class Config
19
19
  attr_reader :indent_string
20
20
  attr_reader :data_indent_string
21
21
  attr_reader :limit_string
22
- attr_reader :non_output_string
23
- attr_reader :cyclic_reference_string
22
+ attr_reader :circular_reference_string
24
23
  attr_reader :varname_value_separator
25
24
  attr_reader :key_value_separator
26
25
  attr_reader :print_suffix_format
@@ -28,51 +27,50 @@ class Config
28
27
  attr_reader :minimum_output_size
29
28
  attr_reader :length_format
30
29
  attr_reader :minimum_output_length
31
- attr_reader :maximum_data_output_width
30
+ attr_reader :data_output_width
32
31
  attr_reader :bytes_count_in_line
33
32
  attr_reader :collection_limit
34
- attr_reader :bytes_limit
35
33
  attr_reader :string_limit
34
+ attr_reader :bytes_limit
36
35
  attr_reader :reflection_limit
37
36
 
38
37
  # Initializes with a yml file in the config_path.
39
38
  #
40
39
  # @param config_path [String] path of the yml file
41
40
  def initialize(config_path)
42
- @config_path = Common.check_type('config_path', config_path, String)
41
+ @config_path = config_path
43
42
  if File.exist?(@config_path)
44
43
  @config = YAML.load_file(@config_path)
45
44
  else
46
45
  @config_path = '<No config file>'
47
46
  @config = nil
48
47
  end
49
- @logger_name = get_value 'logger' , 'stderr'
50
- @log_path = get_value 'log_path' , 'debugtrace.log'
51
- @logging_format = get_value 'logging_format' , "%2$s %1$s %4$s\n"
52
- @logging_datetime_format = get_value 'logging_datetime_format' , '%Y-%m-%d %H:%M:%S.%L%:z'
53
- @enabled = get_value 'enabled' , true
54
- @enter_format = get_value 'enter_format' , 'Enter %1$s (%2$s:%3$d) <- %4$s (%5$s:%6$d)'
55
- @leave_format = get_value 'leave_format' , 'Leave %1$s (%2$s:%3$d) duration: %4$.3f ms'
56
- @thread_boundary_format = get_value 'thread_boundary_format' , '______________________________ %1$s #%2$s ______________________________'
57
- @maximum_indents = get_value 'maximum_indents' , 32
58
- @indent_string = get_value 'indent_string' , '| '
59
- @data_indent_string = get_value 'data_indent_string' , ' '
60
- @limit_string = get_value 'limit_string' , '...'
61
- @non_output_string = get_value 'non_output_string' , '...'
62
- @cyclic_reference_string = get_value 'cyclic_reference_string' , '*** Cyclic Reference ***'
63
- @varname_value_separator = get_value 'varname_value_separator' , ' = '
64
- @key_value_separator = get_value 'key_value_separator' , ': '
65
- @print_suffix_format = get_value 'print_suffix_format' , ' (%2$s:%3$d)'
66
- @size_format = get_value 'size_format' , '(size:%d)'
67
- @minimum_output_size = get_value 'minimum_output_size' , 256
68
- @length_format = get_value 'length_format' , '(length:%d)'
69
- @minimum_output_length = get_value 'minimum_output_length' , 256
70
- @maximum_data_output_width = get_value 'maximum_data_output_width', 70
71
- @bytes_count_in_line = get_value 'bytes_count_in_line' , 16
72
- @collection_limit = get_value 'collection_limit' , 128
73
- @bytes_limit = get_value 'bytes_limit' , 256
74
- @string_limit = get_value 'string_limit' , 256
75
- @reflection_limit = get_value 'reflection_limit' , 4
48
+ @logger_name = get_value 'logger' , 'stderr'
49
+ @log_path = get_value 'log_path' , 'debugtrace.log'
50
+ @rubylogger_format = get_value 'rubylogger_format' , "%2$s %1$s %4$s\n"
51
+ @log_datetime_format = get_value 'log_datetime_format' , '%Y-%m-%d %H:%M:%S.%L%:z'
52
+ @enabled = get_value 'enabled' , true
53
+ @enter_format = get_value 'enter_format' , 'Enter %1$s (%2$s:%3$d) <- %4$s (%5$s:%6$d)'
54
+ @leave_format = get_value 'leave_format' , 'Leave %1$s (%2$s:%3$d) duration: %4$.3f ms'
55
+ @thread_boundary_format = get_value 'thread_boundary_format' , '______________________________ %1$s #%2$d ______________________________'
56
+ @maximum_indents = get_value 'maximum_indents' , 32
57
+ @indent_string = get_value 'indent_string' , '| '
58
+ @data_indent_string = get_value 'data_indent_string' , ' '
59
+ @limit_string = get_value 'limit_string' , '...'
60
+ @circular_reference_string = get_value 'circular_reference_string' , '*** Circular Reference ***'
61
+ @varname_value_separator = get_value 'varname_value_separator' , ' = '
62
+ @key_value_separator = get_value 'key_value_separator' , ': '
63
+ @print_suffix_format = get_value 'print_suffix_format' , ' (%2$s:%3$d)'
64
+ @size_format = get_value 'size_format' , '(size:%d)'
65
+ @minimum_output_size = get_value 'minimum_output_size' , 256
66
+ @length_format = get_value 'length_format' , '(length:%d)'
67
+ @minimum_output_length = get_value 'minimum_output_length' , 256
68
+ @data_output_width = get_value 'data_output_width' , 70
69
+ @bytes_count_in_line = get_value 'bytes_count_in_line' , 16
70
+ @collection_limit = get_value 'collection_limit' , 128
71
+ @string_limit = get_value 'string_limit' , 256
72
+ @bytes_limit = get_value 'bytes_limit' , 256
73
+ @reflection_limit = get_value 'reflection_limit' , 4
76
74
  end
77
75
 
78
76
  # Returns true if logging is enabled, false otherwise.
@@ -27,8 +27,8 @@ class LogBuffer
27
27
  end
28
28
 
29
29
  # Initializes this object.
30
- def initialize(maximum_data_output_width)
31
- @maximum_data_output_width = Common.check_type('maximum_data_output_width', maximum_data_output_width, Integer)
30
+ def initialize(data_output_width)
31
+ @data_output_width = Common.check_type('data_output_width', data_output_width, Integer)
32
32
  @nest_level = 0
33
33
  @append_nest_level = 0
34
34
 
@@ -68,7 +68,7 @@ class LogBuffer
68
68
  Common.check_type('no_break', no_break, TrueClass)
69
69
  unless value.nil?
70
70
  string = value.to_s
71
- line_feed if !no_break && length > 0 && length + string.length > @maximum_data_output_width
71
+ line_feed if !no_break && length > 0 && length + string.length > @data_output_width
72
72
  @append_nest_level = nest_level
73
73
  @last_line += string
74
74
  end
@@ -11,7 +11,7 @@ class LoggerBase
11
11
  # @param message [String] The message to output
12
12
  # @raise [Exception] always
13
13
  def print(message)
14
- raise Exception.new('LoggerBase.print is an abstract method.')
14
+ raise Exception, 'LoggerBase.print is an abstract method.'
15
15
  end
16
16
 
17
17
  # Returns a string representation of this object.
@@ -35,7 +35,7 @@ class StdLogger < LoggerBase
35
35
  # @return [String] the message
36
36
  def print(message)
37
37
  Common::check_type("message", message, String)
38
- datetime_str = Time.now().strftime(@config.logging_datetime_format)
38
+ datetime_str = Time.now().strftime(@config.log_datetime_format)
39
39
  @output.puts "#{datetime_str} #{message}"
40
40
  end
41
41
 
@@ -60,7 +60,7 @@ class StdErrLogger < StdLogger
60
60
  end
61
61
 
62
62
  # A logger class that outputs using Ruby Logger.
63
- class RubyLogger
63
+ class RubyLogger < LoggerBase
64
64
  private
65
65
 
66
66
  class Formatter
@@ -71,8 +71,8 @@ class RubyLogger
71
71
  end
72
72
 
73
73
  def call(severity, datetime, progname, msg)
74
- datetime_str = datetime.strftime(@config.logging_datetime_format)
75
- format(@config.logging_format, severity, datetime_str, progname, msg)
74
+ datetime_str = datetime.strftime(@config.log_datetime_format)
75
+ format(@config.rubylogger_format, severity, datetime_str, progname, msg)
76
76
  end
77
77
  end
78
78
 
@@ -86,7 +86,7 @@ class RubyLogger
86
86
  @logger = Logger.new(
87
87
  @config.log_path,
88
88
  formatter: Formatter.new(@config),
89
- datetime_format: @config.logging_datetime_format)
89
+ datetime_format: @config.log_datetime_format)
90
90
  end
91
91
 
92
92
  # Outputs the message.
@@ -94,7 +94,7 @@ class RubyLogger
94
94
  # @param message [String] The message to output
95
95
  def print(message)
96
96
  Common::check_type("message", message, String)
97
- @logger.log(Logger::Severity::DEBUG, message, 'DebugTrace-rb')
97
+ @logger.log(Logger::Severity::DEBUG, message, 'DebugTrace')
98
98
  return message
99
99
  end
100
100
 
@@ -146,7 +146,7 @@ class FileLogger < LoggerBase
146
146
  def print(message)
147
147
  if File.exist?(@log_path)
148
148
  File.open(@log_path, 'a') { |file|
149
- datetime_str = Time.now().strftime(@config.logging_datetime_format)
149
+ datetime_str = Time.now().strftime(@config.log_datetime_format)
150
150
  file.puts "#{datetime_str} #{message}"
151
151
  }
152
152
  end
@@ -2,5 +2,5 @@
2
2
  # (C) 2025 Masato Kokubo
3
3
 
4
4
  module DebugTrace
5
- VERSION = '0.3.1'
5
+ VERSION = '1.0.1'
6
6
  end
data/lib/debugtrace.rb CHANGED
@@ -44,13 +44,21 @@ module DebugTrace
44
44
  # The logger used by DebugTrace-py
45
45
  @@logger = nil
46
46
 
47
+ # The before thread id
48
+ @@before_thread_id = nil
49
+
50
+ @@DO_NOT_OUTPUT = 'Do not output'
51
+
47
52
  # Initialize this class
48
- #
49
- # @option [String] config_path the path to the configuration file. ./debugtrace.yml if not specified.
50
- def self.initialize(config_path = './debugtrace.yml')
53
+ def self.initialize()
54
+ config_path = ENV['DEBUGTRACE_CONFIG']
55
+ if config_path == nil || config_path.empty?
56
+ config_path = './debugtrace.yml'
57
+ end
58
+
51
59
  @@config = Config.new(config_path)
52
60
 
53
- @@last_log_buff = LogBuffer.new(@@config.maximum_data_output_width)
61
+ @@last_log_buff = LogBuffer.new(@@config.data_output_width)
54
62
 
55
63
  # Decide the logger class
56
64
  case @@config.logger_name.downcase
@@ -69,7 +77,7 @@ module DebugTrace
69
77
 
70
78
  return unless @@config.enabled?
71
79
 
72
- config_path = File.expand_path(@@config.config_path)
80
+ config_path = @@config.config == nil ? @@config.config_path : File.expand_path(@@config.config_path)
73
81
  @@logger.print("DebugTrace-rb #{DebugTrace::VERSION} on Ruby #{RUBY_VERSION}")
74
82
  @@logger.print(" config file: #{config_path}")
75
83
  @@logger.print(" logger: #{@@logger}")
@@ -141,7 +149,7 @@ module DebugTrace
141
149
  # @param value [Object] the value
142
150
  # @param print_options [PrintOptions] the print options
143
151
  def self.to_string(name, value, print_options)
144
- buff = LogBuffer.new(@@config.maximum_data_output_width)
152
+ buff = LogBuffer.new(@@config.data_output_width)
145
153
 
146
154
  unless name.empty?
147
155
  buff.append(name).no_break_append(@@config.varname_value_separator)
@@ -179,10 +187,10 @@ module DebugTrace
179
187
  to_s_string = reflection ? '' : value.to_s
180
188
  if reflection || to_s_string.start_with?('#<')
181
189
  # use reflection
182
- value_buff = LogBuffer.new(@@config.maximum_data_output_width)
190
+ value_buff = LogBuffer.new(@@config.data_output_width)
183
191
  if @@reflected_objects.any? { |obj| value.equal?(obj) }
184
192
  # cyclic reference
185
- value_buff.no_break_append(@@config.cyclic_reference_string)
193
+ value_buff.no_break_append(@@config.circular_reference_string)
186
194
  elsif @@reflected_objects.length > print_options.reflection_limit
187
195
  # over reflection level limitation
188
196
  value_buff.no_break_append(@@config.limit_string)
@@ -207,8 +215,8 @@ module DebugTrace
207
215
  # @param print_options [PrintOptions] the print options
208
216
  def self.to_string_str(value, print_options)
209
217
  double_quote = false
210
- single_quote_buff = LogBuffer.new(@@config.maximum_data_output_width)
211
- double_quote_buff = LogBuffer.new(@@config.maximum_data_output_width)
218
+ single_quote_buff = LogBuffer.new(@@config.data_output_width)
219
+ double_quote_buff = LogBuffer.new(@@config.data_output_width)
212
220
 
213
221
  if value.length >= print_options.minimum_output_length
214
222
  single_quote_buff.no_break_append(format(@@config.length_format, value.length))
@@ -270,7 +278,7 @@ module DebugTrace
270
278
  # @param print_options [PrintOptions] the print options
271
279
  def self.to_string_bytes(value, print_options)
272
280
  bytes_length = value.length
273
- buff = LogBuffer.new(@@config.maximum_data_output_width)
281
+ buff = LogBuffer.new(@@config.data_output_width)
274
282
 
275
283
  if bytes_length >= print_options.minimum_output_length
276
284
  buff.no_break_append(format(@@config.length_format, bytes_length))
@@ -327,13 +335,13 @@ module DebugTrace
327
335
  # @param value [Object] the value
328
336
  # @param print_options [PrintOptions] the print options
329
337
  def self.to_string_reflection(value, print_options)
330
- buff = LogBuffer.new(@@config.maximum_data_output_width)
338
+ buff = LogBuffer.new(@@config.data_output_width)
331
339
 
332
340
  buff.append(get_type_name(value, -1, print_options))
333
341
 
334
342
  body_buff = to_string_reflection_body(value, print_options)
335
343
 
336
- multi_lines = body_buff.multi_lines? || buff.length + body_buff.length > @@config.maximum_data_output_width
344
+ multi_lines = body_buff.multi_lines? || buff.length + body_buff.length > @@config.data_output_width
337
345
 
338
346
  buff.no_break_append('{')
339
347
  if multi_lines
@@ -357,7 +365,7 @@ module DebugTrace
357
365
  # @param value [Object] the value
358
366
  # @param print_options [PrintOptions] the print options
359
367
  def self.to_string_reflection_body(value, print_options)
360
- buff = LogBuffer.new(@@config.maximum_data_output_width)
368
+ buff = LogBuffer.new(@@config.data_output_width)
361
369
  multi_lines = false
362
370
  index = 0
363
371
 
@@ -366,7 +374,7 @@ module DebugTrace
366
374
  buff.no_break_append(', ') if index > 0
367
375
 
368
376
  var_value = value.instance_variable_get(variable)
369
- member_buff = LogBuffer.new(@@config.maximum_data_output_width)
377
+ member_buff = LogBuffer.new(@@config.data_output_width)
370
378
  member_buff.append(variable).no_break_append(@@config.key_value_separator)
371
379
  member_buff.append_buffer(to_string('', var_value, print_options))
372
380
  buff.line_feed if index > 0 && (multi_lines || member_buff.multi_lines?)
@@ -383,7 +391,7 @@ module DebugTrace
383
391
  buff.no_break_append(', ') if index > 0
384
392
 
385
393
  var_value = hash[member]
386
- member_buff = LogBuffer.new(@@config.maximum_data_output_width)
394
+ member_buff = LogBuffer.new(@@config.data_output_width)
387
395
  member_buff.append(member).no_break_append(@@config.key_value_separator)
388
396
  member_buff.append_buffer(to_string('', var_value, print_options))
389
397
  buff.line_feed if index > 0 && (multi_lines || member_buff.multi_lines?)
@@ -415,13 +423,13 @@ module DebugTrace
415
423
  close_char = ']'
416
424
  end
417
425
 
418
- buff = LogBuffer.new(@@config.maximum_data_output_width)
426
+ buff = LogBuffer.new(@@config.data_output_width)
419
427
  buff.append(get_type_name(values, values.size, print_options))
420
428
  buff.no_break_append(open_char)
421
429
 
422
430
  body_buff = to_string_enumerable_body(values, print_options)
423
431
 
424
- multi_lines = body_buff.multi_lines? || buff.length + body_buff.length > @@config.maximum_data_output_width
432
+ multi_lines = body_buff.multi_lines? || buff.length + body_buff.length > @@config.data_output_width
425
433
 
426
434
  if multi_lines
427
435
  buff.line_feed
@@ -445,7 +453,7 @@ module DebugTrace
445
453
  # @param value [Array, Set, Hash] the value
446
454
  # @param print_options [PrintOptions] the print options
447
455
  def self.to_string_enumerable_body(values, print_options)
448
- buff = LogBuffer.new(@@config.maximum_data_output_width)
456
+ buff = LogBuffer.new(@@config.data_output_width)
449
457
 
450
458
  multi_lines = false
451
459
  index = 0
@@ -484,7 +492,7 @@ module DebugTrace
484
492
  # @param value [Object] the value
485
493
  # @param print_options [PrintOptions] the print options
486
494
  def self.to_string_key_value(key, value, print_options)
487
- buff = LogBuffer.new(@@config.maximum_data_output_width)
495
+ buff = LogBuffer.new(@@config.data_output_width)
488
496
  key_buff = to_string('', key, print_options)
489
497
  value_buff = to_string('', value, print_options)
490
498
  buff.append_buffer(key_buff).no_break_append(@@config.key_value_separator).append_buffer(value_buff)
@@ -507,14 +515,12 @@ module DebugTrace
507
515
  return type_name
508
516
  end
509
517
 
510
- @@before_thread_id = nil
511
-
512
518
  # Called at the start of the print method.
513
519
  def self.print_start
514
- if @@before_thread_id == nil
515
- DebugTrace.initialize
516
- return unless @@config.enabled?
520
+ if @@config == nil
521
+ initialize
517
522
  end
523
+ return unless @@config.enabled?
518
524
 
519
525
  thread = Thread.current
520
526
  thread_id = thread.object_id
@@ -527,8 +533,6 @@ module DebugTrace
527
533
  @@before_thread_id = thread_id
528
534
  end
529
535
 
530
- @@DO_NOT_OUTPUT = 'Do not output'
531
-
532
536
  # Prints the message or the value.
533
537
  #
534
538
  # @param name [String] a message if the value is not specified, otherwise the value name
@@ -556,7 +560,7 @@ module DebugTrace
556
560
 
557
561
  if value.equal? @@DO_NOT_OUTPUT
558
562
  # without value
559
- @@last_log_buff = LogBuffer.new(@@config.maximum_data_output_width)
563
+ @@last_log_buff = LogBuffer.new(@@config.data_output_width)
560
564
  @@last_log_buff.no_break_append(name)
561
565
  else
562
566
  # with value
@@ -590,7 +594,7 @@ module DebugTrace
590
594
  end
591
595
  end
592
596
 
593
- return value
597
+ return value.equal? @@DO_NOT_OUTPUT ? nil : value
594
598
  end
595
599
 
596
600
  # Prints the start of the method.
@@ -616,7 +620,7 @@ module DebugTrace
616
620
  @@logger.print(indent_string) # Empty Line
617
621
  end
618
622
 
619
- @@last_log_buff = LogBuffer.new(@@config.maximum_data_output_width)
623
+ @@last_log_buff = LogBuffer.new(@@config.data_output_width)
620
624
  @@last_log_buff.no_break_append(
621
625
  format(@@config.enter_format, name, filename, lineno, parent_name, parent_filename, parent_lineno)
622
626
  )
@@ -649,7 +653,7 @@ module DebugTrace
649
653
 
650
654
  time = (Time.now.utc - state.down_nest) * 1000 # milliseconds <- seconds
651
655
 
652
- @@last_log_buff = LogBuffer.new(@@config.maximum_data_output_width)
656
+ @@last_log_buff = LogBuffer.new(@@config.data_output_width)
653
657
  @@last_log_buff.no_break_append(
654
658
  format(@@config.leave_format, name, filename, lineno, time)
655
659
  )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debugtrace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masato Kokubo
@@ -20,10 +20,12 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - ".rubocop.yml"
22
22
  - CHANGELOG.md
23
- - LICENSE
23
+ - LICENSE.txt
24
24
  - README.md
25
+ - README_ja.md
25
26
  - Rakefile
26
- - debugtrace.yml
27
+ - examples/debugtrace.yml
28
+ - examples/readme-example.rb
27
29
  - lib/debugtrace.rb
28
30
  - lib/debugtrace/common.rb
29
31
  - lib/debugtrace/config.rb
@@ -53,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
55
  - !ruby/object:Gem::Version
54
56
  version: '0'
55
57
  requirements: []
56
- rubygems_version: 3.6.8
58
+ rubygems_version: 3.6.9
57
59
  specification_version: 4
58
60
  summary: DebugTrace-rb is a library that helps debug ruby programs.
59
61
  test_files: []
data/debugtrace.yml DELETED
@@ -1,3 +0,0 @@
1
- logger: file
2
- log_path: logs/debugtrace.log
3
- maximum_data_output_width: 100
File without changes