debugtrace 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9dbd0ded97b4a2115a8f5b86cbac05a7db29da09cefae334091cfbe8652ad1b
4
- data.tar.gz: 7e121c90bbeecf411b3c7079bf900ac646998a3ec08a3a3f71c11faa56e6fc0d
3
+ metadata.gz: 8605af0f8e17c3dffcef132208a051bc3cda499dfb664c669ccc58fe9783055b
4
+ data.tar.gz: 3eb3fca8224a904ea1d9856748feb8ec3b97395ef5c1dff302827de358b0fa9d
5
5
  SHA512:
6
- metadata.gz: ec993125ba921db2f15a4079c57a23735a49a820566b6100e79a3387c9b111a711c4d143eb4350a06913397c3d3a589a5290e4e36bd71bcb352fa16ad591d0ff
7
- data.tar.gz: 32f7121dfb3f6c8fc125a775db3c60eecf53590925df24ef1cbfb0d73327f3c9d830063d1c1058cc1718b561f4ed3f2a332ad25c5dc0c2c36c4e93179cbfacdd
6
+ metadata.gz: c77f35231079526344c403ccd7b2753cfc4a81bb656459acc4a99b69548ad46f65c9f6cf628dc3c516c2c65ceaceabace8d2325f8cb5e1d690d449d5c190c242
7
+ data.tar.gz: 6cee3b4fe007601cab14db3123be8e93db6841c8e95847c8e3724ad00f9ef893b9347de807f4b54764a5726f8b14b87a286cc11168be72e1f27f1f56c5c58cd7
@@ -1,5 +1,5 @@
1
1
  # common.rb
2
- # (C) 2023 Masato Kokubo
2
+ # (C) 2025 Masato Kokubo
3
3
  module Common
4
4
  def self.check_type(value_name, value, type)
5
5
  if !(value_name.is_a? String); raise "Argument value_name (=#{value_name}) must be a String"; end
@@ -1,11 +1,11 @@
1
1
  # config.rb
2
- # (C) 2023 Masato Kokubo
2
+ # (C) 2025 Masato Kokubo
3
3
  require 'yaml'
4
4
  require_relative 'common'
5
5
 
6
6
  class Config
7
7
  def initialize(config_path)
8
- @config_path = Common::check_type('config_path', config_path, String)
8
+ @config_path = Common.check_type('config_path', config_path, String)
9
9
  if File.exist?(@config_path)
10
10
  @config = YAML.load_file(@config_path)
11
11
  else
@@ -16,9 +16,9 @@ class Config
16
16
  @logging_destination = _get_config_value 'logging_destination' , 'STDERR'
17
17
  @logging_format = _get_config_value 'logging_format' , "%2$s %1$s %4$s\n"
18
18
  @logging_datetime_format = _get_config_value 'logging_datetime_format' , '%Y-%m-%d %H:%M:%S.%L%:z'
19
- @enabled = _get_config_value 'enabled?' , true
19
+ @enabled = _get_config_value 'enabled' , true
20
20
  @enter_format = _get_config_value 'enter_format' , 'Enter %1$s (%2$s:%3$d) <- %4$s (%5$s:%6$d)'
21
- @leave_format = _get_config_value 'leave_format' , 'Leave %1$s (%2$s:%3$d) duration: %4$s'
21
+ @leave_format = _get_config_value 'leave_format' , 'Leave %1$s (%2$s:%3$d) duration: %4$.3f ms'
22
22
  @thread_boundary_format = _get_config_value 'thread_boundary_format' , '______________________________ %1$s #%2$s ______________________________'
23
23
  @maximum_indents = _get_config_value 'maximum_indents' , 32
24
24
  @indent_string = _get_config_value 'indent_string' , '| '
@@ -41,34 +41,15 @@ class Config
41
41
  @reflection_nest_limit = _get_config_value 'reflection_nest_limit' , 4
42
42
  end
43
43
 
44
- def config_path ; @config_path ; end
45
- def logger_name ; @logger_name ; end
46
- def logging_destination ; @logging_destination ; end
47
- def logging_format ; @logging_format ; end
48
- def logging_datetime_format ; @logging_datetime_format ; end
49
- def enabled? ; @enabled ; end
50
- def enter_format ; @enter_format ; end
51
- def leave_format ; @leave_format ; end
52
- def thread_boundary_format ; @thread_boundary_format ; end
53
- def maximum_indents ; @maximum_indents ; end
54
- def indent_string ; @indent_string ; end
55
- def data_indent_string ; @data_indent_string ; end
56
- def limit_string ; @limit_string ; end
57
- def non_output_string ; @non_output_string ; end
58
- def cyclic_reference_string ; @cyclic_reference_string ; end
59
- def varname_value_separator ; @varname_value_separator ; end
60
- def key_value_separator ; @key_value_separator ; end
61
- def print_suffix_format ; @print_suffix_format ; end
62
- def count_format ; @count_format ; end
63
- def minimum_output_count ; @minimum_output_count ; end
64
- def length_format ; @length_format ; end
65
- def minimum_output_length ; @minimum_output_length ; end
66
- def maximum_data_output_width; @maximum_data_output_width; end
67
- def bytes_count_in_line ; @bytes_count_in_line ; end
68
- def collection_limit ; @collection_limit ; end
69
- def bytes_limit ; @bytes_limit ; end
70
- def string_limit ; @string_limit ; end
71
- def reflection_nest_limit ; @reflection_nest_limit ; end
44
+ attr_reader :config_path, :logger_name, :logging_destination, :logging_format, :logging_datetime_format,
45
+ :enter_format, :leave_format, :thread_boundary_format, :maximum_indents,
46
+ :indent_string, :data_indent_string, :limit_string, :non_output_string,
47
+ :cyclic_reference_string, :varname_value_separator, :key_value_separator,
48
+ :print_suffix_format, :count_format, :minimum_output_count, :length_format,
49
+ :minimum_output_length, :maximum_data_output_width, :bytes_count_in_line,
50
+ :collection_limit, :bytes_limit, :string_limit, :reflection_nest_limit
51
+
52
+ def enabled? = @enabled
72
53
 
