utilrb 1.6.6 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/Manifest.txt +10 -9
  2. data/README.rd +23 -0
  3. data/Rakefile +40 -37
  4. data/ext/utilrb/extconf.rb +36 -0
  5. data/ext/{proc.cc → utilrb/proc.c} +7 -6
  6. data/ext/utilrb/ruby_allocator.hh +76 -0
  7. data/ext/{ruby_internals-1.8.h → utilrb/ruby_internals-1.8.h} +0 -0
  8. data/ext/{ruby_internals-1.9.h → utilrb/ruby_internals-1.9.h} +0 -0
  9. data/ext/{swap.cc → utilrb/swap.cc} +0 -0
  10. data/ext/utilrb/utilrb.cc +79 -0
  11. data/ext/{value_set.cc → utilrb/value_set.cc} +5 -5
  12. data/ext/{weakref.cc → utilrb/weakref.cc} +0 -0
  13. data/lib/utilrb/common.rb +7 -2
  14. data/lib/utilrb/doc/rake.rb +46 -15
  15. data/lib/utilrb/kernel/load_dsl_file.rb +2 -2
  16. data/lib/utilrb/kernel/options.rb +6 -0
  17. data/lib/utilrb/logger/forward.rb +7 -0
  18. data/lib/utilrb/logger/hierarchy.rb +100 -42
  19. data/lib/utilrb/logger/indent.rb +38 -3
  20. data/lib/utilrb/logger/log_pp.rb +1 -1
  21. data/lib/utilrb/logger/root.rb +27 -12
  22. data/lib/utilrb/module/inherited_enumerable.rb +2 -196
  23. data/lib/utilrb/objectstats.rb +4 -1
  24. data/lib/utilrb/pkgconfig.rb +42 -6
  25. data/lib/utilrb/rake_common.rb +12 -0
  26. data/lib/utilrb/ruby_object_graph.rb +195 -46
  27. data/lib/utilrb/yard.rb +89 -89
  28. data/test/test_array.rb +1 -1
  29. data/test/test_dir.rb +1 -1
  30. data/test/test_enumerable.rb +7 -1
  31. data/test/test_event_loop.rb +407 -0
  32. data/test/test_exception.rb +1 -1
  33. data/test/test_gc.rb +1 -1
  34. data/test/test_hash.rb +57 -1
  35. data/test/test_kernel.rb +52 -21
  36. data/test/test_logger.rb +150 -1
  37. data/test/test_misc.rb +1 -1
  38. data/test/test_models.rb +212 -0
  39. data/test/test_module.rb +41 -71
  40. data/test/test_object.rb +1 -1
  41. data/test/test_objectstats.rb +1 -1
  42. data/test/test_pkgconfig.rb +7 -5
  43. data/test/test_proc.rb +1 -1
  44. data/test/test_set.rb +1 -1
  45. data/test/test_thread_pool.rb +409 -0
  46. data/test/test_time.rb +6 -6
  47. data/test/test_unbound_method.rb +1 -1
  48. metadata +157 -131
  49. data/README.txt +0 -45
  50. data/ext/extconf.rb +0 -29
  51. data/ext/utilrb_ext.cc +0 -144
@@ -9,43 +9,74 @@ module Utilrb
9
9
  rescue LoadError
10
10
  begin
11
11
  require 'rdoc/task'
12
- 'rdoc-new'
12
+ 'rdoc'
13
13
  rescue LoadError
14
- begin
15
- require 'rake/rdoctask'
16
- 'rdoc-old'
17
- rescue LoadError
18
- end
19
14
  end
20
15
  end
21
16
 
17
+ def self.doc?
18
+ if DOC_MODE
19
+ true
20
+ else
21
+ false
22
+ end
23
+ end
24
+
22
25
  def self.doc(target = 'docs', options = Hash.new)
26
+ if target.kind_of?(Hash)
27
+ target, options = 'docs', target
28
+ end
29
+
23
30
  options = Kernel.validate_options options,
24
- :include => [File.join(Dir.pwd, 'lib', '**'), File.join(Dir.pwd, 'ext', '**')],
31
+ :include => [File.join('lib', '**', '*.rb'), File.join('ext', '**', '*.cc')],
25
32
  :exclude => [],
26
33
  :target_dir => 'doc',
27
34
  :title => '',
28
- :plugins => []
35
+ :plugins => [],
36
+ :files => []
29
37
 
30
38
  case DOC_MODE
31
39
  when 'yard'
32
40
  task = YARD::Rake::YardocTask.new(target)
33
41
  task.files.concat(options[:include])
34
42
  task.options << '--title' << options[:title] << '--output-dir' << options[:target_dir]
43
+ if !options[:files].empty?
44
+ task.options << "--files" << options[:files].join(",")
45
+ end
35
46
  options[:plugins].each do |plugin_name|
36
47
  require "#{plugin_name}/yard"
37
48
  end
38
- when /rdoc/
39
- klass = if DOC_MODE == 'rdoc-new'
40
- RDoc::Task
41
- else
42
- ::Rake::RDocTask
43
- end
44
- task = klass.new(target)
49
+
50
+ task_clobber = ::Rake::Task.define_task "clobber_#{target}" do
51
+ FileUtils.rm_rf options[:target_dir]
52
+ end
53
+ task_clobber.add_description "Remove #{target} products"
54
+
55
+ name = ::Rake.application.current_scope.dup
56
+ if name.respond_to?(:path)
57
+ name = name.map(&:to_s).reverse
58
+ end
59
+ name << task.name
60
+ task_re = ::Rake::Task.define_task "re#{target}" do
61
+ FileUtils.rm_rf options[:target_dir]
62
+ ::Rake::Task[name.join(":")].invoke
63
+ end
64
+ task_re.add_description "Force a rebuild of #{target}"
65
+
66
+ when "rdoc"
67
+ task = RDoc::Task.new(target)
45
68
  task.rdoc_files.include(*options[:include])
46
69
  task.rdoc_files.exclude(*options[:exclude])
47
70
  task.title = options[:title]
48
71
  task.rdoc_dir = File.expand_path(options[:target_dir])
72
+
73
+ else
74
+ [target, "re#{target}"].each do |task_name|
75
+ t = ::Rake::Task.define_task(task_name) do
76
+ STDERR.puts "Documentation generation is disabled: install either the yard or rdoc gems"
77
+ end
78
+ t.add_description "Disabled on this installation as neither yard nor rdoc are available"
79
+ end
49
80
  end
50
81
  end
51
82
  end
@@ -150,12 +150,12 @@ module Kernel
150
150
  def dsl_exec_common(file, proxied_object, context, full_backtrace, *exceptions, &code)
151
151
  load_dsl_filter_backtrace(file, full_backtrace, *exceptions) do
152
152
  sandbox = with_module(*context) do
153
- Class.new do
153
+ Class.new(BasicObject) do
154
154
  def self.name; "" end
155
155
  attr_accessor :main_object
156
156
  def initialize(obj); @main_object = obj end
157
157
  def method_missing(*m, &block)
158
- main_object.send(*m, &block)
158
+ main_object.__send__(*m, &block)
159
159
  end
160
160
  end
161
161
  end
@@ -57,6 +57,12 @@ module Kernel
57
57
  return *[known_options, unknown_options]
58
58
  end
59
59
 
60
+ # Normalizes all keys in the given option hash to symbol and returns the
61
+ # modified hash
62
+ def normalize_options(options)
63
+ options.to_sym_keys
64
+ end
65
+
60
66
  # Validates an option hash, with default value support. See #filter_options
61
67
  #
62
68
  # In the first form, +option_hash+ should contain keys which are also
@@ -15,10 +15,17 @@ class Logger
15
15
  EOF
16
16
  end
17
17
 
18
+ # Forwarded to {Logger#silent}
19
+ def log_silent(&block)
20
+ logger.silent(&block)
21
+ end
22
+
23
+ # Forwarded to {Logger#nest}
18
24
  def log_nest(size, level = nil, &block)
19
25
  logger.nest(size, level, &block)
20
26
  end
