This file is indexed.

/usr/share/perl5/FCM/Util/Reporter.pm is in fcm 2016.12.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# ------------------------------------------------------------------------------
# (C) British Crown Copyright 2006-16 Met Office.
#
# This file is part of FCM, tools for managing and building source code.
#
# FCM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FCM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FCM. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
use strict;
use warnings;

# ------------------------------------------------------------------------------
package FCM::Util::Reporter;
use base qw{FCM::Class::CODE};

use Scalar::Util qw{reftype};

use constant {TYPE_OUT => 1, TYPE_ERR => 2};

use constant {  DEFAULT => 1,
    FAIL  => 0, WARN    => 1,
    QUIET => 0, LOW     => 1, MEDIUM => 2, HIGH => 3, DEBUG => 4,
};

use constant {
    PREFIX_DONE => q{[done] },
    PREFIX_FAIL => q{[FAIL] },
    PREFIX_INFO => q{[info] },
    PREFIX_INIT => q{[init] },
    PREFIX_NULL => q{},
    PREFIX_QUIT => q{[quit] },
    PREFIX_WARN => q{[WARN] },
};

# Creates the class.
__PACKAGE__->class(
    {ctx_of => '%'},
    {   init => sub {
            my ($attrib_ref) = @_;
            %{$attrib_ref->{ctx_of}} = (
                stderr => FCM::Util::Reporter::Context->new_err(),
                stdout => FCM::Util::Reporter::Context->new(),
            );
        },
        action_of => {
            add_ctx           => \&_add_ctx,
            del_ctx           => \&_del_ctx,
            get_ctx           => \&_get_ctx,
            get_ctx_of_stderr => sub {$_[0]->{ctx_of}->{stderr}},
            get_ctx_of_stdout => sub {$_[0]->{ctx_of}->{stdout}},
            report            => \&_report,
        }
    },
);

# Adds a named reporter context.
sub _add_ctx {
    my ($attrib_ref, $key, @args) = @_;
    if (exists($attrib_ref->{ctx_of}->{$key})) {
        return;
    }
    $attrib_ref->{ctx_of}->{$key} = FCM::Util::Reporter::Context->new(@args);
}

# Deletes a named reporter context.
sub _del_ctx {
    my ($attrib_ref, $key) = @_;
    if (!exists($attrib_ref->{ctx_of}->{$key})) {
        return;
    }
    delete($attrib_ref->{ctx_of}->{$key});
}

# Returns a named reporter context.
sub _get_ctx {
    my ($attrib_ref, $key) = @_;
    if (!exists($attrib_ref->{ctx_of}->{$key})) {
        return;
    }
    $attrib_ref->{ctx_of}->{$key};
}

# Reports message.
sub _report {
    my ($attrib_ref, @args) = @_;
    if (!@args) {
        return;
    }
    my %option = (
        delimiter => "\n",
        level     => DEFAULT,
        prefix    => undef,
        type      => TYPE_OUT,
    );
    if (ref($args[0]) && reftype($args[0]) eq 'HASH') {
        %option = (%option, %{shift(@args)});
    }
    # Auto remove ctx with closed file handle
    while (my ($key, $ctx) = each(%{$attrib_ref->{ctx_of}})) {
        if (!defined(fileno($ctx->get_handle()))) {
            delete($attrib_ref->{ctx_of}->{$key});
        }
    }
    # Selects handles
    my @ctx_and_prefix_list
        =   map  {
                my $prefix = defined($option{prefix})
                    ? $option{prefix} : $_->get_prefix();
                if (ref($prefix) && reftype($prefix) eq 'CODE') {
                    $prefix = $prefix->($option{level}, $option{type});
                }
                [$_, $prefix],
            }
            grep {  (!$_->get_type() || $_->get_type() eq $option{type})
                &&  $_->get_verbosity() >= $option{level}
            }
            values(%{$attrib_ref->{ctx_of}});
    if (!@ctx_and_prefix_list) {
        return;
    }
    for my $arg (@args) {
        for (@ctx_and_prefix_list) {
            my ($ctx, $prefix) = @{$_};
            my $handle = $ctx->get_handle();
            if ($option{delimiter}) {
                for my $item (
                    map {grep {$_ ne "\n"} split(qr{(\n)}msx)} (
                          !ref($arg)               ? ($arg)
                        : reftype($arg) eq 'ARRAY' ? @{$arg}
                        : reftype($arg) eq 'CODE'  ? $arg->($ctx->get_verbosity())
                        :                            ($arg)
                    )
                ) {
                    print({$handle} $prefix . $item . $option{delimiter});
                }
            }
            else {
                print({$handle} $arg);
            }
        }
    }
    1;
}

# ------------------------------------------------------------------------------
package FCM::Util::Reporter::Context;
use base qw{FCM::Class::HASH};

# Creates the class.
__PACKAGE__->class(
    {   handle    => {isa => '*', default => \*STDOUT                     },
        prefix    => {            default => sub {\&_prefix}              },
        type      => {isa => '$', default => FCM::Util::Reporter->TYPE_OUT},
        verbosity => {isa => '$', default => FCM::Util::Reporter->DEFAULT },
    },
);

