Class: RUtilAnts::Logging::Logger

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

Overview

The logger class singleton

Constant Summary collapse

GUI_WX =

Constants used for GUI dialogs selection

0

Instance Method Summary collapse

Constructor Details

#initialize(iLibRootDir, iBugTrackerURL, iSilentSTDOut = false, iSilentSTDErr = false) ⇒ Logger

Constructor

Parameters:

  • iLibRootDir (String): The library root directory that will not appear in the logged stack messages

  • iBugTrackerURL (String): The application’s bug tracker URL, used to report bugs

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

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



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

def initialize(iLibRootDir, iBugTrackerURL, iSilentSTDOut = false, iSilentSTDErr = false)
  @LibRootDir, @BugTrackerURL = iLibRootDir, iBugTrackerURL
  @DebugMode = false
  @LogFile = nil
  @ErrorsStack = nil
  @MessagesStack = nil
  @DialogsGUI = nil
  @ScreenOutput = (!iSilentSTDOut)
  @ScreenOutputErr = (!iSilentSTDErr)
  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

Instance Method Details

#activateLogDebug(iDebugMode) ⇒ Object

Set the debug mode

Parameters:

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



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

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

#debugActivated?Boolean

Is debug mode activated ?

Return:

  • Boolean: Are we in debug mode ?

Returns:

  • (Boolean)


143
144
145
# File 'lib/rUtilAnts/Logging.rb', line 143

def debugActivated?
  return @DebugMode
end

#getBugTrackerURLObject

Get the bug tracker URL

Return:

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



112
113
114
# File 'lib/rUtilAnts/Logging.rb', line 112

def getBugTrackerURL
  return @BugTrackerURL
end

#getLibRootDirObject

Get the library root dir

Return:

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



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

def getLibRootDir
  return @LibRootDir
end

#getLogFileObject

Get the log file used (can be nil)

Return:

  • String: Log file name (can be nil)



96
97
98
# File 'lib/rUtilAnts/Logging.rb', line 96

def getLogFile
  return @LogFile
end

#logBug(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



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

def logBug(iMsg)
  lCompleteMsg = "Bug: #{iMsg}
Stack:
#{getSimpleCaller(caller[0..-2]).join("\n")}"
  # Log into stderr
  if (@ScreenOutputErr)
    $stderr << "!!! BUG !!! #{lCompleteMsg}\n"
  end
  if (@LogFile != nil)
    logFile(lCompleteMsg)
  end
  # Display Bug dialog
  if (showModalWxAvailable?)
    # 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?($rUtilAnts_Platform_Info) != nil)
      $rUtilAnts_Platform_Info.sendMsg("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

#logDebug(iMsg) ⇒ Object

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

Parameters:

  • iMsg (String): Message to log



324
325
326
327
328
329
330
331
332
333
# File 'lib/rUtilAnts/Logging.rb', line 324

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

#logErr(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



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

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

#logExc(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



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

def logExc(iException, iMsg)
  logBug("#{iMsg}
Exception: #{iException}
Exception stack:
#{getSimpleCaller(iException.backtrace, caller).join("\n")}
...")
end

#logInfo(iMsg) ⇒ Object

Log an info. This is just common journal.

Parameters:

  • iMsg (String): Message to log



293
294
295
296
297
298
299
300
301
# File 'lib/rUtilAnts/Logging.rb', line 293

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

#logMsg(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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rUtilAnts/Logging.rb', line 260

def logMsg(iMsg)
  # Log into stderr
  if (@ScreenOutput)
    $stdout << "#{iMsg}\n"
  end
  if (@LogFile != nil)
    logFile(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 (showModalWxAvailable?)
      showModal(Wx::MessageDialog, nil,
        iMsg,
        :caption => 'Notification',
        :style => Wx::OK|Wx::ICON_INFORMATION
      ) do |iModalResult, iDialog|
        # Nothing to do
      end
    elsif (defined?($rUtilAnts_Platform_Info) != nil)
      # Use normal platform dependent message, if the platform has been initialized (otherwise, stick to $stderr)
      $rUtilAnts_Platform_Info.sendMsg(iMsg)
    end
  else
    @MessagesStack << iMsg
  end
end

#logWarn(iMsg) ⇒ Object

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

Parameters:

  • iMsg (String): Message to log



308
309
310
311
312
313
314
315
316
317
# File 'lib/rUtilAnts/Logging.rb', line 308

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

#muteStdErr(iMute = true) ⇒ Object

Mute or unmute error output

Parameters:

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



80
81
82
# File 'lib/rUtilAnts/Logging.rb', line 80

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

#muteStdOut(iMute = true) ⇒ Object

Mute or unmute standard output

Parameters:

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



72
73
74
# File 'lib/rUtilAnts/Logging.rb', line 72

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

#setGUIForDialogs(iGUIToUse) ⇒ Object

Indicate which GUI to be used to display dialogs.

Parameters:

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



120
121
122
# File 'lib/rUtilAnts/Logging.rb', line 120

def setGUIForDialogs(iGUIToUse)
  @DialogsGUI = iGUIToUse
end

#setLogErrorsStack(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



153
154
155
# File 'lib/rUtilAnts/Logging.rb', line 153

def setLogErrorsStack(iErrorsStack)
  @ErrorsStack = iErrorsStack
end

#setLogFile(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)



88
89
90
# File 'lib/rUtilAnts/Logging.rb', line 88

def setLogFile(iFileName)
  @LogFile = iFileName
end

#setLogMessagesStack(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



163
164
165
# File 'lib/rUtilAnts/Logging.rb', line 163

def setLogMessagesStack(iMessagesStack)
  @MessagesStack = iMessagesStack
end