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

    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

  • 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. This setting influences default settings for: PrcLib.log_file

    By default: ~/.<app_name>

    ex:

    PrcLib.data_path = File.join('/etc', 'myapp')
    
  • PrcLib.app_name

    Define the application name. By default ‘lorj’. This setting influences default settings for: PrcLib.data_path, PrcLib.pdata_path and PrcLib.log_file

    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.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 (relative or absolute path) instead of default one. By default, defined as #PrcLib.data_path/#PrcLib.app_name.log

    Ex:

    PrcLib.log_file = "mylog.file.log" # Relative path to the file
    
  • 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.



155
156
157
# File 'lib/prc.rb', line 155

def app_defaults
  @app_defaults
end

.core_levelObject

Returns the value of attribute core_level.



154
155
156
# File 'lib/prc.rb', line 154

def core_level
  @core_level
end

.levelObject

Returns the value of attribute level.



155
156
157
# File 'lib/prc.rb', line 155

def level
  @level
end

.lib_pathObject

Returns the value of attribute lib_path.



155
156
157
# File 'lib/prc.rb', line 155

def lib_path
  @lib_path
end

.logObject

Returns the value of attribute log.



154
155
156
# File 'lib/prc.rb', line 154

def log
  @log
end

Class Method Details

.app_nameObject

Attribute app_name

app_name is set to ‘lorj’ if not set.



164
165
166
167
# File 'lib/prc.rb', line 164

def app_name
  self.app_name = 'lorj' unless @app_name
  @app_name
end

.app_name=(v) ⇒ Object

Attribute app_name setting

You can set the application name only one time



173
174
175
# File 'lib/prc.rb', line 173

def app_name=(v)
  @app_name = v unless @app_name
end

.controller_pathObject

Read Attribute setting for default library controller path



309
310
311
# File 'lib/prc.rb', line 309

def controller_path
  File.expand_path(File.join(@lib_path,  'providers'))
end

.data_pathObject

Attribute data_path

Path to the application data.

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



209
210
211
212
213
214
# File 'lib/prc.rb', line 209

def data_path
  return @data_path unless @data_path.nil?

  self.data_path = File.join('~', '.' + app_name)
  @data_path
end

.data_path=(v) ⇒ Object

Attribute data_path setting

If path doesn’t exist, it will be created.



220
221
222
223
224
225
226
227
# File 'lib/prc.rb', line 220

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) ⇒ Object

Internal heap management to help controller developper to identify issue.



236
237
238
239
240
241
# 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) ⇒ Object

Log to STDOUT and Log file and DEBUG class message



218
219
220
221
# File 'lib/logging.rb', line 218

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:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/prc.rb', line 121

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)



146
147
148
149
150
# File 'lib/prc.rb', line 146

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



230
231
232
233
# File 'lib/logging.rb', line 230

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.



253
254
255
256
257
258
259
260
261
262
263
# 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

.fatal_error(rc, msg) ⇒ Object



140
141
142
143
# File 'lib/prc.rb', line 140

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.



278
279
280
281
# File 'lib/logging.rb', line 278

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



212
213
214
215
# File 'lib/logging.rb', line 212

def info(message, *p)
  log_object.info(format(message, *p))
  nil
end

.log_fileObject

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.



263
264
265
266
267
268
# File 'lib/prc.rb', line 263

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) ⇒ Object

Attribute log_file setting

It ensures that the path to the log file is created.



274
275
276
277
278
279
280
# File 'lib/prc.rb', line 274

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_objectObject

Create a Logging object if missing and return it. Used internally by other functions



193
194
195
196
# File 'lib/logging.rb', line 193

def log_object
  PrcLib.log = PrcLib::Logging.new if PrcLib.log.nil?
  PrcLib.log
end

.log_object=(logger) ⇒ Object



198
199
200
201
202
203
# 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) ⇒ Object

Print out a message, not logged in the log file. This message is printed out systematically as not taking care of logger level.



207
208
209
# File 'lib/logging.rb', line 207

def message(message, *p)
  log_object.unknown(format(message, *p))
end

.modelObject

Lorj::Model object access. If the object doesn’t exist, it will be created



233
234
235
236
# File 'lib/prc.rb', line 233

def model
  @model = Lorj::Model.new if @model.nil?
  @model
end

.pdata_pathObject

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



183
184
185
186
187
# File 'lib/prc.rb', line 183

def pdata_path
  return @pdata_path unless @pdata_path.nil?
  self.pdata_path = File.join('~', '.config', app_name)
  @pdata_path
end

.pdata_path=(v) ⇒ Object

Attribute pdata_path setting

If path doesn’t exist, it will be created with 700 rights (Unix).



193
194
195
196
197
198
199
200
201
# File 'lib/prc.rb', line 193

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_pathObject

Read Attribute setting for default library model/process path



314
315
316
# File 'lib/prc.rb', line 314

def process_path
  File.expand_path(File.join(@lib_path, 'core_process'))
end

.runtime_fail(msg, *p) ⇒ Object

fail extended with format.



244
245
246
# File 'lib/logging.rb', line 244

def runtime_fail(msg, *p)
  fail Lorj::PrcError.new, format(msg, *p)
end

.state(message, *p) ⇒ Object

Print the message to the same line.



271
272
273
274
275
# File 'lib/logging.rb', line 271

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



224
225
226
227
# File 'lib/logging.rb', line 224

def warning(message, *p)
  log_object.warn(format(message, *p))
  nil
end