Class: ConfCtl::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/confctl/logger.rb

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



26
27
28
29
30
# File 'lib/confctl/logger.rb', line 26

def initialize
  @mutex = Mutex.new
  @readers = []
  @keep = false
end

Instance Method Details

#<<(str) ⇒ Object



101
102
103
# File 'lib/confctl/logger.rb', line 101

def <<(str)
  write(str)
end

#add_reader(obj) ⇒ Object

Parameters:

  • obj (#<<)


117
118
119
# File 'lib/confctl/logger.rb', line 117

def add_reader(obj)
  @readers << obj
end

#cli(cmd, gopts, opts, args) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/confctl/logger.rb', line 105

def cli(cmd, gopts, opts, args)
  sync do
    PP.pp({
      command: cmd,
      global_options: prune_opts(gopts.clone),
      command_options: prune_opts(opts.clone),
      arguments: args
    }, @io)
  end
end

#closeObject



47
48
49
50
51
# File 'lib/confctl/logger.rb', line 47

def close
  raise 'log file not open' if @io.nil?

  @io.close
end


87
88
89
90
# File 'lib/confctl/logger.rb', line 87

def close_and_unlink
  close
  unlink
end

#ioObject



53
54
55
56
57
# File 'lib/confctl/logger.rb', line 53

def io
  raise 'log file not open' if @io.nil?

  @io
end

#keepObject



77
78
79
# File 'lib/confctl/logger.rb', line 77

def keep
  @keep = true
end

#keep?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/confctl/logger.rb', line 73

def keep?
  @keep
end

#open(name, output: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/confctl/logger.rb', line 32

def open(name, output: nil)
  if output
    @io = output
  else
    dir = ConfDir.log_dir
    FileUtils.mkdir_p(dir)

    @io = File.new(File.join(dir, file_name(name)), 'w')
  end
end

#open?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/confctl/logger.rb', line 43

def open?
  !@io.nil?
end

#pathObject



59
60
61
62
63
# File 'lib/confctl/logger.rb', line 59

def path
  raise 'log file not open' if @io.nil?

  @io.path
end

#relative_pathObject



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

def relative_path
  return @relative_path if @relative_path

  abs = Pathname.new(path)
  dir = Pathname.new(ConfDir.path)
  abs.relative_path_from(dir).to_s
end

#remove_reader(obj) ⇒ Object

Parameters:

  • obj (#<<)


122
123
124
# File 'lib/confctl/logger.rb', line 122

def remove_reader(obj)
  @readers.delete(obj)
end


81
82
83
84
85
# File 'lib/confctl/logger.rb', line 81

def unlink
  raise 'log file not open' if @io.nil?

  File.unlink(@io.path)
end

#write(str) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/confctl/logger.rb', line 92

def write(str)
  sync do
    @io << str
    @io.flush
  end

  readers.each { |r| r << str }
end