Module: PrcLib
- Defined in:
- lib/prc.rb,
lib/logging.rb
Overview
PrcLib module
This module helps to configure the lorj library. It implements also a Logging class based on logger.
For details about this class capabilities, see PrcLib::Logging
List of possible library settings:
-
PrcLib.log
PrcLib::Logging object. Used internally by PrcLib logging system.
This object is automatically created as soon as a message is printed out
-
PrcLib.core_level
Initialize lorj debug level. from 0 to 5.
ex:
PrcLib.core_level = 4 -
PrcLib.pdata_path
Define the private data local directory. Usually used for any private keys, passwords, etc…
By default: ~/.config/<app_name>
ex:
PrcLib.pdata_path = File.join('~', '.private_myapp') -
PrcLib.data_path
Define the data local directory.
By default: ~/.<app_name>
ex:
PrcLib.data_path = File.join('/etc', 'myapp') -
PrcLib.app_name
Define the application name. By default ‘lorj’. By default, this setting configure PrcLib.data_path and PrcLib.pdata_path automatically, except if you set it before.
ex:
PrcLib.app_name = 'myapp' -
PrcLib.app_defaults
Used by Lorj::Config to identify application defaults and your application data model data.
By default nil. Ex:
puts PrcLib.app_defaults[:data] # To get value of the predefined :data key. -
PrcLib.log_file
Define the log file name used.
By default, defined as ~/.<app_name>/<app_name>.log
-
PrcLib.level logger level used. It can be updated at runtime.
Ex:
PrcLib.level = Logger::FATAL -
PrcLib.model
Model loaded.
-
PrcLib.log_file
Initialize a log file name instead of default one.
Ex:
PrcLib.log_file = "mylog.file.log" -
PrcLib.controller_path
Provides the default controller path.
-
PrcLib.process_path
Provides the default process path.
Defined Under Namespace
Classes: Logging
Class Attribute Summary collapse
-
.app_defaults ⇒ Object
Returns the value of attribute app_defaults.
-
.core_level ⇒ Object
Returns the value of attribute core_level.
-
.data_path ⇒ Object
Returns the value of attribute data_path.
-
.level ⇒ Object
Returns the value of attribute level.
-
.log ⇒ Object
Returns the value of attribute log.
-
.log_file ⇒ Object
Returns the value of attribute log_file.
-
.pdata_path ⇒ Object
Returns the value of attribute pdata_path.
Class Method Summary collapse
- .app_name ⇒ Object
- .app_name=(v) ⇒ Object
- .controller_path ⇒ Object
-
.dcl_fail(msg, *p) ⇒ Object
Internal heap management to help controller developper to identify issue.
-
.debug(message, *p) ⇒ Object
Log to STDOUT and Log file and DEBUG class message.
-
.dir_exists?(path) ⇒ Boolean
Check if dir exists and is fully accessible (rwx).
-
.ensure_dir_exists(path) ⇒ Object
ensure dir exists and is fully accessible (rwx).
-
.error(message, *p) ⇒ Object
Log to STDOUT and Log file and ERROR class message.
-
.fatal(rc, message, *p) ⇒ Object
Log to STDOUT and Log file and FATAL class message then exit the application with a return code.
- .fatal_error(rc, msg) ⇒ Object
-
.high_level_msg(message, *p) ⇒ Object
Not DEBUG and not INFO.
-
.info(message, *p) ⇒ Object
Log to STDOUT and Log file and INFO class message.
- .lib_path=(v) ⇒ Object
-
.log_object ⇒ Object
Create a Logging object if missing and return it.
-
.message(message, *p) ⇒ Object
Print out a message, not logged in the log file.
-
.model ⇒ Object
TODO: Low.
- .process_path ⇒ Object
-
.runtime_fail(msg, *p) ⇒ Object
fail extended with format.
-
.state(message, *p) ⇒ Object
Print the message to the same line.
-
.warning(message, *p) ⇒ Object
Log to STDOUT and Log file and WARNING class message.
Class Attribute Details
.app_defaults ⇒ Object
Returns the value of attribute app_defaults.
144 145 146 |
# File 'lib/prc.rb', line 144 def app_defaults @app_defaults end |
.core_level ⇒ Object
Returns the value of attribute core_level.
143 144 145 |
# File 'lib/prc.rb', line 143 def core_level @core_level end |
.data_path ⇒ Object
Returns the value of attribute data_path.
144 145 146 |
# File 'lib/prc.rb', line 144 def data_path @data_path end |
.level ⇒ Object
Returns the value of attribute level.
144 145 146 |
# File 'lib/prc.rb', line 144 def level @level end |
.log ⇒ Object
Returns the value of attribute log.
143 144 145 |
# File 'lib/prc.rb', line 143 def log @log end |
.log_file ⇒ Object
Returns the value of attribute log_file.
144 145 146 |
# File 'lib/prc.rb', line 144 def log_file @log_file end |
.pdata_path ⇒ Object
Returns the value of attribute pdata_path.
144 145 146 |
# File 'lib/prc.rb', line 144 def pdata_path @pdata_path end |
Class Method Details
.app_name ⇒ Object
154 155 156 157 |
# File 'lib/prc.rb', line 154 def app_name self.app_name = 'Lorj' unless @app_name @app_name end |
.app_name=(v) ⇒ Object
181 182 183 |
# File 'lib/prc.rb', line 181 def app_name=(v) @app_name = v unless @app_name end |
.controller_path ⇒ Object
221 222 223 |
# File 'lib/prc.rb', line 221 def controller_path File.(File.join(@lib_path, 'providers')) end |
.dcl_fail(msg, *p) ⇒ Object
Internal heap management to help controller developper to identify issue.
245 246 247 248 249 250 |
# File 'lib/logging.rb', line 245 def dcl_fail(msg, *p) msg = "Declaration error:\n" + msg msg += format("\nSee at %s", PrcLib.model.heap[0]) if PrcLib.model.heap.is_a?(Array) runtime_fail(msg, *p) end |
.debug(message, *p) ⇒ Object
Log to STDOUT and Log file and DEBUG class message
227 228 229 230 |
# File 'lib/logging.rb', line 227 def debug(, *p) log_object.debug(format(, *p)) nil end |
.dir_exists?(path) ⇒ Boolean
Check if dir exists and is fully accessible (rwx)
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/prc.rb', line 110 def self.dir_exists?(path) return false unless File.exist?(path) unless File.directory?(path) msg = format("'%s' is not a directory. Please fix it.", path) fatal_error(1, msg) end unless File.readable?(path) && File.writable?(path) && File.executable?(path) msg = format("'%s is not a valid directory. "\ 'Check permissions and fix it.', path) fatal_error(1, msg) end true end |
.ensure_dir_exists(path) ⇒ Object
ensure dir exists and is fully accessible (rwx)
135 136 137 138 139 |
# File 'lib/prc.rb', line 135 def self.ensure_dir_exists(path) FileUtils.mkpath(path) unless dir_exists?(path) rescue => e fatal_error(1, e.) end |
.error(message, *p) ⇒ Object
Log to STDOUT and Log file and ERROR class message
239 240 241 242 |
# File 'lib/logging.rb', line 239 def error(, *p) log_object.error(format(, *p)) nil end |
.fatal(rc, message, *p) ⇒ Object
Log to STDOUT and Log file and FATAL class message then exit the application with a return code. fatal retrieve the caller list of functions and save it to the log file if the exception class is given. The exception class should provide message and backtrace.
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/logging.rb', line 262 def fatal(rc, , *p) e = nil if p.length > 0 && p[-1].is_a?(Exception) e = p[-1] p.pop end log_object.fatal(format(, *p), e) puts format('Issues found. Please fix it and retry. Process aborted. '\ "See details in log file '%s'.", PrcLib.log_file) exit rc end |
.fatal_error(rc, msg) ⇒ Object
129 130 131 132 |
# File 'lib/prc.rb', line 129 def self.fatal_error(rc, msg) fail msg if log.nil? log.fatal(rc, msg) end |
.high_level_msg(message, *p) ⇒ Object
Not DEBUG and not INFO. Just printed to the output.
287 288 289 290 |
# File 'lib/logging.rb', line 287 def high_level_msg(, *p) print(format(, *p)) if log_object.level > 1 nil end |
.info(message, *p) ⇒ Object
Log to STDOUT and Log file and INFO class message
221 222 223 224 |
# File 'lib/logging.rb', line 221 def info(, *p) log_object.info(format(, *p)) nil end |
.lib_path=(v) ⇒ Object
215 216 217 |
# File 'lib/prc.rb', line 215 def lib_path=(v) @lib_path = v if @lib_path.nil? end |
.log_object ⇒ Object
Create a Logging object if missing and return it. Used internally by other functions
206 207 208 209 210 211 212 |
# File 'lib/logging.rb', line 206 def log_object if PrcLib.log.nil? PrcLib.log = PrcLib::Logging.new else PrcLib.log end end |
.message(message, *p) ⇒ Object
Print out a message, not logged in the log file. This message is printed out systematically as not taking care of logger level.
216 217 218 |
# File 'lib/logging.rb', line 216 def (, *p) log_object.unknown(format(, *p)) end |
.model ⇒ Object
TODO: Low. Be able to support multiple model.
186 187 188 189 |
# File 'lib/prc.rb', line 186 def model @model = Lorj::Model.new if @model.nil? @model end |
.process_path ⇒ Object
225 226 227 |
# File 'lib/prc.rb', line 225 def process_path File.join(@lib_path, 'core_process') end |
.runtime_fail(msg, *p) ⇒ Object
fail extended with format.
253 254 255 |
# File 'lib/logging.rb', line 253 def runtime_fail(msg, *p) fail Lorj::PrcError.new, format(msg, *p) end |
.state(message, *p) ⇒ Object
Print the message to the same line.
280 281 282 283 284 |
# File 'lib/logging.rb', line 280 def state(, *p) print(format("%s%s ...\r", ANSI.clear_line, format(, *p))) if log_object.level <= Logger::INFO nil end |
.warning(message, *p) ⇒ Object
Log to STDOUT and Log file and WARNING class message
233 234 235 236 |
# File 'lib/logging.rb', line 233 def warning(, *p) log_object.warn(format(, *p)) nil end |