debuglog 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +38 -0
- data/doc/debuglog.markdown +262 -0
- data/lib/debuglog.rb +17 -0
- data/lib/debuglog/auto.rb +2 -0
- data/lib/debuglog/manual.rb +177 -0
- data/test/_setup.rb +14 -0
- data/test/debuglog-auto.rb +66 -0
- data/test/debuglog-manual-1.rb +39 -0
- data/test/debuglog-manual-2.rb +27 -0
- metadata +75 -0
data/README
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
debuglog: zero-conf debug.log file for simple and hassle-free debugging
|
2
|
+
|
3
|
+
Synopsis
|
4
|
+
|
5
|
+
Debuglog gives you debug, trace and time methods that write their output to
|
6
|
+
the file ./debug.log.
|
7
|
+
|
8
|
+
require 'debuglog' # or require 'debuglog/auto'
|
9
|
+
|
10
|
+
debug "Message..."
|
11
|
+
trace :x, binding
|
12
|
+
time('Task') { action }
|
13
|
+
|
14
|
+
You can change the names of the methods and the filename.
|
15
|
+
|
16
|
+
require 'debuglog/manual'
|
17
|
+
|
18
|
+
DebugLog.configure(
|
19
|
+
:debug => :my_debug,
|
20
|
+
:trace => :my_trace,
|
21
|
+
:time => :my_time,
|
22
|
+
:filename => 'log/xyz.log'
|
23
|
+
)
|
24
|
+
|
25
|
+
my_debug "Message..."
|
26
|
+
my_trace :x, binding
|
27
|
+
my_time('Task') { action }
|
28
|
+
|
29
|
+
In either case, the log file will look something like this:
|
30
|
+
|
31
|
+
DebugLog -- 2010-07-25 18:58:22 +1000
|
32
|
+
-------------------------------------
|
33
|
+
[00.3] Message...
|
34
|
+
[00.5] x == 5
|
35
|
+
[00.6] Task: 1.0831 sec
|
36
|
+
|
37
|
+
|
38
|
+
See http://gsinclair.github.com/debuglog.html for full details.
|
@@ -0,0 +1,262 @@
|
|
1
|
+
---
|
2
|
+
layout: default
|
3
|
+
title: Debuglog
|
4
|
+
---
|
5
|
+
|
6
|
+
# Debuglog -- a zero-conf debug.log file
|
7
|
+
|
8
|
+
**Status: awaiting release (July 2010)**
|
9
|
+
|
10
|
+
* This will be replaced by a table of contents
|
11
|
+
{:toc}
|
12
|
+
|
13
|
+
|
14
|
+
## Synopsis
|
15
|
+
|
16
|
+
Debuglog gives you `debug`, `trace` and `time` methods that write their output
|
17
|
+
to the file `./debug.log`.
|
18
|
+
|
19
|
+
{% highlight ruby %}
|
20
|
+
|
21
|
+
require 'debuglog' # or require 'debuglog/auto'
|
22
|
+
|
23
|
+
debug "Message..."
|
24
|
+
trace :x, binding
|
25
|
+
time('Task') { action }
|
26
|
+
|
27
|
+
{% endhighlight %}
|
28
|
+
|
29
|
+
You can change the names of the methods and the filename.
|
30
|
+
|
31
|
+
{% highlight ruby %}
|
32
|
+
|
33
|
+
require 'debuglog/manual'
|
34
|
+
|
35
|
+
DebugLog.configure(
|
36
|
+
:debug => :my_debug,
|
37
|
+
:trace => :my_trace,
|
38
|
+
:time => :my_time,
|
39
|
+
:filename => 'log/xyz.log'
|
40
|
+
)
|
41
|
+
|
42
|
+
my_debug "Message..."
|
43
|
+
my_trace :x, binding
|
44
|
+
my_time('Task') { action }
|
45
|
+
|
46
|
+
{% endhighlight %}
|
47
|
+
|
48
|
+
In either case, the log file will look something like this:
|
49
|
+
|
50
|
+
DebugLog -- 2010-07-25 18:58:22 +1000
|
51
|
+
-------------------------------------
|
52
|
+
[00.3] Message...
|
53
|
+
[00.5] x == 5
|
54
|
+
[00.6] Task: 1.0831 sec
|
55
|
+
|
56
|
+
The `[00.3]` etc. is the number of seconds (rounded) since the program started
|
57
|
+
(well, since `require 'debuglog'`, anyway).
|
58
|
+
|
59
|
+
More nuanced configuration is possible; see [Configuration](#Configuration).
|
60
|
+
|
61
|
+
### Installation
|
62
|
+
|
63
|
+
$ [sudo] gem install debuglog
|
64
|
+
|
65
|
+
Source code is hosted on Github. See [Project details](#project_details).
|
66
|
+
|
67
|
+
|
68
|
+
## Description
|
69
|
+
|
70
|
+
Debuglog allows you easy access to a log file named `debug.log` in the current
|
71
|
+
directory. In that file, you can record:
|
72
|
+
* arbitrary messages with `debug`
|
73
|
+
* the value of variables with `trace`
|
74
|
+
* the time taken to execute some code with `time`
|
75
|
+
|
76
|
+
Of course, any or all of those methods names might be used by another library or
|
77
|
+
by your own code. You can choose different method names and a different
|
78
|
+
filename; see [Configuration](#Configuration). Debuglog will raise an
|
79
|
+
exception (`DebugLog::Error`) if it detects a method clash.
|
80
|
+
|
81
|
+
### `debug`
|
82
|
+
|
83
|
+
The `debug` method is straightforward. It calls `to_s` on its argument(s) and
|
84
|
+
writes them to the log file.
|
85
|
+
|
86
|
+
[col]: http://gsinclair.github.com/col.html
|
87
|
+
|
88
|
+
### `trace`
|
89
|
+
|
90
|
+
`trace` requires you to pass the binding with the `binding` method. The two
|
91
|
+
following lines are equivalent:
|
92
|
+
|
93
|
+
{% highlight ruby %}
|
94
|
+
|
95
|
+
trace :radius, binding
|
96
|
+
debug "radius == #{radius.pretty_inspect}"
|
97
|
+
|
98
|
+
{% endhighlight %}
|
99
|
+
|
100
|
+
> Tip: You may choose to `alias _b binding` for convenience; DebugLog doesn't do that
|
101
|
+
> for you.
|
102
|
+
|
103
|
+
If you want the output truncated, pass an integer argument:
|
104
|
+
|
105
|
+
{% highlight ruby %}
|
106
|
+
|
107
|
+
trace :text, binding, 30
|
108
|
+
|
109
|
+
{% endhighlight %}
|
110
|
+
|
111
|
+
### `time`
|
112
|
+
|
113
|
+
`time` calculates the amount of time taken to execute the code in the block
|
114
|
+
given and records it in the log file.
|
115
|
+
|
116
|
+
{% highlight ruby %}
|
117
|
+
|
118
|
+
time("Process configuration file") { @options = parse_config }
|
119
|
+
|
120
|
+
{% endhighlight %}
|
121
|
+
|
122
|
+
It requires a single string (`#to_s`) argument and a block.
|
123
|
+
|
124
|
+
### Notes
|
125
|
+
|
126
|
+
`Debuglog` is a synonym for `DebugLog`, so you don't have to trouble yourself to
|
127
|
+
remember the capitalisation.
|
128
|
+
|
129
|
+
The text written to the log file has some nice touches:
|
130
|
+
* Multi-line output is indented correctly.
|
131
|
+
* `-------` is inserted each time an extra second of running time has elapsed.
|
132
|
+
This gives you a quick visual indication of how much logfile activity is
|
133
|
+
taking place. If more than one second has elapsed since the last log item,
|
134
|
+
something like `------- (3 sec)` is emitted.
|
135
|
+
|
136
|
+
|
137
|
+
## Configuration
|
138
|
+
|
139
|
+
The [Synopsis](#synopsis) gave a good example of configuration:
|
140
|
+
|
141
|
+
{% highlight ruby %}
|
142
|
+
|
143
|
+
require 'debuglog/manual'
|
144
|
+
|
145
|
+
DebugLog.configure(
|
146
|
+
:debug => :my_debug,
|
147
|
+
:trace => :my_trace,
|
148
|
+
:time => :my_time,
|
149
|
+
:filename => 'log/xyz.log'
|
150
|
+
)
|
151
|
+
|
152
|
+
{% endhighlight %}
|
153
|
+
|
154
|
+
This changes the names of the methods that Debuglog defines. The motivation for
|
155
|
+
that, of course, is another library or your own code defining methods of those
|
156
|
+
names. Debuglog will raise an exception (`DebugLog::Error`) if it detects a
|
157
|
+
method name clash. (Of course, you might load the other library _after_
|
158
|
+
Debuglog, in which case it won't detect the clash.) This precaution is taken
|
159
|
+
because they are common method names at the top-level, and it's just not right
|
160
|
+
for a debugging library to _cause_ bugs.
|
161
|
+
|
162
|
+
If you omit a method name from the configuration, that method will not be
|
163
|
+
defined. The following code defines the method `d` instead of `debug`, but does
|
164
|
+
_not_ define `trace` or `time`. The standard filename `debug.log` is used.
|
165
|
+
|
166
|
+
{% highlight ruby %}
|
167
|
+
|
168
|
+
DebugLog.configure(
|
169
|
+
:debug => :d,
|
170
|
+
)
|
171
|
+
|
172
|
+
{% endhighlight %}
|
173
|
+
|
174
|
+
If you want to change one or two methods but leave the rest as standard, simply
|
175
|
+
do:
|
176
|
+
|
177
|
+
{% highlight ruby %}
|
178
|
+
|
179
|
+
DebugLog.configure(
|
180
|
+
:debug => :d,
|
181
|
+
:trace => :trace,
|
182
|
+
:time => :time
|
183
|
+
)
|
184
|
+
|
185
|
+
{% endhighlight %}
|
186
|
+
|
187
|
+
Once you have called `DebugLog.configure`, any further calls to it will be
|
188
|
+
ignored with a message on STDERR. That includes this case:
|
189
|
+
|
190
|
+
{% highlight ruby %}
|
191
|
+
|
192
|
+
require 'debuglog' # should be 'debuglog/manual'
|
193
|
+
|
194
|
+
DebugLog.configure(...)
|
195
|
+
|
196
|
+
{% endhighlight %}
|
197
|
+
|
198
|
+
The code `require 'debuglog` is equivalent to the following code, meaning
|
199
|
+
your one shot at calling `configure` has been taken.
|
200
|
+
|
201
|
+
{% highlight ruby %}
|
202
|
+
|
203
|
+
require 'debuglog/manual'
|
204
|
+
|
205
|
+
DebugLog.configure(
|
206
|
+
:debug => :debug,
|
207
|
+
:trace => :trace,
|
208
|
+
:time => :time,
|
209
|
+
:file => 'debug.log'
|
210
|
+
)
|
211
|
+
|
212
|
+
{% endhighlight %}
|
213
|
+
|
214
|
+
Final note: if you specify a file that is not writable, an error
|
215
|
+
(`DebugLog::Error`) will be raised.
|
216
|
+
|
217
|
+
|
218
|
+
## Endnotes
|
219
|
+
|
220
|
+
### Motivation
|
221
|
+
|
222
|
+
Debugging to a log file is very useful, even if your program doesn't do
|
223
|
+
"logging" as such. Ages ago I released `dev-utils/debug` which did this and
|
224
|
+
intended to do more. That's outdated now, only working on 1.8, so a revisit was
|
225
|
+
worthwhile with a better name. (If anyone wants the old name for something
|
226
|
+
else, they're welcome to it.)
|
227
|
+
|
228
|
+
### Dependencies and requirements
|
229
|
+
|
230
|
+
Debuglog does not depend on any other libraries and works in Ruby 1.8 and Ruby
|
231
|
+
1.9.
|
232
|
+
|
233
|
+
Unit tests are implemented in [Attest](http://gsinclair.github.com/attest.html).
|
234
|
+
|
235
|
+
### Project details
|
236
|
+
|
237
|
+
* Author: Gavin Sinclair (user name: `gsinclair`; mail server: `gmail.com`)
|
238
|
+
* Date: July 2010
|
239
|
+
* Licence: MIT licence
|
240
|
+
* Project homepage: [http://gsinclair.github.com/debuglog.html][home]
|
241
|
+
* Source code: [http://github.com/gsinclair/debuglog][code]
|
242
|
+
* Documentation: (project homepage)
|
243
|
+
|
244
|
+
[home]: http://gsinclair.github.com/debuglog.html
|
245
|
+
[code]: http://github.com/gsinclair/debuglog
|
246
|
+
|
247
|
+
### Possible future enhancements
|
248
|
+
|
249
|
+
Color. For instance, `debug "!! Object pool overloaded"` could print the
|
250
|
+
message (minus the exclamation marks) in red. Traces could be in yellow. Times
|
251
|
+
could be in dark cyan, etc.
|
252
|
+
|
253
|
+
Further to the above: symbol arguments to `debug` could provide some color
|
254
|
+
using the `Col` library. E.g. `debug "blah...", :yb` for yellow bold.
|
255
|
+
|
256
|
+
Method to turn it off and on: `DebugLog.off` and `DebugLog.on` or something.
|
257
|
+
|
258
|
+
Indenting via `DebugLog.indent` and `DebugLog.outdent`.
|
259
|
+
|
260
|
+
Options for `trace` output: `p` for `:inspect`; `y` for `:to_yaml` etc. I
|
261
|
+
don't see why the current `:pretty_inspect` would ever be insufficient, but of
|
262
|
+
course there may be cases.
|
data/lib/debuglog.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#
|
2
|
+
# Usage:
|
3
|
+
#
|
4
|
+
# require 'debuglog'
|
5
|
+
# debug "..." # -> writes "..." to 'debug.log'
|
6
|
+
#
|
7
|
+
# Note: +debug+ is the _default_ method name. You can set a different one like
|
8
|
+
# this:
|
9
|
+
#
|
10
|
+
# require 'debuglog/manual'
|
11
|
+
# DebugLog.configure(...) # todo: complete
|
12
|
+
# dbg "..." # -> writes "..." to 'debug.log'
|
13
|
+
#
|
14
|
+
# If +debug+ (or the name you choose manually) is the name of an existing
|
15
|
+
# method, it will _not_ be overwritten.
|
16
|
+
|
17
|
+
require 'debuglog/auto'
|
@@ -0,0 +1,177 @@
|
|
1
|
+
#
|
2
|
+
# require 'debuglog/manual'
|
3
|
+
#
|
4
|
+
# DebugLog.configure(:debug => :dbg)
|
5
|
+
# dbg "..." # -> writes "..." to file 'debug.log'
|
6
|
+
#
|
7
|
+
|
8
|
+
class DebugLog
|
9
|
+
|
10
|
+
@@instance = nil
|
11
|
+
|
12
|
+
DEFAULT_CONFIGURATION = {
|
13
|
+
:debug => :debug,
|
14
|
+
:trace => :trace,
|
15
|
+
:time => :time,
|
16
|
+
:file => 'debug.log'
|
17
|
+
}
|
18
|
+
|
19
|
+
class Error < StandardError; end
|
20
|
+
|
21
|
+
def DebugLog.err(string)
|
22
|
+
raise ::DebugLog::Error, "DebugLog error -- #{string}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def DebugLog.configure(hash)
|
26
|
+
if @@instance.nil?
|
27
|
+
@@instance = DebugLog.new(hash)
|
28
|
+
else
|
29
|
+
err("DebugLog already configured") # todo: replace
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def DebugLog.autoconfigure
|
34
|
+
configure(DEFAULT_CONFIGURATION)
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(config)
|
38
|
+
@kernel_methods_defined = []
|
39
|
+
_create_kernel_method(config[:debug], :debug)
|
40
|
+
_create_kernel_method(config[:trace], :trace)
|
41
|
+
_create_kernel_method(config[:time], :time)
|
42
|
+
@filename = config[:filename] || 'debug.log'
|
43
|
+
begin
|
44
|
+
@fh = File.open(@filename, 'w')
|
45
|
+
rescue => e
|
46
|
+
raise DebugLog::Error, "#{e.class} (#{e.message})"
|
47
|
+
end
|
48
|
+
@fh.sync = true
|
49
|
+
@start_time = Time.now
|
50
|
+
header = "DebugLog -- #{@start_time}"
|
51
|
+
@fh.puts header
|
52
|
+
@fh.puts('-' * header.length)
|
53
|
+
end
|
54
|
+
|
55
|
+
def debug(*args)
|
56
|
+
string = args.map { |x| x.to_s }.join
|
57
|
+
_write(string)
|
58
|
+
end
|
59
|
+
|
60
|
+
def trace(expr, _binding, *options)
|
61
|
+
value = eval expr, _binding
|
62
|
+
require 'pp'
|
63
|
+
formatter = :pretty_inspect
|
64
|
+
## if (m = options.find { |o| o.is_a? Symbol })
|
65
|
+
## case m
|
66
|
+
## when :p then :inspect
|
67
|
+
## when :s, :to_s then :to_s
|
68
|
+
## when :pp then (require 'pp'; :pretty_inspect)
|
69
|
+
## when :yaml then (require 'yaml'; :to_yaml)
|
70
|
+
## when :ap then (require 'ap'; :ap)
|
71
|
+
## else then :inspect
|
72
|
+
## end
|
73
|
+
## else
|
74
|
+
## :inspect
|
75
|
+
## end
|
76
|
+
value = value.send(formatter)
|
77
|
+
if (n = options.find { |o| o.is_a? Integer })
|
78
|
+
value = value[0...n] + "..."
|
79
|
+
end
|
80
|
+
message =
|
81
|
+
if value.index("\n")
|
82
|
+
value = value.gsub(/^/, ' ')
|
83
|
+
"#{expr} ==\n#{value}"
|
84
|
+
else
|
85
|
+
"#{expr} == #{value}"
|
86
|
+
end
|
87
|
+
_write(message)
|
88
|
+
end
|
89
|
+
|
90
|
+
def time(task, &block)
|
91
|
+
result = nil
|
92
|
+
message =
|
93
|
+
if block.nil?
|
94
|
+
"*** Debuglog.task: block required (#{caller[0]}) ***"
|
95
|
+
else
|
96
|
+
t = Time.now
|
97
|
+
result = block.call
|
98
|
+
t = sprintf "%.3f", (Time.now - t)
|
99
|
+
"#{task}: #{t} sec"
|
100
|
+
end
|
101
|
+
_write(message)
|
102
|
+
result
|
103
|
+
end
|
104
|
+
|
105
|
+
def _write(message)
|
106
|
+
time = (Time.now - @start_time)
|
107
|
+
if time.to_i != @time.to_i
|
108
|
+
elapsed = time.to_i - @time.to_i
|
109
|
+
if elapsed > 1
|
110
|
+
@fh.puts "------- (#{elapsed} sec)"
|
111
|
+
else
|
112
|
+
@fh.puts "-------"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
@time = time
|
116
|
+
time = sprintf "%04.1f", time.to_f
|
117
|
+
if message.index("\n")
|
118
|
+
lines = message.split("\n")
|
119
|
+
@fh.puts "[#{time}] #{lines.shift}"
|
120
|
+
indent = " " * (time.size+3)
|
121
|
+
lines.each do |line| @fh.puts "#{indent}#{line}" end
|
122
|
+
else
|
123
|
+
text = "[#{time}] #{message}"
|
124
|
+
@fh.puts(text)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def _create_kernel_method(name, target)
|
129
|
+
if name.nil?
|
130
|
+
return
|
131
|
+
elsif Kernel.respond_to? name
|
132
|
+
DebugLog.err "DebugLog: Method clash in Kernel: #{name.inspect}"
|
133
|
+
else
|
134
|
+
Kernel.module_eval %{
|
135
|
+
def #{name}(*args, &block)
|
136
|
+
DebugLog.call_method(:#{target}, *args, &block)
|
137
|
+
end
|
138
|
+
}, __FILE__, __LINE__ - 4
|
139
|
+
@kernel_methods_defined << name
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
DEBUG_METHODS = [:debug, :trace, :time]
|
144
|
+
def DebugLog.call_method(name, *args, &block)
|
145
|
+
if DEBUG_METHODS.include? name
|
146
|
+
if @@instance
|
147
|
+
@@instance.send(name, *args, &block)
|
148
|
+
else
|
149
|
+
err %{
|
150
|
+
~ DebugLog is not configured. You can:
|
151
|
+
~ * require 'debuglog/auto' or call DebugLog.autoconfigure; or
|
152
|
+
~ * call DebugLog.configure(...) to configure it manually
|
153
|
+
}.strip.gsub(/^\s+~ /, '')
|
154
|
+
end
|
155
|
+
else
|
156
|
+
err "illegitimate method called: #{name}"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def terminate # For testing
|
161
|
+
@fh.close unless @fh.closed?
|
162
|
+
@kernel_methods_defined.each do |m|
|
163
|
+
Kernel.send(:remove_method, m)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
private :terminate
|
167
|
+
|
168
|
+
def DebugLog.wipe_slate_clean_for_testing
|
169
|
+
if @@instance
|
170
|
+
@@instance.send(:terminate)
|
171
|
+
@@instance = nil
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
end # class DebugLog
|
176
|
+
|
177
|
+
Debuglog = DebugLog
|
data/test/_setup.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'debuglog'
|
2
|
+
require 'ruby-debug'
|
3
|
+
|
4
|
+
Attest.custom :debuglog, {
|
5
|
+
:description => "Last line of log file",
|
6
|
+
:parameters => [ [:regex, Regexp], [:filename, String] ],
|
7
|
+
:run => proc {
|
8
|
+
file_data = File.read(filename)
|
9
|
+
test('file exists') { N! file_data }
|
10
|
+
test('file has data in it') { file_data.size > 0 }
|
11
|
+
last_line = file_data.split("\n").last
|
12
|
+
test('match') { Mt last_line, regex }
|
13
|
+
}
|
14
|
+
}
|
@@ -0,0 +1,66 @@
|
|
1
|
+
D "Debuglog auto configuration" do
|
2
|
+
D.<< {
|
3
|
+
DebugLog.send :wipe_slate_clean_for_testing
|
4
|
+
DebugLog.autoconfigure
|
5
|
+
}
|
6
|
+
|
7
|
+
D "Debuglog and DebugLog are the same thing" do
|
8
|
+
Id Debuglog, DebugLog
|
9
|
+
end
|
10
|
+
D "Methods are defined in Kernel" do
|
11
|
+
kernel_methods = Kernel.instance_methods
|
12
|
+
T { kernel_methods.include? :debug }
|
13
|
+
T { kernel_methods.include? :trace }
|
14
|
+
T { kernel_methods.include? :time }
|
15
|
+
end
|
16
|
+
D "debug" do
|
17
|
+
debug "abc123"
|
18
|
+
T :debuglog, /abc123/, "debug.log"
|
19
|
+
debug -189
|
20
|
+
T :debuglog, /-189/, "debug.log"
|
21
|
+
D "multiple arguments" do
|
22
|
+
x = 5
|
23
|
+
debug "The value of x is ", x, "!"
|
24
|
+
T :debuglog, /The value of x is 5!/, "debug.log"
|
25
|
+
end
|
26
|
+
D "multi-line text" do
|
27
|
+
text = "I must go down to the seas again\nTo the lonely sea and the sky\nAnd all I want is a tall ship\nAnd a star to steer her by\n -- John Masefield"
|
28
|
+
debug text
|
29
|
+
log_text_lines = File.read("debug.log").split("\n").last(5)
|
30
|
+
# The text in the log file should be indented to look good.
|
31
|
+
Mt log_text_lines.shift, /\[\d\d\.\d\] I must go down to the seas again$/
|
32
|
+
Eq log_text_lines.shift, " To the lonely sea and the sky"
|
33
|
+
Eq log_text_lines.shift, " And all I want is a tall ship"
|
34
|
+
Eq log_text_lines.shift, " And a star to steer her by"
|
35
|
+
Eq log_text_lines.shift, " -- John Masefield"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
D "trace" do
|
39
|
+
D "array" do
|
40
|
+
foo = [1,2,3]
|
41
|
+
trace :foo, binding
|
42
|
+
T :debuglog, /foo == \[1, 2, 3\]/, "debug.log"
|
43
|
+
end
|
44
|
+
D "string" do
|
45
|
+
str = "blah"
|
46
|
+
trace :str, binding
|
47
|
+
T :debuglog, /str == "blah"/, "debug.log"
|
48
|
+
end
|
49
|
+
D "truncate output" do
|
50
|
+
str = "x" * 100
|
51
|
+
trace :str, binding, 30
|
52
|
+
T :debuglog, /str == "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\.\.\./, "debug.log"
|
53
|
+
end
|
54
|
+
D "different formats" do
|
55
|
+
# not really interested in this feature at the moment
|
56
|
+
end
|
57
|
+
end
|
58
|
+
D "time" do
|
59
|
+
time('sum to 10') { 1+2+3+4+5+6+7+8+9+10 }
|
60
|
+
T :debuglog, /sum to 10: .* sec/, "debug.log"
|
61
|
+
D "return value of block is accessible" do
|
62
|
+
sum = time('sum') { 1 + 1 }
|
63
|
+
Eq sum, 2
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
D "DebugLog manual configuration (successful)" do
|
2
|
+
D.< { DebugLog.send :wipe_slate_clean_for_testing }
|
3
|
+
|
4
|
+
D ":debug => :my_debug, :filename => 'xyz.txt'" do
|
5
|
+
Debuglog.configure(:debug => :my_debug, :filename => 'xyz.txt')
|
6
|
+
D "my_debug method works" do
|
7
|
+
my_debug "abc123"
|
8
|
+
T :debuglog, /abc123/, "xyz.txt"
|
9
|
+
end
|
10
|
+
D "debug, trace and time methods are not defined" do
|
11
|
+
E(NoMethodError) { debug "abc123" }
|
12
|
+
E(NoMethodError) { trace :x, binding }
|
13
|
+
E(NoMethodError) { time('task') { :foo } }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
D ":trace => :my_trace, :time => :my_time" do
|
18
|
+
Debuglog.configure(:trace => :my_trace, :time => :my_time)
|
19
|
+
D "my_trace and my_time methods work" do
|
20
|
+
foo = :chorus
|
21
|
+
my_trace "foo", binding
|
22
|
+
xT :debuglog, /foo == :chorus/, "debug.log"
|
23
|
+
my_time('blah') { :dotdotdot }
|
24
|
+
T :debuglog, /blah: .* sec/, "debug.log"
|
25
|
+
end
|
26
|
+
D "debug method not defined" do
|
27
|
+
E(NoMethodError) { debug "..." }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
D "empty configuration" do
|
32
|
+
Debuglog.configure({})
|
33
|
+
D "debug, trace and time methods don't work" do
|
34
|
+
E(NoMethodError) { debug "abc123" }
|
35
|
+
E(NoMethodError) { trace :x, binding }
|
36
|
+
E(NoMethodError) { time('task') { :foo } }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
D "DebugLog manual configuration (unsuccessful)" do
|
2
|
+
D.< { DebugLog.send :wipe_slate_clean_for_testing }
|
3
|
+
|
4
|
+
D "clash with existing :debug method" do
|
5
|
+
def Kernel.debug() :foo end
|
6
|
+
E(DebugLog::Error) { DebugLog.autoconfigure }
|
7
|
+
end
|
8
|
+
|
9
|
+
D "clash with custom methods" do
|
10
|
+
def Kernel.my_debug() :foo end
|
11
|
+
def Kernel.my_trace() :foo end
|
12
|
+
def Kernel.my_time() :foo end
|
13
|
+
E(DebugLog::Error) { DebugLog.configure(:debug => :my_debug) }
|
14
|
+
E(DebugLog::Error) { DebugLog.configure(:trace => :my_trace) }
|
15
|
+
E(DebugLog::Error) { DebugLog.configure(:time => :my_time) }
|
16
|
+
end
|
17
|
+
|
18
|
+
D "calling methods without having configured -> error" do
|
19
|
+
# At this point DebugLog is not configured.
|
20
|
+
E(NameError) { debug }
|
21
|
+
E(DebugLog::Error) { DebugLog.call_method(:debug, "...") }
|
22
|
+
end
|
23
|
+
|
24
|
+
D "specifying unwritable log file -> error" do
|
25
|
+
E(DebugLog::Error) { DebugLog.configure(:filename => '/fodsfw/fgsg/e/debug.log') }
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: debuglog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Gavin Sinclair
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-12 00:00:00 +11:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: " require 'debuglog' and record debugging information (including variable traces\n and timing information) to the file debug.log -- cheap and easy.\n"
|
22
|
+
email: gsinclair@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/debuglog/auto.rb
|
31
|
+
- lib/debuglog/manual.rb
|
32
|
+
- lib/debuglog.rb
|
33
|
+
- README
|
34
|
+
- test/_setup.rb
|
35
|
+
- test/debuglog-auto.rb
|
36
|
+
- test/debuglog-manual-1.rb
|
37
|
+
- test/debuglog-manual-2.rb
|
38
|
+
- doc/debuglog.markdown
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://gsinclair.github.com/debuglog.html
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Zero-conf debug.log file with 'debug', 'trace' and 'time' methods
|
71
|
+
test_files:
|
72
|
+
- test/_setup.rb
|
73
|
+
- test/debuglog-auto.rb
|
74
|
+
- test/debuglog-manual-1.rb
|
75
|
+
- test/debuglog-manual-2.rb
|