73
54
  private
74
55
 
@@ -77,14 +58,14 @@ class Config
77
58
  # @param defalut_value (Object): Value to return when the value related the key is undefined
78
59
  # @return Object: Value related the key
79
60
  def _get_config_value(key, defalut_value)
80
- Common::check_type('key', key, String)
61
+ Common.check_type('key', key, String)
81
62
  value = defalut_value
82
- if @config != nil
63
+ unless @config.nil?
83
64
  value = @config[key]
84
- if value == nil
65
+ if value.nil?
85
66
  value = defalut_value
86
67
  else
87
- Common::check_type("config[#{key}]", value, defalut_value.class)
68
+ Common.check_type("config[#{key}]", value, defalut_value.class)
88
69
  end
89
70
  end
90
71
  value
@@ -1,5 +1,5 @@
1
1
  # log_buffer.rb
2
- # (C) 2023 Masato Kokubo
2
+ # (C) 2025 Masato Kokubo
3
3
  require_relative 'common'
4
4
 
5
5
  # Buffers logs.
@@ -11,14 +11,8 @@ class LogBuffer
11
11
  @log = Common.check_type('log', log, String)
12
12
  end
13
13
 
14
- def nest_level
15
- @nest_level
16
- end
14
+ attr_reader :nest_level, :log
17
15
 
18
- def log
19
- @log
20
- end
21
-
22
16
  def to_s
23
17
  "(LogBuffer.LevelAndLog){nest_level: #{@nest_level}, log: \"#{@log}\"}"
24
18
  end
@@ -34,12 +28,12 @@ class LogBuffer
34
28
  @lines = []
35
29
 
36
30
  # buffer for a line of logs
37
- @last_line = ""
31
+ @last_line = ''
38
32
  end
39
33
 
40
34
  # Breaks the current line.
41
35
  def line_feed
42
- @lines << LevelAndLog.new(@nest_level + @append_nest_level, @last_line.rstrip())
36
+ @lines << LevelAndLog.new(@nest_level + @append_nest_level, @last_line.rstrip)
43
37
  @append_nest_level = 0
44
38
  @last_line = ''
45
39
  end
@@ -63,11 +57,9 @@ class LogBuffer
63
57
  def append(value, nest_level = 0, no_break = false)
64
58
  Common.check_type('nest_level', nest_level, Integer)
65
59
  Common.check_type('no_break', no_break, TrueClass)
66
- if value != nil
60
+ unless value.nil?
67
61
  string = value.to_s
68
- if !no_break && length > 0 && length + string.length > @maximum_data_output_width
69
- line_feed()
70
- end
62
+ line_feed if !no_break && length > 0 && length + string.length > @maximum_data_output_width
71
63
  @append_nest_level = nest_level
72
64
  @last_line += string
73
65
  end
@@ -83,22 +75,18 @@ class LogBuffer
83
75
  end
84
76
 
85
77
  # Appends lines of another LogBuffer.
86
- # @param
87
- # @param separator (String): The separator string to append if not ""
78
+ # @param
79
+ # @param separator (String): The separator string to append if not ''
88
80
  # @param buff (LogBuffer): Another LogBuffer
89
81
  # @returns LogBuffer: This object
90
82
  def append_buffer(separator, buff)
91
83
  Common.check_type('separator', separator, String)
92
84
  Common.check_type('buff', buff, LogBuffer)
93
- if separator != ""
94
- append(separator, 0, true)
95
- end
85
+ append(separator, 0, true) if separator != ''
96
86
  index = 0
97
87
  for line in buff.lines
98
- if index > 0
99
- line_feed()
100
- end
101
- append(line.nest_level, line.log, index == 0 && separator != "")
88
+ line_feed if index > 0
89
+ append(line.log, line.nest_level, index == 0 && separator != '')
102
90
  index += 1
103
91
  end
104
92
  self
@@ -117,9 +105,7 @@ class LogBuffer
117
105
  # A list of tuple of data indentation level && log string.
118
106
  def lines
119
107
  lines = @lines.dup
120
- if length > 0
121
- lines << LevelAndLog.new(@nest_level, @last_line)
122
- end
108
+ lines << LevelAndLog.new(@nest_level, @last_line) if length > 0
123
109
  lines
124
110
  end
125
111
  end
@@ -1,5 +1,5 @@
1
1
  # state.rb
2
- # (C) 2023 Masato Kokubo
2
+ # (C) 2025 Masato Kokubo
3
3
  require_relative 'common'
4
4
 
5
5
  # Have the trace state for a thread
@@ -1,5 +1,6 @@
1
+ # (C) 2025 Masato Kokubo
1
2
  # frozen_string_literal: true
2
3
 
3
4
  module DebugTrace
4
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
5
6
  end
data/lib/debugtrace.rb CHANGED
@@ -1,6 +1,5 @@
1
- # main.rb
1
+ # debugtrace.rb
2
2
  # (C) 2025 Masato Kokubo
