Module: RUtilAnts::Logging::LoggerInterface

Included in:
Logger
Defined in:
lib/rUtilAnts/Logging.rb

Overview

The logger interface, can be used to decorate any class willing to have de

Instance Method Summary collapse

Instance Method Details

#activate_log_debug(iDebugMode) ⇒ Object

Set the debug mode

Parameters
  • iDebugMode (Boolean): Are we in debug mode ?



130
131
132
133
134
135
136
137
138
139
# File 'lib/rUtilAnts/Logging.rb', line 130

def activate_log_debug(iDebugMode)
  if (@DebugMode != iDebugMode)
    @DebugMode = iDebugMode
    if (iDebugMode)
      log_info 'Activated log debug'
    else
      log_info 'Deactivated log debug'
    end
  end
end

#debug_activated?Boolean

Is debug mode activated ?

Return
  • Boolean: Are we in debug mode ?

Returns:

  • (Boolean)


145
146
147
# File 'lib/rUtilAnts/Logging.rb', line 145

def debug_activated?
  return @DebugMode
end

#get_bug_tracker_urlObject

Get the bug tracker URL

Return
  • String: The bug tracker URL, as defined when initialized



114
115
116
# File 'lib/rUtilAnts/Logging.rb', line 114

def get_bug_tracker_url
  return @BugTrackerURL
end

#get_lib_root_dirObject

Get the library root dir

Return
  • String: The library root dir, as defined when initialized



106
107
108
# File 'lib/rUtilAnts/Logging.rb', line 106

def get_lib_root_dir
  return @LibRootDir
end

#get_log_fileObject

Get the log file used (can be nil)

Return
  • String: Log file name (can be nil)



98
99
100
# File 'lib/rUtilAnts/Logging.rb', line 98

def get_log_file
  return @LogFile
end

#init_logger(iOptions = {}) ⇒ Object

Initializer of the logger variables

Parameters
  • iOptions (map<Symbol,Object>): Options [optional = {}]

    • :lib_root_dir (String): The library root directory that will not appear in the logged stack messages [optional = nil]

    • :bug_tracker_url (String): The application’s bug tracker URL, used to report bugs [optional = nil]

    • :mute_stdout (Boolean): Do we silent normal output (nothing sent to $stdout) ? [optional = false]

    • :mute_stderr (Boolean): Do we silent error output (nothing sent to $stderr) ? [optional = false]

    • :no_dialogs (Boolean): Do we forbid dialogs usage ? [optional = false]

    • :debug_mode (Boolean): Do we activate debug mode ? [optional = false]

    • :log_file (String): Specify a log file [optional = nil]

    • :errors_stack (list<String>): Specify an errors stack [optional = nil]

    • :messages_stack (list<String>): Specify a messages stack [optional = nil]

    • :gui_for_dialogs (Integer): Specify a GUI constant for dialogs [optional = nil]



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
62
63
64
65
66
67
68
# File 'lib/rUtilAnts/Logging.rb', line 25

def init_logger(iOptions = {})
  @LibRootDir = iOptions[:lib_root_dir]
  @BugTrackerURL = iOptions[:bug_tracker_url]
  @DebugMode = (iOptions[:debug_mode] == nil) ? false : iOptions[:debug_mode]
  @LogFile = iOptions[:log_file]
  @ErrorsStack = iOptions[:errors_stack]
  @MessagesStack = iOptions[:messages_stack]
  @DialogsGUI = iOptions[:gui_for_dialogs]
  @ScreenOutput = (iOptions[:mute_stdout] == nil) ? true : (!iOptions[:mute_stdout])
  @ScreenOutputErr = (iOptions[:mute_stderr] == nil) ? true : (!iOptions[:mute_stderr])
  @NoDialogs = (iOptions[:no_dialogs] == nil) ? false : iOptions[:no_dialogs]
  if (!@ScreenOutput)
    # Test if we can write to stdout
    begin
      $stdout << "Launch Logging - stdout\n"
    rescue Exception
      # Redirect to a file if possible
      begin
        lFile = File.open('./stdout', 'w')
        $stdout.reopen(lFile)
        $stdout << "Launch Logging - stdout\n"
      rescue Exception
        # Disable
        @ScreenOutput = false
      end
    end
  end
  if (!@ScreenOutputErr)
    # Test if we can write to stderr
    begin
      $stderr << "Launch Logging - stderr\n"
    rescue Exception
      # Redirect to a file if possible
      begin
        lFile = File.open('./stderr', 'w')
        $stderr.reopen(lFile)
        $stderr << "Launch Logging - stderr\n"
      rescue Exception
        # Disable
        @ScreenOutputErr = false
      end
    end
  end
end

#log_bug(iMsg) ⇒ Object

Log a bug This is called when there is a bug in the program. It has been set in many places to detect bugs.

Parameters
  • iMsg (String): Message to log



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
# File 'lib/rUtilAnts/Logging.rb', line 188

def log_bug(iMsg)
  lCompleteMsg = "Bug: #{iMsg}
Stack:
#{get_simple_caller(caller[0..-2]).join("\n")}"
  # Log into stderr
  if (@ScreenOutputErr)
    $stderr << "!!! BUG !!! #{lCompleteMsg}\n"
  end
  if (@LogFile != nil)
    log_file(lCompleteMsg)
  end
  # Display Bug dialog
  if (show_modal_wx_available?)
    # We require the file here, as we hope it will not be required often
    require 'rUtilAnts/GUI/BugReportDialog'
    showModal(GUI::BugReportDialog, nil, lCompleteMsg, @BugTrackerURL) do |iModalResult, iDialog|
      # Nothing to do
    end
  else
    # Use normal platform dependent message, if the platform has been initialized (otherwise, stick to $stderr)
    if ((defined?(display_msg) != nil) and
        (!@NoDialogs))
      display_msg("A bug has just occurred.
Normally you should never see this message, but this application is not bug-less.
We are sorry for the inconvenience caused.
If you want to help improving this application, please inform us of this bug:
take the time to open a ticket at the bugs tracker.
We will always try our best to correct bugs.
Thanks.

Details:
#{lCompleteMsg}
")
    end
  end
end

#log_debug(iMsg) ⇒ Object

Log a debugging info. This is used when debug is activated

Parameters
  • iMsg (String): Message to log



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/rUtilAnts/Logging.rb', line 329

def log_debug(iMsg)
  # Log into stdout
  if (@DebugMode)
    if (@ScreenOutput)
      $stdout << "#{iMsg}\n"
    end
    if (@LogFile != nil)
      log_file(iMsg)
    end
  end
end

#log_err(iMsg) ⇒ Object

Log an error. Those errors can be normal, as they mainly depend on external factors (lost connection, invalid user file…)

Parameters
  • iMsg (String): Message to log



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rUtilAnts/Logging.rb', line 230

def log_err(iMsg)
  lMsg = "!!! ERR !!! #{iMsg}"
  # Log into stderr
  if (@ScreenOutputErr)
    $stderr << "#{lMsg}\n"
  end
  if (@LogFile != nil)
    log_file(lMsg)
  end
  # Display dialog only if we are not redirecting messages to a stack
  if (@ErrorsStack == nil)
    if (show_modal_wx_available?)
      showModal(Wx::MessageDialog, nil,
        iMsg,
        :caption => 'Error',
        :style => Wx::OK|Wx::ICON_ERROR
      ) do |iModalResult, iDialog|
        # Nothing to do
      end
    elsif ((defined?(display_msg) != nil) and
           (!@NoDialogs))
      # Use normal platform dependent message, if the platform has been initialized (otherwise, stick to $stderr)
      display_msg(iMsg)
    end
  else
    @ErrorsStack << iMsg
  end
end

#log_exc(iException, iMsg) ⇒ Object

Log an exception This is called when there is a bug due to an exception in the program. It has been set in many places to detect bugs.

Parameters
  • iException (Exception): Exception

  • iMsg (String): Message to log



175
176
177
178
179
180
181
# File 'lib/rUtilAnts/Logging.rb', line 175

def log_exc(iException, iMsg)
  log_bug("#{iMsg}
Exception: #{iException}
Exception stack:
#{get_simple_caller(iException.backtrace, caller).join("\n")}
...")
end

#log_info(iMsg) ⇒ Object

Log an info. This is just common journal.

Parameters
  • iMsg (String): Message to log



298
299
300
301
302
303
304
305
306
# File 'lib/rUtilAnts/Logging.rb', line 298

def log_info(iMsg)
  # Log into stdout
  if (@ScreenOutput)
    $stdout << "#{iMsg}\n"
  end
  if (@LogFile != nil)
    log_file(iMsg)
  end
end

#log_msg(iMsg) ⇒ Object

Log a normal message to the user This is used to display a simple message to the user

Parameters
  • iMsg (String): Message to log



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rUtilAnts/Logging.rb', line 264

def log_msg(iMsg)
  # Log into stderr
  if (@ScreenOutput)
    $stdout << "#{iMsg}\n"
  end
  if (@LogFile != nil)
    log_file(iMsg)
  end
  # Display dialog only if we are not redirecting messages to a stack
  if (@MessagesStack == nil)
    # Display dialog only if showModal exists and that we are currently running the application
    if (show_modal_wx_available?)
      showModal(Wx::MessageDialog, nil,
        iMsg,
        :caption => 'Notification',
        :style => Wx::OK|Wx::ICON_INFORMATION
      ) do |iModalResult, iDialog|
        # Nothing to do
      end
    elsif ((defined?(display_msg) != nil) and
           (!@NoDialogs))
      # Use normal platform dependent message, if the platform has been initialized (otherwise, stick to $stderr)
      display_msg(iMsg)
    end
  else
    @MessagesStack << iMsg
  end
end

#log_warn(iMsg) ⇒ Object

Log a warning. Warnings are not errors but still should be highlighted.

Parameters
  • iMsg (String): Message to log



313
314
315
316
317
318
319
320
321
322
# File 'lib/rUtilAnts/Logging.rb', line 313

def log_warn(iMsg)
  # Log into stdout
  lMsg = "!!! WARNING !!! - #{iMsg}"
  if (@ScreenOutput)
    $stdout << "#{lMsg}\n"
  end
  if (@LogFile != nil)
    log_file(lMsg)
  end
end

#mute_stderr(iMute = true) ⇒ Object

Mute or unmute error output

Parameters
  • iMute (Boolean): Do we mute error output ? [optional = true]



82
83
84
# File 'lib/rUtilAnts/Logging.rb', line 82

def mute_stderr(iMute = true)
  @ScreenOutputErr = (!iMute)
end

#mute_stdout(iMute = true) ⇒ Object

Mute or unmute standard output

Parameters
  • iMute (Boolean): Do we mute standard output ? [optional = true]



74
75
76
# File 'lib/rUtilAnts/Logging.rb', line 74

def mute_stdout(iMute = true)
  @ScreenOutput = (!iMute)
end

#set_gui_for_dialogs(iGUIToUse) ⇒ Object

Indicate which GUI to be used to display dialogs.

Parameters
  • iGUIToUse (Integer): The GUI constant, or nil if no GUI is provided



122
123
124
# File 'lib/rUtilAnts/Logging.rb', line 122

def set_gui_for_dialogs(iGUIToUse)
  @DialogsGUI = iGUIToUse
end

#set_log_errors_stack(iErrorsStack) ⇒ Object

Set the stack of the errors to fill. If set to nil, errors will be displayed as they appear. If set to a stack, errors will silently be added to the list.

Parameters
  • iErrorsStack (list<String>): The stack of errors, or nil to unset it



155
156
157
# File 'lib/rUtilAnts/Logging.rb', line 155

def set_log_errors_stack(iErrorsStack)
  @ErrorsStack = iErrorsStack
end

#set_log_file(iFileName) ⇒ Object

Set the log file to use (can be nil to stop logging into a file)

Parameters
  • iFileName (String): Log file name (can be nil)



90
91
92
# File 'lib/rUtilAnts/Logging.rb', line 90

def set_log_file(iFileName)
  @LogFile = iFileName
end

#set_log_messages_stack(iMessagesStack) ⇒ Object

Set the stack of the messages to fill. If set to nil, messages will be displayed as they appear. If set to a stack, messages will silently be added to the list.

Parameters
  • iMessagesStack (list<String>): The stack of messages, or nil to unset it



165
166
167
# File 'lib/rUtilAnts/Logging.rb', line 165

def set_log_messages_stack(iMessagesStack)
  @MessagesStack = iMessagesStack
end