File: logger.rb

package info (click to toggle)
ruby-fog-core 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 684 kB
  • sloc: ruby: 4,812; makefile: 5
file content (46 lines) | stat: -rw-r--r-- 1,219 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Fog
  class Logger
    @channels = {
      deprecation: ::STDERR,
      warning: ::STDERR
    }

    @channels[:debug] = ::STDERR if ENV["DEBUG"]

    # provide an env var with narrower scope in case of namespace conflicts
    @channels[:debug] = ::STDERR if ENV["FOG_DEBUG"]

    def self.[](channel)
      @channels[channel]
    end

    def self.[]=(channel, value)
      @channels[channel] = value
    end

    def self.debug(message)
      write(:debug, "[light_black][fog][DEBUG] #{message}[/]\n")
    end

    def self.deprecation(message)
      write(:deprecation, "[yellow][fog][DEPRECATION] #{message}[/]\n")
    end

    def self.warning(message)
      write(:warning, "[yellow][fog][WARNING] #{message}[/]\n")
    end

    def self.write(key, value)
      channel = @channels[key]
      if channel
        message = if channel.tty?
                    value.gsub(Fog::Formatador::PARSE_REGEX) { "\e[#{Fog::Formatador::STYLES[$1.to_sym]}m" }.gsub(Fog::Formatador::INDENT_REGEX, "")
                  else
                    value.gsub(Fog::Formatador::PARSE_REGEX, "").gsub(Fog::Formatador::INDENT_REGEX, "")
                  end
        channel.write(message)
      end
      nil
    end
  end
end