debugtrace 0.2.2 → 0.2.4

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: 5937a3c3f6b9d19ab3bb040ef763ada32e262c5046532331a7082f3edc3128cb
4
- data.tar.gz: 31b5c3027993f50d06409ec9c54d55adff5e68563a8f02b66ae6e618027ec550
3
+ metadata.gz: ce8fe4113e5e6d49353414a4e204e96fd0bb18f303972a5e4aef48e0e7fa07d5
4
+ data.tar.gz: 0cdba9d53b9edbed09ef2ed76a76756abdc57fe5fbabd7ab886d06e3ab1d1623
5
5
  SHA512:
6
- metadata.gz: 33e2b5d74dcfcabfc0868652118694478e47ad5c6066e70c75090b50db7dc61008cfdfe5d4a9b438d7d7a2df91be97110d3766bfbbbd43536dfbaeacf0414c22
7
- data.tar.gz: bd78a8be204cd636d3104d06e3ff777ade2458f0492c5e50fd6f115f7108d290d4348cf02454721ca2680ef6a872455122b50e95fa836e8f9ed7efb1a17e7e6b
6
+ metadata.gz: cbdba0458650b624c0a7e6872c294f578e855fd3cb5d6e050ec4801a356a94fd29b3c3f5a00e01ef155860a8cde98e7b093c2f5f1b1cf5f0925c1904536e17d9
7
+ data.tar.gz: 34acc1fc0519aea58496c807f7716944696f596f2810d6403cee8c9e7b1184f29579420ab58cbe3a310d895e43ee9b351f90112c5d9154f71dfb5983075b58a1
@@ -1,22 +1,30 @@
1
1
  # common.rb
2
2
  # (C) 2025 Masato Kokubo
3
+
4
+ # Defines commonly used functions.
5
+ # @author Masato Kokubo
3
6
  module Common
7
+ # Check the value types.
8
+ # @param value_name [String] the value name
9
+ # @param value [String] the value
10
+ # @param type [Class] the type
11
+ # @raise if the value is not an instance of the type or the subclass of the type
4
12
  def self.check_type(value_name, value, type)
5
- if !(value_name.is_a? String); raise "Argument value_name (=#{value_name}) must be a String"; end
6
- if !(type.is_a? Class); raise "Argument type (=#{type}) must be a Class"; end
13
+ raise "Argument value_name (=#{value_name}) must be a String" unless value_name.is_a?(String)
14
+ raise "Argument type (=#{type}) must be a Class" unless type.is_a?(Class)
7
15
 
8
16
  error = false
9
17
  if type == FalseClass || type == TrueClass
10
18
  # false or true
11
19
  if value.class != FalseClass && value.class != TrueClass
12
- check_error = true
20
+ error = true
13
21
  end
14
22
  else
15
- error = value.class != type
23
+ error = !value.is_a?(type)
16
24
  end
17
25
 
18
26
  if error
19
- value_string = value.class == String ? "\"#{value}\"" : "#{value}"
27
+ value_string = value.instance_of?(String) ? "\"#{value}\"" : "#{value}"
20
28
  top_type_name = type.name.slice(0).upcase
21
29
  a = top_type_name == 'A' || top_type_name == 'I' || top_type_name == 'U' ||
22
30
  top_type_name == 'E' || top_type_name == 'O' ? 'an' : 'a'
@@ -3,7 +3,40 @@
3
3
  require 'yaml'
4
4
  require_relative 'common'
5
5
 
6
+ # Retains the contents defined in debugtrace.yml.
7
+ # @author Masato Kokubo
6
8
  class Config
9
+ attr_reader :config_path
10
+ attr_reader :config
11
+ attr_reader :logger_name
12
+ attr_reader :log_path
13
+ attr_reader :logging_format
14
+ attr_reader :logging_datetime_format
15
+ attr_reader :enter_format
16
+ attr_reader :leave_format
17
+ attr_reader :thread_boundary_format
18
+ attr_reader :maximum_indents
19
+ attr_reader :indent_string
20
+ attr_reader :data_indent_string
21
+ attr_reader :limit_string
22
+ attr_reader :non_output_string
23
+ attr_reader :cyclic_reference_string
24
+ attr_reader :varname_value_separator
25
+ attr_reader :key_value_separator
26
+ attr_reader :print_suffix_format
27
+ attr_reader :size_format
28
+ attr_reader :minimum_output_size
29
+ attr_reader :length_format
30
+ attr_reader :minimum_output_length
31
+ attr_reader :maximum_data_output_width
32
+ attr_reader :bytes_count_in_line
33
+ attr_reader :collection_limit
34
+ attr_reader :bytes_limit
35
+ attr_reader :string_limit
36
+ attr_reader :reflection_limit
37
+
38
+ # Initializes with a yml file in the config_path.
39
+ # @param config_path [String] path of the yml file
7
40
  def initialize(config_path)
