Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #7 from fabiokung/master

first attempt at avoiding race conditions
  • Loading branch information...
commit bdd22b7eceb34559a115fb1f93ea408491426504 2 parents a48f818 + 89e8f4b
Curt Micol asenchi authored

Showing 2 changed files with 27 additions and 22 deletions. Show diff stats Hide diff stats

  1. +26 22 lib/scrolls.rb
  2. +1 0  scrolls.gemspec
48 lib/scrolls.rb
... ... @@ -1,4 +1,5 @@
1 1 require "thread"
  2 +require "atomic"
2 3
3 4 require "scrolls/version"
4 5
@@ -13,8 +14,12 @@ def log_exception(data, e)
13 14 Log.log_exception(data, e)
14 15 end
15 16
  17 + def global_context(data)
  18 + Log.global_context = data
  19 + end
  20 +
16 21 def context(data, &blk)
17   - Log.set_context(data, &blk)
  22 + Log.with_context(data, &blk)
18 23 end
19 24
20 25 module Log
@@ -34,7 +39,15 @@ module Log
34 39 "debug" => 7
35 40 }
36 41
37   - attr_accessor :stream, :context
  42 + attr_accessor :stream
  43 +
  44 + def context
  45 + Thread.current[:scrolls_context] ||= {}
  46 + end
  47 +
  48 + def context=(hash)
  49 + Thread.current[:scrolls_context] = hash
  50 + end
38 51
39 52 def start(out = nil)
40 53 # This allows log_exceptions below to pick up the defined output,
@@ -42,6 +55,7 @@ def start(out = nil)
42 55 @defined = out.nil? ? false : true
43 56
44 57 sync_stream(out)
  58 + @global_context = Atomic.new({})
45 59 end
46 60
47 61 def sync_stream(out = nil)
@@ -87,11 +101,8 @@ def unparse(data)
87 101 end
88 102
89 103 def log(data, &blk)
90   - if @context
91   - logdata = @context.merge(data)
92   - else
93   - logdata = data
94   - end
  104 + merged_context = @global_context.value.merge(context)
  105 + logdata = merged_context.merge(data)
95 106
96 107 unless blk
97 108 write(logdata)
@@ -145,23 +156,16 @@ def log_level_ok?(level)
145 156 end
146 157 end
147 158
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
  159 + def with_context(prefix)
  160 + return unless block_given?
  161 + old_context = context
  162 + self.context = old_context.merge(prefix)
  163 + yield if block_given?
  164 + self.context = old_context
160 165 end
161 166
162   - def clear_context
163   - @stash = []
164   - @context = {}
  167 + def global_context=(data)
  168 + @global_context.update { |_| data }
165 169 end
166 170
167 171 end
1  scrolls.gemspec
@@ -13,4 +13,5 @@ Gem::Specification.new do |gem|
13 13 gem.name = "scrolls"
14 14 gem.require_paths = ["lib"]
15 15 gem.version = Scrolls::VERSION
  16 + gem.add_dependency("atomic", "~> 1.0.0")
16 17 end

0 comments on commit bdd22b7

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