class Fluent::Plugin::MacOsLogInput

Attributes

parser[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_macoslog.rb, line 12
def initialize
  super
  @pf_file = nil
  @log_start_regex = nil
  @compiled_command = nil
end

Public Instance Methods

compile_command() click to toggle source
# File lib/fluent/plugin/in_macoslog.rb, line 76
def compile_command
  result = @command

  if @style
    result += " --style #{@style}"
  elsif not result =~ /"--style"/
    result += " --style default"
  end

  if @predicate
    result += " --predicate '#{@predicate}'"
  end

  if @levels.length > 0
    compiled_level = @levels.map { |level| "--#{level}" }.join(" ")
    result += " #{compiled_level}"
  end

  result
end
configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_macoslog.rb, line 53
def configure(conf)
  compat_parameters_convert(conf, :parser)

  super

  unless @tag
    raise Fluent::ConfigError, "'tag' option is required on macoslog input"
  end

  @compiled_command = compile_command

  $log.info "MacOs log command '#{@compiled_command}'"

  @parser = parser_create

  unless @pos_file
    $log.warn "'pos_file PATH' parameter is not set to a 'macoslog' source."
    $log.warn "this parameter is highly recommended to save the position to resume from."
  end

  @log_start_regex = Regexp.compile("\\A#{@log_line_start}")
end
multi_workers_ready?() click to toggle source
# File lib/fluent/plugin/in_macoslog.rb, line 97
def multi_workers_ready?
  true
end
on_record(time, record) click to toggle source
# File lib/fluent/plugin/in_macoslog.rb, line 190
def on_record(time, record)
  tag = extract_tag_from_record(record)
  tag ||= @tag
  time ||= extract_time_from_record(record) || Fluent::EventTime.now
  router.emit(tag, time, record)
rescue => e
  log.error "macoslog failed to emit", tag: tag, record: Yajl.dump(record), error: e
  router.emit_error_event(tag, time, record, e) if tag && time && record
end
parse_line_json(io) click to toggle source
# File lib/fluent/plugin/in_macoslog.rb, line 157
def parse_line_json(io)
  logs = Queue.new
  io.each_line.with_index do |line,index|
    logs.push(line.chomp("\n"))
    if index >= @log_header_lines
      @parser.parse(logs.pop, &method(:on_record))
    end
  end
end
parse_timestamp_base(io) click to toggle source
# File lib/fluent/plugin/in_macoslog.rb, line 167
def parse_timestamp_base(io)
  log = ""
  io.each_line.with_index do |line,index|
    # Skips log header
    if index >= @log_header_lines
      if line =~ @log_start_regex
        if log.empty?
          log = line
        else
          @parser.parse(log.chomp("\n"), &method(:on_record))
          log = line
        end
      else
        log += line
      end
    end
  end

  unless log.empty?
    @parser.parse(log.chomp("\n"), &method(:on_record))
  end
end
run(io) click to toggle source
# File lib/fluent/plugin/in_macoslog.rb, line 147
def run(io)
  unless io.eof
    if @style == :ndjson
      parse_line_json(io)
    else
      parse_timestamp_base(io)
    end
  end
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_macoslog.rb, line 141
def shutdown
  @pf_file.close if @pf_file

  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/in_macoslog.rb, line 101
def start
  super

  if @pos_file
    pos_file_dir = File.dirname(@pos_file)
    FileUtils.mkdir_p(pos_file_dir, mode: @dir_perm) unless Dir.exist?(pos_file_dir)
    @pf_file = File.open(@pos_file, File::RDWR|File::CREAT|File::BINARY, @file_perm)
    @pf_file.sync = true

    start = @pf_file.read.to_i
    if start == 0
      start = Fluent::EventTime.now.to_int
      @pf_file.write(start)
    end
  else
    start = Fluent::EventTime.now.to_int
    @pf_file.write(start) if @pf_file
  end

  oldest = Fluent::EventTime.now.to_int - @max_age.to_i
  if start < oldest
    log.info "Start timestamp over max_age ", start: start, oldest: oldest
    start = oldest
  end

  time_callback = -> (timestamp) {
    if @pf_file
      @pf_file.rewind
      @pf_file.write(timestamp)
    end
  }

  timer_process_execute(:exec_input,
                        @compiled_command,
                        start, @run_interval,
                        time_callback,
                        delay_seconds: 1,
                        immediate: true, mode: [@connect_mode], &method(:run))
end