debugtrace 0.2.5 → 0.2.7

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: 665b9852fb34caab345012ab6bac6e18c0e8951d689125ea9cf0c399a0c7637e
4
- data.tar.gz: d0940c27f8beec19e0aecfc609ddab7a35ecefedcde6251b1de5382d9d8ea319
3
+ metadata.gz: 4ba0b48484b98dad04cefc154c88afc5e82e88d551ab00d81dd3ddbe323a0e7a
4
+ data.tar.gz: 6f858f96393d92d0b13f4ec5f34877d9d48623990d0251cd8c6c0f81d6616c1c
5
5
  SHA512:
6
- metadata.gz: 3b3a87106db56eb5c88c42c28f5c25eaf17a19a502c82411853554200c7ddaa7c5117248d5b0059206a467b53a47abdf9e37a4aa4b736199f051b76cbe4ecd0d
7
- data.tar.gz: db33d2a6c4df894006aa1daada1fa777cdb0908ffeb4ccdb9b5de95e86dbece0835d0bea23a39c7f185dcf3fa36b6304ad680a185f58598a8cbe74b6d6bcb328
6
+ metadata.gz: 55d162f057c2686e1c350caac7c3b4a48f493d04b344711ea2beb8c5a995ff9066334c845fc58e0d6a82dee76d17cfe001f22a74f76d47841b8ad076877a6e61
7
+ data.tar.gz: ce01d78482c3fa14a84f21b8d7b7f68a5ffe2efd3dee453d5ae622256818509b4ec7b9df037f121e8caaf032dfe84a10ee3ea205e4b2793877a222a3b7395c1e
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2025 TODO: Write your name
3
+ Copyright (c) 2025 Masato Kokubo
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -2,16 +2,17 @@
2
2
  # (C) 2025 Masato Kokubo
3
3
 
4
4
  # Defines commonly used functions.
5
- # @author Masato Kokubo
6
5
  module Common
7
6
  # Check the value types.
7
+ #
8
8
  # @param value_name [String] the value name
9
9
  # @param value [String] the value
10
10
  # @param type [Class] the type
11
- # @raise if the value is not an instance of the type or the subclass of the type
11
+ # @return [String] the value
12
+ # @raise [TypeError] if the value is not an instance of the type or the subclass of the type
12
13
  def self.check_type(value_name, value, type)
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)
14
+ raise TypeError("Argument value_name (=#{value_name}) must be a String") unless value_name.is_a?(String)
15
+ raise TypeError("Argument type (=#{type}) must be a Class") unless type.is_a?(Class)
15
16
 
16
17
  error = false
17
18
  if type == FalseClass || type == TrueClass
@@ -28,8 +29,8 @@ module Common
28
29
  top_type_name = type.name.slice(0).upcase
29
30
  a = top_type_name == 'A' || top_type_name == 'I' || top_type_name == 'U' ||
30
31
  top_type_name == 'E' || top_type_name == 'O' ? 'an' : 'a'
31
- raise "Argument #{value_name} (=#{value_string}) must be #{a} #{type}"
32
+ raise TypeError("Argument #{value_name} (=#{value_string}) must be #{a} #{type}")
32
33
  end
33
- value
34
+ return value
34
35
  end
35
36
  end
@@ -4,7 +4,6 @@ require 'yaml'
4
4
  require_relative 'common'
5
5
 
6
6
  # Retains the contents defined in debugtrace.yml.
7
- # @author Masato Kokubo
8
7
  class Config
9
8
  attr_reader :config_path
10
9
  attr_reader :config
@@ -36,6 +35,7 @@ class Config
36
35
  attr_reader :reflection_limit
37
36
 
38
37
  # Initializes with a yml file in the config_path.
38
+ #
39
39
  # @param config_path [String] path of the yml file
40
40
  def initialize(config_path)
41
41
  @config_path = Common.check_type('config_path', config_path, String)
@@ -75,14 +75,16 @@ class Config
75
75
  end
76
76
 
77
77
  # Returns true if logging is enabled, false otherwise.
78
- # @return true if logging is enabled, false otherwise
78
+ #
79
+ # @return [TrueClass, FalseClass] true if logging is enabled, false otherwise
79
80
  def enabled?
80
- @enabled
81
+ return @enabled
81
82
  end
82
83
 
83
84
  private
84
85
 
85
86
  # Gets the value related the key from debugtrace.yml file.
87
+ #
86
88
  # @param key [String] the key
87
89
  # @param defalut_value [Object] value to return if the value related the key is undefined
88
90
  # @return [Object] value related the key
@@ -97,6 +99,6 @@ class Config
97
99
  Common.check_type("config[#{key}]", value, defalut_value.class)
98
100
  end
99
101
  end
100
- value
102
+ return value
101
103
  end
102
104
  end
@@ -3,19 +3,25 @@
3
3
  require_relative 'common'
4
4
 
5
5
  # Buffers logs.
6
- # @author Masato Kokubo
7
6
  class LogBuffer
7
+ # Contains a single line indentation level and log content.
8
8
  class LevelAndLog
9
+ attr_reader :nest_level, :log
10
+
9
11
  # Initializes this object.
12
+ #
13
+ # @param nest_level [Integer] the nesting level
14
+ # @param log [String] the log message
10
15
  def initialize(nest_level, log)
11
16
  @nest_level = Common.check_type('nest_level', nest_level, Integer)
12
17
  @log = Common.check_type('log', log, String)
13
18
  end
14
19
 
15
- attr_reader :nest_level, :log
16
-
20
+ # Returns a string representation of this object.
21
+ #
22
+ # @return [String] a string representation of this object.
17
23
  def to_s
18
- "(LogBuffer.LevelAndLog){nest_level: #{@nest_level}, log: \"#{@log}\"}"
24
+ return "(LogBuffer.LevelAndLog){nest_level: #{@nest_level}, log: \"#{@log}\"}"
19
25
  end
20
26
  end
21
27
 
@@ -50,11 +56,12 @@ class LogBuffer
50
56
  end
51
57
 
52
58
  # Appends a string representation of the value.
59
+ #
53
60
  # @param value [Object] The value to append
54
61
  # @param nest_level (int, optional): The nest level of the value. Defaults to 0
55
62
  # @param no_break (bool, optional): If true, does not break even if the maximum width is exceeded.
56
63
  # Defaults to false
57
- # @return LogBuffer: This object
64
+ # @return [LogBuffer] this object
58
65
  def append(value, nest_level = 0, no_break = false)
59
66
  Common.check_type('nest_level', nest_level, Integer)
60
67
  Common.check_type('no_break', no_break, TrueClass)
@@ -64,22 +71,23 @@ class LogBuffer
64
71
  @append_nest_level = nest_level
65
72
  @last_line += string
66
73
  end
67
- self
74
+ return self
68
75
  end
69
76
 
70
77
  # Appends a string representation of the value.
71
78
  # Does not break even if the maximum width is exceeded.
79
+ #
72
80
  # @param value [Object] The value to append
73
- # @return LogBuffer: This object
81
+ # @return [LogBuffer] this object
74
82
  def no_break_append(value)
75
- append(value, 0, true)
83
+ return append(value, 0, true)
76
84
  end
77
85
 
78
86
  # Appends lines of another LogBuffer.
79
- # @param
80
- # @param separator [String] The separator string to append if not ''
81
- # @param buff (LogBuffer): Another LogBuffer
82
- # @returns LogBuffer: This object
87
+ #
88
+ # @param separator [String] the separator string to append if not ''
89
+ # @param buff [LogBuffer] another LogBuffer
90
+ # @return [LogBuffer] this object
83
91
  def append_buffer(separator, buff)
84
92
  Common.check_type('separator', separator, String)
85
93
  Common.check_type('buff', buff, LogBuffer)
@@ -90,23 +98,27 @@ class LogBuffer
90
98
  append(line.log, line.nest_level, index == 0 && separator != '')
91
99
  index += 1
92
100
  end
93
- self
101
+ return self
94
102
  end
95
103
 
96
104
  # The length of the last line.
97
105
  def length
98
- @last_line.length
106
+ return @last_line.length
99
107
  end
100
108
 
101
- # true if multiple line, false otherwise.
109
+ # Returns true if multiple line, false otherwise.
110
+ #
111
+ # @return [FalseClass, TrueClass] true if multiple line, false otherwise.
102
112
  def multi_lines?
