| 1 | package eris::log::contexts; | ||||||||||||||||||||||
| 2 | # ABSTRACT: Discovery and access for context objects | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 2 | ▶ | use List::Util qw(any); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 2 | ▶ | use Ref::Util qw(is_ref is_arrayref is_coderef is_regexpref); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 2 | ▶ | use Time::HiRes qw(gettimeofday tv_interval); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 2 | ▶ | use Types::Standard qw( ArrayRef HashRef ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 2 | ▶ | use Moo; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 2 | with qw( | |||||||||||||||||||||
| 13 | eris::role::pluggable | ||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.008'; # VERSION | |||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 0 | ▶ | sub _build_namespace { 'eris::log::context' } | ||||||||||||||||||||
|
Subroutine
_build_namespace: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 2 | my $_lookup; | |||||||||||||||||||||
| 23 | sub contextualize { | ||||||||||||||||||||||
| 24 | 0 | ▶ | my ($self,$log) = @_; | ||||||||||||||||||||
|
Subroutine
contextualize: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 0 | my %t = (); | |||||||||||||||||||||
| 27 | 0 | foreach my $ctxt ( @{ $self->plugins } ) { | |||||||||||||||||||||
| 28 | 0 | my $field = $ctxt->field; | |||||||||||||||||||||
| 29 | 0 | my $matcher = $ctxt->matcher; | |||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | # Initialize to the catchall condition | ||||||||||||||||||||||
| 32 | 0 | ▶ | my $matched = $field eq '*' && $matcher eq '*'; | ||||||||||||||||||||
|
Condition0%0/3
$field eq '*' && $matcher eq '*'
MC/DC0%0/2
$field eq '*' && $matcher eq '*'
$field eq '*'
$matcher eq '*'
Truth table0%0/3
A && B
A= $field eq '*'B= $matcher eq '*'
| |||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 0 | ▶ | if( !$matched ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 35 | # log context maybe updated | ||||||||||||||||||||||
| 36 | 0 | my %c = %{ $log->context }; | |||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 0 | ▶ | if( $field eq '_exists_' ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 39 | # match against the key space | ||||||||||||||||||||||
| 40 | 0 | ▶ | if( !is_ref($matcher) ) { | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 41 | # simplest case string | ||||||||||||||||||||||
| 42 | 0 | $matched = exists $c{$matcher}; | |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | elsif( is_regexpref($matcher) ) { | ||||||||||||||||||||||
| 45 | # regexp match | ||||||||||||||||||||||
| 46 | 0 | ▶ | $matched = any { /$matcher/ } keys %c; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||
| 48 | elsif( is_arrayref($matcher) ) { | ||||||||||||||||||||||
| 49 | # list match | ||||||||||||||||||||||
| 50 | 0 | ▶ | $matched = any { exists $c{$_} } @{ $matcher }; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||
| 53 | elsif( exists $c{$field} ) { | ||||||||||||||||||||||
| 54 | 0 | ▶ | if( !is_ref($matcher) ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 55 | # Simplest case, we're a string | ||||||||||||||||||||||
| 56 | 0 | $matched = lc $c{$field} eq lc $matcher; | |||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||
| 58 | elsif( is_regexpref($matcher) ) { | ||||||||||||||||||||||
| 59 | # regexp match | ||||||||||||||||||||||
| 60 | 0 | $matched = $c{$field} =~ /$matcher/; | |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||
| 62 | elsif( is_arrayref($matcher) ) { | ||||||||||||||||||||||
| 63 | # list match | ||||||||||||||||||||||
| 64 | 0 | ▶ | $matched = any { lc $c{$field} eq lc $_ } @{ $matcher }; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | elsif( is_coderef($matcher) ) { | ||||||||||||||||||||||
| 67 | # call the code ref | ||||||||||||||||||||||
| 68 | eval { | ||||||||||||||||||||||
| 69 | 0 | $matched = $matcher->( $c{$field} ); | |||||||||||||||||||||
| 70 | 0 | 1; | |||||||||||||||||||||
| 71 | 0 | ▶ | } or do { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 72 | # Catch an exception in the matcher | ||||||||||||||||||||||
| 73 | 0 | my $err = $@; | |||||||||||||||||||||
| 74 | 0 | warn sprintf "[%s] matcher coderef died: %s", | |||||||||||||||||||||
| 75 | $ctxt->name, $err; | ||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||
| 80 | 0 | ▶ | if( $matched ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 81 | 0 | my $t0 = [gettimeofday]; | |||||||||||||||||||||
| 82 | 0 | $ctxt->contextualize_message($log); | |||||||||||||||||||||
| 83 | 0 | my $tdiff = tv_interval($t0); | |||||||||||||||||||||
| 84 | 0 | my $name = sprintf "context::%s", $ctxt->name; | |||||||||||||||||||||
| 85 | 0 | $t{$name} = $tdiff; | |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | # Record timing data | ||||||||||||||||||||||
| 90 | 0 | $log->add_timing(%t); | |||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | 0 | return $log; # Return the log object | |||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | 2 | 1; | |||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||
| 99 | |||||||||||||||||||||||
| 100 | =pod | ||||||||||||||||||||||
| 101 | |||||||||||||||||||||||
| 102 | =encoding UTF-8 | ||||||||||||||||||||||
| 103 | |||||||||||||||||||||||
| 104 | =head1 NAME | ||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | eris::log::contexts - Discovery and access for context objects | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | =head1 VERSION | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | version 0.008 | ||||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | This module isn't meant to be a public interface, you should probably see: | ||||||||||||||||||||||
| 115 | L<eris::log::contextualizer> so you're doing it right. | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | =head1 ATTRIBUTES | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | =head2 namespace | ||||||||||||||||||||||
| 120 | |||||||||||||||||||||||
| 121 | Defaults to 'eris::log::context', add more by setting C<search_path> in your | ||||||||||||||||||||||
| 122 | config. | ||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | --- | ||||||||||||||||||||||
| 125 | contexts: | ||||||||||||||||||||||
| 126 | search_path: | ||||||||||||||||||||||
| 127 | - 'my::app::context' | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | =head1 METHODS | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | =head2 contextualize | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | Takes an instance of an L<eris::log> object that's been returned from a | ||||||||||||||||||||||
| 134 | a call to L<eris::log::decoders>'s C<decode()> method. | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | Discovers and iterates through available contexts. Any contexts which | ||||||||||||||||||||||
| 137 | successfully match will be passed the L<eris::log> instance to their | ||||||||||||||||||||||
| 138 | C<contextualize_message> method. | ||||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | =head1 SEE ALSO | ||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | L<eris::log::contextualizer>, L<eris::role::context>, L<eris::log>, | ||||||||||||||||||||||
| 143 | L<eris::log::contexts::sshd> | ||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | =head1 AUTHOR | ||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | Brad Lhotsky <brad@divisionbyzero.net> | ||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||||||||||||||||||||||
| 152 | |||||||||||||||||||||||
| 153 | This is free software, licensed under: | ||||||||||||||||||||||
| 154 | |||||||||||||||||||||||
| 155 | The (three-clause) BSD License | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||