# Returns a new reporter context to STDERR.
sub new_err {
    my ($class, $attrib_ref) = @_;
    $class->new({
        handle => \*STDERR,
        type   => FCM::Util::Reporter->TYPE_ERR,
        (defined($attrib_ref) ? %{$attrib_ref} : ()),
    });
}

# The default prefix function.
sub _prefix {
    my ($level, $type) = @_;
      $type eq FCM::Util::Reporter->TYPE_OUT ? FCM::Util::Reporter->PREFIX_INFO
    : $level > FCM::Util::Reporter->FAIL     ? FCM::Util::Reporter->PREFIX_WARN
    :                                          FCM::Util::Reporter->PREFIX_FAIL
    ;
}

# ------------------------------------------------------------------------------
1;
__END__

=head1 NAME

FCM::Reporter

=head1 SYNOPSIS

    use FCM::Util::Reporter;
    $reporter = FCM::Util::Reporter->new({verbosity => $verbosity});
    $reporter->($message);
    $reporter->(\@messages);
    $reporter->(sub {return @some_strings});
    $reporter->({level => $reporter->MEDIUM}, $message);

=head1 DESCRIPTION

A simple message reporter.

This module is part of L<FCM::Util|FCM::Util>. See also the description of the
$u->report() method in L<FCM::Util|FCM::Util>.

=head1 METHODS

=over 4

=item $class->new(\%attrib)

Returns a new instance of this class, which is a CODE reference. %attrib can
contain the following:

=over 4

=item ctx_of

A HASH containing a map to the named reporter contexts. At initialisation, a new
ctx for "stdout" and a new ctx for "stderr" is created automatically.

=back

=item $reporter->add_ctx($key,%option)

Creates a new reporter context, and adds it to the ctx_of HASH, if a context
with the same $key does not already exist. The %option is given to the
constructir of L</FCM::Util::Reporter::Context>. Return the context on success.

=item $reporter->del_ctx($key)

Removes a new reporter context named $key. Return the context on success.

=item $reporter->get_ctx($key)

Returns a named reporter context L</FCM::Util::Reporter::Context>.

=item $reporter->get_ctx_of_stderr()

Shorthand for $reporter->get_ctx('stderr').

=item $reporter->get_ctx_of_stdout()

Shorthand for $reporter->get_ctx('stdout').

=item $reporter->report(\%option,$message)

Reports the message. If %option is not given, reports using the default options.
In the form, the following %options can be specified:

=over 4

=item delimiter

The delimiter of each message in the list. The default is "\n". If the delimiter
is set to the empty string, the items in $message will be treated as raw
strings, i.e. it will also ignore any "prefix" options.

=item level

The level of the current message. The default is DEFAULT.

=item prefix

The message prefix. It can be a string or a CODE reference. If it is a string,
it is simply preprended to the message. If it is a code reference, it is calls
as $prefix_ref->($option{level}, $option{type}), and its result (if defined) is
prepended to the message.

=item type

The message type. It can be REPORT_ERR or REPORT_OUT (default).

=back

=back

=head1 CONSTANTS

=over 4

=item $reporter->FAIL, $reporter->QUIET

The verbosity level 0.

=item $reporter->DEFAULT, $reporter->LOW, $reporter->WARN

The verbosity level 1.

=item $reporter->MEDIUM

The verbosity level 2.

=item $reporter->HIGH

The verbosity level 3.

=item $reporter->DEBUG

The verbosity level 4.

=item $reporter->PREFIX_DONE

The prefix for a task "done" message.

=item $reporter->PREFIX_FAIL

The prefix for a fatal error message.

=item $reporter->PREFIX_INFO

The prefix for an "info" message.

=item $reporter->PREFIX_INIT

The prefix for a task "init" message.

=item $reporter->PREFIX_NULL

An empty string.

=item $reporter->PREFIX_QUIT

The prefix for a quit/abort message.

=item $reporter->PREFIX_WARN

The prefix for a warning message.

=item $reporter->REPORT_ERR

The message type for exception message.

=item $reporter->REPORT_OUT

The message type for info message.

=back

=head1 FCM::Util::Reporter::Context

An instance of this class represents the context for a reporter for the
L<FCM::Util->report()|FCM::Util>. This class is a sub-class of
L<FCM::Class::HASH|FCM::Class::HASH>. It has the following attributes:

=over 4

=item handle

The file handle for info messages. (Default=\*STDOUT)

=item prefix

The message prefix. It can be a string or a CODE reference. If it is a string,
it is simply preprended to the message. If it is a code reference, it is calls
as $prefix_ref->($option{level}, $option{type}), and its result (if defined) is
prepended to the message. The default is a CODE that returns PREFIX_INFO for
TYPE_OUT messages, PREFIX_WARN for TYPE_ERR messages at WARN level or above or
PREFIX_FAIL for TYPE_ERR messages at FAIL level.

=item type

Reporter type. (Default=TYPE_OUT)

=item verbosity

The verbosity of the reporter. Only messages at a level above or equal to the
verbosity will be reported. The default is DEFAULT.

=back


=head1 COPYRIGHT

(C) Crown copyright Met Office. All rights reserved.

=cut