103
- @lines.length > 1 || @lines.length == 1 && length > 0
113
+ return @lines.length > 1 || @lines.length == 1 && length > 0
104
114
  end
105
115
 
106
- # A list of tuple of data indentation level && log string.
116
+ # Returns the LevelAndLog objects.
117
+ #
118
+ # @return [Array<LevelAndLog>] the LevelAndLog objects.
107
119
  def lines
108
120
  lines = @lines.dup
109
121
  lines << LevelAndLog.new(@nest_level, @last_line) if length > 0
110
- lines
122
+ return lines
111
123
  end
112
124
  end
@@ -4,37 +4,38 @@ require 'logger'
4
4
  require_relative 'common'
5
5
  require_relative 'config'
6
6
 
7
- # Abstract base class for logger classes.
8
- # @author Masato Kokubo
7
+ # @abstract Base class for logger classes.
9
8
  class LoggerBase
10
- # Outputs the message.
9
+ # @abstract Outputs the message.
11
10
  # @param message [String] The message to output
11
+ # @raise [Exception] always
12
12
  def print(message)
13
- raise 'LoggerBase.print is an abstract method.'
13
+ raise Exception.new('LoggerBase.print is an abstract method.')
14
14
  end
15
15
 
16
16
  # Returns a string representation of this object.
17
17
  # @return [String] A string representation of this object
18
18
  def to_s
19
- "#{self.class.name}"
19
+ return "#{self.class.name}"
20
20
  end
21
21
  end
22
22
 
23
23
  # Abstract base class for StdOut and StdErr classes.
24
24
  class StdLogger < LoggerBase
25
25
  # Initializes this object.
26
- # @param iostream: Output destination
27
- def initialize(config, iostream)
26
+ # @param [IO] Output destination
27
+ def initialize(config, output)
28
28
  @config = Common::check_type("config", config, Config)
29
- @iostream = iostream
29
+ @output = output
30
30
  end
31
31
 
32
32
  # Outputs the message.
33
- # @param message [String] The message to output
33
+ # @param message [String] the message to output
34
+ # @return [String] the message
34
35
  def print(message)
35
36
  Common::check_type("message", message, String)
36
37
  datetime_str = Time.now().strftime(@config.logging_datetime_format)
37
- @iostream.puts "#{datetime_str} #{message}"
38
+ @output.puts "#{datetime_str} #{message}"
38
39
  end
39
40
 
40
41
  end
@@ -42,6 +43,7 @@ end
42
43
  # A logger class that outputs to $stdout.
43
44
  class StdOutLogger < StdLogger
44
45
  # Initializes this object.
46
+ # config [Config] a configuration object
45
47
  def initialize(config)
46
48
  super(config, $stdout)
47
49
  end
@@ -50,18 +52,21 @@ end
50
52
  # A logger class that outputs to $stderr.
51
53
  class StdErrLogger < StdLogger
52
54
  # Initializes this object.
55
+ # config [Config] a configuration object
53
56
  def initialize(config)
54
57
  super(config, $stderr)
55
58
  end
56
59
  end
57
60
 
58
- # A logger class that outputs using the logging library.
61
+ # A logger class that outputs using Ruby Logger.
59
62
  class RubyLogger
60
63
  private
61
64
 
62
65
  class Formatter
66
+ # Initializes this object.
67
+ # config [Config] a configuration object
63
68
  def initialize(config)
64
- @config = config
69
+ @config = Common::check_type("config", config, Config)
65
70
  end
66
71
 
67
72
  def call(severity, datetime, progname, msg)
@@ -72,6 +77,9 @@ class RubyLogger
72
77
 
73
78
  public
74
79
 
80
+ # Initializes this object.
81
+ #
82
+ # @param config [Config] a configuration object
75
83
  def initialize(config)
76
84
  @config = Common::check_type("config", config, Config)
77
85
  @logger = Logger.new(
@@ -81,16 +89,19 @@ class RubyLogger
81
89
  end
82
90
 
83
91
  # Outputs the message.
92
+ #
84
93
  # @param message [String] The message to output
85
94
  def print(message)
86
95
  Common::check_type("message", message, String)
87
96
  @logger.log(Logger::Severity::DEBUG, message, 'DebugTrace-rb')
97
+ return message
88
98
  end
89
99
 
90
100
  # Returns a string representation of this object.
101
+ #
91
102
  # @return [String] A string representation of this object
92
103
  def to_s
93
- "Ruby #{Logger.name} path: #{@config.log_path}"
104
+ return "Ruby #{Logger.name} path: #{@config.log_path}"
94
105
  end
95
106
  end
96
107
 
@@ -98,6 +109,9 @@ end
98
109
  class FileLogger < LoggerBase
99
110
  @@log_path_default = 'debugtrace.log'
100
111
 
112
+ # Initializes this object.
113
+ #
114
+ # @parm config [Config] a configuration object
101
115
  def initialize(config)
102
116
  @log_path = @@log_path_default
103
117
  @config = Common::check_type("config", config, Config)
@@ -124,19 +138,24 @@ class FileLogger < LoggerBase
124
138
  end
125
139
  end
126
140
 
141
+ # Outputs the message.
142
+ #
143
+ # @param message [String] the message to output
144
+ # @return [String] the message
127
145
  def print(message)
128
- # Common::check_type("message", message, String)
129
146
  if File.exist?(@log_path)
130
147
  File.open(@log_path, 'a') { |file|
131
148
  datetime_str = Time.now().strftime(@config.logging_datetime_format)
132
149
  file.puts "#{datetime_str} #{message}"
133
150
  }
134
151
  end
152
+ return message
135
153
  end
136
154
 
137
155
  # Returns a string representation of this object.
156
+ #
138
157
  # @return [String] A string representation of this object
139
158
  def to_s
140
- "#{self.class.name} path: #{@log_path}, append: #{@append}"
159
+ return "#{self.class.name} path: #{@log_path}, append: #{@append}"
141
160
  end
142
161
  end
@@ -2,49 +2,32 @@
2
2
  # (C) 2025 Masato Kokubo
3
3
  require_relative 'common'
4
4
 
5
- # Have the trace state for a thread
6
- # @author Masato Kokubo
5
+ # Contains the trace state for a thread
7
6
  class State
7
+ attr_reader :thread_id
8
+ attr_reader :nest_level
9
+ attr_reader :previous_nest_level
10
+
11
+ # Initializes this object.
12
+ #
13
+ # @param thread_id [Integer] the object id of the thread
8
14
  def initialize(thread_id)
9
- @thread_id = Common::check_type('thread_id', thread_id, Integer)
15
+ @thread_id = Common.check_type('thread_id', thread_id, Integer)
10
16
  reset()
11
17
  end
12
18
 
13
- # @return the thread id.
14
- def thread_id
15
- @thread_id
16
- end
17
-
18
- # @return the nest level.
19
- def nest_level
20
- @nest_level
21
- end
22
-
23
- # @return the previous nest level.
24
- def previous_nest_level
25
- @previous_nest_level
26
- end
27
-
28
- # @return the previous line count.
29
- def previous_line_count
30
- @previous_line_count
31
- end
32
-
33
- # Sets the previous line count.
34
- # @param value the previous line count
35
- def previous_line_count=(value)
36
- @previous_line_count = Common::check_type('value', value, Integer)
37
- end
38
-
19
+ # Resets this object
39
20
  def reset
40
21
  @nest_level = 0
41
22
  @previous_nest_level = 0
42
- @previous_line_count = 0
43
23
  @times = []
44
24
  end
45
25
 
26
+ # Returns a string representation of this object.
27
+ #
28
+ # @return [String] A string representation of this object
46
29
  def to_s()
47
- "(State){thread_id: #{@thread_id}, nest_level: #{@nest_level}, previous_nest_level: #{@previous_nest_level}, previous_line_count: #{@previous_line_count}, times: #{@times}}"
30
+ return "(State){thread_id: #{@thread_id}, nest_level: #{@nest_level}, previous_nest_level: #{@previous_nest_level}, times: #{@times}}"
48
31
  end
49
32
 
50
33
  # Ups the nest level.
@@ -56,8 +39,9 @@ class State
56
39
  @nest_level += 1
57
40
  end
58
41
 
59
- #Downs the nest level.
60
- # @return Time: The time when the corresponding upNest method was invoked
42
+ # Downs the nest level.
43
+ #
44
+ # @return [Float] The time when the corresponding up_nest method was invoked
61
45
  def down_nest
62
46
  @previous_nest_level = @nest_level
63
47
  @nest_level -= 1
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module DebugTrace
5
- VERSION = '0.2.5'
5
+ VERSION = '0.2.7'
6
6
  end
data/lib/debugtrace.rb CHANGED
@@ -10,13 +10,13 @@ require_relative 'debugtrace/log_buffer'
10
10
  require_relative 'debugtrace/loggers'
11
11
  require_relative 'debugtrace/state'
12
12
 
13
- # @author Masato Kokubo
13
+ # The main module of DebugTrace-rb.
14
14
  module DebugTrace
15
15
  # Configuration values
16
16
  @@config = nil
17
17
 
18
18
  def self.config
19
- @@config
19
+ return @@config
20
20
  end
21
21
 
22
22
  # A Mutex for thread safety
@@ -37,6 +37,9 @@ module DebugTrace
37
37
  # The logger used by DebugTrace-py
38
38
  @@logger = nil
39
39
 
40
+ # Initialize this class
41
+ #
42
+ # @option [String] config_path the path to the configuration file. ./debugtrace.yml if not specified.
40
43
  def self.initialize(config_path = './debugtrace.yml')
41
44
  @@config = Config.new(config_path)
42
45
 
@@ -59,16 +62,28 @@ module DebugTrace
59
62
 
60
63
  return unless @@config.enabled?
61
64
 
65
+ config_path = File.expand_path(@@config.config_path)
62
66
  @@logger.print("DebugTrace-rb #{DebugTrace::VERSION} on Ruby #{RUBY_VERSION}")
63
- @@logger.print(" config file: #{@@config.config_path}")
67
+ @@logger.print(" config file: #{config_path}")
64
68
  @@logger.print(" logger: #{@@logger}")
65
69
  end
66
70
 
71
+ # Contains options to pass to the print method.
67
72
  class PrintOptions
68
- attr_reader :minimum_output_size, :minimum_output_length,
73
+ attr_reader :reflection, :minimum_output_size, :minimum_output_length,
69
74
  :collection_limit, :bytes_limit, :string_limit, :reflection_limit
70
75
 
76
+ # Initializes this object.
77
+ #
78
+ # @param reflection [TrueClass, FalseClass] use reflection if true
79
+ # @param minimum_output_size [Integer] the minimum value to output the number of elements for Array and Hash (overrides debugtarace.yml value)
80
+ # @param minimum_output_length [Integer] the minimum value to output the length of String and byte array (overrides debugtarace.yml value)
81
+ # @param collection_limit [Integer] Output limit of collection elements (overrides debugtarace.yml value)
82
+ # @param bytes_limit [Integer] the limit value of elements for bytes and bytearray to output (overrides debugtarace.yml value)
83
+ # @param string_limit [Integer] the limit value of characters for string to output (overrides debugtarace.yml value)
84
+ # @param reflection_limit [Integer] reflection limits when using reflection (overrides debugtarace.yml value)
71
85
  def initialize(
86
+ reflection,
72
87
  minimum_output_size,
73
88
  minimum_output_length,
74
89
  collection_limit,
@@ -76,6 +91,7 @@ module DebugTrace
76
91
  string_limit,
77
92
  reflection_limit
78
93
  )
94
+ @reflection = reflection
79
95
  @minimum_output_size = minimum_output_size == -1 ? DebugTrace.config.minimum_output_size : minimum_output_size
80
96
  @minimum_output_length = minimum_output_length == -1 ? DebugTrace.config.minimum_output_length : minimum_output_length
81
97
  @collection_limit = collection_limit == -1 ? DebugTrace.config.collection_limit : collection_limit
@@ -85,6 +101,9 @@ module DebugTrace
85
101
  end
86
102
  end
87
103
 
104
+ # Returns the current state.
105
+ #
106
+ # @return [State] the current state
88
107
  def self.current_state
89
108
  thread_id = Thread.current.object_id
90
109
 
@@ -95,15 +114,25 @@ module DebugTrace
95
114
  @@state_hash[thread_id] = state
96
115
  end
97
116
 
98
- state
117
+ return state
99
118
  end
100
119
 
120
+ # Returns the current indent string.
121
+ #
122
+ # @param nest_level [Integer] the code nesting level
123
+ # @param data_nest_level [Integer] the data nesting level
124
+ # @return [Sring] the current indent string
101
125
  def self.get_indent_string(nest_level, data_nest_level)
102
126
  indent_str = @@config.indent_string * [[0, nest_level].max, @@config.maximum_indents].min
103
127
  data_indent_str = @@config.data_indent_string * [[0, data_nest_level].max, @@config.maximum_indents].min
104
- indent_str + data_indent_str
128
+ return indent_str + data_indent_str
105
129
  end
106
130
 
131
+ # Returns a string representation of the variable contents.
132
+ #
133
+ # @param name [String] the variable name
134
+ # @param value [Object] the value
135
+ # @param print_options [PrintOptions] the print options
107
136
  def self.to_string(name, value, print_options)
108
137
  buff = LogBuffer.new(@@config.maximum_data_output_width)
109
138
 
@@ -125,7 +154,8 @@ module DebugTrace
125
154
  when Module
126
155
  buff.no_break_append(separator).append(value.name).no_break_append(' module')
127
156
  when String
128
- value_buff = to_string_str(value, print_options)
157
+ value_buff = value.encoding == Encoding::ASCII_8BIT ?
158
+ to_string_bytes(value, print_options) : to_string_str(value, print_options)
129
159
  buff.append_buffer(separator, value_buff)
130
160
  when DateTime
131
161
  buff.no_break_append(separator).append(value.strftime('%Y-%m-%d %H:%M-%S.%L%:z'))
@@ -137,25 +167,33 @@ module DebugTrace
137
167
  value_buff = to_string_enumerable(value, print_options)
138
168
  buff.append_buffer(separator, value_buff)
139
169
  else
140
- # use reflection
141
- value_buff = LogBuffer.new(@@config.maximum_data_output_width)
142
- if @@reflected_objects.any? { |obj| value.equal?(obj) }
143
- # cyclic reference
144
- value_buff.no_break_append(@@config.cyclic_reference_string)
145
- elsif @@reflected_objects.length > print_options.reflection_limit
146
- # over reflection level limitation
147
- value_buff.no_break_append(@@config.limit_string)
170
+ if print_options.reflection
171
+ # use reflection
172
+ value_buff = LogBuffer.new(@@config.maximum_data_output_width)
173
+ if @@reflected_objects.any? { |obj| value.equal?(obj) }
174
+ # cyclic reference
175
+ value_buff.no_break_append(@@config.cyclic_reference_string)
176
+ elsif @@reflected_objects.length > print_options.reflection_limit
177
+ # over reflection level limitation
178
+ value_buff.no_break_append(@@config.limit_string)
179
+ else
180
+ @@reflected_objects.push(value)
181
+ value_buff = to_string_reflection(value, print_options)
182
+ @@reflected_objects.pop
183
+ end
184
+ buff.append_buffer(separator, value_buff)
148
185
  else
149
- @@reflected_objects.push(value)
150
- value_buff = to_string_reflection(value, print_options)
151
- @@reflected_objects.pop
186
+ buff.no_break_append(separator).append(value.to_s)
152
187
  end
153
- buff.append_buffer(separator, value_buff)
154
188
  end
155
189
 
156
- buff
190
+ return buff
157
191
  end
158
192
 
193
+ # Returns a string representation of the string value
194
+ #
195
+ # @param value [String] the value
196
+ # @param print_options [PrintOptions] the print options
159
197
  def self.to_string_str(value, print_options)
160
198
  has_single_quote = false
161
199
  has_double_quote = false
@@ -215,21 +253,16 @@ module DebugTrace
215
253
  double_quote_buff.no_break_append('"')
216
254
  single_quote_buff.no_break_append("'")
217
255
 
218
- return double_quote_buff if has_single_quote && !has_double_quote
219
-
220
- single_quote_buff
256
+ return has_single_quote && !has_double_quote ? double_quote_buff : single_quote_buff
221
257
  end
222
258
 
259
+ # Returns a string representation of the string value which encoding is ASCII_8BIT.
260
+ #
261
+ # @param value [String] the value
262
+ # @param print_options [PrintOptions] the print options
223
263
  def self.to_string_bytes(value, print_options)
224
264
  bytes_length = value.length
225
265
  buff = LogBuffer.new(@@config.maximum_data_output_width)
226
- buff.no_break_append('(')
227
-
228
- if value.is_a?(String)
229
- buff.no_break_append('bytes')
230
- elsif value.is_a?(Array)
231
- buff.no_break_append('bytearray')
232
- end
233
266
 
234
267
  if bytes_length >= @@config.minimum_output_length
235
268
  buff.no_break_append(format(@@config.size_format, bytes_length))
@@ -278,9 +311,13 @@ module DebugTrace
278
311
  end
279
312
  buff.no_break_append(']')
280
313
 
281
- buff
314
+ return buff
282
315
  end
283
316
 
317
+ # Returns a string representation of the value using reflection.
318
+ #
319
+ # @param value [Object] the value
320
+ # @param print_options [PrintOptions] the print options
284
321
  def self.to_string_reflection(value, print_options)
285
322
  buff = LogBuffer.new(@@config.maximum_data_output_width)
286
323
 
@@ -304,9 +341,13 @@ module DebugTrace
304
341
  end
305
342
  buff.no_break_append('}')
306
343
 
307
- buff
344
+ return buff
308
345
  end
309
346
 
347
+ # Returns a string representation of the value using reflection.
348
+ #
349
+ # @param value [Object] the value
350
+ # @param print_options [PrintOptions] the print options
310
351
  def self.to_string_reflection_body(value, print_options)
311
352
  buff = LogBuffer.new(@@config.maximum_data_output_width)
312
353
 
@@ -328,9 +369,13 @@ module DebugTrace
328
369
  index += 1
329
370
  end
330
371
 
331
- buff
372
+ return buff
332
373
  end
333
374
 
375
+ # Returns a string representation of an Array, Set or Hash.
376
+ #
377
+ # @param value [Array, Set, Hash] the value
378
+ # @param print_options [PrintOptions] the print options
334
379
  def self.to_string_enumerable(values, print_options)
335
380
  open_char = '[' # Array
336
381
  close_char = ']'
@@ -367,9 +412,13 @@ module DebugTrace
367
412
 
368
413
  buff.no_break_append(close_char)
369
414
 
370
- buff
415
+ return buff
371
416
  end
372
417
 
418
+ # Returns a string representation of the Array, Set or Hash value.
419
+ #
420
+ # @param value [Array, Set, Hash] the value
421
+ # @param print_options [PrintOptions] the print options
373
422
  def self.to_string_enumerable_body(values, print_options)
374
423
  buff = LogBuffer.new(@@config.maximum_data_output_width)
375
424
 
@@ -401,9 +450,14 @@ module DebugTrace
401
450
 
402
451
  buff.no_break_append(':') if values.is_a?(Hash) && values.empty?
403
452
 
404
- buff
453
+ return buff
405
454
  end
406
455
 
456
+ # Returns a string representation the key and the value.
457
+ #
458
+ # @param key [Object] the key
459
+ # @param value [Object] the value
460
+ # @param print_options [PrintOptions] the print options
407
461
  def self.to_string_key_value(key, value, print_options)
408
462
  buff = LogBuffer.new(@@config.maximum_data_output_width)
409
463
  key_buff = to_string('', key, print_options)
@@ -412,28 +466,24 @@ module DebugTrace
412
466
  buff
413
467
  end
414
468
 
415
- def self.get_type_name(value, count = -1)
469
+ # Returns the type name.
470
+ #
471
+ # @param value [Object] the value
472
+ # @option size [Object] the size of Array, Set or Hash
473
+ def self.get_type_name(value, size = -1)
416
474
  type_name = value.class.to_s
417
475
  type_name = '' if %w[Array Hash Set].include?(type_name)
418
476
 
419
- if count >= @@config.minimum_output_size
420
- type_name += @@config.size_format % count
477
+ if size >= @@config.minimum_output_size
478
+ type_name += @@config.size_format % size
421
479
  end
422
480
 
423
- type_name
424
- end
425
-
426
- def self.has_to_s_method?(value)
427
- begin
428
- value.public_method('to_s')
429
- rescue
430
- return false
431
- end
432
- return true
481
+ return type_name
433
482
  end
434
483
 
435
484
  @@before_thread_id = nil
436
485
 
486
+ # Called at the start of the print method.
437
487
  def self.print_start
438
488
  if @@before_thread_id == nil
439
489
  DebugTrace.initialize
@@ -453,7 +503,19 @@ module DebugTrace
453
503
 
454
504
  @@DO_NOT_OUTPUT = 'Do not output'
455
505
 
506
+ # Prints the message or the value.
507
+ #
508
+ # @param name [String] a message if the value is not specified, otherwise the value name
509
+ # @option value [Object] the value
510
+ # @option reflection [TrueClass, FalseClass] use reflection if true
511
+ # @option minimum_output_size [Integer] the minimum value to output the number of elements for Array and Hash (overrides debugtarace.yml value)
512
+ # @option minimum_output_length [Integer] the minimum value to output the length of String and byte array (overrides debugtarace.yml value)
513
+ # @option collection_limit [Integer] Output limit of collection elements (overrides debugtarace.yml value)
514
+ # @option bytes_limit [Integer] the limit value of elements for bytes and bytearray to output (overrides debugtarace.yml value)
515
+ # @option string_limit [Integer] the limit value of characters for string to output (overrides debugtarace.yml value)
516
+ # @option reflection_limit [Integer] reflection limits when using reflection (overrides debugtarace.yml value)
456
517
  def self.print(name, value = @@DO_NOT_OUTPUT,
518
+ reflection: false,
457
519
  minimum_output_size: -1, minimum_output_length: -1,
458
520
  collection_limit: -1, bytes_limit: -1,
459
521
  string_limit: -1, reflection_limit: -1)
@@ -473,6 +535,7 @@ module DebugTrace
473
535
  else
474
536
  # with value
475
537
  print_options = PrintOptions.new(
538
+ reflection,
476
539
  minimum_output_size, minimum_output_length,
477
540
  collection_limit, bytes_limit,
478
541
  string_limit, reflection_limit
@@ -501,9 +564,10 @@ module DebugTrace
501
564
  end
502
565
  end
503
566
 
504
- value
567
+ return value
505
568
  end
506
569
 
570
+ # Prints the start of the method.
507
571
  def self.enter
508
572
  @@thread_mutex.synchronize do
509
573
  print_start
@@ -537,10 +601,14 @@ module DebugTrace
537
601
  end
538
602
  end
539
603
 
540
- def self.leave
604
+ # Prints the end of the method.
605
+ #
606
+ # @option [Object] the return value
607
+ # @return [Object] return_value if specified, otherwise nil
608
+ def self.leave(return_value = nil)
541
609
  @@thread_mutex.synchronize do
542
610
  print_start
543
- return unless @@config.enabled?
611
+ return return_value unless @@config.enabled?
544
612
 
545
613
  state = current_state
546
614
 
@@ -561,18 +629,20 @@ module DebugTrace
561
629
  )
562
630
  @@last_log_buff.line_feed
563
631
  @@logger.print(get_indent_string(state.nest_level, 0) + @@last_log_buff.lines[0].log)
632
+ return return_value
564
633
  end
565
634
  end
566
635
 
636
+ # Returns the last print string.
567
637
  def self.last_print_string
568
638
  lines = @@last_log_buff.lines
569
- buff_string = lines.map { |line| _config.data_indent_string * line[0] + line[1] }.join("\n")
639
+ buff_string = lines.map { |line| @@config.data_indent_string * line.nest_level + line.log }.join("\n")
570
640
 
571
641
  state = nil
572
642
  @@thread_mutex.synchronize do
573
643
  state = current_state
574
644
  end
575
645
 
576
- "#{get_indent_string(state.nest_level, 0)}#{buff_string}"
646
+ return "#{get_indent_string(state.nest_level, 0)}#{buff_string}"
577
647
  end
578
648
  end
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.5
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masato Kokubo