l2meter 0.4.1 → 0.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1cb1df98a25ea5a5da6e78bc8b074601d36bd00a
4
- data.tar.gz: a720220af04ea3d3a2d12c2090072e25c002e58c
3
+ metadata.gz: e9da3c4d1d2be86ce7bd40b98d85467eea70490e
4
+ data.tar.gz: 2e8a3deb41608f27ae8ddc67cbeda41938cdb454
5
5
  SHA512:
6
- metadata.gz: bbcf29e153ecb87f62e9fb090863c4a57b96b0c8a8ab73685e3acd16052648a2163510a62ff6e5df1d2d2abeb79c1484ada7eb250eb77858054798d349fdca3b
7
- data.tar.gz: cecb45d744a0818bdf16f62f74edb4c48ec724a40083d475c6ccc16e0559419874aab0e77d166befdb61cd4323df2d71d081e2e704554367b6c60abbeda6f603
6
+ metadata.gz: 32ee41b3d8f5cd1c13583bea3fa05204ccef57aadcdfdcc1703a2a740ed7be288dec294fc6f6a0de8555e8b0329d79ed28a786cd28c3e5126f67fe4aecad08df
7
+ data.tar.gz: 496b78a386a8eb6968323b319db4e766b6013a8362916e609f6c1da6886fc9c90cfef5ddc510154ad81f51875e943d4573fea0de3409b4cee6350b9e7caa1449
@@ -4,6 +4,8 @@ module L2meter
4
4
 
5
5
  def initialize(configuration: Configuration.new)
6
6
  @configuration = configuration
7
+ @buffer = {}
8
+ @autoflush = true
7
9
  @start_times = []
8
10
  @contexts = []
9
11
  @outputs = []
@@ -63,7 +65,7 @@ module L2meter
63
65
  push_context context_data
64
66
  yield
65
67
  ensure
66
- context_data.length.times { @contexts.pop }
68
+ context_data.length.times { @contexts.pop } if block_given?
67
69
  end
68
70
 
69
71
  def clone
@@ -71,10 +73,11 @@ module L2meter
71
73
  end
72
74
 
73
75
  def batch
74
- @outputs.push StringIO.new
76
+ @autoflush = false
75
77
  yield
76
78
  ensure
77
- flush_buffer @outputs.pop
79
+ @autoflush = true
80
+ flush_buffer
78
81
  end
79
82
 
80
83
  protected
@@ -93,8 +96,11 @@ module L2meter
93
96
 
94
97
  def unwrap(*args)
95
98
  params = Hash === args.last ? args.pop : {}
96
- args = args.compact.map { |key| [key, true] }.to_h
97
- args.merge(params)
99
+ args.compact.map { |key| [key, true] }.to_h.merge(params)
100
+ end
101
+
102
+ def stringify_keys(hash)
103
+ hash.each_with_object({}) { |(k, v), a| a[k.to_s] = v }
98
104
  end
99
105
 
100
106
  def merge_contexts(params)
@@ -129,22 +135,9 @@ module L2meter
129
135
  configuration.key_formatter.call(key)
130
136
  end
131
137
 
132
- def format_keys(params)
133
- params.inject({}) do |normalized, (key, value)|
134
- normalized.tap { |n| n[format_key(key)] = value }
135
- end
136
- end
137
-
138
138
  def write(params)
139
- tokens = format_keys(params).map do |key, value|
140
- value == true ? key : [key, format_value(value)] * ?=
141
- end
142
-
143
- emit configuration.sort?? tokens.sort : tokens
144
- end
145
-
146
- def emit(tokens)
147
- output_queue.last.puts [*tokens].join(" ")
139
+ @buffer.merge! stringify_keys(params)
140
+ flush_buffer if @autoflush
148
141
  end
149
142
 
150
143
  def log_with_prefix(method, key, value, unit: nil)
@@ -185,11 +178,17 @@ module L2meter
185
178
  [configuration.output, *@outputs].compact
186
179
  end
187
180
 
188
- def flush_buffer(buffer)
189
- output = buffer.tap(&:rewind).read
190
- tokens = output.split(/\s+/).reverse
191
- tokens.uniq! { |token| token.split(?=, 2).first }
192
- emit tokens.reverse
181
+ def flush_buffer
182
+ tokens = @buffer.map do |key, value|
183
+ key = format_key(key)
184
+ value == true ? key : "#{key}=#{format_value(value)}"
185
+ end
186
+
187
+ tokens.sort! if configuration.sort?
188
+
189
+ output_queue.last.puts [*tokens].join(" ")
190
+ ensure
191
+ @buffer.clear
193
192
  end
194
193
  end
195
194
  end
@@ -1,3 +1,3 @@
1
1
  module L2meter
2
- VERSION = "0.4.1".freeze
2
+ VERSION = "0.5.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: l2meter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pravosud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-06 00:00:00.000000000 Z
11
+ date: 2016-04-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: