public
Rubygem
Description: log_buddy is your friendly little log buddy by your side
Homepage: http://opensource.thinkrelevance.com/wiki/log_buddy
Clone URL: git://github.com/relevance/logbuddy.git
Search Repo:
reorg a bit and add docs
rsanheim (author)
Fri Apr 04 10:47:14 -0700 2008
commit  5da27646ca4692bfb17f70efeaeadf35d36f34c2
tree    0cc4b518a42f08865c9c2ffb2235b9595307fd4f
parent  13984eefc08466ba325090e829c3ae83255b9ab4
...
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
0
@@ -1,3 +1,7 @@
0
+=== 0.0.2
0
+* rdocs
0
+* support for multiple statements in one "d" call separated by semicolons
0
+
0
 === 0.0.1 / 2008-03/28
0
 
0
 * Initial commit to github
...
1
2
3
4
5
6
7
...
1
2
3
 
4
5
6
0
@@ -1,7 +1,6 @@
0
 History.txt
0
 Manifest.txt
0
 README.rdoc
0
-README.txt
0
 Rakefile
0
 bin/log_buddy
0
 examples.rb
...
16
17
18
19
 
20
 
21
22
23
24
25
 
26
27
28
29
...
30
31
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
34
35
36
37
 
 
38
39
40
41
42
43
44
...
43
44
45
46
 
 
47
48
49
50
 
 
51
52
 
 
53
54
 
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
...
16
17
18
 
19
20
21
22
23
24
25
26
27
28
29
30
31
...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
...
78
79
80
 
81
82
83
84
85
86
87
88
89
 
90
91
92
93
94
95
96
97
98
99
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
102
103
0
@@ -16,13 +16,15 @@
0
     
0
 =end
0
 class LogBuddy
0
- VERSION = '0.0.1'
0
+ VERSION = '0.0.2'
0
 
0
+ # Use LogBuddy!
0
   def self.init(options = {})
0
     @logger = options[:default_logger]
0
     mixin_to_object
0
   end
0
   
0
+ # Add the LogBuddy::Mixin to Object instance and class level.
0
   def self.mixin_to_object
0
     Object.class_eval {
0
       include LogBuddy::Mixin
0
0
@@ -30,11 +32,44 @@
0
     }
0
   end
0
   
0
+ # The main Mixin that gets added on the #init call
0
+ module Mixin
0
+ # This is where the magic happens. This method can take a plain old string, and it will log
0
+ # it like any call to Logger#debug. To get the name of the thing you are logging and its value,
0
+ # use the block form:
0
+ # d { @a }
0
+ #
0
+ # Seperate with semicolons for multiple things - pretty much any valid ruby will work.
0
+ # d { @@foo; MY_CONST }
0
+ # d { @person; @@place; object.method() }
0
+ #
0
+ def d(msg = nil, &blk)
0
+ LogBuddy.debug(msg) if msg
0
+ return unless block_given?
0
+ logged_line = LogBuddy.read_line(caller[0])
0
+ arguments = LogBuddy.parse_args(logged_line)
0
+ arguments.each do |arg|
0
+ result = eval(arg, blk.binding)
0
+ LogBuddy.debug(%[#{arg} = '#{result}'\n])
0
+ end
0
+ end
0
+
0
+ # Add a default logger to everything, everywhere.
0
+ def logger
0
+ LogBuddy.default_logger
0
+ end
0
+ end
0
+
0
+ private
0
+
0
+ # Default logger LogBuddy will use
0
   def self.default_logger
0
     return @logger if @logger
0
     @logger = init_default_logger
0
   end
0
   
0
+ # Attempt to establish a default logger - first try RAILS_DEFAULT_LOGGER,
0
+ # then fallback to default.
0
   def self.init_default_logger
0
     if Object.const_defined?("RAILS_DEFAULT_LOGGER")
0
       @logger = Object.const_get("RAILS_DEFAULT_LOGGER")
0
0
0
0
0
@@ -43,36 +78,26 @@
0
       @logger = Logger.new(STDOUT)
0
     end
0
   end
0
-
0
+
0
+ # Just debug it
0
   def self.debug(str)
0
     default_logger.debug(str)
0
   end
0
   
0
+ # Returns array of arguments in the block
0
+ # You must ues the brace form (ie d { "hi" }) and not do...end
0
   def self.parse_args(logged_line)
0
- block_args = logged_line[/\{(.*)\}/, 1].strip
0
+ block_contents = logged_line[/\{(.*)\}/, 1]
0
+ args = block_contents.split(";").map {|arg| arg.strip }
0
   end
0
   
0
+ # Return the calling line
0
   def self.read_line(frame)
0
     file, line_number = frame.split(/:/, 2)
0
     line_number = line_number.to_i
0
     lines = File.readlines(file)
0
     
0
     lines[line_number - 1]
0
- end
0
-
0
- module Mixin
0
- def d(msg = nil, &blk)
0
- LogBuddy.debug(msg) if msg
0
- return unless block_given?
0
- logged_line = LogBuddy.read_line(caller[0])
0
- arguments = LogBuddy.parse_args(logged_line)
0
- result = eval(arguments, blk.binding)
0
- LogBuddy.debug(%[#{arguments} = '#{result}'\n])
0
- end
0
-
0
- def logger
0
- LogBuddy.default_logger
0
- end
0
   end
0
   
0
 end
...
93
94
95
 
 
 
 
 
 
96
97
98
...
104
105
106
 
 
 
 
 
 
 
 
 
 
107
108
...
93
94
95
96
97
98
99
100
101
102
103
104
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
0
@@ -93,6 +93,12 @@
0
       @a = "foo"
0
       d { @a }
0
     end
0
+
0
+ it "should output constants" do
0
+ FOO_CONST = "yo!"
0
+ LogBuddy.expects(:debug).with(%[FOO_CONST = 'yo!'\n])
0
+ d { FOO_CONST }
0
+ end
0
   
0
     it "should output class vars" do
0
       LogBuddy.expects(:debug).with(%[@@class_var = 'hi'\n])
0
@@ -104,6 +110,16 @@
0
       LogBuddy.expects(:debug).with(%[SomeModule.say_something("dude!!!!") = 'hello dude!!!!'\n])
0
       d { SomeModule.say_something("dude!!!!") }
0
     end
0
+
0
+ it "should output multiple things with each having their own log calls" do
0
+ local1 = '1'
0
+ local2 = '2'
0
+ @ivar1 = '1'
0
+ LogBuddy.expects(:debug).with(%[local1 = '1'\n])
0
+ LogBuddy.expects(:debug).with(%[local2 = '2'\n])
0
+ LogBuddy.expects(:debug).with(%[@ivar1 = '1'\n])
0
+ d { local1; local2; @ivar1 }
0
+ end
0
   end
0
 end

Comments

    No one has commented yet.