21
27
 
28
+ # Forwarded to {Logger#log_pp}
22
29
  def log_pp(level, object, *first_line_format)
23
30
  logger.log_pp(level, object, *first_line_format)
24
31
  end
@@ -1,9 +1,65 @@
1
1
  require 'facets/module/spacename'
2
2
  require 'facets/kernel/constant'
3
+ require 'utilrb/object/attribute'
3
4
  require 'utilrb/object/singleton_class'
4
5
  require 'utilrb/logger/forward'
5
6
 
6
7
  class Logger
8
+ module HierarchyElement
9
+ attribute :log_children do
10
+ Array.new
11
+ end
12
+
13
+ # Makes it so that this level of the module hierarchy has its own
14
+ # logger. If +new_progname+ and/or +new_level+ are nil, the associated
15
+ # value are taken from the parent's logger.
16
+ def make_own_logger(new_progname = nil, new_level = nil)
17
+ new_logger = @logger || self.logger.dup
18
+ if new_progname
19
+ new_logger.progname = new_progname
20
+ end
21
+ if new_level
22
+ new_logger.level = new_level
23
+ end
24
+ self.logger = new_logger
25
+ end
26
+
27
+ # Allows to change the logger object at this level of the hierarchy
28
+ #
29
+ # This is usually not used directly: a new logger can be created with
30
+ # Hierarchy#make_own_logger and removed with Hierarchy#reset_own_logger
31
+ def logger=(new_logger)
32
+ @logger = new_logger
33
+ log_children.each do |child|
34
+ child.reset_default_logger
35
+ end
36
+ end
37
+
38
+ # Removes a logger defined at this level of the module hierarchy. The
39
+ # logging methods will now access the parent's module logger.
40
+ def reset_own_logger
41
+ @logger = nil
42
+ log_children.each do |child|
43
+ child.reset_default_logger
44
+ end
45
+ end
46
+
47
+ def reset_default_logger
48
+ @__utilrb_hierarchy__default_logger = nil
49
+ log_children.each do |child|
50
+ child.reset_default_logger
51
+ end
52
+ end
53
+
54
+ def logger
55
+ if defined?(@logger) && @logger
56
+ return @logger
57
+ elsif defined?(@__utilrb_hierarchy__default_logger) && @__utilrb_hierarchy__default_logger
58
+ return @__utilrb_hierarchy__default_logger
59
+ end
60
+ end
61
+ end
62
+
7
63
  # Define a hierarchy of loggers mapped to the module hierarchy.
8
64
  #
9
65
  # It defines the #logger accessor which either returns the logger
@@ -26,26 +82,11 @@ class Logger
26
82
  # "extend Hierarchy" will also add the Forward support if the parent module
27
83
  # has it.
28
84
  module Hierarchy
29
- # Allows to change the logger object at this level of the hierarchy
30
- #
31
- # This is usually not used directly: a new logger can be created with
32
- # Hierarchy#make_own_logger and removed with Hierarchy#reset_own_logger
33
- attr_writer :logger
85
+ include HierarchyElement
34
86
 
35
- def self.included(obj) # :nodoc:
36
- if obj.singleton_class.ancestors.include?(Logger::Forward)
37
- obj.send(:include, Logger::Forward)
38
- end
39
- end
40
-
41
- def self.extended(obj) # :nodoc:
42
- if obj.kind_of?(Module)
43
- parent_module = constant(obj.spacename)
44
- if (parent_module.singleton_class.ancestors.include?(Logger::Forward))
45
- obj.send(:extend, Logger::Forward)
46
- end
47
- end
48
- end
87
+ # Exception raised when a module/class in the logger hierarchy cannot
88
+ # find a parent logger
89
+ class NoParentLogger < RuntimeError; end
49
90
 
50
91
  # Returns true if the local module has its own logger, and false if it
51
92
  # returns the logger of the parent
@@ -53,40 +94,57 @@ class Logger
53
94
  defined?(@logger) && @logger
54
95
  end
55
96
 
