Module: HelperClasses::DPuts

Extended by:
DPuts
Included in:
DPuts, Service, System, System
Defined in:
lib/helper_classes/dputs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#log_fileObject

Returns the value of attribute log_file.



7
8
9
# File 'lib/helper_classes/dputs.rb', line 7

def log_file
  @log_file
end

#logall_fileObject

Returns the value of attribute logall_file.



7
8
9
# File 'lib/helper_classes/dputs.rb', line 7

def logall_file
  @logall_file
end

#max_msg_lenObject

Returns the value of attribute max_msg_len.



7
8
9
# File 'lib/helper_classes/dputs.rb', line 7

def max_msg_len
  @max_msg_len
end

#mutexObject

Returns the value of attribute mutex.



7
8
9
# File 'lib/helper_classes/dputs.rb', line 7

def mutex
  @mutex
end

#show_timeObject

Returns the value of attribute show_time.



7
8
9
# File 'lib/helper_classes/dputs.rb', line 7

def show_time
  @show_time
end

#silentObject

Returns the value of attribute silent.



7
8
9
# File 'lib/helper_classes/dputs.rb', line 7

def silent
  @silent
end

#terminal_widthObject

Returns the value of attribute terminal_width.



7
8
9
# File 'lib/helper_classes/dputs.rb', line 7

def terminal_width
  @terminal_width
end

Instance Method Details

#ddputs(n, &s) ⇒ Object



106
107
108
109
110
# File 'lib/helper_classes/dputs.rb', line 106

def ddputs(n, &s)
  s = yield s
  #dp caller(0)
  dputs_out(-n, s, caller(0)[1])
end

#dlog_msg(mod, msg) ⇒ Object



127
128
129
# File 'lib/helper_classes/dputs.rb', line 127

def dlog_msg(mod, msg)
  ddputs(1) { "Info from #{mod}: #{msg}" }
end

#dp(s) ⇒ Object



112
113
114
115
# File 'lib/helper_classes/dputs.rb', line 112

def dp(s)
  dputs_out(0, s.class == String ? s : s.inspect, caller(0)[1])
  s
end

#dputs(n, &s) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/helper_classes/dputs.rb', line 97

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_funcObject



87
88
89
90
# File 'lib/helper_classes/dputs.rb', line 87

def dputs_func
  $DPUTS_FUNCS ||= []
  $DPUTS_FUNCS.push(dputs_getcaller) unless $DPUTS_FUNCS.index(dputs_getcaller)
end

#dputs_getcallerObject



83
84
85
# File 'lib/helper_classes/dputs.rb', line 83

def dputs_getcaller
  caller(0)[2].sub(/:.*:in/, '').sub(/block .*in /, '')
end

#dputs_out(n, s, call) ⇒ Object



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/helper_classes/dputs.rb', line 30

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
      str = "\n   *** It is now: " +
               Time.now.strftime('%Y-%m-%d %H:%M:%S') + "\n"
      dputs_write(str)
      $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
    # Don't show enormous strings
    s_trunc = s[0..DPuts.max_msg_len]
    while (pos < s_trunc.length)
      len = width
      if s_trunc.length - pos > width
        len = s_trunc.rindex(/[, .;=&>]/, pos + width)
        len = len ? len - pos + 1 : width
        if len < width / 2
          len = width
        end
      end
      lines.push s_trunc.slice(pos, len)
      pos += len
    end
    str = who + ' ' + lines.shift.to_s + "\n"
    lines.each { |l|
      str += ' ' * (32) + l + "\n"
    }
    dputs_write(str)
  end
end

#dputs_unfuncObject



92
93
94
95
# File 'lib/helper_classes/dputs.rb', line 92

def dputs_unfunc
  $DPUTS_FUNCS ||= []
  $DPUTS_FUNCS.index(dputs_getcaller) and $DPUTS_FUNCS.delete(dputs_getcaller)
end

#dputs_write(str) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/helper_classes/dputs.rb', line 22

def dputs_write(str)
  if logfile_valid(DPuts.logall_file)
    IO.write(DPuts.logall_file, str, mode: 'a')
  else
    puts str
  end
end

#log_msg(mod, msg) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/helper_classes/dputs.rb', line 117

def log_msg(mod, msg)
  dputs(1) { "Info from #{mod}: #{msg}" }
  if logfile_valid(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
end

#logfile_valid(f) ⇒ Object



18
19
20
# File 'lib/helper_classes/dputs.rb', line 18

def logfile_valid(f)
  return f && f.to_s.length > 0 && File.exists?(File.dirname(f))
end