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

Class Method Summary collapse

Class Attribute Details

.app_defaultsObject

Returns the value of attribute app_defaults.



144
145
146
# File 'lib/prc.rb', line 144

def app_defaults
  @app_defaults
end

.core_levelObject

Returns the value of attribute core_level.



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

def core_level
  @core_level
end

.data_pathObject

Returns the value of attribute data_path.



144
145
146
# File 'lib/prc.rb', line 144

def data_path
  @data_path
end

.levelObject

Returns the value of attribute level.



144
145
146
# File 'lib/prc.rb', line 144

def level
  @level
end

.logObject

Returns the value of attribute log.



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

def log
  @log
end

.log_fileObject

Returns the value of attribute log_file.



144
145
146
# File 'lib/prc.rb', line 144

def log_file
  @log_file
end

.pdata_pathObject

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_nameObject



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_pathObject



221
222
223
# File 'lib/prc.rb', line 221

def controller_path
  File.expand_path(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(message, *p)
  log_object.debug(format(message, *p))
  nil
end

.dir_exists?(path) ⇒ Boolean

Check if dir exists and is fully accessible (rwx)

Returns:



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.message)
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(message, *p)
  log_object.error(format(message, *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, message, *p)
  e = nil
  if p.length > 0 && p[-1].is_a?(Exception)
    e = p[-1]
    p.pop
  end
  log_object.fatal(format(message, *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(message, *p)
  print(format(message, *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(message, *p)
  log_object.info(format(message, *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_objectObject

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 message(message, *p)
  log_object.unknown(format(message, *p))
end

.modelObject

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_pathObject



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(message, *p)
  print(format("%s%s ...\r", ANSI.clear_line,
               format(message, *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(message, *p)
  log_object.warn(format(message, *p))
  nil
end