Fluent-LibFluentBit

 view release on metacpan or  search on metacpan

lib/Fluent/LibFluentBit.pm  view on Meta::CPAN

package Fluent::LibFluentBit;
our $VERSION = '0.03'; # VERSION
use strict;
use warnings;
use Carp;
use Scalar::Util;
use Exporter;

# ABSTRACT: Perl interface to libfluent-bit.so


require XSLoader;
XSLoader::load('Fluent::LibFluentBit', $Fluent::LibFluentBit::VERSION);

our @EXPORT_OK= qw(
  flb_create flb_service_set flb_input flb_input_set flb_filter flb_filter_set
  flb_output flb_output_set flb_start flb_stop flb_destroy
  flb_lib_push flb_lib_config_file
  FLB_LIB_ERROR FLB_LIB_NONE FLB_LIB_OK FLB_LIB_NO_CONFIG_MAP
);

sub import {
   # handle the -config option.
   for (my $i= 1; $i < @_; $i++) {
      if ($_[$i] eq '-config') {
         ref $_[$i+1] eq 'HASH'
            or croak "-config must be followed by a hashref";
         __PACKAGE__->default_instance->configure($_[$i+1]);
         splice(@_, $i, 2);
         --$i;
      }
   }
   goto \&Exporter::import;
}

our ( %instances, $default_instance );
sub default_instance {
   $default_instance //= Fluent::LibFluentBit->new();
}
# Before program exit, try to cleanly flush all messages
sub END {
   defined $_ && $_->{started} && $_->stop
      for values %instances;
   %instances= ();
}

# constructor registers the instance
sub new {
   my $class= shift;
   my $self= Fluent::LibFluentBit::flb_create();
   bless $self, $class if $class ne 'Fluent::LibFluentBit';
   Scalar::Util::weaken( $instances{0+$self}= $self );
   $self->configure((@_ == 1 && ref $_[0] eq 'HASH')? %{$_[0]} : @_);
}

# destructor flushes cached messages and unregisters the instance
sub DESTROY {
   my $self= shift;
   delete $instances{0+$self};
   $self->stop;
   # XS calls flb_destroy when the hash goes out of scope
}

sub _ctx {
   ref $_[0]? $_[0] : $_[0]->default_instance
}


sub inputs { _ctx(shift)->{inputs} }
sub filters { _ctx(shift)->{filters} }
sub outputs { _ctx(shift)->{outputs} }
sub started { !!_ctx(shift)->{started} }


sub configure {
   my $self= _ctx(shift);
   my %conf= @_ == 1 && ref $_[0] eq 'HASH'? %{$_[0]} : @_;

   my $inputs= delete $conf{inputs};
   my $filters= delete $conf{filters};
   my $outputs= delete $conf{outputs};
   for (keys %conf) {
      if ($self->flb_service_set($_, $conf{$_}) >= 0) {
         $self->{$_}= $conf{$_};
      } else {
         carp "Invalid fluent-bit context attribute '$_' = '$conf{$_}'";
      }
   }
   if ($inputs) {
      $self->add_input($_) for @$inputs;
   }
   if ($outputs) {
      $self->add_output($_) for @$outputs;
   }
   if ($filters) {
      $self->add_filter($_) for @$filters;
   }
   return $self;
}


sub _collect_subobject_config {
   my %cfg;
   $cfg{name}= shift if @_ && !ref $_[0];
   my @attrs= (ref $_[0] eq 'HASH')? %{$_[0]} : @_;
   for (my $i= 0; $i < @attrs; $i+= 2) {
      # Make all keys lowercase
      $cfg{lc $attrs[$i]}= $attrs[$i+1];
   }
   # name must be defined
   defined $cfg{name} or croak "Missing ->{name} in object config";
   \%cfg;



( run in 0.962 second using v1.01-cache-2.11-cpan-39bf76dae61 )