Module: Superhosting::Helper::Logger

Instance Method Summary collapse

Instance Method Details

#__debugObject



39
40
41
# File 'lib/superhosting/helper/logger.rb', line 39

def __debug
  Thread.current[:debug]
end

#__dry_runObject



20
21
22
# File 'lib/superhosting/helper/logger.rb', line 20

def __dry_run
  Thread.current[:dry_run]
end

#__dry_run=(val) ⇒ Object



24
25
26
# File 'lib/superhosting/helper/logger.rb', line 24

def __dry_run=(val)
  Thread.current[:dry_run] = val
end

#__loggerObject



4
5
6
# File 'lib/superhosting/helper/logger.rb', line 4

def __logger
  Thread.current[:logger]
end

#__logger=(val) ⇒ Object



8
9
10
# File 'lib/superhosting/helper/logger.rb', line 8

def __logger=(val)
  Thread.current[:logger] = val
end

#debug(msg = nil, indent: true, desc: nil, &b) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/superhosting/helper/logger.rb', line 52

def debug(msg=nil, indent: true, desc: nil, &b)
  unless self.__logger.nil?
    unless desc.nil?
      (desc[:data] ||= {})[:msg] = msg
      msg = t(desc: desc)
    end
    msg = indent ? with_indent(msg) : msg.chomp
    self.__logger.debug(msg, &b)
  end
  {} # net_status
end

#debug_block(desc: nil, operation: false, &b) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/superhosting/helper/logger.rb', line 84

def debug_block(desc: nil, operation: false, &b)
  old = self.indent

  self.debug(desc: desc)
  self.indent_step

  status = :failed
  resp = yield
  status = :ok

  resp
rescue Exception => e
  raise
ensure
  self.debug(desc: { code: status })
  self.indent = old
end

#debug_operation(desc: nil, &b) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/superhosting/helper/logger.rb', line 64

def debug_operation(desc: nil, &b)
  old = self.indent

  status = :failed
  diff = nil
  resp = b.call do |resp|
    status = resp[:code] || :ok
    diff = resp[:diff]
  end

  resp
rescue Exception => e
  raise
ensure
  desc[:code] = :"#{desc[:code]}.#{status}"
  self.debug(desc: desc)
  self.debug(diff, indent: false) if !diff.nil? and self.__debug
  self.indent = old
end

#indentObject



110
111
112
# File 'lib/superhosting/helper/logger.rb', line 110

def indent
  @@indent ||= self.indent_reset
end

#indent=(val) ⇒ Object



114
115
116
# File 'lib/superhosting/helper/logger.rb', line 114

def indent=(val)
  @@indent = val
end

#indent_resetObject



118
119
120
# File 'lib/superhosting/helper/logger.rb', line 118

def indent_reset
  self.indent = 0
end

#indent_stepObject



122
123
124
# File 'lib/superhosting/helper/logger.rb', line 122

def indent_step
  self.indent += 1
end

#indent_step_backObject



126
127
128
# File 'lib/superhosting/helper/logger.rb', line 126

def indent_step_back
  self.indent -= 1
end

#info(msg = nil, indent: true, desc: nil, **kwargs, &b) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/superhosting/helper/logger.rb', line 43

def info(msg=nil, indent: true, desc: nil, **kwargs, &b)
  unless self.__logger.nil?
    msg = indent ? with_indent(msg) : msg.chomp
    msg = kwargs if msg.empty? # TODO
    self.__logger.info(msg, &b)
  end
  {} # net_status
end

#storageObject



35
36
37
# File 'lib/superhosting/helper/logger.rb', line 35

def storage
  Thread.current[:dry_storage] ||= {}
end

#t(desc: {}, context: nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/superhosting/helper/logger.rb', line 102

def t(desc: {}, context: nil)
  code = desc[:code]
  data = desc[:data]
  ::I18n.t [:debug, context, code].join('.'), [:debug, code].join('.'), **data, raise: true
rescue ::I18n::MissingTranslationData => e
  raise NetStatus::Exception, { code: :missing_translation, data: { code: code } }
end

#with_dry_runObject



28
29
30
31
32
33
# File 'lib/superhosting/helper/logger.rb', line 28

def with_dry_run
  old = __dry_run
  yield old
ensure
  __dry_run = old
end

#with_indent(msg) ⇒ Object



130
131
132
133
# File 'lib/superhosting/helper/logger.rb', line 130

def with_indent(msg)
  ind = "#{' ' * 4 * self.indent }"
  "#{ind}#{msg.to_s.sub("\n", "\n#{ind}")}"
end

#with_logger(logger: nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/superhosting/helper/logger.rb', line 12

def with_logger(logger: nil)
  old = self.__logger
  self.__logger = nil if logger.is_a? FalseClass
  yield
ensure
  self.__logger = old
end