Module: RgssDb::Debug

Defined in:
lib/rgss_db/model/debug.rb

Overview

Debug module

Constant Summary collapse

DEBUG_FILE_NAME =

Debug log file name

Returns:

  • (String)
"rgss-db.log"
DEBUG_MODE_DISABLE =

Debug mode disabled

Returns:

  • (Integer)
0
DEBUG_MODE_ERROR =

Debug mode errors and below

Returns:

  • (Integer)
1
DEBUG_MODE_WARNING =

Debug mode warnings and below

Returns:

  • (Integer)
2
DEBUG_MODE_INFO =

Debug mode info and below

Returns:

  • (Integer)
3

Class Method Summary collapse

Class Method Details

.debug_modeInteger

Gets the debug mode

Returns:

  • (Integer)


66
67
68
# File 'lib/rgss_db/model/debug.rb', line 66

def self.debug_mode
  @debug_mode
end

.disabled?Boolean

Checks whether the debug module is disabled or not

Returns:

  • (Boolean)


168
169
170
# File 'lib/rgss_db/model/debug.rb', line 168

def self.disabled?
  @debug_mode == DEBUG_MODE_DISABLE
end

.error?Boolean

Checks whether the debug mode is error or not

Returns:

  • (Boolean)


186
187
188
# File 'lib/rgss_db/model/debug.rb', line 186

def self.error?
  @debug_mode >= DEBUG_MODE_ERROR
end

.info?Boolean

Checks whether the debug mode is info or not

Returns:

  • (Boolean)


177
178
179
# File 'lib/rgss_db/model/debug.rb', line 177

def self.info?
  @debug_mode >= DEBUG_MODE_INFO
end

.log(string) ⇒ Object

Logs the string

Parameters:

  • string (String)


106
107
108
# File 'lib/rgss_db/model/debug.rb', line 106

def self.log(string)
  File.write(@debug_file_path, "#{string}\n", mode: "a") unless disabled?
end

.log_error(string) ⇒ Object

Logs the string as an error

Parameters:

  • string (String)


124
125
126
# File 'lib/rgss_db/model/debug.rb', line 124

def self.log_error(string)
  log("error: #{string}") if error?
end

.log_exception(exception, app_options = nil) ⇒ Object

Logs the given exception as an error

Optionally logs the application options if given

Parameters:

  • app_options (Hash) (defaults to: nil)
  • exception (StandardError)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rgss_db/model/debug.rb', line 146

def self.log_exception(exception, app_options = nil)
  # Logs the exception
  log_error("Application Exception:")
  log_error("\t#{exception.message} (#{exception.class})")
  exception.backtrace.each do |str|
    log_error("\t\tfrom #{str}")
  end

  return unless app_options.is_a?(Hash)

  # Logs the application options
  log_error("Application options:")
  app_options.each_pair do |opt_id, opt_value|
    log_error("\t- #{opt_id} => #{opt_value}")
  end
end

.log_info(string) ⇒ Object

Logs the string as information

Parameters:

  • string (String)


115
116
117
# File 'lib/rgss_db/model/debug.rb', line 115

def self.log_info(string)
  log("info: #{string}") if info?
end

.log_warning(string) ⇒ Object

Logs the string as a warning

Parameters:

  • string (String)


133
134
135
# File 'lib/rgss_db/model/debug.rb', line 133

def self.log_warning(string)
  log("warning: #{string}") if warning?
end

.start(debug_file_path, debug_mode = nil) ⇒ Object

Starts the debug module with the given information

Optionally sets the debug mode

Parameters:

  • debug_file_path (String)

    Path to the debug file

  • debug_mode (Integer) (defaults to: nil)

    Debug mode



50
51
52
53
54
55
56
57
58
59
# File 'lib/rgss_db/model/debug.rb', line 50

def self.start(debug_file_path, debug_mode = nil)
  @debug_file_path = File.join(debug_file_path, DEBUG_FILE_NAME)
  @debug_mode = debug_mode unless debug_mode.nil?

  # Creates the directory (recursively)
  FileUtils.mkdir_p(debug_file_path)

  # Overwrites file if it exists already
  File.truncate(@debug_file_path, 0) if File.exist?(@debug_file_path)
end

.update_debug_mode(debug_mode) ⇒ Object

Sets the debug mode

Set to 0 to disable

Parameters:

  • debug_mode (Integer)

    Debug mode



77
78
79
# File 'lib/rgss_db/model/debug.rb', line 77

def self.update_debug_mode(debug_mode)
  @debug_mode = debug_mode
end

.update_extra_info(*extra_info) ⇒ Object

Sets the debug extra information

Parameters:

  • extra_info (Array<String>)


86
87
88
# File 'lib/rgss_db/model/debug.rb', line 86

def self.update_extra_info(*extra_info)
  @debug_extra_info = extra_info
end

.warning?Boolean

Checks whether the debug mode is warning or not

Returns:

  • (Boolean)


195
196
197
# File 'lib/rgss_db/model/debug.rb', line 195

def self.warning?
  @debug_mode >= DEBUG_MODE_WARNING
end

.write_debug_headerObject

Writes the debug header to the log file



93
94
95
96
97
98
99
# File 'lib/rgss_db/model/debug.rb', line 93

def self.write_debug_header
  log("<--- debug module started --->")
  log("debug mode: #{Debug.debug_mode}")
  log("debug extra:")
  @debug_extra_info.each { |str| log("\t- #{str}") }
  log("<---------------------------->")
end