Class: LeapCli::LeapLogger

Inherits:
Object show all
Defined in:
lib/leap_cli/log.rb

Constant Summary collapse

FILE_TITLES =

these are log titles typically associated with files

%w(updated created removed missing nochange loading)
IMPORTANT =

TODO: use these

0
INFO =
1
DEBUG =
2
TRACE =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLeapLogger

Returns a new instance of LeapLogger.



65
66
67
68
69
70
71
# File 'lib/leap_cli/log.rb', line 65

def initialize()
  @log_level = 1
  @indent_level = 0
  @log_file = nil
  @log_output_stream = nil
  @log_in_color = true
end

Instance Attribute Details

#indent_levelObject

Returns the value of attribute indent_level.



63
64
65
# File 'lib/leap_cli/log.rb', line 63

def indent_level
  @indent_level
end

#log_fileObject

Returns the value of attribute log_file.



62
63
64
# File 'lib/leap_cli/log.rb', line 62

def log_file
  @log_file
end

#log_in_colorObject

Returns the value of attribute log_in_color.



63
64
65
# File 'lib/leap_cli/log.rb', line 63

def 
  @log_in_color
end

#log_levelObject

Returns the value of attribute log_level.



63
64
65
# File 'lib/leap_cli/log.rb', line 63

def log_level
  @log_level
end

#log_output_streamObject (readonly)

Returns the value of attribute log_output_stream.



62
63
64
# File 'lib/leap_cli/log.rb', line 62

def log_output_stream
  @log_output_stream
end

Instance Method Details

#colorize(str, color, style = nil) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/leap_cli/log.rb', line 226

def colorize(str, color, style=nil)
  codes = [FG_COLORS[color] || FG_COLORS[:default]]
  if style
    codes << EFFECTS[style] || EFFECTS[:nothing]
  end
  if str.is_a?(String)
    ["\033[%sm" % codes.join(';'), str, NO_COLOR].join
  elsif str.is_a?(Array)
    str.map { |s|
      ["\033[%sm" % codes.join(';'), s, NO_COLOR].join
    }
  end
end

#debug(*args) ⇒ Object



177
178
179
# File 'lib/leap_cli/log.rb', line 177

def debug(*args)
  self.log(3, *args)
end

#log(*args) ⇒ Object

master logging function.

arguments can be a String, Integer, Symbol, or Hash, in any order.

  • String: treated as the message to log.

  • Integer: the log level (0, 1, 2)

  • Symbol: the prefix title to colorize. may be one of

    :error, :warning, :info, :updated, :created, :removed, :no_change, :missing
  • Hash: a hash of options.

    :wrap -- if true, appy intend to each line in message.
    :color -- apply color to message or prefix
    :style -- apply style to message or prefix
    


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/leap_cli/log.rb', line 97

def log(*args)
  level   = args.grep(Integer).first || 1
  title   = args.grep(Symbol).first
  message = args.grep(String).first
  options = args.grep(Hash).first || {}
  host    = options[:host]
  if title
    title = title.to_s
  end
  if @log_level < level || (title.nil? && message.nil?)
    return
  end

  #
  # transform absolute path names
  #
  if title && FILE_TITLES.include?(title) && message =~ /^\//
    message = LeapCli::Path.relative_path(message)
  end

  #
  # apply filters
  # LogFilter will not be defined if no platform was loaded.
  #
  if defined?(LeapCli::LogFilter)
    if title
      title, filter_flags = LogFilter.apply_title_filters(title)
    else
      message, filter_flags = LogFilter.apply_message_filters(message)
      return if message.nil?
    end
    options = options.merge(filter_flags)
  end

  #
  # set line prefix
  #
  if (host)
    host = host.split('.').first
  end
  prefix = prefix_str(host, title)

  #
  # write to the log file, always
  #
  log_raw(:log, nil, prefix) { message }

  #
  # log to stdout, maybe in color
  #
  if @log_in_color
    if options[:wrap]
      message = message.split("\n")
    end
    if options[:color]
      if host
        host = colorize(host, options[:color], options[:style])
      elsif title
        title = colorize(title, options[:color], options[:style])
      else
        message = colorize(message, options[:color], options[:style])
      end
    elsif title
      title = colorize(title, :cyan, :bold)
    end
    # new colorized prefix:
    prefix = prefix_str(host, title)
  end
  log_raw(:stdout, options[:indent], prefix) { message }

  #
  # run block indented, if given
  #
  if block_given?
    @indent_level += 1
    yield
    @indent_level -= 1
  end
end

#log_raw(mode, indent = nil, prefix = nil, &block) ⇒ Object

Add a raw log entry, without any modifications (other than indent). Content to be logged is yielded by the block. Block may be either a string or array of strings.

if mode == :stdout, output is sent to STDOUT. if mode == :log, output is sent to log file, if present.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/leap_cli/log.rb', line 189

def log_raw(mode, indent=nil, prefix=nil, &block)
  # NOTE: using 'print' produces better results than 'puts'
  # when multiple threads are logging)
  if mode == :log
    if @log_output_stream
      messages = [yield].compact.flatten
      if messages.any?
        timestamp = Time.now.strftime("%b %d %H:%M:%S")
        messages.each do |message|
          message = message.rstrip
          next if message.empty?
          @log_output_stream.print("#{timestamp} #{prefix} #{message}\n")
        end
        @log_output_stream.flush
      end
    end
  elsif mode == :stdout
    messages = [yield].compact.flatten
    if messages.any?
      indent ||= @indent_level
      indent_str = ""
      indent_str += "  " * indent.to_i
      if indent.to_i > 0
        indent_str += ' - '
      else
        indent_str += ' = '
      end
      indent_str += prefix if prefix
      messages.each do |message|
        message = message.rstrip
        next if message.empty?
        STDOUT.print("#{indent_str}#{message}\n")
      end
    end
  end
end