3
- require 'thread'
4
3
  require 'logger'
5
4
 
6
5
  # Require necessary files
@@ -9,19 +8,14 @@ require_relative 'debugtrace/common'
9
8
  require_relative 'debugtrace/config'
10
9
  require_relative 'debugtrace/log_buffer'
11
10
  require_relative 'debugtrace/loggers'
12
- require_relative 'debugtrace/print_options'
13
11
  require_relative 'debugtrace/state'
14
12
 
15
13
  module DebugTrace
16
- # class Error < StandardError; end
17
- @@AUTHOR = 'Masato Kokubo <masatokokubo@gmail.com>'
18
- # @@VERSION = '1.0.0 dev 1'
19
-
20
14
  # Configuration values
21
15
  @@config = nil
22
16
 
23
17
  def self.config
24
- return @@config
18
+ @@config
25
19
  end
26
20
 
27
21
  # A Mutex for thread safety
@@ -61,22 +55,22 @@ module DebugTrace
61
55
  Pr._print("debugtrace: (#{@@config.config_path}) logger = #{@@config.logger_name} is unknown", STDERR)
62
56
  end
63
57
 
64
- if @@config.enabled?
65
- ruby_version = RUBY_VERSION
66
- @@logger.print("DebugTrace-rb #{DebugTrace::VERSION} on Ruby #{ruby_version}")
67
- @@logger.print(" config file path: #{@@config.config_path}")
68
- @@logger.print(" logger: #{@@logger}")
69
- end
58
+ return unless @@config.enabled?
59
+
60
+ ruby_version = RUBY_VERSION
61
+ @@logger.print("DebugTrace-rb #{DebugTrace::VERSION} on Ruby #{ruby_version}")
62
+ @@logger.print(" config file path: #{@@config.config_path}")
63
+ @@logger.print(" logger: #{@@logger}")
70
64
  end
71
65
 
72
66
  class PrintOptions
73
- attr_reader :force_reflection, :output_private, :output_method, :minimum_output_count, :minimum_output_length,
67
+ attr_reader :force_reflection,
68
+ # :output_private, :output_method,
69
+ :minimum_output_count, :minimum_output_length,
74
70
  :collection_limit, :bytes_limit, :string_limit, :reflection_nest_limit
75
71
 
76
72
  def initialize(
77
73
  force_reflection,
78
- output_private,
79
- output_method,
80
74
  minimum_output_count,
81
75
  minimum_output_length,
82
76
  collection_limit,
@@ -85,8 +79,6 @@ module DebugTrace
85
79
  reflection_nest_limit
86
80
  )
87
81
  @force_reflection = force_reflection
88
- @output_private = output_private
89
- @output_method = output_method
90
82
  @minimum_output_count = minimum_output_count == -1 ? DebugTrace.config.minimum_output_count : minimum_output_count
91
83
  @minimum_output_length = minimum_output_length == -1 ? DebugTrace.config.minimum_output_length : minimum_output_length
92
84
  @collection_limit = collection_limit == -1 ? DebugTrace.config.collection_limit : collection_limit
@@ -117,7 +109,7 @@ module DebugTrace
117
109
 
118
110
  def self.to_string(name, value, print_options)
119
111
  buff = LogBuffer.new(@@config.maximum_data_output_width)
120
-
112
+
121
113
  separator = ''
122
114
  unless name.empty?
123
115
  buff.append(name)
@@ -126,31 +118,34 @@ module DebugTrace
126
118
 
127
119
  case value
128
120
  when nil
129
- # None
121
+ # nil
130
122
  buff.no_break_append(separator).append('nil')
131
- when String
123
+ when String # ''.class # String
132
124
  # String
133
125
  value_buff = to_string_str(value, print_options)
134
126
  buff.append_buffer(separator, value_buff)
135
- when Integer, Float, Date, Time, DateTime
136
- # int, float, Date, Time, DateTime
127
+ when DateTime
128
+ # DateTime
129
+ buff.no_break_append(separator).append(value.strftime('%Y-%m-%d %H:%M-%S.%L%:z'))
130
+ when Date
131
+ # DateTime
132
+ buff.no_break_append(separator).append(value.strftime('%Y-%m-%d'))
133
+ when Time
134
+ # DateTime
135
+ buff.no_break_append(separator).append(value.strftime('%H:%M-%S.%L%:z'))
136
+ when Integer, Float
137
+ # Integer, Float, Date, Time
137
138
  buff.no_break_append(separator).append(value.to_s)
138
139
  when Array, Set, Hash
139
- # list, set, tuple, dict
140
- value_buff = to_string_iterable(value, print_options)
140
+ # Array, Set, Hash
141
+ value_buff = to_string_enumerable(value, print_options)
141
142
  buff.append_buffer(separator, value_buff)
142
143
  else
143
- has_str, has_repr = has_str_repr_method(value)
144
144
  value_buff = LogBuffer.new(@@config.maximum_data_output_width)
145
- if !print_options.force_reflection && (has_str || has_repr)
145
+ if !print_options.force_reflection && has_to_s_method?(value)
146
146
  # has to_s or inspect method
147
- if has_repr
148
- value_buff.append('inspect(): ')
149
- value_buff.no_break_append(value.inspect)
150
- else
151
- value_buff.append('to_s(): ')
152
- value_buff.no_break_append(value.to_s)
153
- end
147
+ value_buff.append('to_s: ')
148
+ value_buff.no_break_append(value.to_s)
154
149
  buff.append_buffer(separator, value_buff)