8
41
  @config_path = Common.check_type('config_path', config_path, String)
9
42
  if File.exist?(@config_path)
@@ -12,52 +45,48 @@ class Config
12
45
  @config_path = '<No config file>'
13
46
  @config = nil
14
47
  end
15
- @logger_name = _get_config_value 'logger' , 'stderr'
16
- @logging_destination = _get_config_value 'logging_destination' , 'STDERR'
17
- @logging_format = _get_config_value 'logging_format' , "%2$s %1$s %4$s\n"
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
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$.3f ms'
22
- @thread_boundary_format = _get_config_value 'thread_boundary_format' , '______________________________ %1$s #%2$s ______________________________'
23
- @maximum_indents = _get_config_value 'maximum_indents' , 32
24
- @indent_string = _get_config_value 'indent_string' , '| '
25
- @data_indent_string = _get_config_value 'data_indent_string' , ' '
26
- @limit_string = _get_config_value 'limit_string' , '...'
27
- @non_output_string = _get_config_value 'non_output_string' , '...'
28
- @cyclic_reference_string = _get_config_value 'cyclic_reference_string' , '*** Cyclic Reference ***'
29
- @varname_value_separator = _get_config_value 'varname_value_separator' , ' = '
30
- @key_value_separator = _get_config_value 'key_value_separator' , ': '
31
- @print_suffix_format = _get_config_value 'print_suffix_format' , ' (%2$s:%3$d)'
32
- @count_format = _get_config_value 'count_format' , 'count:%d'
33
- @minimum_output_count = _get_config_value 'minimum_output_count' , 16
34
- @length_format = _get_config_value 'length_format' , 'length:%d'
35
- @minimum_output_length = _get_config_value 'minimum_output_length' , 16
36
- @maximum_data_output_width = _get_config_value 'maximum_data_output_width' , 70
37
- @bytes_count_in_line = _get_config_value 'bytes_count_in_line' , 16
38
- @collection_limit = _get_config_value 'collection_limit' , 128
39
- @bytes_limit = _get_config_value 'bytes_limit' , 256
40
- @string_limit = _get_config_value 'string_limit' , 256
41
- @reflection_limit = _get_config_value 'reflection_limit' , 4
48
+ @logger_name = get_value 'logger' , 'stderr'
49
+ @log_path = get_value 'log_path' , 'debugtrace.log'
50
+ @logging_format = get_value 'logging_format' , "%2$s %1$s %4$s\n"
51
+ @logging_datetime_format = get_value 'logging_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$s ______________________________'
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
+ @non_output_string = get_value 'non_output_string' , '...'
61
+ @cyclic_reference_string = get_value 'cyclic_reference_string' , '*** Cyclic Reference ***'
62
+ @varname_value_separator = get_value 'varname_value_separator' , ' = '
63
+ @key_value_separator = get_value 'key_value_separator' , ': '
64
+ @print_suffix_format = get_value 'print_suffix_format' , ' (%2$s:%3$d)'
65
+ @size_format = get_value 'size_format' , 'size:%d'
66
+ @minimum_output_size = get_value 'minimum_output_size' , 16
67
+ @length_format = get_value 'length_format' , 'length:%d'
68
+ @minimum_output_length = get_value 'minimum_output_length' , 16
69
+ @maximum_data_output_width = get_value 'maximum_data_output_width', 70
70
+ @bytes_count_in_line = get_value 'bytes_count_in_line' , 16
71
+ @collection_limit = get_value 'collection_limit' , 128
72
+ @bytes_limit = get_value 'bytes_limit' , 256
73
+ @string_limit = get_value 'string_limit' , 256
74
+ @reflection_limit = get_value 'reflection_limit' , 4
42
75
  end
43
76
 
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_limit
51
-
52
- def enabled? = @enabled
77
+ # Returns true if logging is enabled, false otherwise.
78
+ # @return true if logging is enabled, false otherwise
79
+ def enabled?
80
+ @enabled
81
+ end
53
82
 
