Module: Baltix::Log

Included in:
CLI, Loader, Baltix::Loader::Certain, Source::Base, Space
Defined in:
lib/baltix/log.rb

Constant Summary collapse

DEFAULT_IO_NAMES =
{
   none: nil,
   error: 'stderr',
   warn: 'stderr',
   info: 'stderr',
   debug: 'stderr',
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_prefix(ios) ⇒ Object



31
32
33
# File 'lib/baltix/log.rb', line 31

def default_prefix ios
   ios.keys.map {|kind| [kind, "[baltix][#{kind.upcase}]> " ] }.to_h
end

.io_name_parse(io_names) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/baltix/log.rb', line 53

def io_name_parse io_names
   io_names.map do |(kind, io_name)|
      io =
         case io_name
         when '-', 'stdout'
            $stdout
         when '--', 'stderr'
            $stderr
         when '', nil
            nil
         else
            File.open(io_name, 'a+')
         end

      [kind, io]
   end.to_h
end

.iosObject



39
40
41
# File 'lib/baltix/log.rb', line 39

def ios
   @@ios ||= io_name_parse(DEFAULT_IO_NAMES)
end

.levelObject



43
44
45
# File 'lib/baltix/log.rb', line 43

def level
   @@level ||= :info
end

.prefixObject



27
28
29
# File 'lib/baltix/log.rb', line 27

def prefix
   @@prefix ||= default_prefix(ios)
end

.prefix_for(kind, prefix) ⇒ Object



23
24
25
# File 'lib/baltix/log.rb', line 23

def prefix_for kind, prefix
   @@prefix[kind] = prefix
end

.setup(level = :info, io_names = DEFAULT_IO_NAMES, prefix = nil) ⇒ Object



47
48
49
50
51
# File 'lib/baltix/log.rb', line 47

def setup level = :info, io_names = DEFAULT_IO_NAMES, prefix = nil
   @@ios = io_name_parse(io_names)
   @@level = level
   @@prefix = prefix || default_prefix(ios)
end

.setup_kind(kind, io) ⇒ Object



35
36
37
# File 'lib/baltix/log.rb', line 35

def setup_kind kind, io
   ios[kind] = io
end

Instance Method Details

#level_match(kind) ⇒ Object



18
19
20
# File 'lib/baltix/log.rb', line 18

def level_match kind
   Baltix::Log.ios[kind] && Baltix::Log.ios.keys.index(kind) <= Baltix::Log.ios.keys.index(Baltix::Log.level)
end

#log(kind, message) ⇒ Object



14
15
16
# File 'lib/baltix/log.rb', line 14

def log kind, message
   Baltix::Log.ios[kind].puts("#{Baltix::Log.prefix[kind]}#{message}")
end