123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836 |
- package Automake::Channels;
- use 5.006;
- use strict;
- use Exporter;
- use Carp;
- use File::Basename;
- use vars qw (@ISA @EXPORT %channels $me);
- @ISA = qw (Exporter);
- @EXPORT = qw ($exit_code $warnings_are_errors
- &reset_local_duplicates &reset_global_duplicates
- ®ister_channel &msg &exists_channel &channel_type
- &setup_channel &setup_channel_type
- &dup_channel_setup &drop_channel_setup
- &buffer_messages &flush_messages
- &setup_channel_queue &pop_channel_queue
- US_GLOBAL US_LOCAL
- UP_NONE UP_TEXT UP_LOC_TEXT);
- $me = basename $0;
- use vars qw ($exit_code);
- $exit_code = 0;
- use vars qw ($warnings_are_errors);
- $warnings_are_errors = 0;
- use constant UP_NONE => 0;
- use constant UP_TEXT => 1;
- use constant UP_LOC_TEXT => 2;
- use constant US_LOCAL => 0;
- use constant US_GLOBAL => 1;
- use vars qw (%_default_options %_global_duplicate_messages
- %_local_duplicate_messages);
- %_default_options =
- (
- type => 'warning',
- exit_code => 1,
- file => \*STDERR,
- silent => 0,
- ordered => 1,
- queue => 0,
- queue_key => undef,
- uniq_scope => US_LOCAL,
- uniq_part => UP_LOC_TEXT,
- header => '',
- footer => '',
- backtrace => 0,
- partial => 0,
- );
- %_local_duplicate_messages = ();
- %_global_duplicate_messages = ();
- sub _reset_duplicates (\%)
- {
- my ($ref) = @_;
- my $dup = 0;
- foreach my $k (keys %$ref)
- {
- $dup += $ref->{$k};
- }
- %$ref = ();
- return $dup;
- }
- sub reset_local_duplicates ()
- {
- return _reset_duplicates %_local_duplicate_messages;
- }
- sub reset_global_duplicates ()
- {
- return _reset_duplicates %_global_duplicate_messages;
- }
- sub _merge_options (\%%)
- {
- my ($hash, %options) = @_;
- local $_;
- foreach (keys %options)
- {
- if (exists $hash->{$_})
- {
- $hash->{$_} = $options{$_}
- }
- else
- {
- confess "unknown option '$_'";
- }
- }
- if ($hash->{'ordered'})
- {
- confess "fatal messages cannot be ordered"
- if $hash->{'type'} eq 'fatal';
- confess "backtrace cannot be output on ordered messages"
- if $hash->{'backtrace'};
- }
- }
- sub register_channel ($;%)
- {
- my ($name, %options) = @_;
- my %channel_opts = %_default_options;
- _merge_options %channel_opts, %options;
- $channels{$name} = \%channel_opts;
- }
- sub exists_channel ($)
- {
- my ($name) = @_;
- return exists $channels{$name};
- }
- sub channel_type ($)
- {
- my ($name) = @_;
- return $channels{$name}{'type'} if exists_channel $name;
- return '';
- }
- sub _format_sub_message ($$)
- {
- my ($leader, $message) = @_;
- return $leader . join ("\n" . $leader, split ("\n", $message)) . "\n";
- }
- use vars qw ($partial);
- $partial = '';
- sub _format_message ($$%)
- {
- my ($location, $message, %opts) = @_;
- my $msg = ($partial eq '' ? $opts{'header'} : '') . $message
- . ($opts{'partial'} ? '' : $opts{'footer'});
- if (ref $location)
- {
-
-
- my $loc = $location->get || $me;
- $msg = _format_sub_message ("$loc: ", $msg);
- for my $pair ($location->get_contexts)
- {
- $msg .= _format_sub_message ($pair->[0] . ": ", $pair->[1]);
- }
- }
- else
- {
- $location ||= $me;
- $msg = _format_sub_message ("$location: ", $msg);
- }
- return $msg;
- }
- sub _enqueue ($$$$$$)
- {
- my ($queue, $key, $uniq_scope, $to_filter, $msg, $file) = @_;
- $queue->enqueue ($key, $msg, $to_filter, $uniq_scope);
- confess "message queuing works only for STDERR"
- if $file ne \*STDERR;
- }
- sub _dequeue ($)
- {
- my ($queue) = @_;
- my $msg = $queue->dequeue || return 0;
- my $to_filter = $queue->dequeue;
- my $uniq_scope = $queue->dequeue;
- my $file = \*STDERR;
- if ($to_filter ne '')
- {
-
- my $dups;
- if ($uniq_scope == US_LOCAL)
- {
- $dups = \%_local_duplicate_messages;
- }
- elsif ($uniq_scope == US_GLOBAL)
- {
- $dups = \%_global_duplicate_messages;
- }
- else
- {
- confess "unknown value for uniq_scope: " . $uniq_scope;
- }
-
- if (exists $dups->{$to_filter})
- {
- ++$dups->{$to_filter};
- return 1;
- }
- else
- {
- $dups->{$to_filter} = 0;
- }
- }
- print $file $msg;
- return 1;
- }
- sub _print_message ($$%)
- {
- my ($location, $message, %opts) = @_;
- return 0 if ($opts{'silent'});
- my $msg = _format_message ($location, $message, %opts);
- if ($opts{'partial'})
- {
-
- $partial .= $msg;
- return;
- }
- else
- {
-
- $msg = $partial . $msg;
- $partial = '';
- }
- msg ('note', '', 'warnings are treated as errors', uniq_scope => US_GLOBAL)
- if ($opts{'type'} eq 'warning' && $warnings_are_errors);
-
- my $to_filter;
- if ($opts{'uniq_part'} ne UP_NONE)
- {
-
- if ($opts{'uniq_part'} eq UP_TEXT)
- {
- $to_filter = $message;
- }
- elsif ($opts{'uniq_part'} eq UP_LOC_TEXT)
- {
- $to_filter = $msg;
- }
- else
- {
- $to_filter = $opts{'uniq_part'};
- }
-
- my $dups;
- if ($opts{'uniq_scope'} == US_LOCAL)
- {
- $dups = \%_local_duplicate_messages;
- }
- elsif ($opts{'uniq_scope'} == US_GLOBAL)
- {
- $dups = \%_global_duplicate_messages;
- }
- else
- {
- confess "unknown value for uniq_scope: " . $opts{'uniq_scope'};
- }
-
- if (exists $dups->{$to_filter})
- {
- ++$dups->{$to_filter};
- return 0;
- }
- else
- {
- $dups->{$to_filter} = 0;
- }
- }
- my $file = $opts{'file'};
- if ($opts{'ordered'} && $opts{'queue'})
- {
- _enqueue ($opts{'queue'}, $opts{'queue_key'}, $opts{'uniq_scope'},
- $to_filter, $msg, $file);
- }
- else
- {
- print $file $msg;
- }
- return 1;
- }
- use vars qw (@backlog %buffering);
- %buffering = ();
- @backlog = ();
- sub msg ($$;$%)
- {
- my ($channel, $location, $message, %options) = @_;
- if (! defined $message)
- {
- $message = $location;
- $location = '';
- }
- confess "unknown channel $channel" unless exists $channels{$channel};
- my %opts = %{$channels{$channel}};
- _merge_options (%opts, %options);
- if (exists $buffering{$opts{'type'}})
- {
- push @backlog, [$channel, $location->clone, $message, %options];
- return;
- }
-
- if (_print_message ($location, $message, %opts))
- {
-
- if ($opts{'type'} eq 'error'
- || $opts{'type'} eq 'fatal'
- || ($opts{'type'} eq 'warning' && $warnings_are_errors))
- {
- my $es = $opts{'exit_code'};
- $exit_code = $es if $es > $exit_code;
- }
-
- confess if $opts{'backtrace'};
- if ($opts{'type'} eq 'fatal')
- {
-
- STDERR->flush;
- exit $exit_code;
- }
- }
- }
- sub setup_channel ($%)
- {
- my ($name, %opts) = @_;
- confess "unknown channel $name" unless exists $channels{$name};
- _merge_options %{$channels{$name}}, %opts;
- }
- sub setup_channel_type ($%)
- {
- my ($type, %opts) = @_;
- foreach my $channel (keys %channels)
- {
- setup_channel $channel, %opts
- if $channels{$channel}{'type'} eq $type;
- }
- }
- use vars qw (@_saved_channels @_saved_werrors);
- @_saved_channels = ();
- @_saved_werrors = ();
- sub dup_channel_setup ()
- {
- my %channels_copy;
- foreach my $k1 (keys %channels)
- {
- $channels_copy{$k1} = {%{$channels{$k1}}};
- }
- push @_saved_channels, \%channels_copy;
- push @_saved_werrors, $warnings_are_errors;
- }
- sub drop_channel_setup ()
- {
- my $saved = pop @_saved_channels;
- %channels = %$saved;
- $warnings_are_errors = pop @_saved_werrors;
- }
- sub buffer_messages (@)
- {
- foreach my $type (@_)
- {
- $buffering{$type} = 1;
- }
- }
- sub flush_messages ()
- {
- %buffering = ();
- foreach my $args (@backlog)
- {
- &msg (@$args);
- }
- @backlog = ();
- }
- sub setup_channel_queue ($$)
- {
- my ($queue, $key) = @_;
- foreach my $channel (keys %channels)
- {
- setup_channel $channel, queue => $queue, queue_key => $key
- if $channels{$channel}{'ordered'};
- }
- }
- sub pop_channel_queue ($)
- {
- my ($queue) = @_;
- return _dequeue ($queue);
- }
- 1;
|