155
150
  else
156
151
  # use reflection
@@ -168,7 +163,7 @@ module DebugTrace
168
163
  buff.append_buffer(separator, value_buff)
169
164
  end
170
165
  end
171
-
166
+
172
167
  buff
173
168
  end
174
169
 
@@ -177,19 +172,19 @@ module DebugTrace
177
172
  has_double_quote = false
178
173
  single_quote_buff = LogBuffer.new(@@config.maximum_data_output_width)
179
174
  double_quote_buff = LogBuffer.new(@@config.maximum_data_output_width)
180
-
175
+
181
176
  if value.length >= @@config.minimum_output_length
182
- single_quote_buff.no_break_append("(")
183
- single_quote_buff.no_break_append(sprintf(@@config.length_format, value.length))
184
- single_quote_buff.no_break_append(")")
185
- double_quote_buff.no_break_append("(")
186
- double_quote_buff.no_break_append(sprintf(@@config.length_format, value.length))
187
- double_quote_buff.no_break_append(")")
177
+ single_quote_buff.no_break_append('(')
178
+ single_quote_buff.no_break_append(format(@@config.length_format, value.length))
179
+ single_quote_buff.no_break_append(')')
180
+ double_quote_buff.no_break_append('(')
181
+ double_quote_buff.no_break_append(format(@@config.length_format, value.length))
182
+ double_quote_buff.no_break_append(')')
188
183
  end
189
-
184
+
190
185
  single_quote_buff.no_break_append("'")
191
186
  double_quote_buff.no_break_append('"')
192
-
187
+
193
188
  count = 1
194
189
  value.each_char do |char|
195
190
  if count > print_options.string_limit
@@ -206,33 +201,37 @@ module DebugTrace
206
201
  single_quote_buff.no_break_append(char)
207
202
  double_quote_buff.no_break_append("\\\"")
208
203
  has_double_quote = true
209
- when '\\'
210
- single_quote_buff.no_break_append('\\\\')
211
- double_quote_buff.no_break_append('\\\\')
212
- when '\n'
213
- single_quote_buff.no_break_append('\\n')
214
- double_quote_buff.no_break_append('\\n')
215
- when '\r'
216
- single_quote_buff.no_break_append('\\r')
217
- double_quote_buff.no_break_append('\\r')
218
- when '\t'
219
- single_quote_buff.no_break_append('\\t')
220
- double_quote_buff.no_break_append('\\t')
221
- when ("\0".."\37").include?(char)
222
- num_str = format('%02X', char.ord)
223
- single_quote_buff.no_break_append("\\x" + num_str)
224
- double_quote_buff.no_break_append("\\x" + num_str)
204
+ when "\\"
205
+ single_quote_buff.no_break_append("\\\\")
206
+ double_quote_buff.no_break_append("\\\\")
207
+ when "\n"
208
+ single_quote_buff.no_break_append("\\n")
209
+ double_quote_buff.no_break_append("\\n")
210
+ when "\r"
211
+ single_quote_buff.no_break_append("\\r")
212
+ double_quote_buff.no_break_append("\\r")
213
+ when "\t"
214
+ single_quote_buff.no_break_append("\\t")
215
+ double_quote_buff.no_break_append("\\t")
225
216
  else
226
- single_quote_buff.no_break_append(char)
227
- double_quote_buff.no_break_append(char)
217
+ char_ord = char.ord
218
+ if char_ord >= 0x00 && char_ord <= 0x1F || char_ord == 0x7F
219
+ num_str = format('%02X', char_ord)
220
+ single_quote_buff.no_break_append("\\x" + num_str)
221
+ double_quote_buff.no_break_append("\\x" + num_str)
222
+ else
223
+ single_quote_buff.no_break_append(char)
224
+ double_quote_buff.no_break_append(char)
225
+ end
228
226
  end
229
227
  count += 1
230
228
  end
231
-
229
+
232
230
  double_quote_buff.no_break_append('"')
233
231
  single_quote_buff.no_break_append("'")
234
-
232
+
235
233
  return double_quote_buff if has_single_quote && !has_double_quote
234
+
236
235
  single_quote_buff
237
236
  end
238
237
 
@@ -240,7 +239,7 @@ module DebugTrace
240
239
  bytes_length = value.length
241
240
  buff = LogBuffer.new(@@config.maximum_data_output_width)
242
241
  buff.no_break_append('(')
243
-
242
+
244
243
  if value.is_a?(String)
245
244
  buff.no_break_append('bytes')
246
245
  elsif value.is_a?(Array)
@@ -249,7 +248,7 @@ module DebugTrace
249
248
 
250
249
  if bytes_length >= @@config.minimum_output_length
251
250
  buff.no_break_append(' ')
252
- buff.no_break_append(sprintf(@@config.length_format, bytes_length))
251
+ buff.no_break_append(format(@@config.length_format, bytes_length))
253
252
  end
254
253
 
255
254
  buff.no_break_append(') [')
@@ -264,20 +263,18 @@ module DebugTrace
264
263
  chars = ''
265
264
  count = 0
266
265
  value.each_byte do |element|
267
- if count != 0 && count % @@config.bytes_count_in_line == 0
268
- if multi_lines
269
- buff.no_break_append('| ')
270
- buff.no_break_append(chars)
271
- buff.line_feed
272
- chars = ''
273
- end
266
+ if count != 0 && count % @@config.bytes_count_in_line == 0 && multi_lines
267
+ buff.no_break_append('| ')
268
+ buff.no_break_append(chars)
269
+ buff.line_feed
270
+ chars = ''
274
271
  end
