Module: PrcLib

Defined in:
lib/prc.rb,
lib/prc.rb,
lib/logging.rb

Overview

Defines module parameters.

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.



162
163
164
# File 'lib/prc.rb', line 162

def app_defaults
  @app_defaults
end

.core_levelObject

Returns the value of attribute core_level.



161
162
163
# File 'lib/prc.rb', line 161

def core_level
  @core_level
end

.levelObject

Returns the value of attribute level.



162
163
164
# File 'lib/prc.rb', line 162

def level
  @level
end

.lib_pathObject

Returns the value of attribute lib_path.



162
163
164
# File 'lib/prc.rb', line 162

def lib_path
  @lib_path
end

.logObject

Returns the value of attribute log.



161
162
163
# File 'lib/prc.rb', line 161

def log
  @log
end

Class Method Details

.app_nameObject

Attribute app_name

app_name is set to ‘lorj’ if not set.



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

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



180
181
182
# File 'lib/prc.rb', line 180

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

.controller_pathObject

Read Attribute setting for default library controller path



324
325
326
# File 'lib/prc.rb', line 324

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



216
217
218
219
220
221
# 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) ⇒ Object

Attribute data_path setting

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



227
228
229
230
231
232
233
234
# 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) ⇒ 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)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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) ⇒ Object

ensure dir exists and is fully accessible (rwx)



150
151
152
153
154
# 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

.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



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

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.



282
283
284
285
# 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) ⇒ 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.



278
279
280
281
282
283
# 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) ⇒ Object

Attribute log_file setting

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



289
290
291
292
293
294
295
# 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_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



240
241
242
243
# File 'lib/prc.rb', line 240

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



190
191
192
193
194
# 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) ⇒ Object

Attribute pdata_path setting

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



200
201
202
203
204
205
206
207
208
# 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_pathObject

Read Attribute setting for default library model/process path



329
330
331
# File 'lib/prc.rb', line 329

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

.processes(p = nil) ⇒ Object

PrcLib.processes



248
249
250
251
# File 'lib/prc.rb', line 248

def processes(p = nil)
  @processes = p unless p.nil?
  @processes
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.



275
276
277
278
279
# 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) ⇒ 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