Class: Ol
Overview
Meant to log short succinct messages to help with troubleshooting while coding. Log statements hyperlink back to the line that logged it.
Constant Summary collapse
- @@last =
[Time.now - 1000]
- @@timed_last =
Time.now
Class Method Summary collapse
- .<<(txt) ⇒ Object
- .browser(html) ⇒ Object
- .camel_case(s) ⇒ Object
- .extract_label(h) ⇒ Object
- .file_path ⇒ Object
- .limit_stack(stack, pattern = /^\/projects\//) ⇒ Object
- .line(txt = nil, l = nil, indent = "", name = nil, time = nil) ⇒ Object
- .log(txt, l = nil, name = nil, time = nil) ⇒ Object
-
.log_directly(txt, line, name = nil) ⇒ Object
For when the caller constructs what to log on its own.
- .open_last_output ⇒ Object
- .parse_line(path) ⇒ Object
- .pause_since_last?(time = nil, no_reset = nil) ⇒ Boolean
-
.remove_redundance(stack, last_stack) ⇒ Object
Remove parents from stack that are common to last_stack.
-
.stack(n = 6, nth = 1) ⇒ Object
Logs short succinct stack trace.
- .time(nth = 1) ⇒ Object
- .write_to_file(path, txt) ⇒ Object
- .write_to_file_lines(path, txt) ⇒ Object
Class Method Details
.<<(txt) ⇒ Object
89 90 91 |
# File 'lib/xiki/ol.rb', line 89 def self.<< txt self.line txt, caller(0)[1] end |
.browser(html) ⇒ Object
178 179 180 181 182 183 184 |
# File 'lib/xiki/ol.rb', line 178 def self.browser html path = "/tmp/browser.#{Time.now.usec}.html" url = "file://#{path}" File.open(path, "w") { |f| f << html } `open '#{url}'` end |
.camel_case(s) ⇒ Object
137 138 139 |
# File 'lib/xiki/ol.rb', line 137 def self.camel_case s s.gsub(/_([a-z]+)/) {"#{$1.capitalize}"}.sub(/(.)/) {$1.upcase}.gsub("_", "") end |
.extract_label(h) ⇒ Object
119 120 121 |
# File 'lib/xiki/ol.rb', line 119 def self.extract_label h "- #{h[:clazz]}.#{h[:method]} #{h[:line]})" end |
.file_path ⇒ Object
133 134 135 |
# File 'lib/xiki/ol.rb', line 133 def self.file_path "/tmp/output_ol.notes" end |
.limit_stack(stack, pattern = /^\/projects\//) ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'lib/xiki/ol.rb', line 152 def self.limit_stack stack, pattern=/^\/projects\// # Cut off until it doesn't match first = stack.first stack.delete_if{|o| o !~ pattern} stack.reverse! # Be sure to leave one, if they're all deleted stack << first if stack == [] stack end |
.line(txt = nil, l = nil, indent = "", name = nil, time = nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/xiki/ol.rb', line 101 def self.line txt=nil, l=nil, indent="", name=nil, time=nil l ||= caller(0)[1] l.sub! /^\(eval\)/, 'eval' # So "(" doesn't mess up the label h = self.parse_line(l) txt = txt ? " #{txt}" : '' if h[:clazz] self.log "#{indent}#{self.extract_label(h)}#{txt}", l, name, time else display = l.sub(/_html_haml'$/, '') display.sub!(/.+(.{18})/, "\\1...") self.log "#{indent}- #{display})#{txt}", l, name, time end end |
.log(txt, l = nil, name = nil, time = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/xiki/ol.rb', line 14 def self.log txt, l=nil, name=nil, time=nil path = name ? "/tmp/#{name}_ol.notes" : self.file_path if l.nil? # If just txt, delegate to line self return self.line(txt, caller(0)[1]) end # If n seconds passed since last call heading = self.pause_since_last?(time) ? "\n>\n" : nil if l.is_a?(Array) # If an array of lines was passed result = "" result_lines = "" if heading result << heading result_lines << "\n\n" end l.each_with_index do |o, i| next unless o h = Ol.parse_line(o) result << "#{' '*i}#{self.extract_label(h)}#{i+1 == l.size ? " #{txt}" : ''}\n" result_lines << "#{h[:path]}:#{h[:line]}\n" end self.write_to_file path, result self.write_to_file_lines path, result_lines return txt end # Indent lines if multi-line (except for first) txt.gsub!("\n", "\n ") txt.sub!(/ +\z/, '') # Remove trailing h = Ol.parse_line(l) self.write_to_file path, "#{heading}#{txt}\n" # Multiline txt: Write path to .line file once for each number of lines l = "#{h[:path]}:#{h[:line]}\n" result = "" result << "\n\n" if heading txt.split("\n", -1).size.times { result << l } self.write_to_file_lines "#{path}", result txt end |
.log_directly(txt, line, name = nil) ⇒ Object
For when the caller constructs what to log on its own
8 9 10 11 12 |
# File 'lib/xiki/ol.rb', line 8 def self.log_directly txt, line, name=nil path = name ? "/tmp/#{name}_ol.notes" : self.file_path self.write_to_file path, txt self.write_to_file_lines path, line end |
.open_last_output ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/xiki/ol.rb', line 186 def self.open_last_output prefix = Keys.prefix :clear=>1 View.layout_output if prefix == :u View.to_highest Search.forward "^-" else View.to_bottom Line.previous end Launcher.launch end |
.parse_line(path) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/xiki/ol.rb', line 124 def self.parse_line path method = path[/`(.+)'/, 1] # ` path, l = path.match(/(.+):(\d+)/)[1..2] path = File. path clazz = path[/.+\/(.+)\.rb/, 1] clazz = self.camel_case(clazz) if clazz {:path=>path, :line=>l, :method=>method, :clazz=>clazz} end |
.pause_since_last?(time = nil, no_reset = nil) ⇒ Boolean
81 82 83 84 85 86 87 |
# File 'lib/xiki/ol.rb', line 81 def self.pause_since_last? time=nil, no_reset=nil time ||= @@last difference = Time.now - time[0] time[0] = Time.now unless no_reset difference > 5 end |
.remove_redundance(stack, last_stack) ⇒ Object
Remove parents from stack that are common to last_stack
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/xiki/ol.rb', line 163 def self.remove_redundance stack, last_stack result = [] # For each stack, copy it over if different, or nil if the same stack.each_with_index do |o, i| # If it's the last one, don't nil it out if i+1 == stack.size result << o next end result << (o == last_stack[i] ? nil : o) end result end |
.stack(n = 6, nth = 1) ⇒ Object
Logs short succinct stack trace
142 143 144 145 146 147 148 149 150 |
# File 'lib/xiki/ol.rb', line 142 def self.stack n=6, nth=1 ls ||= caller(0)[nth..(n+nth)] self.line "stack...", ls.shift, "" ls.each do |l| self.line nil, l, " " end end |
.time(nth = 1) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/xiki/ol.rb', line 93 def self.time nth=1 now = Time.now elapsed = self.pause_since_last? ? nil : (now - @@timed_last) self.line "#{elapsed ? "(#{elapsed}) " : ''}#{now.strftime('%I:%M:%S').sub(/^0/, '')}:#{now.usec.to_s.rjust(6, '0')}", caller(0)[nth] @@timed_last = now end |
.write_to_file(path, txt) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/xiki/ol.rb', line 62 def self.write_to_file path, txt existed = File.exists? path # If file doesn't exist, chmod it to world writable later File.open(path, "a") do |f| f << txt f.chmod 0666 if ! existed end end |
.write_to_file_lines(path, txt) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/xiki/ol.rb', line 71 def self.write_to_file_lines path, txt path = "#{path}.lines" existed = File.exists? path # If file doesn't exist, chmod it to world writable later File.open(path, "a", 0666) do |f| f << txt f.chmod 0666 if ! existed end end |