Class: BlackStack::BaseLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/baselogger.rb

Direct Known Subclasses

DummyLogger, LocalLogger

Constant Summary collapse

NEWLINE =
"\n\r"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(the_filename = nil) ⇒ BaseLogger

Returns a new instance of BaseLogger.



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

def initialize(the_filename=nil)
  self.filename = the_filename
  self.initialize_attributes
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



104
105
106
# File 'lib/baselogger.rb', line 104

def filename
  @filename
end

#levelObject

Returns the value of attribute level.



104
105
106
# File 'lib/baselogger.rb', line 104

def level
  @level
end

#level_children_linesObject

Returns the value of attribute level_children_lines.



104
105
106
# File 'lib/baselogger.rb', line 104

def level_children_lines
  @level_children_lines
end

#level_open_callersObject

Returns the value of attribute level_open_callers.



104
105
106
# File 'lib/baselogger.rb', line 104

def level_open_callers
  @level_open_callers
end

Instance Method Details

#blank_lineObject



132
133
134
# File 'lib/baselogger.rb', line 132

def blank_line
  self.log('')
end

#done(details: nil) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/baselogger.rb', line 232

def done(details: nil)
  if details.nil?
    self.logf("done".green)
  else
    self.logf("done".green + " (#{details.to_s})")
  end
end

#error(e = nil) ⇒ Object



248
249
250
251
# File 'lib/baselogger.rb', line 248

def error(e=nil)
  self.logf("error".red) if e.nil?
  self.logf("error: #{e.to_console}.".red) if !e.nil?
end

#initialize_attributesObject



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

def initialize_attributes()
    self.level = 0
    self.level_children_lines = {}      
    self.level_open_callers = {}
end

#log(s, datetime = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/baselogger.rb', line 121

def log(s, datetime=nil)
  t = !datetime.nil? ? datetime : Time.now 
  ltime = t.strftime("%Y-%m-%d %H:%M:%S").to_s
  ltime += " - level #{self.level.to_s.blue}" if Logger.show_nesting_level
  ltext = ltime + ": " + s + NEWLINE
  # print
  print ltext
  # return
  ltext
end

#logf(s, datetime = nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/baselogger.rb', line 188

def logf(s, datetime=nil)
  ltext = ""

  # clear the caller who opened the level that I am closing
  self.level_open_callers[self.level-1] = nil
  
  # if the parent level has children
  if self.level_children_lines[self.level].to_i > 0
    t = !datetime.nil? ? datetime : Time.now 
    ltime = t.strftime("%Y-%m-%d %H:%M:%S").to_s
    ltime += " - level #{self.level.to_s.blue}" if Logger.show_nesting_level
  
    ltext += "#{ltime}: "

    i=1
    while (i<self.level)
      ltext += "> "
      i+=1
    end
  
  end

  # since I am closing a level, set the number of children to 0
  self.level_children_lines[self.level] = 0

  # nesting assertion
  if self.level <= 0
    # force the level to 2, so I can use the loger to trace the error after raising the exceptiopn.
    self.level = 1
    # raise the exception
    if Logger.nesting_assertion
      raise LogNestingError.new("Log nesting assertion: You are closing 2 times the level started, or you missed to open that lavel, or you closed the another level in the middle 2 times.") 
    end
  end

  self.level -= 1
  ltext += s + NEWLINE 
        
  # print
  print ltext
  # return
  ltext
end

#logs(s, datetime = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/baselogger.rb', line 136

def logs(s, datetime=nil)

  # Nesting assertion:
  # - How to find out from which line number the method was called in Ruby?
  # - Referneces:
  #   - https://stackoverflow.com/questions/37564928/how-to-find-out-from-which-line-number-the-method-was-called-in-ruby
  #   - https://ruby-doc.org/core-2.2.3/Thread/Backtrace/Location.html
#binding.pry if s == "Looking for number 3... "
  caller = caller_locations(0..).last
#binding.pry if s == '1... '
#binding.pry if s == '4... '

  # if the parent level was called from the same line, I am missing to close the parent.
  if self.level_open_callers[self.level-1].to_s == caller.to_s
    if Logger.nesting_assertion
      raise LogNestingError.new("Log nesting assertion: You missed to close the log-level that you opened at #{caller.to_s}.")
    end
  else
    self.level_open_callers[self.level] = caller.to_s
  end

  t = !datetime.nil? ? datetime : Time.now 
  ltime = t.strftime("%Y-%m-%d %H:%M:%S").to_s
  ltime += " - level #{self.level.to_s.blue}" if Logger.show_nesting_level
  ltime += " - caller #{caller.to_s.blue}" if Logger.show_nesting_caller

#binding.pry if self.level>0
  # start in a new line, if this line is the first child of the parent level has opened lines
  ltext = ""
  ltext += NEWLINE if self.level > 0 && self.level_children_lines[self.level].to_i == 0
  ltext += ltime + ": "

  # increase the number of children of the parent level
  self.level_children_lines[self.level] = self.level_children_lines[self.level].to_i + 1

  self.level += 1

  i=1
  while (i<self.level)
    ltext += "> "
    i+=1
  end

  ltext += s
              
  # print
  print ltext
  
  # return
  ltext
end

#noObject



261
262
263
# File 'lib/baselogger.rb', line 261

def no()
  self.logf("no".yellow)
end

#okObject



257
258
259
# File 'lib/baselogger.rb', line 257

def ok()
  self.logf("ok".green)
end

#resetObject



117
118
119
# File 'lib/baselogger.rb', line 117

def reset()
  self.initialize_attributes
end

#skip(details: nil) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/baselogger.rb', line 240

def skip(details: nil)
  if details.nil?
    self.logf("skip".yellow)
  else
    self.logf("skip".yellow + " (#{details.to_s})")
  end
end

#yesObject



253
254
255
# File 'lib/baselogger.rb', line 253

def yes()
  self.logf("yes".green)
end