Class: Mamiya::Logger::LogDev

Inherits:
Object
  • Object
show all
Defined in:
lib/mamiya/logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(outputs) ⇒ LogDev

Returns a new instance of LogDev.



135
136
137
138
# File 'lib/mamiya/logger.rb', line 135

def initialize(outputs)
  @outputs = normalize_outputs(outputs)
  @mutex = Mutex.new
end

Instance Method Details

#add(*outputs) ⇒ Object



167
168
169
170
171
172
# File 'lib/mamiya/logger.rb', line 167

def add(*outputs)
  @mutex.synchronize do
    @outputs.push(*normalize_outputs(outputs))
  end
  self
end

#closeObject



151
152
153
154
155
156
# File 'lib/mamiya/logger.rb', line 151

def close
  @outputs.each do |output|
    output.close unless output.respond_to?(:closed?) && output.closed?
  end
  self
end

#remove(*removing_outputs) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mamiya/logger.rb', line 174

def remove(*removing_outputs)
  @mutex.synchronize do
    removing_outputs.each do |removing|
      case removing
      when File
        @outputs.reject! { |out| out.kind_of?(File) && out.path == removing.path }
      when IO
        @outputs.reject! { |out| out.kind_of?(IO) && out.fileno == removing.fileno }
      when String
        @outputs.reject! { |out| out.kind_of?(File) && out.path == removing }
      when Integer
        @outputs.reject! { |out| out.kind_of?(IO) && out.fileno == removing }
      else
        @outputs.reject! { |out| out == removing }
      end
    end
  end
  self
end

#reopenObject



158
159
160
161
162
163
164
165
# File 'lib/mamiya/logger.rb', line 158

def reopen
  @outputs.select { |io| io.respond_to?(:path) }.each do |io|
    sync = io.sync
    io.reopen(io.path, 'a')
    io.sync = sync
    io.puts("Log reopened")
  end
end

#tty?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/mamiya/logger.rb', line 140

def tty?
  @outputs.all?(&:tty?)
end

#write(*args) ⇒ Object



144
145
146
147
148
149
# File 'lib/mamiya/logger.rb', line 144

def write(*args)
  @outputs.each do |output|
    output.write(*args) unless output.respond_to?(:closed?) && output.closed?
  end
  self
end