From: merch-redmine@... Date: 2017-10-24T22:55:40+00:00 Subject: [ruby-core:83549] [Ruby trunk Feature#12882] Add caller/file/line information to internal Kernel#warn calls Issue #12882 has been updated by jeremyevans0 (Jeremy Evans). File 0001-Add-uplevel-keyword-to-Kernel-warn-and-use-it.patch added Attached is a patch to add the uplevel keyword to Kernel#warn. It is based on nobu's patch, but I've modified it so that it only uses the path and lineno of the backtrace location (not the method name/label), and it also prepends the string "warning: " before the message. This way if you use the uplevel keyword, you get the same format that rb_warn would use. This patch includes changes to lib (except for bundler, rubygems, and rdoc) to use the uplevel keyword when calling `Kernel#warn`. It also contains changes to net/ftp and net/imap to use `Kernel#warn` for warnings instead of operating directly on $stderr, and changes to lib/cgi/core and tempfile to use $stderr directly instead of `Kernel#warn` for debug messages that are not warnings. ---------------------------------------- Feature #12882: Add caller/file/line information to internal Kernel#warn calls https://bugs.ruby-lang.org/issues/12882#change-67577 * Author: jeremyevans0 (Jeremy Evans) * Status: Feedback * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) * Target version: ---------------------------------------- Most internal uses of Kernel#warn do not begin with caller/file/line information, making debugging such warnings more difficult, and hindering filtering/processing of warnings on a per-file/directory/gem basis when overriding Warning.warn (a new feature in ruby 2.4). I think it would be better if internal calls to Kernel#warn in ruby code included caller information, so the warnings generated are similar to those generated by rb_warn in C code. I'm attaching an initial attempt at this. 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, probably because 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, if we want to do that, we should probably add a method to Thread::Backtrace::Location that returns just the absolute_path and lineno as a string, so we could standardize on `caller_locations(1,1).first.file_and_line` (or whatever the method is called). There are a few questionable changes in the attached patch, 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. The attached patch does not include changes for rdoc and rubygems, as those are maintained in separate repositories. Assuming that this patch or a similar one is accepted, I can submit patches to both rdoc and rubygems for their warning messages. ---Files-------------------------------- 0001-Prepend-warning-messages-with-caller-information.patch (30.6 KB) 0001-Add-uplevel-keyword-to-Kernel-warn-and-use-it.patch (26 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: