From f2d6ad9d67e9e7a5795e02a1ac0d3bc7719cd6b6 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 28 Oct 2016 13:22:31 -0700 Subject: [PATCH] Prepend warning messages with caller information This changes most of the internal callers to Kernel#warn to prepend caller information to the logged messages, for similarity with using the rb_warn method. This change allows users to see what is generating the warning messages, making debugging much easier. It also allows a warning processor that can filter/process warning messages by file path (i.e. on a per-gem basis) to correctly handle warnings from Kernel#warn in addition to those from rb_warn. There was a lot of internal inconsistency in existing warning messages that include caller information. I choose to standardize on "caller(1,1).first". Specifying 1 as the length argument to caller provides a 5-10x speedup compared to the default of nil as the length argument, and since warning messages only use the first line, there's no need to generate additional lines. I also benchmarked using caller_locations, but that didn't seem to perform better in my benchmarking, since the result is just converted to a string anyway. Some existing warning messages that include caller information use #caller but attempt to strip out the method information via gsub. I'm guessing it would be better to use #caller_locations. However, that's kind of cumbersome right now, we should probably add a method to Thread::Backtrace::Location that includes just the absolute_path and lineno as a string, so we could standardize on caller_locations(1,1).first.file_and_line. There are a few questionable changes in here, such as the use of caller(0,1) in certain cases (when the changes were at top level), or including caller information in warning messages that were only output in DEBUG mode. This does not include changes for rdoc and rubygems, as those are maintained in separate repositories. --- ext/json/lib/json/ext.rb | 2 +- ext/openssl/lib/openssl/config.rb | 4 ++-- ext/openssl/lib/openssl/pkey.rb | 2 +- ext/openssl/lib/openssl/x509.rb | 2 +- ext/psych/lib/psych/coder.rb | 2 +- ext/psych/lib/psych/core_ext.rb | 2 +- ext/psych/lib/psych/deprecated.rb | 16 ++++++++-------- ext/psych/lib/psych/visitors/to_ruby.rb | 2 +- ext/psych/lib/psych/visitors/yaml_tree.rb | 4 ++-- ext/win32/lib/Win32API.rb | 2 +- lib/cgi/core.rb | 2 +- lib/cmath.rb | 2 +- lib/delegate.rb | 2 +- lib/forwardable.rb | 2 +- lib/irb/locale.rb | 2 +- lib/logger.rb | 10 +++++----- lib/mathn.rb | 2 +- lib/matrix.rb | 16 ++++++++-------- lib/net/http.rb | 2 +- lib/net/http/generic_request.rb | 4 ++-- lib/net/http/header.rb | 4 ++-- lib/net/http/response.rb | 6 +++--- lib/rexml/cdata.rb | 2 +- lib/rexml/comment.rb | 2 +- lib/rexml/element.rb | 2 +- lib/rexml/instruction.rb | 2 +- lib/rexml/node.rb | 2 +- lib/rexml/text.rb | 2 +- lib/rss/rss.rb | 2 +- lib/tempfile.rb | 6 +++--- lib/uri/common.rb | 8 ++++---- lib/uri/generic.rb | 2 +- lib/webrick/utils.rb | 2 +- lib/yaml.rb | 8 +++++--- test/logger/test_logdevice.rb | 2 +- 35 files changed, 68 insertions(+), 66 deletions(-) diff --git a/ext/json/lib/json/ext.rb b/ext/json/lib/json/ext.rb index 7264a85..e3e9794 100644 --- a/ext/json/lib/json/ext.rb +++ b/ext/json/lib/json/ext.rb @@ -6,7 +6,7 @@ module JSON module Ext require 'json/ext/parser' require 'json/ext/generator' - $DEBUG and warn "Using Ext extension for JSON." + $DEBUG and warn "#{caller(0,1).first}: Using Ext extension for JSON." JSON.parser = Parser JSON.generator = Generator end diff --git a/ext/openssl/lib/openssl/config.rb b/ext/openssl/lib/openssl/config.rb index 8822545..e6aa7ab 100644 --- a/ext/openssl/lib/openssl/config.rb +++ b/ext/openssl/lib/openssl/config.rb @@ -285,7 +285,7 @@ def get_value(section, key) # # Use #get_value instead def value(arg1, arg2 = nil) # :nodoc: - warn('Config#value is deprecated; use Config#get_value') + warn("#{caller(1,1).first}: Config#value is deprecated; use Config#get_value") if arg2.nil? section, key = 'default', arg1 else @@ -346,7 +346,7 @@ def [](section) # # Use #[] instead def section(name) # :nodoc: - warn('Config#section is deprecated; use Config#[]') + warn("#{caller(1,1).first}: Config#section is deprecated; use Config#[]") @data[name] || {} end diff --git a/ext/openssl/lib/openssl/pkey.rb b/ext/openssl/lib/openssl/pkey.rb index 9af5f78..f31903c 100644 --- a/ext/openssl/lib/openssl/pkey.rb +++ b/ext/openssl/lib/openssl/pkey.rb @@ -28,7 +28,7 @@ class DH # :nodoc: DEFAULT_TMP_DH_CALLBACK = lambda { |ctx, is_export, keylen| - warn "using default DH parameters." if $VERBOSE + warn "#{caller(1,1).first}: using default DH parameters." if $VERBOSE case keylen when 1024 then OpenSSL::PKey::DH::DEFAULT_1024 when 2048 then OpenSSL::PKey::DH::DEFAULT_2048 diff --git a/ext/openssl/lib/openssl/x509.rb b/ext/openssl/lib/openssl/x509.rb index aef3456..75e0c35 100644 --- a/ext/openssl/lib/openssl/x509.rb +++ b/ext/openssl/lib/openssl/x509.rb @@ -156,7 +156,7 @@ def pretty_print(q) class StoreContext def cleanup - warn "(#{caller.first}) OpenSSL::X509::StoreContext#cleanup is deprecated with no replacement" if $VERBOSE + warn "#{caller(1,1).first}: OpenSSL::X509::StoreContext#cleanup is deprecated with no replacement" if $VERBOSE end end diff --git a/ext/psych/lib/psych/coder.rb b/ext/psych/lib/psych/coder.rb index 26005f5..8c6c051 100644 --- a/ext/psych/lib/psych/coder.rb +++ b/ext/psych/lib/psych/coder.rb @@ -23,7 +23,7 @@ def initialize tag def scalar *args if args.length > 0 - warn "#{caller[0]}: Coder#scalar(a,b,c) is deprecated" if $VERBOSE + warn "#{caller(1,1).first}: Coder#scalar(a,b,c) is deprecated" if $VERBOSE @tag, @scalar, _ = args @type = :scalar end diff --git a/ext/psych/lib/psych/core_ext.rb b/ext/psych/lib/psych/core_ext.rb index 1a98279..7ae0e92 100644 --- a/ext/psych/lib/psych/core_ext.rb +++ b/ext/psych/lib/psych/core_ext.rb @@ -22,7 +22,7 @@ class Module def psych_yaml_as url return if caller[0].end_with?('rubytypes.rb') if $VERBOSE - warn "#{caller[0]}: yaml_as is deprecated, please use yaml_tag" + warn "#{caller(1,1).first}: yaml_as is deprecated, please use yaml_tag" end Psych.add_tag(url, self) end diff --git a/ext/psych/lib/psych/deprecated.rb b/ext/psych/lib/psych/deprecated.rb index 165d210..210c760 100644 --- a/ext/psych/lib/psych/deprecated.rb +++ b/ext/psych/lib/psych/deprecated.rb @@ -10,7 +10,7 @@ module DeprecatedMethods # :nodoc: end def self.quick_emit thing, opts = {}, &block # :nodoc: - warn "#{caller[0]}: YAML.quick_emit is deprecated" if $VERBOSE && !caller[0].start_with?(File.dirname(__FILE__)) + warn "#{caller(1,1).first}: YAML.quick_emit is deprecated" if $VERBOSE && !caller[0].start_with?(File.dirname(__FILE__)) target = eval 'self', block.binding target.extend DeprecatedMethods metaclass = class << target; self; end @@ -25,7 +25,7 @@ def self.quick_emit thing, opts = {}, &block # :nodoc: # This method is deprecated, use Psych.load_stream instead. def self.load_documents yaml, &block if $VERBOSE - warn "#{caller[0]}: load_documents is deprecated, use load_stream" + warn "#{caller(1,1).first}: load_documents is deprecated, use load_stream" end list = load_stream yaml return list unless block_given? @@ -33,7 +33,7 @@ def self.load_documents yaml, &block end def self.detect_implicit thing - warn "#{caller[0]}: detect_implicit is deprecated" if $VERBOSE + warn "#{caller(1,1).first}: detect_implicit is deprecated" if $VERBOSE return '' unless String === thing return 'null' if '' == thing ss = ScalarScanner.new(ClassLoader.new) @@ -41,27 +41,27 @@ def self.detect_implicit thing end def self.add_ruby_type type_tag, &block - warn "#{caller[0]}: add_ruby_type is deprecated, use add_domain_type" if $VERBOSE + warn "#{caller(1,1).first}: add_ruby_type is deprecated, use add_domain_type" if $VERBOSE domain = 'ruby.yaml.org,2002' key = ['tag', domain, type_tag].join ':' @domain_types[key] = [key, block] end def self.add_private_type type_tag, &block - warn "#{caller[0]}: add_private_type is deprecated, use add_domain_type" if $VERBOSE + warn "#{caller(1,1).first}: add_private_type is deprecated, use add_domain_type" if $VERBOSE domain = 'x-private' key = [domain, type_tag].join ':' @domain_types[key] = [key, block] end def self.tagurize thing - warn "#{caller[0]}: add_private_type is deprecated, use add_domain_type" if $VERBOSE + warn "#{caller(1,1).first}: add_private_type is deprecated, use add_domain_type" if $VERBOSE return thing unless String === thing "tag:yaml.org,2002:#{thing}" end def self.read_type_class type, reference - warn "#{caller[0]}: read_type_class is deprecated" if $VERBOSE + warn "#{caller(1,1).first}: read_type_class is deprecated" if $VERBOSE _, _, type, name = type.split ':', 4 reference = name.split('::').inject(reference) do |k,n| @@ -71,7 +71,7 @@ def self.read_type_class type, reference end def self.object_maker klass, hash - warn "#{caller[0]}: object_maker is deprecated" if $VERBOSE + warn "#{caller(1,1).first}: object_maker is deprecated" if $VERBOSE klass.allocate.tap do |obj| hash.each { |k,v| obj.instance_variable_set(:"@#{k}", v) } end diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index fd1c8e6..564c7b8 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -382,7 +382,7 @@ def init_with o, h, node o.init_with c elsif o.respond_to?(:yaml_initialize) if $VERBOSE - warn "Implementing #{o.class}#yaml_initialize is deprecated, please implement \"init_with(coder)\"" + warn "#{caller(1,1).first}: Implementing #{o.class}#yaml_initialize is deprecated, please implement \"init_with(coder)\"" end o.yaml_initialize c.tag, c.map else diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index 11214ec..84f1ba2 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -57,7 +57,7 @@ def self.new emitter = nil, ss = nil, options = nil return super if emitter && ss && options if $VERBOSE - warn "This API is deprecated, please pass an emitter, scalar scanner, and options or call #{self}.create() (#{caller.first})" + warn "#{caller(1,1).first}: This API is deprecated, please pass an emitter, scalar scanner, and options or call #{self}.create() (#{caller.first})" end create emitter, ss end @@ -145,7 +145,7 @@ def accept target if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/ unless target.respond_to?(:encode_with) if $VERBOSE - warn "implementing to_yaml is deprecated, please implement \"encode_with\"" + warn "#{caller(1,1).first}: implementing to_yaml is deprecated, please implement \"encode_with\"" end target.to_yaml(:nodump => true) diff --git a/ext/win32/lib/Win32API.rb b/ext/win32/lib/Win32API.rb index d03ecc1..aa197f6 100644 --- a/ext/win32/lib/Win32API.rb +++ b/ext/win32/lib/Win32API.rb @@ -2,7 +2,7 @@ # frozen_string_literal: true # for backward compatibility -warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: Win32API is deprecated after Ruby 1.9.1; use fiddle directly instead" if $VERBOSE +warn "#{caller(1,1).first.sub(/:in `.*'\z/, '')}: Win32API is deprecated after Ruby 1.9.1; use fiddle directly instead" if $VERBOSE require 'fiddle/import' diff --git a/lib/cgi/core.rb b/lib/cgi/core.rb index 1a741dc..277746f 100644 --- a/lib/cgi/core.rb +++ b/lib/cgi/core.rb @@ -260,7 +260,7 @@ def nph? #:nodoc: def _header_for_modruby(buf) #:nodoc: request = Apache::request buf.scan(/([^:]+): (.+)#{EOL}/o) do |name, value| - warn sprintf("name:%s value:%s\n", name, value) if $DEBUG + warn sprintf("#{caller(0,1).first}: name:%s value:%s\n", name, value) if $DEBUG case name when 'Set-Cookie' request.headers_out.add(name, value) diff --git a/lib/cmath.rb b/lib/cmath.rb index 41ab06e..33744cb 100644 --- a/lib/cmath.rb +++ b/lib/cmath.rb @@ -50,7 +50,7 @@ module CMath atanh ].each do |meth| define_method(meth + '!') do |*args, &block| - warn("CMath##{meth}! is deprecated; use CMath##{meth} or Math##{meth}") if $VERBOSE + warn("#{caller(1,1).first}: CMath##{meth}! is deprecated; use CMath##{meth} or Math##{meth}") if $VERBOSE RealMath.send(meth, *args, &block) end end diff --git a/lib/delegate.rb b/lib/delegate.rb index b418803..2536869 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -97,7 +97,7 @@ def respond_to_missing?(m, include_private) target = self.__getobj__ {r = false} r &&= target.respond_to?(m, include_private) if r && include_private && !target.respond_to?(m, false) - warn "#{caller(3)[0]}: delegator does not forward private method \##{m}" + warn "#{caller(3,1).first}: delegator does not forward private method \##{m}" return false end r diff --git a/lib/forwardable.rb b/lib/forwardable.rb index b94c9a3..2e7e7c1 100644 --- a/lib/forwardable.rb +++ b/lib/forwardable.rb @@ -209,7 +209,7 @@ def self._delegator_method(obj, accessor, method, ali) method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}" begin; unless ::Kernel.instance_method(:respond_to?).bind(_).call(:"#{method}") - ::Kernel.warn "\#{caller_locations(1)[0]}: "#{mesg.dump}"\#{_.class}"'##{method}' + ::Kernel.warn "\#{caller_locations(1,1).first}: "#{mesg.dump}"\#{_.class}"'##{method}' _#{method_call} else _.#{method}(*args, &block) diff --git a/lib/irb/locale.rb b/lib/irb/locale.rb index df540c8..229aa13 100644 --- a/lib/irb/locale.rb +++ b/lib/irb/locale.rb @@ -31,7 +31,7 @@ def initialize(locale = nil) if @encoding_name begin load 'irb/encoding_aliases.rb'; rescue LoadError; end if @encoding = @@legacy_encoding_alias_map[@encoding_name] - warn "%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"] + warn "#{caller(1,1).first}: %s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"] end @encoding = Encoding.find(@encoding_name) rescue nil end diff --git a/lib/logger.rb b/lib/logger.rb index e0c93c3..21892fc 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -684,17 +684,17 @@ def write(message) begin check_shift_log rescue - warn("log shifting failed. #{$!}") + warn("#{caller(1,1).first}: log shifting failed. #{$!}") end end begin @dev.write(message) rescue - warn("log writing failed. #{$!}") + warn("#{caller(1,1).first}: log writing failed. #{$!}") end end rescue Exception => ignored - warn("log writing failed. #{ignored}") + warn("#{caller(1,1).first}: log writing failed. #{ignored}") end end @@ -802,7 +802,7 @@ def lock_shift_log rescue Errno::ENOENT # @filename file would not exist right after #rename and before #create_logfile if retry_limit <= 0 - warn("log rotation inter-process lock failed. #{$!}") + warn("#{caller(1,1).first}: log rotation inter-process lock failed. #{$!}") else sleep retry_sleep retry_limit -= 1 @@ -811,7 +811,7 @@ def lock_shift_log end end rescue - warn("log rotation inter-process lock failed. #{$!}") + warn("#{caller(1,1).first}: log rotation inter-process lock failed. #{$!}") end end diff --git a/lib/mathn.rb b/lib/mathn.rb index 9cb9401..f161507 100644 --- a/lib/mathn.rb +++ b/lib/mathn.rb @@ -38,7 +38,7 @@ # class Numeric follows to make this documentation findable in a reasonable # location -warn('lib/mathn.rb is deprecated') if $VERBOSE +warn("#{caller(0,1).first}: lib/mathn.rb is deprecated") if $VERBOSE class Numeric; end diff --git a/lib/matrix.rb b/lib/matrix.rb index fe61b6d..73cb1f0 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1246,7 +1246,7 @@ def determinant_bareiss # deprecated; use Matrix#determinant # def determinant_e - warn "#{caller(1)[0]}: warning: Matrix#determinant_e is deprecated; use #determinant" + warn "#{caller(1,1).first}: warning: Matrix#determinant_e is deprecated; use #determinant" determinant end alias det_e determinant_e @@ -1304,7 +1304,7 @@ def rank # deprecated; use Matrix#rank # def rank_e - warn "#{caller(1)[0]}: warning: Matrix#rank_e is deprecated; use #rank" + warn "#{caller(1,1).first}: warning: Matrix#rank_e is deprecated; use #rank" rank end @@ -1490,17 +1490,17 @@ def to_a end def elements_to_f - warn "#{caller(1)[0]}: warning: Matrix#elements_to_f is deprecated, use map(&:to_f)" + warn "#{caller(1,1).first}: warning: Matrix#elements_to_f is deprecated, use map(&:to_f)" map(&:to_f) end def elements_to_i - warn "#{caller(1)[0]}: warning: Matrix#elements_to_i is deprecated, use map(&:to_i)" + warn "#{caller(1,1).first}: warning: Matrix#elements_to_i is deprecated, use map(&:to_i)" map(&:to_i) end def elements_to_r - warn "#{caller(1)[0]}: warning: Matrix#elements_to_r is deprecated, use map(&:to_r)" + warn "#{caller(1,1).first}: warning: Matrix#elements_to_r is deprecated, use map(&:to_r)" map(&:to_r) end @@ -2122,17 +2122,17 @@ def to_a end def elements_to_f - warn "#{caller(1)[0]}: warning: Vector#elements_to_f is deprecated" + warn "#{caller(1,1).first}: warning: Vector#elements_to_f is deprecated" map(&:to_f) end def elements_to_i - warn "#{caller(1)[0]}: warning: Vector#elements_to_i is deprecated" + warn "#{caller(1,1).first}: warning: Vector#elements_to_i is deprecated" map(&:to_i) end def elements_to_r - warn "#{caller(1)[0]}: warning: Vector#elements_to_r is deprecated" + warn "#{caller(1,1).first}: warning: Vector#elements_to_r is deprecated" map(&:to_r) end diff --git a/lib/net/http.rb b/lib/net/http.rb index 4738bc6..dfea612 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -683,7 +683,7 @@ def inspect # http.start { .... } # def set_debug_output(output) - warn 'Net::HTTP#set_debug_output called after HTTP started' if started? + warn "#{caller(1,1).first}: Net::HTTP#set_debug_output called after HTTP started" if started? @debug_output = output end diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 6c5ceaf..8ccd62b 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -82,7 +82,7 @@ def response_body_permitted? end def body_exist? - warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?" if $VERBOSE + warn "#{caller(1,1).first}: Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?" if $VERBOSE response_body_permitted? end @@ -299,7 +299,7 @@ def flush_buffer(out, buf, chunked_p) def supply_default_content_type return if content_type() - warn 'net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded' if $VERBOSE + warn "#{caller(1,1).first}: net/http: warning: Content-Type did not set; using application/x-www-form-urlencoded" if $VERBOSE set_content_type 'application/x-www-form-urlencoded' end diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index 63a163a..4c44869 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -14,9 +14,9 @@ def initialize_http_header(initheader) @header = {} return unless initheader initheader.each do |key, value| - warn "net/http: warning: duplicated HTTP header: #{key}" if key?(key) and $VERBOSE + warn "#{caller(1,1).first}: net/http: warning: duplicated HTTP header: #{key}" if key?(key) and $VERBOSE if value.nil? - warn "net/http: warning: nil HTTP header: #{key}" if $VERBOSE + warn "#{caller(1,1).first}: net/http: warning: nil HTTP header: #{key}" if $VERBOSE else @header[key.downcase] = [value.strip] end diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index e2964c4..6b0fc55 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -138,17 +138,17 @@ def uri= uri # :nodoc: # def response #:nodoc: - warn "#{caller(1)[0]}: warning: Net::HTTPResponse#response is obsolete" if $VERBOSE + warn "#{caller(1,1).first}: warning: Net::HTTPResponse#response is obsolete" if $VERBOSE self end def header #:nodoc: - warn "#{caller(1)[0]}: warning: Net::HTTPResponse#header is obsolete" if $VERBOSE + warn "#{caller(1,1).first}: warning: Net::HTTPResponse#header is obsolete" if $VERBOSE self end def read_header #:nodoc: - warn "#{caller(1)[0]}: warning: Net::HTTPResponse#read_header is obsolete" if $VERBOSE + warn "#{caller(1,1).first}: warning: Net::HTTPResponse#read_header is obsolete" if $VERBOSE self end diff --git a/lib/rexml/cdata.rb b/lib/rexml/cdata.rb index fe9b49b..b62356e 100644 --- a/lib/rexml/cdata.rb +++ b/lib/rexml/cdata.rb @@ -58,7 +58,7 @@ def value # c = CData.new( " Some text " ) # c.write( $stdout ) #-> def write( output=$stdout, indent=-1, transitive=false, ie_hack=false ) - Kernel.warn( "#{self.class.name}.write is deprecated" ) + Kernel.warn( "#{caller(1,1).first}: #{self.class.name}.write is deprecated" ) indent( output, indent ) output << START output << @string diff --git a/lib/rexml/comment.rb b/lib/rexml/comment.rb index 746af77..061cb20 100644 --- a/lib/rexml/comment.rb +++ b/lib/rexml/comment.rb @@ -48,7 +48,7 @@ def clone # ie_hack:: # Needed for conformity to the child API, but not used by this class. def write( output, indent=-1, transitive=false, ie_hack=false ) - Kernel.warn("Comment.write is deprecated. See REXML::Formatters") + Kernel.warn("#{caller(1,1).first}: Comment.write is deprecated. See REXML::Formatters") indent( output, indent ) output << START output << @string diff --git a/lib/rexml/element.rb b/lib/rexml/element.rb index f725d5a..ee2fc9b 100644 --- a/lib/rexml/element.rb +++ b/lib/rexml/element.rb @@ -686,7 +686,7 @@ def texts # doc.write( out ) #-> doc is written to the string 'out' # doc.write( $stdout ) #-> doc written to the console def write(output=$stdout, indent=-1, transitive=false, ie_hack=false) - Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters") + Kernel.warn("#{caller(1,1).first}: #{self.class.name}.write is deprecated. See REXML::Formatters") formatter = if indent > -1 if transitive require "rexml/formatters/transitive" diff --git a/lib/rexml/instruction.rb b/lib/rexml/instruction.rb index 576939c..47b8319 100644 --- a/lib/rexml/instruction.rb +++ b/lib/rexml/instruction.rb @@ -43,7 +43,7 @@ def clone # See the rexml/formatters package # def write writer, indent=-1, transitive=false, ie_hack=false - Kernel.warn( "#{self.class.name}.write is deprecated" ) + Kernel.warn( "#{caller(1,1).first}: #{self.class.name}.write is deprecated" ) indent(writer, indent) writer << START.sub(/\\/u, '') writer << @target diff --git a/lib/rexml/node.rb b/lib/rexml/node.rb index c7a3936..6ee63e9 100644 --- a/lib/rexml/node.rb +++ b/lib/rexml/node.rb @@ -26,7 +26,7 @@ def previous_sibling_node # REXML::Formatters package for changing the output style. def to_s indent=nil unless indent.nil? - Kernel.warn( "#{self.class.name}.to_s(indent) parameter is deprecated" ) + Kernel.warn( "#{caller(1,1).first}: #{self.class.name}.to_s(indent) parameter is deprecated" ) f = REXML::Formatters::Pretty.new( indent ) f.write( self, rv = "" ) else diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb index 9ea8ba9..c721dd5 100644 --- a/lib/rexml/text.rb +++ b/lib/rexml/text.rb @@ -293,7 +293,7 @@ def indent_text(string, level=1, style="\t", indentfirstline=true) # See REXML::Formatters # def write( writer, indent=-1, transitive=false, ie_hack=false ) - Kernel.warn("#{self.class.name}.write is deprecated. See REXML::Formatters") + Kernel.warn("#{caller(1,1).first}: #{self.class.name}.write is deprecated. See REXML::Formatters") formatter = if indent > -1 REXML::Formatters::Pretty.new( indent ) else diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index d161366..00512a1 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -596,7 +596,7 @@ def #{accessor_name}(*args) def #{accessor_name}=(*args) receiver = self.class.name - warn("Warning:\#{caller.first.sub(/:in `.*'\z/, '')}: " \ + warn("Warning:\#{caller(1,1).first.sub(/:in `.*'\z/, '')}: " \ "Don't use `\#{receiver}\##{accessor_name} = XXX'/" \ "`\#{receiver}\#set_#{accessor_name}(XXX)'. " \ "Those APIs are not sense of Ruby. " \ diff --git a/lib/tempfile.rb b/lib/tempfile.rb index b0ebd0b..04431cf 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -124,7 +124,7 @@ class Tempfile < DelegateClass(File) # If Tempfile.new cannot find a unique filename within a limited # number of tries, then it will raise an exception. def initialize(basename="", tmpdir=nil, mode: 0, **options) - warn "Tempfile.new doesn't call the given block." if block_given? + warn "#{caller(1,1).first}: Tempfile.new doesn't call the given block." if block_given? @unlinked = false @mode = mode|File::RDWR|File::CREAT|File::EXCL @@ -250,7 +250,7 @@ def initialize(tmpfile) def call(*args) return if @pid != Process.pid - warn "removing #{@tmpfile.path}..." if $DEBUG + warn "#{caller(1,1).first}: removing #{@tmpfile.path}..." if $DEBUG @tmpfile.close unless @tmpfile.closed? begin @@ -258,7 +258,7 @@ def call(*args) rescue Errno::ENOENT end - warn "done" if $DEBUG + warn "#{caller(1,1).first}: done" if $DEBUG end end diff --git a/lib/uri/common.rb b/lib/uri/common.rb index 48bf697..0d303fd 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -100,7 +100,7 @@ module Escape # # => "@%3F@%21" # def escape(*arg) - warn "#{caller(1)[0]}: warning: URI.escape is obsolete" if $VERBOSE + warn "#{caller(1,1).first}: warning: URI.escape is obsolete" if $VERBOSE DEFAULT_PARSER.escape(*arg) end alias encode escape @@ -126,7 +126,7 @@ def escape(*arg) # # => "http://example.com/?a=\t\r" # def unescape(*arg) - warn "#{caller(1)[0]}: warning: URI.unescape is obsolete" if $VERBOSE + warn "#{caller(1,1).first}: warning: URI.unescape is obsolete" if $VERBOSE DEFAULT_PARSER.unescape(*arg) end alias decode unescape @@ -294,7 +294,7 @@ def self.join(*str) # # => ["http://foo.example.com/bla", "mailto:test@example.com"] # def self.extract(str, schemes = nil, &block) - warn "#{caller(1)[0]}: warning: URI.extract is obsolete" if $VERBOSE + warn "#{caller(1,1).first}: warning: URI.extract is obsolete" if $VERBOSE DEFAULT_PARSER.extract(str, schemes, &block) end @@ -330,7 +330,7 @@ def self.extract(str, schemes = nil, &block) # end # def self.regexp(schemes = nil) - warn "#{caller(1)[0]}: warning: URI.regexp is obsolete" if $VERBOSE + warn "#{caller(1,1).first}: warning: URI.regexp is obsolete" if $VERBOSE DEFAULT_PARSER.make_regexp(schemes) end diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index 2b95b47..6dd6d4b 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -1526,7 +1526,7 @@ def find_proxy(env=ENV) elsif name == 'http_proxy' unless proxy_uri = env[name] if proxy_uri = env[name.upcase] - warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.' + warn "#{caller(1,1).first}: The environment variable HTTP_PROXY is discouraged. Use http_proxy." end end else diff --git a/lib/webrick/utils.rb b/lib/webrick/utils.rb index 8b34b66..2f3bf83 100644 --- a/lib/webrick/utils.rb +++ b/lib/webrick/utils.rb @@ -37,7 +37,7 @@ def su(user) Process::Sys::setgid(pw.gid) Process::Sys::setuid(pw.uid) else - warn("WEBrick::Utils::su doesn't work on this platform") + warn("#{caller(1,1).first}: WEBrick::Utils::su doesn't work on this platform") end end module_function :su diff --git a/lib/yaml.rb b/lib/yaml.rb index 0c33305..f23a388 100644 --- a/lib/yaml.rb +++ b/lib/yaml.rb @@ -5,9 +5,11 @@ begin require 'psych' rescue LoadError - warn "#{caller[0]}:" - warn "It seems your ruby installation is missing psych (for YAML output)." - warn "To eliminate this warning, please install libyaml and reinstall your ruby." + warn <<-eowarn +#{caller(1,1).first} +It seems your ruby installation is missing psych (for YAML output). +To eliminate this warning, please install libyaml and reinstall your ruby. + eowarn raise end diff --git a/test/logger/test_logdevice.rb b/test/logger/test_logdevice.rb index edb3ff5..e0eee7b 100644 --- a/test/logger/test_logdevice.rb +++ b/test/logger/test_logdevice.rb @@ -86,7 +86,7 @@ class << (stderr = '') logdev.close $stderr, stderr = stderr, $stderr end - assert_equal "log writing failed. disk is full\n", stderr + assert_include stderr, "log writing failed. disk is full\n" end def test_close -- 2.10.1