scrolls 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+
4
+ require "rake/testtask"
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "test/test_*.rb"
7
+ end
8
+
9
+ task :default => :test
@@ -1,3 +1,3 @@
1
1
  module Scrolls
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
data/lib/scrolls.rb CHANGED
@@ -13,6 +13,10 @@ module Scrolls
13
13
  Log.log_exception(data, e)
14
14
  end
15
15
 
16
+ def context(data, &blk)
17
+ Log.set_context(data, &blk)
18
+ end
19
+
16
20
  module Log
17
21
  extend self
18
22
 
@@ -30,12 +34,13 @@ module Scrolls
30
34
  "debug" => 7
31
35
  }
32
36
 
33
- attr_accessor :stream
37
+ attr_accessor :stream, :context
34
38
 
35
39
  def start(out = nil)
36
40
  # This allows log_exceptions below to pick up the defined output,
37
41
  # otherwise stream out to STDERR
38
42
  @defined = out.nil? ? false : true
43
+
39
44
  sync_stream(out)
40
45
  end
41
46
 
@@ -53,7 +58,11 @@ module Scrolls
53
58
  if log_level_ok?(data[:level])
54
59
  msg = unparse(data)
55
60
  mtx.synchronize do
56
- @stream.puts(msg)
61
+ begin
62
+ @stream.puts(msg)
63
+ rescue NoMethodError => e
64
+ puts "You need to start your logger, `Scrolls::Log.start`"
65
+ end
57
66
  end
58
67
  end
59
68
  end
@@ -78,16 +87,22 @@ module Scrolls
78
87
  end
79
88
 
80
89
  def log(data, &blk)
90
+ if @context
91
+ logdata = @context.merge(data)
92
+ else
93
+ logdata = data
94
+ end
95
+
81
96
  unless blk
82
- write(data)
97
+ write(logdata)
83
98
  else
84
99
  start = Time.now
85
100
  res = nil
86
- log(data.merge(:at => :start))
101
+ log(logdata.merge(:at => :start))
87
102
  begin
88
103
  res = yield
89
104
  rescue StandardError, Timeout::Error => e
90
- log(data.merge(
105
+ log(logdata.merge(
91
106
  :at => :exception,
92
107
  :reraise => true,
93
108
  :class => e.class,
@@ -97,7 +112,7 @@ module Scrolls
97
112
  ))
98
113
  raise(e)
99
114
  end
100
- log(data.merge(:at => :finish, :elapsed => Time.now - start))
115
+ log(logdata.merge(:at => :finish, :elapsed => Time.now - start))
101
116
  res
102
117
  end
103
118
  end
@@ -130,5 +145,24 @@ module Scrolls
130
145
  end
131
146
  end
132
147
 
148
+ def set_context(prefix, &blk)
149
+ # Initialize an empty context if the variable doesn't exist
150
+ @context = {} unless @context
151
+ @stash = [] unless @stash
152
+ @stash << @context
153
+ # Why isn't this merging
154
+ @context = @context.merge(prefix)
155
+
156
+ if blk
157
+ yield
158
+ @context = @stash.pop
159
+ end
160
+ end
161
+
162
+ def clear_context
163
+ @stash = []
164
+ @context = {}
165
+ end
166
+
133
167
  end
134
168
  end
@@ -0,0 +1,31 @@
1
+ require "stringio"
2
+ require "minitest/autorun"
3
+
4
+ $: << "../lib"
5
+ require "scrolls"
6
+
7
+ class TestScrollsParser < MiniTest::Unit::TestCase
8
+ def test_unparse_tags
9
+ data = {:test => true, :tag => true}
10
+ assert "test tag" == Scrolls::Log.unparse(data)
11
+ end
12
+
13
+ def test_unparse_strings
14
+ data = {:test => "strings"}
15
+ assert "test=strings" == Scrolls::Log.unparse(data)
16
+
17
+ data = {:s => "echo 'hello' \"world\""}
18
+ assert 's="echo \'hello\' ..."' == Scrolls::Log.unparse(data)
19
+
20
+ data = {:s => "hello world"}
21
+ assert 's="hello world"' == Scrolls::Log.unparse(data)
22
+
23
+ data = {:s => "hello world\\"}
24
+ assert 's="hello world\"' == Scrolls::Log.unparse(data)
25
+ end
26
+
27
+ def test_unparse_floats
28
+ data = {:test => 0.3}
29
+ assert "test=0.300" == Scrolls::Log.unparse(data)
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrolls
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-10 00:00:00.000000000 Z
12
+ date: 2012-04-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Logging, easier, more consistent.
15
15
  email:
@@ -26,6 +26,7 @@ files:
26
26
  - lib/scrolls.rb
27
27
  - lib/scrolls/version.rb
28
28
  - scrolls.gemspec
29
+ - test/test_scrolls.rb
29
30
  homepage: https://github.com/asenchi/scrolls
30
31
  licenses: []
31
32
  post_install_message:
@@ -50,4 +51,5 @@ rubygems_version: 1.8.11
50
51
  signing_key:
51
52
  specification_version: 3
52
53
  summary: When do we log? All the time.
53
- test_files: []
54
+ test_files:
55
+ - test/test_scrolls.rb