56
- # Makes it so that this level of the module hierarchy has its own
57
- # logger. If +new_progname+ and/or +new_level+ are nil, the associated
58
- # value are taken from the parent's logger.
59
- def make_own_logger(new_progname = nil, new_level = nil)
60
- if !has_own_logger?
61
- @logger = self.logger.dup
62
- end
63
- if new_progname
64
- @logger.progname = new_progname
65
- end
66
- if new_level
67
- @logger.level = new_level
97
+ def self.included(obj) # :nodoc:
98
+ if obj.singleton_class.ancestors.include?(::Logger::Forward)
99
+ obj.send(:include, ::Logger::Forward)
68
100
  end
69
- @logger
70
101
  end
71
102
 
72
- # Removes a logger defined at this level of the module hierarchy. The
73
- # logging methods will now access the parent's module logger.
74
- def reset_own_logger
75
- @logger = nil
103
+ def self.extended(obj) # :nodoc:
104
+ obj.logger # initialize the default logger. Also does some checking
105
+ if obj.kind_of?(Module) && !obj.spacename.empty?
106
+ parent_module = constant(obj.spacename)
107
+ if (parent_module.singleton_class.ancestors.include?(::Logger::Forward))
108
+ obj.send(:extend, ::Logger::Forward)
109
+ end
110
+ end
76
111
  end
77
112
 
78
113
  # Returns the logger object that should be used to log at this level of
79
114
  # the module hierarchy
80
115
  def logger
81
- if defined?(@logger) && @logger
82
- return @logger
83
- elsif defined?(@default_logger) && @default_logger
84
- return @default_logger
116
+ if logger = super
117
+ return logger
85
118
  end
86
119
 
87
- @default_logger ||=
120
+ @__utilrb_hierarchy__default_logger =
88
121
  if kind_of?(Module)
89
- constant(self.spacename).logger
122
+ m = self
123
+ while m
124
+ if m.name && !m.spacename.empty?
125
+ parent_module =
126
+ begin
127
+ constant(m.spacename)
128
+ rescue NameError
129
+ end
130
+ if parent_module.respond_to?(:logger)
131
+ break
132
+ end
133
+ end
134
+ if m.respond_to?(:superclass)
135
+ m = m.superclass
136
+ else
137
+ m = nil; break
138
+ end
139
+ end
140
+
141
+ if !m
142
+ raise NoParentLogger, "cannot find a logger for #{self}"
143
+ end
144
+ if parent_module.respond_to? :log_children
145
+ parent_module.log_children << self
146
+ end
147
+ parent_module.logger
90
148
  else
91
149
  self.class.logger
92
150
  end
@@ -1,14 +1,47 @@
1
1
  require 'utilrb/object/attribute'
2
2
  class Logger
3
+ # [Integer] the current nest size
3
4
  attribute(:nest_size) { 0 }
5
+
6
+ # Sets the absolute number of spaces that should be prepended to every
7
+ # message
8
+ #
9
+ # You usually want to increment / decrement this with {nest}
4
10
  def nest_size=(new_value)
5
11
  @nest_string = nil
12
+ if new_value < 0
13
+ raise ArgumentError, "negative value for nest_size. You probably have unbalanced nest calls"
14
+ end
6
15
  @nest_size = new_value
7
16
  end
8
17
 
9
- def nest(size, level = nil)
10
- if level
11
- send(level) do
18
+ # Adds a certain number number of spaces to the current indentation level
19
+ #
20
+ # @overload nest(size)
21
+ # Permanently adds a number of spaces to the current indentation
22
+ # @param [Integer] size the number of spaces that should be added to
23
+ # {nest_size}
24
+ #
25
+ # @overload nest(size) { }
26
+ # Adds a number of spaces to the current indentation for the duration of
27
+ # the block, and restores the original indentation afterwards.
28
+ #
29
+ # @param [Integer] size the number of spaces that should be added to
30
+ # {nest_size}
31
+ #
32
+ # @overload nest(size, log_level) { }
33
+ # Shortcut for
34
+ # @example
35
+ #
36
+ # logger.send(log_level) do
37
+ # logger.nest(size) do
38
+ # ...
39
+ # end
40
+ # end
41
+ #
42
+ def nest(size, log_level = nil)
43
+ if log_level
44
+ send(log_level) do
12
45
  nest(size) do
