Class: Carioca::Services::Output::Provider

Inherits:
Object
  • Object
show all
Includes:
FormatsMapping
Defined in:
lib/carioca/services/output.rb

Constant Summary collapse

MODE =
%i[mono dual log].freeze
@@alias =
ALIAS.dup
@@colors =
COLORS.dup
@@emoji =
EMOJI.dup

Constants included from FormatsMapping

FormatsMapping::ALIAS, FormatsMapping::COLORS, FormatsMapping::EMOJI, FormatsMapping::LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level: :debug, mode: :mono, emoji: true, colors: true, target: :stdout) ⇒ Provider

constructor



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/carioca/services/output.rb', line 112

def initialize(level: :debug, mode: :mono, emoji: true, colors: true, target: :stdout)
  registry = Carioca::Registry.get
  @logger = registry.get_service name: :logger
  @i18n = registry.get_service name: :i18n
  @debug = Carioca::Registry.config.debug?
  self.level = level
  @target = target
  @mode = mode
  @emoji = check_unicode_term ? emoji : false
  @color = colors
  set = []
  set.push mode
  set.push :emoji if @emoji
  set.push :colors if @color
  set.push target
  @logger.debug('Carioca->Output') { @i18n.t('output.load.context', confset: set.to_s) } if @debug
  raise "Unknown output mode : #{@mode}" unless MODE.include? @mode
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



69
70
71
# File 'lib/carioca/services/output.rb', line 69

def color
  @color
end

#emojiObject

Returns the value of attribute emoji.



69
70
71
# File 'lib/carioca/services/output.rb', line 69

def emoji
  @emoji
end

#modeObject

Returns the value of attribute mode.



69
70
71
# File 'lib/carioca/services/output.rb', line 69

def mode
  @mode
end

#targetObject

Returns the value of attribute target.



69
70
71
# File 'lib/carioca/services/output.rb', line 69

def target
  @target
end

Instance Method Details

#add_alias(newalias:, level:) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/carioca/services/output.rb', line 102

def add_alias(newalias:, level:)
  raise 'Alias must be a Symbol' unless newalias.instance_of?(Symbol)
  raise "Bad Level : #{level}" unless LEVELS.include? level

  self.class.define_method(newalias) do |message, session = nil|
    display({ level: newalias, message:, session: })
  end
end

#check_unicode_termBoolean

check if unicode must be used with term ENV

Returns:

  • (Boolean)


155
156
157
158
159
160
# File 'lib/carioca/services/output.rb', line 155

def check_unicode_term
  return false unless ENV.include? 'TERM'

  ENV.values_at('LC_ALL', 'LC_CTYPE',
                'LANG').compact.first.include?('UTF-8') && ENV.values_at('TERM').first.include?('xterm')
end

#display(level:, message:, session:, source:) ⇒ Object

abstract method for log wrapper

Parameters:



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/carioca/services/output.rb', line 167

def display(level:, message:, session:, source:)
  message << " (#{session})" if session
  save = message.dup
  target_level = @@alias.keys.include?(level) ? @@alias[level] : level
  if @active_levels.include? target_level
    if @color
      pastel = ::Pastel.new
      message = pastel.send @@colors[level], message
    end
    if @@emoji.include? level
      pattern = @emoji ? @@emoji[level][:value] : @@emoji[level][:alt]
      pattern = "#{pattern} #{@@emoji[level][:text]}" if @@emoji[level].include?(:text) && !@emoji
      message = "#{pattern} #{message}" unless pattern.empty?
    end
    if @mode == :dual
      save = "#{@@emoji[level][:text]} #{save}" if !LEVELS.include?(level) && (@@emoji[level].include? :text)
      block = proc { save }
      @logger.send target_level, source, &block
    end
    if @target == :stderr
      warn message if (@mode == :mono) || (@mode == :dual)
    elsif (@mode == :mono) || (@mode == :dual)
      $stdout.puts message
    end
  end
end

#generate_sessionString

build a session number

Returns:

  • (String)

    Session number



133
134
135
# File 'lib/carioca/services/output.rb', line 133

def generate_session
  "#{Time.now.to_i}#{rand(999)}"
end

#levelSymbol

getter for the current level

Returns:

  • (Symbol)

    level



139
140
141
# File 'lib/carioca/services/output.rb', line 139

def level
  @active_levels.first
end

#level=(level) ⇒ Object

virtual setter for level, set the current level

Parameters:

  • level (Symbol)

Raises:

  • a badLevel in case of bad level



146
147
148
149
150
151
# File 'lib/carioca/services/output.rb', line 146

def level=(level)
  raise "Bad Level : #{level}" unless LEVELS.include? level

  @active_levels = LEVELS.dup
  @active_levels.shift(LEVELS.index(level))
end

#map_color(color:, analias:) ⇒ Object



88
89
90
91
92
93
# File 'lib/carioca/services/output.rb', line 88

def map_color(color:, analias:)
  raise 'Color must be a Symbol' unless color.instance_of?(Symbol)
  raise "Missing alias : #{analias}" unless LEVELS.include? analias

  @@alias[analias] = color
end

#map_emoji(emoji:, analias:) ⇒ Object



95
96
97
98
99
100
# File 'lib/carioca/services/output.rb', line 95

def map_emoji(emoji:, analias:)
  raise 'Emoji must be a String' unless color.instance_of?(String)
  raise "Missing alias : #{analias}" unless LEVELS.include? analias

  @@alias[analias] = emoji
end