Module: HelperClasses::DPuts
- Extended by:
- DPuts
- Defined in:
- lib/helperclasses/dputs.rb
Instance Attribute Summary collapse
-
#log_file ⇒ Object
Returns the value of attribute log_file.
-
#mutex ⇒ Object
Returns the value of attribute mutex.
-
#show_time ⇒ Object
Returns the value of attribute show_time.
-
#silent ⇒ Object
Returns the value of attribute silent.
-
#terminal_width ⇒ Object
Returns the value of attribute terminal_width.
Instance Method Summary collapse
- #ddputs(n, &s) ⇒ Object
- #dlog_msg(mod, msg) ⇒ Object
- #dp(s) ⇒ Object
- #dputs(n, &s) ⇒ Object
- #dputs_func ⇒ Object
- #dputs_getcaller ⇒ Object
- #dputs_out(n, s, call) ⇒ Object
- #dputs_unfunc ⇒ Object
- #log_msg(mod, msg) ⇒ Object
Instance Attribute Details
#log_file ⇒ Object
Returns the value of attribute log_file.
7 8 9 |
# File 'lib/helperclasses/dputs.rb', line 7 def log_file @log_file end |
#mutex ⇒ Object
Returns the value of attribute mutex.
7 8 9 |
# File 'lib/helperclasses/dputs.rb', line 7 def mutex @mutex end |
#show_time ⇒ Object
Returns the value of attribute show_time.
7 8 9 |
# File 'lib/helperclasses/dputs.rb', line 7 def show_time @show_time end |
#silent ⇒ Object
Returns the value of attribute silent.
7 8 9 |
# File 'lib/helperclasses/dputs.rb', line 7 def silent @silent end |
#terminal_width ⇒ Object
Returns the value of attribute terminal_width.
7 8 9 |
# File 'lib/helperclasses/dputs.rb', line 7 def terminal_width @terminal_width end |
Instance Method Details
#ddputs(n, &s) ⇒ Object
87 88 89 90 91 |
# File 'lib/helperclasses/dputs.rb', line 87 def ddputs(n, &s) s = yield s #dp caller(0) dputs_out(-n, s, caller(0)[1]) end |
#dlog_msg(mod, msg) ⇒ Object
107 108 109 |
# File 'lib/helperclasses/dputs.rb', line 107 def dlog_msg(mod, msg) ddputs(1){"Info from #{mod}: #{msg}"} end |
#dp(s) ⇒ Object
93 94 95 96 |
# File 'lib/helperclasses/dputs.rb', line 93 def dp(s) dputs_out(0, s.class == String ? s : s.inspect, caller(0)[1]) s end |
#dputs(n, &s) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/helperclasses/dputs.rb', line 78 def dputs(n, &s) n *= -1 if ($DPUTS_FUNCS and $DPUTS_FUNCS.index(dputs_getcaller)) if !self.class.const_defined?(:DEBUG_LVL) or self.class.const_get(:DEBUG_LVL) >= n s = yield s dputs_out(n, s, caller(0)[1]) end end |
#dputs_func ⇒ Object
68 69 70 71 |
# File 'lib/helperclasses/dputs.rb', line 68 def dputs_func $DPUTS_FUNCS ||= [] $DPUTS_FUNCS.push(dputs_getcaller) unless $DPUTS_FUNCS.index(dputs_getcaller) end |
#dputs_getcaller ⇒ Object
64 65 66 |
# File 'lib/helperclasses/dputs.rb', line 64 def dputs_getcaller caller(0)[2].sub(/:.*:in/, '').sub(/block .*in /, '') end |
#dputs_out(n, s, call) ⇒ Object
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 61 |
# File 'lib/helperclasses/dputs.rb', line 15 def dputs_out(n, s, call) return if DPuts.silent if precision = DPuts.show_time $dputs_time ||= Time.now - 120 now = Time.now show = false case precision when /sec/ show = now.to_i != $dputs_time.to_i when /min/ show = (now.to_i / 60).floor != ($dputs_time.to_i / 60).floor when /hour/ show = (now.to_i / 3600).floor != ($dputs_time.to_i / 3600).floor end if show puts "\n *** It is now: " + Time.now.strftime('%Y-%m-%d %H:%M:%S') $dputs_time = now end end DPuts.mutex.synchronize do width = DPuts.terminal_width.to_i || 160 width = [width - 30.0, 10].max file, func = call.split(' ') file = file[/^.*\/([^.]*)/, 1] who = (':' + n.to_s + ':' + file.to_s + func.to_s).ljust(30, ['X', 'x', '*', '-', '.', ' '][n]) lines = [] pos = 0 while (pos < s.length) len = width if s.length - pos > width len = s.rindex(/[, .;=&>]/, pos + width) len = len ? len - pos + 1 : width if len < width / 2 len = width end end lines.push s.slice(pos, len) pos += len end puts who + ' ' + lines.shift.to_s lines.each { |l| puts ' ' * (32) + l } end end |
#dputs_unfunc ⇒ Object
73 74 75 76 |
# File 'lib/helperclasses/dputs.rb', line 73 def dputs_unfunc $DPUTS_FUNCS ||= [] $DPUTS_FUNCS.index(dputs_getcaller) and $DPUTS_FUNCS.delete(dputs_getcaller) end |
#log_msg(mod, msg) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/helperclasses/dputs.rb', line 98 def log_msg(mod, msg) dputs(1) { "Info from #{mod}: #{msg}" } return if not DPuts.log_file File.open(DPuts.log_file, 'a') { |f| str = Time.now.strftime("%a %y.%m.%d-%H:%M:%S #{mod}: #{msg}") f.puts str } end |