Module: LeapCli::Log
- Included in:
- LeapCli
- Defined in:
- lib/leap_cli/log.rb
Constant Summary collapse
- FILE_TITLES =
these are log titles typically associated with files
[:updated, :created, :removed, :missing, :nochange, :loading]
Instance Method Summary collapse
-
#log(*args) ⇒ Object
master logging function.
-
#log_raw(mode, indent = nil, &block) ⇒ Object
Add a raw log entry, without any modifications (other than indent).
Instance Method Details
#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. so far, only :indent is supported.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 |
# File 'lib/leap_cli/log.rb', line 69 def log(*args) level = args.grep(Integer).first || 1 title = args.grep(Symbol).first = args.grep(String).first = args.grep(Hash).first || {} unless && LeapCli.log_level >= level return end # prefix clear_prefix = colored_prefix = "" if title = case title when :error then ['error', :red, :bold] when :fatal_error then ['fatal error', :red, :bold] when :warning then ['warning:', :yellow, :bold] when :info then ['info', :cyan, :bold] when :updated then ['updated', :cyan, :bold] when :updating then ['updating', :cyan, :bold] when :created then ['created', :green, :bold] when :removed then ['removed', :red, :bold] when :nochange then ['no change', :magenta] when :loading then ['loading', :magenta] when :missing then ['missing', :yellow, :bold] when :skipping then ['skipping', :yellow, :bold] when :run then ['run', :magenta] when :failed then ['FAILED', :red, :bold] when :completed then ['completed', :green, :bold] when :ran then ['ran', :green, :bold] when :bail then ['bailing out', :red, :bold] when :invalid then ['invalid', :red, :bold] else [title.to_s, :cyan, :bold] end if [:host] clear_prefix = "[%s] %s " % [[:host], [0]] colored_prefix = "[%s] %s " % [Paint[[:host], [1], [2]], [0]] else clear_prefix = "%s " % [0] colored_prefix = "%s " % Paint[[0], [1], [2]] end elsif [:host] clear_prefix = colored_prefix = "[%s] " % [:host] end # transform absolute path names if title && FILE_TITLES.include?(title) && =~ /^\// = LeapCli::Path.relative_path() end log_raw(:log, nil) { [clear_prefix, ].join } if LeapCli.log_in_color log_raw(:stdout, [:indent]) { [colored_prefix, ].join } else log_raw(:stdout, [:indent]) { [clear_prefix, ].join } end # run block, if given if block_given? LeapCli.indent_level += 1 yield LeapCli.indent_level -= 1 end end |
#log_raw(mode, indent = 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.
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 |
# File 'lib/leap_cli/log.rb', line 141 def log_raw(mode, indent=nil, &block) # NOTE: print message (using 'print' produces better results than 'puts' when multiple threads are logging) if mode == :log if LeapCli.log_output_stream = [yield].compact.flatten if .any? = Time.now.strftime("%b %d %H:%M:%S") .each do || LeapCli.log_output_stream.print("#{} #{}\n") end LeapCli.log_output_stream.flush end end elsif mode == :stdout = [yield].compact.flatten if .any? indent ||= LeapCli.indent_level indent_str = "" indent_str += " " * indent.to_i if indent.to_i > 0 indent_str += ' - ' else indent_str += ' = ' end .each do || STDOUT.print("#{indent_str}#{}\n") end end end end |