Skip to content
Browse files

Polish awesome_print functionality from #7

  • Loading branch information...
1 parent f90e727 commit f8940de6c9601de4c67cb3e4d5ddceced1b35e97 @cldwalker cldwalker committed Apr 20, 2012
Showing with 28 additions and 34 deletions.
  1. +13 −12 lib/log_buddy.rb
  2. +11 −14 lib/log_buddy/utils.rb
  3. +4 −8 spec/log_buddy/log_buddy_spec.rb
View
25 lib/log_buddy.rb
@@ -4,7 +4,7 @@
=begin rdoc
LogBuddy is a developer tool for easy logging while testing, debugging, and inspecting.
-
+
The <tt>d</tt> log method to give you easy, concise output of variables with their names and values.
Requiring 'log_buddy' will _automatically_ mixin the <tt>d</tt> method into Object. You can avoid this
@@ -20,16 +20,17 @@ def bark
d { a } # logs "a = 'foo'"
d { @a } # logs "@a = 'my var'"
d { bark } # logs "bark = woof!"
-
+
=end
module LogBuddy
# Configure and include LogBuddy into Object.
# You can pass in any of the following configuration options:
- #
- # * <tt>:logger</tt> - the logger instance that LogBuddy should use (if not provided,
+ #
+ # * <tt>:logger</tt> - the logger instance that LogBuddy should use (if not provided,
# tries to default to RAILS_DEFAULT_LOGGER, and then to a STDOUT logger).
# * <tt):log_to_stdout</tt> - whether LogBuddy should _also_ log to STDOUT, very helpful for Autotest (default is +true+).
# * <tt>:disabled</tt> - when true, LogBuddy will not produce any output
+ # * <tt>:use_awesome_print</tt> - when true, LogBuddy will log object with awesome_print
def self.init(options = {})
@logger = options[:logger]
@log_to_stdout = options.has_key?(:log_to_stdout) ? options[:log_to_stdout] : true
@@ -40,7 +41,7 @@ def self.init(options = {})
# Add the LogBuddy::Mixin to Object instance and class level.
def self.mixin_to_object
- Object.class_eval {
+ Object.class_eval {
include LogBuddy::Mixin
extend LogBuddy::Mixin
}
@@ -52,17 +53,17 @@ def logger
return @logger if @logger
@logger = init_default_logger
end
-
+
def log_to_stdout?
@log_to_stdout
end
def use_awesome_print?
@use_awesome_print
end
-
+
private
-
+
# First try Rails.logger, then RAILS_DEFAULT_LOGGER (for older
# versions of Rails), then just use a STDOUT Logger
def init_default_logger
@@ -75,12 +76,12 @@ def init_default_logger
Logger.new(STDOUT)
end
end
-
+
def rails_environment
Object.const_defined?("Rails") && Object.const_get("Rails")
end
-
+
end
-
+
init unless ENV["SAFE_LOG_BUDDY"]
-end
+end
View
25 lib/log_buddy/utils.rb
@@ -1,54 +1,51 @@
module LogBuddy
module Utils
-
+
def debug(obj)
return if @disabled
str = obj_to_string(obj)
stdout_puts(str) if log_to_stdout?
logger.debug(str)
end
-
+
def arg_and_blk_debug(arg, blk)
result = eval(arg, blk.binding)
result_str = obj_to_string(result, :quote_strings => true)
LogBuddy.debug(%[#{arg} = #{result_str}\n])
end
-
+
def stdout_puts(str)
puts str
end
-
+
# Returns array of arguments in the block
# You must use the brace form (ie d { "hi" }) and not do...end
def parse_args(logged_line)
block_contents = logged_line[/\{(.*?)\}/, 1]
args = block_contents ? block_contents.split(";").map {|arg| arg.strip } : []
end
-
+
# Return the calling line
def read_line(frame)
file, line_number = frame.split(/:/, 2)
line_number = line_number.to_i
lines = File.readlines(file)
-
+
lines[line_number - 1]
end
-
+
def obj_to_string(obj, options = {})
quote_strings = options.delete(:quote_strings)
case obj
when ::String
- quote_strings ? %["#{obj}"] : obj
+ quote_strings ? %["#{obj}"] : obj
when ::Exception
"#{ obj.message } (#{ obj.class })\n" <<
(obj.backtrace || []).join("\n")
else
- if LogBuddy.use_awesome_print?
- obj.respond_to?(:ai) ? obj.ai : obj.inspect
- else
- obj.inspect
- end
+ LogBuddy.use_awesome_print? && obj.respond_to?(:ai) ?
+ obj.ai : obj.inspect
end
end
end
-end
+end
View
12 spec/log_buddy/log_buddy_spec.rb
@@ -1,19 +1,15 @@
require File.expand_path(File.join(File.dirname(__FILE__), *%w[spec_helper]))
describe LogBuddy do
-
+
it "has logger" do
LogBuddy.should respond_to(:logger)
end
-
+
it "has stdout config option" do
LogBuddy.should respond_to(:log_to_stdout?)
end
- it "has awesome_print config option" do
- LogBuddy.should respond_to(:use_awesome_print?)
- end
-
it "can override the default logger" do
file_logger = Logger.new "test.log"
LogBuddy.init :logger => file_logger
@@ -41,5 +37,5 @@
LogBuddy.use_awesome_print?.should == true
end
end
-
-end
+
+end

0 comments on commit f8940de

Please sign in to comment.
Something went wrong with that request. Please try again.