54
83
  private
55
84
 
56
- # Gets the value related the key from debugtrace.ini file.
57
- # @param key (String): The key
58
- # @param defalut_value (Object): Value to return when the value related the key is undefined
59
- # @return Object: Value related the key
60
- def _get_config_value(key, defalut_value)
85
+ # Gets the value related the key from debugtrace.yml file.
86
+ # @param key [String] the key
87
+ # @param defalut_value [Object] value to return if the value related the key is undefined
88
+ # @return [Object] value related the key
89
+ def get_value(key, defalut_value)
61
90
  Common.check_type('key', key, String)
62
91
  value = defalut_value
63
92
  unless @config.nil?
@@ -3,6 +3,7 @@
3
3
  require_relative 'common'
4
4
 
5
5
  # Buffers logs.
6
+ # @author Masato Kokubo
6
7
  class LogBuffer
7
8
  class LevelAndLog
8
9
  # Initializes this object.
@@ -49,7 +50,7 @@ class LogBuffer
49
50
  end
50
51
 
51
52
  # Appends a string representation of the value.
52
- # @param value (Object): The value to append
53
+ # @param value [Object] The value to append
53
54
  # @param nest_level (int, optional): The nest level of the value. Defaults to 0
54
55
  # @param no_break (bool, optional): If true, does not break even if the maximum width is exceeded.
55
56
  # Defaults to false
@@ -68,7 +69,7 @@ class LogBuffer
68
69
 
69
70
  # Appends a string representation of the value.
70
71
  # Does not break even if the maximum width is exceeded.
71
- # @param value (Object): The value to append
72
+ # @param value [Object] The value to append
72
73
  # @return LogBuffer: This object
73
74
  def no_break_append(value)
74
75
  append(value, 0, true)
@@ -76,7 +77,7 @@ class LogBuffer
76
77
 
77
78
  # Appends lines of another LogBuffer.
78
79
  # @param
79
- # @param separator (String): The separator string to append if not ''
80
+ # @param separator [String] The separator string to append if not ''
80
81
  # @param buff (LogBuffer): Another LogBuffer
81
82
  # @returns LogBuffer: This object
82
83
  def append_buffer(separator, buff)
@@ -5,12 +5,19 @@ require_relative 'common'
5
5
  require_relative 'config'
6
6
 
7
7
  # Abstract base class for logger classes.
8
+ # @author Masato Kokubo
8
9
  class LoggerBase
9
10
  # Outputs the message.
10
- # @param message (String): The message to output
11
+ # @param message [String] The message to output
11
12
  def print(message)
12
13
  raise 'LoggerBase.print is an abstract method.'
13
14
  end
15
+
16
+ # Returns a string representation of this object.
17
+ # @return [String] A string representation of this object
18
+ def to_s
19
+ "#{self.class.name}"
20
+ end
14
21
  end
15
22
 
16
23
  # Abstract base class for StdOut and StdErr classes.
@@ -23,7 +30,7 @@ class StdLogger < LoggerBase
23
30
  end
24
31
 
25
32
  # Outputs the message.
26
- # @param message (String): The message to output
33
+ # @param message [String] The message to output
27
34
  def print(message)
28
35
  Common::check_type("message", message, String)
29
36
  datetime_str = Time.now().strftime(@config.logging_datetime_format)
@@ -38,12 +45,6 @@ class StdOutLogger < StdLogger
38
45
  def initialize(config)
39
46
  super(config, $stdout)
40
47
  end
41
-
42
- # Returns a string representation of this object.
43
- # @return String: A string representation of this object
44
- def to_s
45
- '$stdout logger'
46
- end
47
48
  end
48
49
 
49
50
  # A logger class that outputs to $stderr.
@@ -52,16 +53,10 @@ class StdErrLogger < StdLogger
52
53
  def initialize(config)
53
54
  super(config, $stderr)
54
55
  end
55
-
56
- # Returns a string representation of this object.
57
- # @return String: A string representation of this object
58
- def to_s
59
- '$stderr logger'
60
- end
61
56
  end
62
57
 
63
58
  # A logger class that outputs using the logging library.
64
- class LoggerLogger
59
+ class RubyLogger
65
60
  private
66
61
 
67
62
  class Formatter
@@ -80,43 +75,59 @@ class LoggerLogger
80
75
  def initialize(config)
81
76
  @config = Common::check_type("config", config, Config)
82
77
  @logger = Logger.new(
83
- @config.logging_destination,
78
+ @config.log_path,
84
79
  formatter: Formatter.new(@config),
85
80
  datetime_format: @config.logging_datetime_format)
86
81
  end
87
82
 
88
83
  # Outputs the message.
89
- # @param message (String): The message to output
84
+ # @param message [String] The message to output
90
85
  def print(message)
91
86
  Common::check_type("message", message, String)
92
87
  @logger.log(Logger::Severity::DEBUG, message, 'DebugTrace-rb')
93
88
  end
94
89
 
95
90
  # Returns a string representation of this object.
96
- # @return String: A string representation of this object
91
+ # @return [String] A string representation of this object
97
92
  def to_s
98
- 'logging.Logger logger'
93
+ "Ruby #{Logger.name} path: #{@config.log_path}"
99
94
  end
100
95
  end
101
96
 
102
97
  # A logger class that outputs the file.
103
98
  class FileLogger < LoggerBase
104
- def initialize(config, log_path)
99
+ @@log_path_default = 'debugtrace.log'
100
+
101
+ def initialize(config)
102
+ @log_path = @@log_path_default
105
103
  @config = Common::check_type("config", config, Config)
106
- Common::check_type("log_path", log_path, String)
107
- dir_path = File.dirname(log_path)
108
- if Dir.exist?(dir_path)
109
- @log_path = log_path
110
- else
111
- $stderr.puts "DebugTrace-rb: FileLogger: The directory '#{dir_path}' cannot be found."
112
- @log_path = ''
104
+ Common::check_type("log_path", config.log_path, String)
105
+ @log_path = config.log_path
106
+ @append = false
107
+
108
+ if @log_path.start_with?('+')
109
+ @log_path = @log_path[1..-1]
110
+ @append = true
111
+ end
112
+
113
+ dir_path = File.dirname(@log_path)
114
+
115
+ if !Dir.exist?(dir_path)
116
+ @log_path = @@log_path_default
117
+ @append = true
118
+ print("DebugTrace-rb: FileLogger: The directory '#{dir_path}' cannot be found.\n")
119
+ end
120
+
121
+ if !@append
122
+ File.open(@log_path, 'w') { |file|
123
+ }
113
124
  end
114
125
  end
115
126
 
116
127
  def print(message)
117
- Common::check_type("message", message, String)
118
- if @log_path != ''
119
- File.open(@log_path, 'a') {|file|
128
+ # Common::check_type("message", message, String)
129
+ if File.exist?(@log_path)
130
+ File.open(@log_path, 'a') { |file|
120
131
  datetime_str = Time.now().strftime(@config.logging_datetime_format)
121
132
  file.puts "#{datetime_str} #{message}"
122
133
  }
@@ -124,8 +135,8 @@ class FileLogger < LoggerBase
124
135
  end
125
136
 
126
137
  # Returns a string representation of this object.
127
- # @return String: A string representation of this object
138
+ # @return [String] A string representation of this object
128
139
  def to_s
129
- "File logger: '#{@log_path}"
140
+ "#{self.class.name} path: #{@log_path}, append: #{@append}"
130
141
  end
131
142
  end
@@ -3,6 +3,7 @@
3
3
  require_relative 'common'
4
4
 
5
5
  # Have the trace state for a thread
6
+ # @author Masato Kokubo
6
7
  class State
7
8
  def initialize(thread_id)
8
9
  @thread_id = Common::check_type('thread_id', thread_id, Integer)
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module DebugTrace
5
- VERSION = '0.2.2'
5
+ VERSION = '0.2.4'
6
6
  end
data/lib/debugtrace.rb CHANGED
@@ -10,6 +10,7 @@ require_relative 'debugtrace/log_buffer'
10
10
  require_relative 'debugtrace/loggers'
11
11
  require_relative 'debugtrace/state'
12
12
 
13
+ # @author Masato Kokubo
13
14
  module DebugTrace
14
15
  # Configuration values
15
16
  @@config = nil
@@ -47,35 +48,35 @@ module DebugTrace
47
48
  @@logger = StdOutLogger.new(@@config)
48
49
  when 'stderr'
49
50
  @@logger = StdErrLogger.new(@@config)
50
- when 'logger'
51
- @@logger = LoggerLogger.new(@@config)
52
- when /^file:/
51
+ when 'rubylogger'
52
+ @@logger = RubyLogger.new(@@config)
53
+ when 'file'
53
54
  @@logger = FileLogger.new(@@config)
54
55
  else
55
- Pr._print("debugtrace: (#{@@config.config_path}) logger = #{@@config.logger_name} is unknown", STDERR)
56
+ @@logger = StdErrLogger.new(@@config)
57
+ @@logger.print("DebugTrace-rb: logger = #{@@config.logger_name} is unknown. (#{@@config.config_path}) \n")
56
58
  end
57
59
 
58
60
  return unless @@config.enabled?
59
61
 
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}")
62
+ @@logger.print("DebugTrace-rb #{DebugTrace::VERSION} on Ruby #{RUBY_VERSION}")
63
+ @@logger.print(" config file: #{@@config.config_path}")
63
64
  @@logger.print(" logger: #{@@logger}")
