module PrcLib
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:
-
Set a logger object. By default,
Lorj
creates a Lorj::Logging object which enhance a double logging system (output and file at the same time)You can set your own logger system. This logger instance requires to have following features:
-
functions : unknown/warn/fatal/error/debug/info(message)
-
Is level functions: info?/debug?/warn?/error?/fatal? NOTE: Those functions are currently not used but may be used in the future
-
attribute : level
This object is automatically created as soon as a message is printed out
-
-
Initialize lorj debug level. from 0 to 5.
ex:
PrcLib.core_level = 4
-
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')
-
Define the data local directory. This setting influences default settings for:
PrcLib.log_file
By default: ~/.<app_name>
ex:
PrcLib.data_path = File.join('/etc', 'myapp')
-
Define the application name. By default 'lorj'. This setting influences default settings for:
PrcLib.data_path
,PrcLib.pdata_path
andPrcLib.log_file
ex:
PrcLib.app_name = 'myapp'
-
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.level
logger level used. It can be updated at runtime.Ex:
PrcLib.level = Logger::FATAL
-
Model loaded.
-
Processes loaded.
-
Initialize a log file name (relative or absolute path) instead of default one. By default, defined as #{data_path}/#{app_name}.log
Ex:
PrcLib.log_file = "mylog.file.log" # Relative path to the file
-
Provides the default controller path.
-
Provides the default process path.
Defines module parameters.
Attributes
Public Class Methods
Check if dir exists and is fully accessible (rwx)
# File lib/prc.rb, line 125 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 and is fully accessible (rwx)
# File lib/prc.rb, line 150 def self.ensure_dir_exists(path) FileUtils.mkpath(path) unless dir_exists?(path) rescue => e fatal_error(1, e.message) end
# File lib/prc.rb, line 144 def self.fatal_error(rc, msg) fail msg if log.nil? log.fatal(rc, msg) end
Public Instance Methods
Attribute app_defaults
Used to define where the application defaults.yaml is located.
# File lib/prc.rb, line 259 def app_defaults=(v) return if @app_defaults v = File.join(File.dirname(__FILE__), v) unless v.include?('/') @app_defaults = File.expand_path(v) end
Attribute app_name
setting
You can set the application name only one time
# File lib/prc.rb, line 180 def app_name=(v) @app_name = v unless @app_name end
Read Attribute setting for default library controller path
# File lib/prc.rb, line 324 def controller_path File.expand_path(File.join(@lib_path, 'providers')) end
Attribute data_path
setting
If path doesn't exist, it will be created.
# File lib/prc.rb, line 227 def data_path=(v) @data_path = File.expand_path(v) unless @data_path begin ensure_dir_exists(@data_path) rescue => e fatal_error(1, e.message) end end
Internal heap management to help controller developper to identify issue.
# File lib/logging.rb, line 236 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
Log to STDOUT and Log file and DEBUG class message
# File lib/logging.rb, line 218 def debug(message, *p) log_object.debug(format(message, *p)) nil end
Log to STDOUT and Log file and ERROR class message
# File lib/logging.rb, line 230 def error(message, *p) log_object.error(format(message, *p)) nil end
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.
# File lib/logging.rb, line 253 def fatal(rc, message, *p) if p.length > 0 && p[-1].is_a?(Exception) e = p[-1] p.pop message = format("%s\n%s\n%s", message, e.message, e.backtrace.join("\n")) end log_object.fatal(format(message, *p)) puts format('Issues found. Please fix it and retry. Process aborted. '\ "See details in log file '%s'.", PrcLib.log_file) exit rc end
Not DEBUG and not INFO. Just printed to the output.
# File lib/logging.rb, line 282 def high_level_msg(message, *p) print(format(message, *p)) if log_object.level > 1 nil end
Log to STDOUT and Log file and INFO class message
# File lib/logging.rb, line 212 def info(message, *p) log_object.info(format(message, *p)) nil end
# File lib/logging.rb, line 270 def level log_object.level end
# File lib/logging.rb, line 265 def level=(level) log_object.level = level nil end
log_file
module attribute
by default, log_file
is nil. The user can define a log_file
name or path The path is created (if possible) as soon a log_file
is set. The file name is created by the logging class.
args
-
+log file+ : absolute or relative path to a log file.
# File lib/prc.rb, line 278 def log_file return @log_file unless @log_file.nil? self.log_file = File.join(data_path, app_name + '.log') @log_file end
Attribute log_file
setting
It ensures that the path to the log file is created.
# File lib/prc.rb, line 289 def log_file=(v) file = File.basename(v) dir = File.dirname(File.expand_path(v)) ensure_dir_exists(dir) @log_file = File.join(dir, file) end
Create a Logging
object if missing and return it. Used internally by other functions
# File lib/logging.rb, line 193 def log_object PrcLib.log = PrcLib::Logging.new if PrcLib.log.nil? PrcLib.log end
# File lib/logging.rb, line 198 def log_object=(logger) return PrcLib.log unless logger.is_a?(Logger) PrcLib.log = logger if PrcLib.log.nil? PrcLib.log end
Print out a message, not logged in the log file. This message is printed out systematically as not taking care of logger level.
# File lib/logging.rb, line 207 def message(message, *p) log_object.unknown(format(message, *p)) end
Lorj::Model
object access. If the object doesn't exist, it will be created
# File lib/prc.rb, line 240 def model @model = Lorj::Model.new if @model.nil? @model end
Attribute pdata_path
Path to a private data, like encrypted keys.
It uses pdata_path
= to set the default path if not set ~/.config/#{app_name}
# File lib/prc.rb, line 190 def pdata_path return @pdata_path unless @pdata_path.nil? self.pdata_path = File.join('~', '.config', app_name) @pdata_path end
Attribute pdata_path
setting
If path doesn't exist, it will be created with 700 rights (Unix).
# File lib/prc.rb, line 200 def pdata_path=(v) @pdata_path = File.expand_path(v) unless @pdata_path begin ensure_dir_exists(@pdata_path) FileUtils.chmod(0700, @pdata_path) # no-op on windows rescue => e fatal_error(1, e.message) end end
Read Attribute setting for default library model/process path
# File lib/prc.rb, line 329 def process_path File.expand_path(File.join(@lib_path, 'core_process')) end
# File lib/prc.rb, line 248 def processes(p = nil) @processes = p unless p.nil? @processes end
fail extended with format.
# File lib/logging.rb, line 244 def runtime_fail(msg, *p) fail Lorj::PrcError.new, format(msg, *p) end
Print the message to the same line.
# File lib/logging.rb, line 275 def state(message, *p) print(format("%s%s ...\r", ANSI.clear_line, format(message, *p))) if log_object.level <= Logger::INFO nil end
Log to STDOUT and Log file and WARNING class message
# File lib/logging.rb, line 224 def warning(message, *p) log_object.warn(format(message, *p)) nil end