=head1 NAME

CFX5::Output::CCLUpdateLog - output routines for an in-memory array

=head1 SYNOPSIS

  use CFX5::Output;

=head1 DESCRIPTION

The C<CFX5::Output::CCLUpdateLog> class implements copying of CCL upgrade
log messages to an array in memory, for later access.

=cut

package CFX5::Output::CCLUpdateLog;
@ISA=qw(CFX5::Output::Base);
use strict;

use CFX5::Output::Base;

use CFX5::Output qw(:all);

=head2 STANDARD METHODS

=head3 init

=cut

sub init {
  my $self=shift;

  $self->{__PACKAGE__ . '::output'} = [];

  unless (exists $self->{settings}) {
    # The default on/off settings
    #    1 => display
    #    0 => do not display
    $self->{settings}=
      {
       'internal' => 1,
       'abort'    => 1,
       'error'    => 1,
       'warn'     => 1,
       'info'     => 1,
       'moreinfo' => 0,
       'trace'    => 0,
       'debug'    => 0,
       'stack'    => 0,
       'print'    => 1,
      };
  }

  1;
}

=head3 output

This version of the C<output> function uses the on/off settings from
C<set_verbosity> to decide whether to output a given message or not.

=cut

sub output {
  my $self=shift;
  my $type=shift;
  my $severity=shift;
  my $title=shift;
  my $text=join('',@_);

  return unless $self->{settings}{$type};

  my @changes = split("\n", $text);
  push @{$self->{__PACKAGE__ . '::output'}}, \@changes;
  return 1;
}

=head2 CUSTOM METHODS

=head3 append

Append CCL update messages to log. This simply calls print(), which
would otherwise be confusing, when it doesn't actually print.

=cut

sub append {
  my $self = shift;
  return $self->print(@_);
}

=head3 have_data

  my @text=$log->have_dat();

Returns true if data is stored, false otherwise.

=cut

sub have_data {
  my $self=shift;
  return 1 if scalar @{$self->{__PACKAGE__ . '::output'}};
  return '';
}

=head3 get_next

  my @text=$log->get_next();

Returns a formatted string of CCL update error messages for a particular
group of updates. Returns the group in the same order they were added.

=cut

sub get_next {
  my $self=shift;
  my $arrayref = shift @{$self->{__PACKAGE__ . '::output'}};
  return undef unless defined $arrayref;
  return join '', map {"$_\n\n" } @{$arrayref};
}

1;