275
272
  if count >= print_options.bytes_limit
276
273
  buff.no_break_append(@@config.limit_string)
277
274
  break
278
275
  end
279
- buff.no_break_append(sprintf('%02X ', element))
280
- chars += (element >= 0x20 && element <= 0x7E) ? element.chr : '.'
276
+ buff.no_break_append(format('%02X ', element))
277
+ chars += element >= 0x20 && element <= 0x7E ? element.chr : '.'
281
278
  count += 1
282
279
  end
283
280
 
@@ -297,13 +294,13 @@ module DebugTrace
297
294
  end
298
295
  buff.no_break_append(']')
299
296
 
300
- return buff
297
+ buff
301
298
  end
302
299
 
303
300
  def self.to_string_reflection(value, print_options)
304
301
  buff = LogBuffer.new(@@config.maximum_data_output_width)
305
302
 
306
- buff.append(_get_type_name(value))
303
+ buff.append(get_type_name(value))
307
304
 
308
305
  body_buff = to_string_reflection_body(value, print_options)
309
306
 
@@ -323,72 +320,52 @@ module DebugTrace
323
320
  end
324
321
  buff.no_break_append('}')
325
322
 
326
- return buff
323
+ buff
327
324
  end
328
325
 
329
326
  def self.to_string_reflection_body(value, print_options)
330
327
  buff = LogBuffer.new(@@config.maximum_data_output_width)
331
328
 
332
- members = []
333
- begin
334
- base_members = value.methods(false).map { |m| [m, value.send(m)] }
335
- members = base_members.select do |m|
336
- name, _ = m
337
- !name.start_with?('__') || !name.end_with?('__') &&
338
- (print_options.output_method || !value.method(name).owner.nil?) &&
339
- (print_options.output_private || !name.start_with?('_'))
340
- end
341
- rescue => ex
342
- buff.append(ex.to_s)
343
- return buff
344
- end
329
+ variables = value.instance_variables
345
330
 
346
331
  multi_lines = false
347
332
  index = 0
348
- members.each do |member|
349
- if index > 0
350
- buff.no_break_append(', ')
351
- end
333
+ variables.each do |variable|
334
+ buff.no_break_append(', ') if index > 0
352
335
 
353
- name, value = member
336
+ var_value = value.instance_variable_get(variable)
354
337
  member_buff = LogBuffer.new(@@config.maximum_data_output_width)
355
- member_buff.append(name)
356
- member_buff.append_buffer(@@config.key_value_separator, to_string('', value, print_options))
357
- if index > 0 && (multi_lines || member_buff.multi_lines?)
358
- buff.line_feed
359
- end
338
+ member_buff.append(variable)
339
+ member_buff.append_buffer(@@config.key_value_separator, to_string('', var_value, print_options))
340
+ buff.line_feed if index > 0 && (multi_lines || member_buff.multi_lines?)
360
341
  buff.append_buffer('', member_buff)
361
342
 
362
343
  multi_lines = member_buff.multi_lines?
363
344
  index += 1
364
345
  end
365
346
 
366
- return buff
347
+ buff
367
348
  end
368
349
 
369
- def self.to_string_iterable(values, print_options)
370
- open_char = '{' # set, frozenset, dict
371
- close_char = '}'
372
-
373
- if values.is_a?(Array)
374
- # list
375
- open_char = '['
350
+ def self.to_string_enumerable(values, print_options)
351
+ open_char = '[' # Array
352
+ close_char = ']'
353
+
354
+ if values.is_a?(Hash)
355
+ # Array
356
+ open_char = '{'
357
+ close_char = '}'
358
+ elsif values.is_a?(Set)
359
+ # Sete
360
+ open_char = 'Set['
376
361
  close_char = ']'
377
- elsif values.is_a?(Tuple)
378
- # tuple
379
- open_char = '('
380
- close_char = ')'
381
362
  end
382
363
 
383
364
  buff = LogBuffer.new(@@config.maximum_data_output_width)
384
- buff.append(_get_type_name(values, values.length))
365
+ buff.append(get_type_name(values, values.length))
385
366
  buff.no_break_append(open_char)
386
367
 
387
- body_buff = to_string_iterable_body(values, print_options)
388
- if open_char == '(' && values.length == 1
389
- # A tuple with 1 element
390
- body_buff.no_break_append(',')
391
- end
368
+ body_buff = to_string_enumerable_body(values, print_options)
392
369
 
393
370
  multi_lines = body_buff.multi_lines? || buff.length + body_buff.length > @@config.maximum_data_output_width
394
371
 
@@ -406,48 +383,41 @@ module DebugTrace
406
383
 
407
384
  buff.no_break_append(close_char)
408
385
 
409
- return buff
386
+ buff
410
387
  end
411
388
 
412
- def self.to_string_iterable_body(values, print_options)
389
+ def self.to_string_enumerable_body(values, print_options)
413
390
  buff = LogBuffer.new(@@config.maximum_data_output_width)
414
391
 
415
392
  multi_lines = false
416
393
  index = 0
417
394
 
418
395
  values.each do |element|
419
- if index > 0
420
- buff.no_break_append(', ')
421
- end
396
+ buff.no_break_append(', ') if index > 0
422
397
 
423
398
  if index >= print_options.collection_limit
424
399
  buff.append(@@config.limit_string)
425
400
  break
