Module: Press::Printer
- Defined in:
- lib/press/printer.rb
Class Method Summary collapse
-
.cpd(*data) ⇒ Object
count print data.
-
.cpdfm(file, m, *data) ⇒ Object
count print data file method.
-
.ctx=(data) ⇒ Object
context.
- .cwrite(file, tag, data) ⇒ Object
- .errorify(e) ⇒ Object
- .hashify(*data, initial) ⇒ Object
-
.mpd(*data, &blk) ⇒ Object
measure print data.
-
.mpde(e, *data) ⇒ Object
measure print data exception.
-
.mpdfm(file, m, *data, &blk) ⇒ Object
measure print data file method.
-
.mpdfme(file, m, e, *data) ⇒ Object
measure print data file method exception.
-
.mtx=(tag) ⇒ Object
measure tag.
- .mutex ⇒ Object
- .mwrite(file, tag, data, &blk) ⇒ Object
-
.pd(*data, &blk) ⇒ Object
print data.
-
.pde(e, *data) ⇒ Object
print data exception.
-
.pdfm(file, m, *data, &blk) ⇒ Object
print data file method.
-
.pdfme(file, m, e, *data) ⇒ Object
print data file method exception.
-
.spd(*data) ⇒ Object
sample print data.
-
.spdfm(file, m, *data) ⇒ Object
sample print data file method.
- .stringify(data) ⇒ Object
- .swrite(file, tag, data) ⇒ Object
- .write(file, data, &blk) ⇒ Object
Class Method Details
.cpd(*data) ⇒ Object
count print data
32 33 34 |
# File 'lib/press/printer.rb', line 32 def self.cpd(*data) cwrite($stdout, @mtx, hashify(*data, {})) end |
.cpdfm(file, m, *data) ⇒ Object
count print data file method
52 53 54 |
# File 'lib/press/printer.rb', line 52 def self.cpdfm(file, m, *data) cwrite($stdout, [@mtx, File.basename(file, ".rb"), m].compact.join("."), hashify(*data, :file => File.basename(file, ".rb"), :fn => m)) end |
.ctx=(data) ⇒ Object
context
7 8 9 |
# File 'lib/press/printer.rb', line 7 def self.ctx=(data) @ctx = data end |
.cwrite(file, tag, data) ⇒ Object
141 142 143 |
# File 'lib/press/printer.rb', line 141 def self.cwrite(file, tag, data) write(file, data.tap { |d| d["count##{[tag, d[:event]].compact.join(".")}"] = d[:val] || 1 if tag }) end |
.errorify(e) ⇒ Object
76 77 78 |
# File 'lib/press/printer.rb', line 76 def self.errorify(e) { :at => "error", :class => e.class, :message => e..lines.to_a.first, :trace => e.backtrace.map { |i| i.match(/(#{Gem.dir}|#{Dir.getwd})?\/(.*):in (.*)/) && $2 }[0..5].compact.inspect} end |
.hashify(*data, initial) ⇒ Object
80 81 82 |
# File 'lib/press/printer.rb', line 80 def self.hashify(*data, initial) data.compact.reduce(initial.merge(@ctx || {})) { |d, v| d.merge v } end |
.mpd(*data, &blk) ⇒ Object
measure print data
22 23 24 |
# File 'lib/press/printer.rb', line 22 def self.mpd(*data, &blk) mwrite($stdout, @mtx, hashify(*data, {}), &blk) end |
.mpde(e, *data) ⇒ Object
measure print data exception
62 63 64 |
# File 'lib/press/printer.rb', line 62 def self.mpde(e, *data) mwrite($stderr, [@mtx, "error"].compact.join("."), hashify(*data, errorify(e))) end |
.mpdfm(file, m, *data, &blk) ⇒ Object
measure print data file method
42 43 44 |
# File 'lib/press/printer.rb', line 42 def self.mpdfm(file, m, *data, &blk) mwrite($stdout, [@mtx, File.basename(file, ".rb"), m].compact.join("."), hashify(*data, :file => File.basename(file, ".rb"), :fn => m), &blk) end |
.mpdfme(file, m, e, *data) ⇒ Object
measure print data file method exception
72 73 74 |
# File 'lib/press/printer.rb', line 72 def self.mpdfme(file, m, e, *data) mwrite($stderr, [@mtx, "error"].compact.join("."), hashify(*data, errorify(e).merge(:file => File.basename(file, ".rb"), :fn => m))) end |
.mtx=(tag) ⇒ Object
measure tag
12 13 14 |
# File 'lib/press/printer.rb', line 12 def self.mtx=(tag) @mtx = tag end |
.mutex ⇒ Object
104 105 106 |
# File 'lib/press/printer.rb', line 104 def self.mutex @mutex ||= Mutex.new end |
.mwrite(file, tag, data, &blk) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/press/printer.rb', line 124 def self.mwrite(file, tag, data, &blk) unless blk write(file, data.tap { |d| d["measure##{[tag, d[:event]].compact.join(".")}"] = d[:val] || 1 if tag }) else start = Time.now write(file, { :at => "start" }.merge(data)) if ENV.key?("PRESS_DEBUG") yield.tap do elapsed = Time.now - start; mwrite(file, tag, { :at => "finish", :elapsed => elapsed }.merge(data).tap { |d| d[:val] = elapsed if tag }) end end end |
.pd(*data, &blk) ⇒ Object
print data
17 18 19 |
# File 'lib/press/printer.rb', line 17 def self.pd(*data, &blk) write($stdout, hashify(*data, {}), &blk) end |
.pde(e, *data) ⇒ Object
print data exception
57 58 59 |
# File 'lib/press/printer.rb', line 57 def self.pde(e, *data) write($stderr, hashify(*data, errorify(e))) end |
.pdfm(file, m, *data, &blk) ⇒ Object
print data file method
37 38 39 |
# File 'lib/press/printer.rb', line 37 def self.pdfm(file, m, *data, &blk) write($stdout, hashify(*data, :file => File.basename(file, ".rb"), :fn => m), &blk) end |
.pdfme(file, m, e, *data) ⇒ Object
print data file method exception
67 68 69 |
# File 'lib/press/printer.rb', line 67 def self.pdfme(file, m, e, *data) write($stderr, hashify(*data, errorify(e).merge(:file => File.basename(file, ".rb"), :fn => m))) end |
.spd(*data) ⇒ Object
sample print data
27 28 29 |
# File 'lib/press/printer.rb', line 27 def self.spd(*data) swrite($stdout, @mtx, hashify(*data, {})) end |
.spdfm(file, m, *data) ⇒ Object
sample print data file method
47 48 49 |
# File 'lib/press/printer.rb', line 47 def self.spdfm(file, m, *data) swrite($stdout, [@mtx, File.basename(file, ".rb"), m].compact.join("."), hashify(*data, :file => File.basename(file, ".rb"), :fn => m)) end |
.stringify(data) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/press/printer.rb', line 84 def self.stringify(data) data.map do |(k, v)| case v when Hash "#{k}={.." when Array "#{k}=[.." when NilClass "#{k}=nil" when Float "#{k}=#{format("%.3f", v)}" when Time "#{k}=#{v.iso8601}" else v_str = v.to_s.strip v_str.match(/\s/) ? "#{k}=\'#{v_str}\'" : "#{k}=#{v_str}" end end.join(" ") end |
.swrite(file, tag, data) ⇒ Object
137 138 139 |
# File 'lib/press/printer.rb', line 137 def self.swrite(file, tag, data) write(file, data.tap { |d| d["sample##{[tag, d[:event]].compact.join(".")}"] = d[:val] if tag }) end |
.write(file, data, &blk) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/press/printer.rb', line 108 def self.write(file, data, &blk) unless blk mutex.synchronize do file.puts stringify(data) file.flush end nil else start = Time.now write(file, { :at => "start" }.merge(data)) if ENV.key?("PRESS_DEBUG") yield.tap do write(file, { :at => "finish", :elapsed => Time.now - start }.merge(data)) end end end |