| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | |||
| 5 | package Rex::Output; | ||
| 6 | |||
| 7 | use v5.14.4; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | my $handle; | ||
| 11 | use vars qw($output_object); | ||
| 12 | |||
| 13 | BEGIN { IPC::Shareable->use; } | ||
| 14 | END { IPC::Shareable->clean_up_all; } | ||
| 15 | |||
| 16 | use base 'Rex::Output::Base'; | ||
| 17 | |||
| 18 | our $VERSION = '1.16.1'; # VERSION | ||
| 19 | |||
| 20 | sub get { | ||
| 21 | my ( $class, $output_module ) = @_; | ||
| 22 | |||
| 23 | return $output_object if ($output_object); | ||
| 24 | |||
| 25 | return unless ($output_module); | ||
| 26 | |||
| 27 | $handle = tie $output_object, 'IPC::Shareable', undef, { destroy => 1 } | ||
| 28 | unless $handle; | ||
| 29 | |||
| 30 | eval "use Rex::Output::$output_module;"; | ||
| 31 | if ($@) { | ||
| 32 | die("Output Module ,,$output_module'' not found."); | ||
| 33 | } | ||
| 34 | |||
| 35 | my $output_class = "Rex::Output::$output_module"; | ||
| 36 | $output_object = $output_class->new; | ||
| 37 | |||
| 38 | return $class; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _action { | ||
| 42 | my ( $class, $action, @args ) = @_; | ||
| 43 | |||
| 44 | return unless ( defined $output_object ); | ||
| 45 | $handle->shlock(); | ||
| 46 | $output_object->$action(@args); | ||
| 47 | $handle->shunlock(); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub add { | ||
| 51 | my $class = shift; | ||
| 52 | |||
| 53 | return $class->_action( 'add', @_ ); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub error { | ||
| 57 | my $class = shift; | ||
| 58 | |||
| 59 | return $class->_action( 'error', @_ ); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub write { | ||
| 63 | my $class = shift; | ||
| 64 | |||
| 65 | return $class->_action( 'write', @_ ); | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; |