426
401
  end
427
402
 
428
- element_buff = LogBuffer.new(@@config.maximum_data_output_width)
429
- if values.is_a?(Hash)
430
- # dictionary
431
- element_buff = to_string_key_value(element, values[element], print_options)
432
- else
433
- # list, set, frozenset, or tuple
434
- element_buff = to_string('', element, print_options)
435
- end
403
+ element_buff = if values.is_a?(Hash)
404
+ # Hash
405
+ to_string_key_value(element[0], element[1], print_options)
406
+ else
407
+ # Array
408
+ to_string('', element, print_options)
409
+ end
436
410
 
437
- if index > 0 && (multi_lines || element_buff.multi_lines?)
438
- buff.line_feed
439
- end
411
+ buff.line_feed if index > 0 && (multi_lines || element_buff.multi_lines?)
440
412
  buff.append_buffer('', element_buff)
441
413
 
442
414
  multi_lines = element_buff.multi_lines?
443
415
  index += 1
444
416
  end
445
417
 
446
- if values.is_a?(Hash) && values.empty?
447
- buff.no_break_append(':')
448
- end
418
+ buff.no_break_append(':') if values.is_a?(Hash) && values.empty?
449
419
 
450
- return buff
420
+ buff
451
421
  end
452
422
 
453
423
  def self.to_string_key_value(key, value, print_options)
@@ -455,94 +425,58 @@ module DebugTrace
455
425
  key_buff = to_string('', key, print_options)
456
426
  value_buff = to_string('', value, print_options)
457
427
  buff.append_buffer('', key_buff).append_buffer(@@config.key_value_separator, value_buff)
458
- return buff
428
+ buff
459
429
  end
460
430
 
461
431
  def self.get_type_name(value, count = -1)
462
- value_type = value.class
463
- type_name = get_simple_type_name(value.class, 0)
464
- if ['Array', 'Hash', 'Set', 'Tuple'].include?(type_name)
465
- type_name = ''
466
- end
432
+ type_name = value.class.to_s
433
+ type_name = '' if %w[Array Hash Set].include?(type_name)
467
434
 
468
435
  if count >= @@config.minimum_output_count
469
436
  type_name += ' ' unless type_name.empty?
470
437
  type_name += @@config.count_format % count
471
438
  end
472
439
 
473
- if !type_name.empty?
474
- type_name = "(#{type_name})"
475
- end
476
- return type_name
477
- end
478
-
479
- def self.get_simple_type_name(value_type, nest)
480
- type_name = nest == 0 ? value_type.to_s : value_type.name
481
- if type_name.start_with?("<Class '")
482
- type_name = type_name[8..-1]
483
- elsif type_name.start_with?("<Enum '")
484
- type_name = 'enum ' + type_name[7..-1]
485
- end
486
- if type_name.end_with?("'>")
487
- type_name = type_name[0..-3]
488
- end
489
-
490
- base_names = value_type.ancestors.reject { |base| base == Object }
491
- base_names = base_names.map { |base| get_simple_type_name(base, nest + 1) }
492
-
493
- if base_names.any?
494
- type_name += '(' + base_names.join(', ') + ')'
495
- end
496
-
497
- return type_name
440
+ type_name
498
441
  end
499
442
 
500
- def self.has_str_repr_method(value)
443
+ def self.has_to_s_method?(value)
501
444
  begin
502
- members = value.methods
503
- has_str = members.include?(:to_s)
504
- has_repr = members.include?(:inspect)
505
- return has_str, has_repr
445
+ value.public_method('to_s')
506
446
  rescue
507
- return false, false
447
+ return false
508
448
  end
449
+ return true
509
450
  end
510
451
 
511
- # def self.get_frame_summary(limit)
512
- # begin
513
- # raise 'RuntimeError'
514
- # rescue => e
515
- # return caller_locations(limit: limit).first
516
- # end
517
- # return nil
518
- # end
519
-
520
452
  @@before_thread_id = nil
521
453
 
522
454
  def self.print_start
455
+ if @@before_thread_id == nil
456
+ DebugTrace.initialize
457
+ return unless @@config.enabled?
458
+ end
459
+
523
460
  thread = Thread.current
524
461
  thread_id = thread.object_id
525
- if thread_id != @@before_thread_id
526
- # Thread changing
527
- @@logger.print('')
528
- @@logger.print(@@config.thread_boundary_format % [thread.name, thread.object_id])
529
- @@logger.print('')
530
- @@before_thread_id = thread_id
531
- end
462
+ return unless thread_id != @@before_thread_id
463
+
464
+ # Thread changing
465
+ @@logger.print('')
466
+ @@logger.print(format(@@config.thread_boundary_format, thread.name, thread_id))
467
+ @@logger.print('')
468
+ @@before_thread_id = thread_id
532
469
  end
533
470
 
534
471
  @@DO_NOT_OUTPUT = 'Do not output'
535
472
 
536
473
  def self.print(name, value = @@DO_NOT_OUTPUT, force_reflection: false,
537
- output_private: false, output_method: false,
538
- minimum_output_count: -1, minimum_output_length: -1,
539
- collection_limit: -1, bytes_limit: -1,
540
- string_limit: -1, reflection_nest_limit: -1)
541
-
542
- return value unless @@config.enabled?
543
-
544
- Mutex.new.synchronize do
474
+ minimum_output_count: -1, minimum_output_length: -1,
475
+ collection_limit: -1, bytes_limit: -1,
476
+ string_limit: -1, reflection_nest_limit: -1)
477
+ @@thread_mutex.synchronize do
545
478
  print_start