64
65
  end
65
66
 
66
67
  class PrintOptions
67
- attr_reader :minimum_output_count, :minimum_output_length,
68
+ attr_reader :minimum_output_size, :minimum_output_length,
68
69
  :collection_limit, :bytes_limit, :string_limit, :reflection_limit
69
70
 
70
71
  def initialize(
71
- minimum_output_count,
72
+ minimum_output_size,
72
73
  minimum_output_length,
73
74
  collection_limit,
74
75
  bytes_limit,
75
76
  string_limit,
76
77
  reflection_limit
77
78
  )
78
- @minimum_output_count = minimum_output_count == -1 ? DebugTrace.config.minimum_output_count : minimum_output_count
79
+ @minimum_output_size = minimum_output_size == -1 ? DebugTrace.config.minimum_output_size : minimum_output_size
79
80
  @minimum_output_length = minimum_output_length == -1 ? DebugTrace.config.minimum_output_length : minimum_output_length
80
81
  @collection_limit = collection_limit == -1 ? DebugTrace.config.collection_limit : collection_limit
81
82
  @bytes_limit = bytes_limit == -1 ? DebugTrace.config.bytes_limit : bytes_limit
@@ -117,6 +118,12 @@ module DebugTrace
117
118
  buff.no_break_append(separator).append('nil')
118
119
  when FalseClass, TrueClass, Integer, Float
119
120
  buff.no_break_append(separator).append(value.to_s)
121
+ when Symbol
122
+ buff.no_break_append(separator).append(':').no_break_append(value.name)
123
+ when Class
124
+ buff.no_break_append(separator).append(value.name).no_break_append(' class')
125
+ when Module
126
+ buff.no_break_append(separator).append(value.name).no_break_append(' module')
120
127
  when String
121
128
  value_buff = to_string_str(value, print_options)
122
129
  buff.append_buffer(separator, value_buff)
@@ -230,7 +237,7 @@ module DebugTrace
230
237
 
231
238
  if bytes_length >= @@config.minimum_output_length
232
239
  buff.no_break_append(' ')
233
- buff.no_break_append(format(@@config.length_format, bytes_length))
240
+ buff.no_break_append(format(@@config.size_format, bytes_length))
234
241
  end
235
242
 
236
243
  buff.no_break_append(') [')
@@ -414,9 +421,9 @@ module DebugTrace
414
421
  type_name = value.class.to_s
415
422
  type_name = '' if %w[Array Hash Set].include?(type_name)
416
423
 
417
- if count >= @@config.minimum_output_count
424
+ if count >= @@config.minimum_output_size
418
425
  type_name += ' ' unless type_name.empty?
419
- type_name += @@config.count_format % count
426
+ type_name += @@config.size_format % count
420
427
  end
421
428
 
422
429
  type_name
@@ -453,7 +460,7 @@ module DebugTrace
453
460
  @@DO_NOT_OUTPUT = 'Do not output'
454
461
 
455
462
  def self.print(name, value = @@DO_NOT_OUTPUT,
456
- minimum_output_count: -1, minimum_output_length: -1,
463
+ minimum_output_size: -1, minimum_output_length: -1,
457
464
  collection_limit: -1, bytes_limit: -1,
458
465
  string_limit: -1, reflection_limit: -1)
459
466
  @@thread_mutex.synchronize do
@@ -472,7 +479,7 @@ module DebugTrace
472
479
  else
473
480
  # with value
474
481
  print_options = PrintOptions.new(
475
- minimum_output_count, minimum_output_length,
482
+ minimum_output_size, minimum_output_length,
476
483
  collection_limit, bytes_limit,
477
484
  string_limit, reflection_limit
478
485
  )
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.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masato Kokubo