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:

Defines module parameters.

Attributes

app_defaults[R]
core_level[RW]
level[R]
lib_path[R]
log[RW]

Public Class Methods

dir_exists?(path) click to toggle source

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(path) click to toggle source

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
fatal_error(rc, msg) click to toggle source
# 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

app_defaults=(v) click to toggle source

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
app_name() click to toggle source

Attribute app_name

app_name is set to 'lorj' if not set.

# File lib/prc.rb, line 171
def app_name
  self.app_name = 'lorj' unless @app_name
  @app_name
end
app_name=(v) click to toggle source

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
controller_path() click to toggle source

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
data_path() click to toggle source

Attribute data_path

Path to the application data.

It uses data_path= to set the default path if not set ~/.#{app_name}

# File lib/prc.rb, line 216
def data_path
  return @data_path unless @data_path.nil?

  self.data_path = File.join('~', '.' + app_name)
  @data_path
end
data_path=(v) click to toggle source

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
dcl_fail(msg, *p) click to toggle source

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
debug(message, *p) click to toggle source

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
error(message, *p) click to toggle source

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
fatal(rc, message, *p) click to toggle source

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
high_level_msg(message, *p) click to toggle source

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
info(message, *p) click to toggle source

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
level() click to toggle source
# File lib/logging.rb, line 270
def level
  log_object.level
end
level=(level) click to toggle source
# File lib/logging.rb, line 265
def level=(level)
  log_object.level = level
  nil
end
lib_path=(v) click to toggle source

Attribute lib_path setting

initialize the Lorj library path Used by Lorj module declaration See lib/lorj.rb

This setting cannot be updated later.

# File lib/prc.rb, line 315
def lib_path=(v)
  @lib_path = v if @lib_path.nil?
end
log_file() click to toggle source

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
log_file=(v) click to toggle source

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
log_object() click to toggle source

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
log_object=(logger) click to toggle source
# 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
message(message, *p) click to toggle source

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
model() click to toggle source

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
pdata_path() click to toggle source

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
pdata_path=(v) click to toggle source

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
process_path() click to toggle source

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
processes(p = nil) click to toggle source

PrcLib.processes

# File lib/prc.rb, line 248
def processes(p = nil)
  @processes = p unless p.nil?
  @processes
end
runtime_fail(msg, *p) click to toggle source

fail extended with format.

# File lib/logging.rb, line 244
def runtime_fail(msg, *p)
  fail Lorj::PrcError.new, format(msg, *p)
end
state(message, *p) click to toggle source

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
warning(message, *p) click to toggle source

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