479
+ return value unless @@config.enabled?
546
480
 
547
481
  state = current_state
548
482
  @@reflected_objects.clear
@@ -556,22 +490,22 @@ module DebugTrace
556
490
  else
557
491
  # with value
558
492
  print_options = PrintOptions.new(
559
- force_reflection, output_private, output_method,
493
+ force_reflection,
560
494
  minimum_output_count, minimum_output_length,
561
- collection_limit, bytes_limit, string_limit, reflection_nest_limit
495
+ collection_limit, bytes_limit,
496
+ string_limit, reflection_nest_limit
562
497
  )
563
498
  @@last_log_buff = to_string(name, value, print_options)
564
499
  end
565
500
 
566
501
  # append print suffix
567
- # frame_summary = get_frame_summary(3)
568
502
  location = caller_locations(3, 3)[0]
569
- name = location != nil ? location.base_label : ''
570
- filename = location != nil ? File.basename(location.absolute_path) : ''
571
- lineno = location != nil ? location.lineno : 0
503
+ name = !location.nil? ? location.base_label : ''
504
+ filename = !location.nil? ? File.basename(location.absolute_path) : ''
505
+ lineno = !location.nil? ? location.lineno : 0
572
506
 
573
507
  @@last_log_buff.no_break_append(
574
- @@config.print_suffix_format % [name, filename, lineno]
508
+ format(@@config.print_suffix_format, name, filename, lineno)
575
509
  )
576
510
 
577
511
  @@last_log_buff.line_feed
@@ -585,28 +519,25 @@ module DebugTrace
585
519
  end
586
520
  end
587
521
 
588
- return value
522
+ value
589
523
  end
590
524
 
591
-
592
525
  def self.enter
593
- return unless @@config.enabled?
594
- Mutex.new.synchronize do
526
+ @@thread_mutex.synchronize do
595
527
  print_start
528
+ return unless @@config.enabled?
596
529
 
597
530
  state = current_state
598
531
 
599
- # frame_summary = get_frame_summary(4)
600
532
  location = caller_locations(3, 3)[0]
601
- name = location != nil ? location.base_label : ''
602
- filename = location != nil ? File.basename(location.absolute_path) : ''
603
- lineno = location != nil ? location.lineno : 0
533
+ name = !location.nil? ? location.base_label : ''
534
+ filename = !location.nil? ? File.basename(location.absolute_path) : ''
535
+ lineno = !location.nil? ? location.lineno : 0
604
536
 
605
- # parent_frame_summary = get_frame_summary(5)
606
537
  parent_location = caller_locations(4, 4)[0]
607
- parent_name = parent_location != nil ? parent_location.base_label : ''
608
- parent_filename = parent_location != nil ? File.basename(parent_location.absolute_path) : ''
609
- parent_lineno = parent_location != nil ? parent_location.lineno : 0
538
+ parent_name = !parent_location.nil? ? parent_location.base_label : ''
539
+ parent_filename = !parent_location.nil? ? File.basename(parent_location.absolute_path) : ''
540
+ parent_lineno = !parent_location.nil? ? parent_location.lineno : 0
610
541
 
611
542
  indent_string = get_indent_string(state.nest_level, 0)
612
543
  if state.nest_level < state.previous_nest_level || @@last_log_buff.multi_lines?
@@ -615,7 +546,7 @@ module DebugTrace
615
546
 
616
547
  @@last_log_buff = LogBuffer.new(@@config.maximum_data_output_width)
617
548
  @@last_log_buff.no_break_append(
618
- @@config.enter_format % [name, filename, lineno, parent_name, parent_filename, parent_lineno]
549
+ format(@@config.enter_format, name, filename, lineno, parent_name, parent_filename, parent_lineno)
619
550
  )
620
551
  @@last_log_buff.line_feed
621
552
  @@logger.print(indent_string + @@last_log_buff.lines[0].log)
@@ -625,10 +556,9 @@ module DebugTrace
625
556
  end
626
557
 
627
558
  def self.leave
628
- return unless @@config.enabled?
629
-
630
- Mutex.new.synchronize do
559
+ @@thread_mutex.synchronize do
631
560
  print_start
561
+ return unless @@config.enabled?
632
562
 
633
563
  state = current_state
634
564
 
@@ -641,11 +571,11 @@ module DebugTrace
641
571
  @@logger.print(get_indent_string(state.nest_level, 0)) # Empty Line
642
572
  end
643
573
 
644
- time = Time.now.utc - state.down_nest
574
+ time = (Time.now.utc - state.down_nest) * 1000 # milliseconds <- seconds
645
575
 
646
576
  @@last_log_buff = LogBuffer.new(@@config.maximum_data_output_width)
647
577
  @@last_log_buff.no_break_append(
648
- @@config.leave_format % [name, filename, lineno, time]
578
+ format(@@config.leave_format, name, filename, lineno, time)
649
579
  )
650
580
  @@last_log_buff.line_feed
651
581
  @@logger.print(get_indent_string(state.nest_level, 0) + @@last_log_buff.lines[0].log)
@@ -657,13 +587,10 @@ module DebugTrace
657
587
  buff_string = lines.map { |line| _config.data_indent_string * line[0] + line[1] }.join("\n")
658
588
 
659
589
  state = nil
660
- Mutex.new.synchronize do
590
+ @@thread_mutex.synchronize do
661
591
  state = current_state
662
592
  end
663
593
 
