class Messagex::Messagex

終了ステータス管理、ログ機能管理のクラス

Attributes

logger[R]

@return [Loggerx] Loggerxクラスのインスタンス

Public Class Methods

new(initial_exitcode, initial_num, debug=:warn, logger=nil, logfname=nil) click to toggle source

初期化

@param initial_exitcode [String] 最初に登録する終了ステータスの識別名 @param initial_num [Integer] 登録する終了ステータスの初期値 @param debug [Symbol] ログ機能のログレベル指定 `:debug`、`:verbose`、その他 @param logger [Loggerx] 複数のLoggerクラスのインスタンスを持つことが出来るLoggerxクラスのインスタンス @param logfname [String] ログの出力先ファイル名 @note 引数debugの値はLoggerクラスのログレベル設定とは指定方法が異なる

# File lib/messagex.rb, line 47
def initialize(initial_exitcode, initial_num, debug=:warn, logger=nil, logfname=nil)
  @exit_code = {}
  set_initial_exitcode(initial_exitcode, initial_num)

  if logger
    @logger = logger
  else
    log_fname = (!logfname.nil? && !logfname.empty?) ? logfname : "log.txt"
    @logger = Loggerx.new(log_fname)
    #    Logger::WARN , Logger::INFO

    case debug
    when :debug
      @logger.level = Logger::DEBUG
    # UNKNOWN > FATAL > ERROR > WARN > INFO > DEBUG
    when :verbose
      @logger.level = Logger::INFO
    else
      @logger.level = Logger::WARN
    end

    #    @logger.datetime_format = '%Y-%m-%d %H:%M:%S'
    @logger.datetime_format = ""
    # logger.formatter = proc do |severity, datetime, progname, msg|
    #   ">>>>>> #{msg}\n"
    # end
    @logger.formatter = proc do |_severity, _datetime, _progname, msg|
      "#{msg}\n"
    end
  end

  register_exitcodes
end

Public Instance Methods

add_exitcode(str) click to toggle source

終了ステータスの値の自動割り当て

@param str [String] 値の自動割り当て対象の終了ステータスの識別名 @return [Integer] 自動割り当てされた終了ステータスの値

# File lib/messagex.rb, line 129
def add_exitcode(str)
  return if @exit_code[str]

  num = (@cur_exit_code + 1)
  @exit_code[str] = num
  @cur_exit_code = num
end
ec(name) click to toggle source

識別名で指定された終了ステータスを得る

@param name [String] 登録された終了ステータスの識別名 @return [Integer] 終了ステータス

# File lib/messagex.rb, line 108
def ec(name)
  @exit_code[name]
end
output_debug(msg) click to toggle source

ログレベルが DEBUG のメッセージを出力

@param msg [String] @return [void]

# File lib/messagex.rb, line 168
def output_debug(msg)
  if @logger
    @logger.debug(msg)
  else
    STDOUT.puts(msg)
  end
end
output_error(msg) click to toggle source

ログレベルが ERROR のメッセージを出力

@param msg [String] @return [void]

# File lib/messagex.rb, line 142
def output_error(msg)
  if @logger
    @logger.error(msg)
  else
    error(msg)
  end
end
output_exception(exception) click to toggle source

例外処理

@param exception [Exception] 発生した例外 @return [void]

# File lib/messagex.rb, line 207
def output_exception(exception)
  output_fatal(exception.class)
  output_fatal(exception.message)
  output_fatal(exception.backtrace.join("\n"))
end
output_fatal(msg) click to toggle source

ログレベルが FATAL のメッセージを出力

@param msg [String] @return [void]

# File lib/messagex.rb, line 155
def output_fatal(msg)
  if @logger
    @logger.fatal(msg)
  else
    STDOUT.puts(msg)
  end
end
output_info(msg) click to toggle source

ログレベルが INFO のメッセージを出力

@param msg [String] @return [void]

# File lib/messagex.rb, line 181
def output_info(msg)
  if @logger
    @logger.info(msg)
  else
    STDOUT.puts(msg)
  end
end
output_warn(msg) click to toggle source

ログレベルが WARN のメッセージを出力

@param msg [String] @return [void]

# File lib/messagex.rb, line 194
def output_warn(msg)
  if @logger
    @logger.warn(msg)
  else
    STDOUT.puts(msg)
  end
end
register_exc() click to toggle source

I/O関連例外処理管理クラスのインスタンスの登録

@return [void]

# File lib/messagex.rb, line 99
def register_exc
  @exc_inst = Exc.new(self)
end
register_exitcodes() click to toggle source

Messagexクラスで利用する終了ステータスの登録

@return [void]

# File lib/messagex.rb, line 85
def register_exitcodes
  add_exitcode("EXIT_CODE_CANNOT_READ_FILE")
  add_exitcode("EXIT_CODE_CANNOT_WRITE_FILE")
  add_exitcode("EXIT_CODE_CANNOT_FIND_DIRECTORY")
  add_exitcode("EXIT_CODE_CANNOT_CHANGE_DIRECTORY")
  add_exitcode("EXIT_CODE_CANNOT_MAKE_DIRECTORY")
  add_exitcode("EXIT_CODE_CANNOT_OPEN_FILE")
  add_exitcode("EXIT_CODE_CANNOT_COPY_FILE")
end
set_initial_exitcode(name, num) click to toggle source

終了ステータスの最初の登録

@param name [String] 登録する終了ステータスの識別名 @param num [Integer] 登録する終了ステータスの値 @return [void]

# File lib/messagex.rb, line 118
def set_initial_exitcode(name, num)
  @exit_code ||= {}
  @exit_code[name] = num
  @cur_exit_code = num
end