13
46
  yield
14
47
  end
@@ -29,6 +62,8 @@ class Logger
29
62
  end
30
63
  end
31
64
 
65
+ # Overloaded from the base logger implementation to add the current
66
+ # indentation
32
67
  def format_message(severity, datetime, progname, msg)
33
68
  if !@nest_string
34
69
  @nest_string = " " * self.nest_size
@@ -34,7 +34,7 @@ class Logger
34
34
  def log_pp(level, object, *first_line_format)
35
35
  send(level) do
36
36
  first_line = !first_line_format.empty? && defined?(HighLine)
37
- Logger.pp_to_array(object).each do |line|
37
+ self.class.pp_to_array(object).each do |line|
38
38
  if first_line
39
39
  line = color(line, *first_line_format)
40
40
  first_line = false
@@ -1,3 +1,4 @@
1
+ require 'utilrb/logger/hierarchy'
1
2
  class Logger
2
3
  HAS_COLOR =
3
4
  begin
@@ -16,9 +17,12 @@ class Logger
16
17
  # Defines a logger on a module, allowing to use that module as a root in a
17
18
  # hierarchy (i.e. having submodules use the Logger::Hierarchy support)
18
19
  #
19
- # +progname+ is used as the logger's program name
20
- #
21
- # +base_level+ is the level at which the logger is initialized
20
+ # @param [String] progname is used as the logger's program name
21
+ # @param [Integer/Symbol] base_level is the level at which the logger is
22
+ # initialized, this can be either a symbol from [:DEBUG, :INFO, :WARN,
23
+ # :ERROR, :FATAL] or the integer constants from Logger::DEBUG,
24
+ # Logger::INFO, etc. This value is overriden if the BASE_LOG_LEVEL
25
+ # environment variable is set.
22
26
  #
23
27
  # If a block is given, it will be provided the message severity, time,
24
28
  # program name and text and should return the formatted message.
@@ -27,15 +31,22 @@ class Logger
27
31
  # accessed. Moreover, it includes Logger::Forward, which allows to access
28
32
  # the logger's output methods on the module directly
29
33
  #
30
- # Example:
31
- #
34
+ # @example
32
35
  # module MyModule
33
- # extend Logger.Root('MyModule', :WARN)
36
+ # extend Logger.Root('MyModule', Logger::WARN)
34
37
  # end
35
38
  #
36
39
  # MyModule.info "text"
37
40
  # MyModule.warn "warntext"
41
+ #
38
42
  def self.Root(progname, base_level, &block)
43
+ begin
44
+ base_level = ENV['BASE_LOG_LEVEL'].upcase.to_sym if ENV['BASE_LOG_LEVEL']
45
+ base_level = Logger.const_get( base_level ) if base_level.is_a? Symbol
46
+ rescue Exception => e
47
+ raise ArgumentError, "Log level #{base_level} is not available in the ruby Logger"
48
+ end
49
+
39
50
  console = @console
40
51
  formatter =
41
52
  if block then lambda(&block)
@@ -48,17 +59,21 @@ class Logger
48
59
 
49
60
  Module.new do
50
61
  include Logger::Forward
62
+ include Logger::HierarchyElement
51
63
 
52
- singleton = (class << self; self end)
53
- singleton.send(:define_method, :extended) do |mod|
54
- logger = Logger.new(STDOUT)
64
+ def has_own_logger?; true end
65
+
66
+ define_method :logger do
67
+ if logger = super()
68
+ return logger
69
+ end
70
+
71
+ logger = ::Logger.new(STDOUT)
55
72
  logger.level = base_level
56
73
  logger.progname = progname
57
74
  logger.formatter = formatter
58
- mod.instance_variable_set(:@logger, logger)
75
+ @__utilrb_hierarchy__default_logger = logger
59
76
  end
60
-
61
- attr_accessor :logger
62
77
  end
63
78
  end
64
79
  end