664
594
  "#{get_indent_string(state.nest_level, 0)}#{buff_string}"
665
595
  end
666
596
  end
667
-
668
- DebugTrace.initialize
669
-
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debugtrace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masato Kokubo
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-13 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Insert DebugTrace.enter and Debug.leave at the beginning and end of the
13
13
  function you want to debug, and Debug.print('foo', foo) if there are any variables
@@ -28,10 +28,8 @@ files:
28
28
  - lib/debugtrace/config.rb
29
29
  - lib/debugtrace/log_buffer.rb
30
30
  - lib/debugtrace/loggers.rb
31
- - lib/debugtrace/print_options.rb
32
31
  - lib/debugtrace/state.rb
33
32
  - lib/debugtrace/version.rb
34
- - lib/temp.rb
35
33
  - sig/debugtrace.rbs
36
34
  homepage: https://github.com/MasatoKokubo/DebugTrace-rb
37
35
  licenses:
@@ -54,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
52
  - !ruby/object:Gem::Version
55
53
  version: '0'
56
54
  requirements: []
57
- rubygems_version: 3.6.6
55
+ rubygems_version: 3.6.8
58
56
  specification_version: 4
59
57
  summary: DebugTrace-rb is a library that helps debug ruby programs.
60
58
  test_files: []
@@ -1,24 +0,0 @@
1
- # print_options.rb
2
- # (C) 2023 Masato Kokubo
3
- # Hold output option values.
4
- class PrintOptions
5
- # Initializes this object.
6
- # @param force_reflection (bool): If true, outputs using reflection even if it has a @_str__or @_repr__ method
7
- # @param output_private (bool): If true, also outputs private members when using reflection
8
- # @param output_method (bool): If true, also outputs method members when using reflection
9
- # @param collection_limit (Integer): Output limit of collection elements (overrides debugtarace.ini value)
10
- # @param string_limit (Integer): Output limit of string characters (overrides debugtarace.ini value)
11
- # @param bytes_limit (Integer): Output limit of byte array elements (overrides debugtarace.ini value)
12
- # @param reflection_nest_limit (Integer): Nest limits when using reflection (overrides debugtarace.ini value)
13
- def initialize(
14
- force_reflection, output_private, output_method,
15
- collection_limit, string_limit, bytes_limit, reflection_nest_limit)
16
- @force_reflection = force_reflection
17
- @output_private = output_private
18
- @output_method = output_method
19
- @collection_limit = collection_limit == -1 ? @config.collection_limit : collection_limit
20
- @string_limit = string_limit == -1 ? @config.string_limit : string_limit
21
- @bytes_limit = bytes_limit == -1 ? @config.bytes_limit : bytes_limit
22
- @reflection_nest_limit = reflection_nest_limit == -1 ? @config.reflection_nest_limit : reflection_nest_limit
23
- end
24
- end
data/lib/temp.rb DELETED
@@ -1,74 +0,0 @@
1
- class DebugTrace
2
- # Outputs an entering log when initializing and outputs a leaving log when deleting.
3
- #
4
- # 初期化時に進入ログを出力し、削除時に退出ログを出力します。
5
-
6
- attr_accessor :name, :filename, :lineno
7
-
8
- def initialize(invoker)
9
- return unless @@config.is_enabled
10
-
11
- Mutex.new.synchronize do
12
- print_start
13
-
14
- state = current_state
15
- if invoker.nil?
16
- @name = ''
17
- else
18
- @name = invoker.class.name
19
- if @name == 'Class'
20
- @name = invoker.to_s
21
- end
22
- @name += '.'
23
- end
24
-
25
- # frame_summary = get_frame_summary(4)
26
- location = caller_locations(1, 1)[0]
27
- @name += location.base_label
28
- @filename = File.basename(location.absolute_path)
29
- @lineno = location.lineno
30
-
31
- # parent_frame_summary = get_frame_summary(5)
32
- parent_location = caller_locations(2, 2)[0]
33
- parent_filename = File.basename(parent_location.absolute_path)
34
- parent_lineno = parent_location.lineno
35
-
36
- indent_string = get_indent_string(state.nest_level, 0)
37
- if state.nest_level < state.previous_nest_level || _last_print_buff.is_multi_lines
38
- _logger.print(indent_string) # Empty Line
39
- end
40
-
41
- _last_print_buff = LogBuffer.new(@@config.maximum_data_output_width)
42
- _last_print_buff.no_break_append(
43
- @@config.enter_format % [@name, @filename, @lineno, parent_filename, parent_lineno]
44
- )
45
- _last_print_buff.line_feed
46
- _logger.print(indent_string + _last_print_buff.lines[0][1])
47
-
48
- state.up_nest
49
- end
50
- end
51
-
52
- def finalize
53
- return unless @@config.is_enabled
54
-
55
- Mutex.new.synchronize do
56
- print_start
57
-
58
- state = current_state
59
-
60
- if _last_print_buff.is_multi_lines
61
- _logger.print(get_indent_string(state.nest_level, 0)) # Empty Line
62
- end
63
-
64
- time = Time.now.utc - state.down_nest
65
-
66
- _last_print_buff = LogBuffer.new(@@config.maximum_data_output_width)
67
- _last_print_buff.no_break_append(
68
- @@config.leave_format % [@name, @filename, @lineno, time]
69
- )
70
- _last_print_buff.line_feed
71
- _logger.print(get_indent_string(state.nest_level, 0) + _last_print_buff.lines[0][1])
72
- end
73
- end
74
- end