| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Plugin::TestServerWarnings; | ||
| 5 | use base qw/Jifty::Plugin/; | ||
| 6 | |||
| 7 | use LWP::Simple qw//; | ||
| 8 | use Jifty::Plugin::TestServerWarnings::Appender; | ||
| 9 | use Log::Log4perl::Level; | ||
| 10 | |||
| 11 | __PACKAGE__->mk_accessors(qw(clear_screen)); | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Jifty::Plugin::TestServerWarnings - Stores server warnings away for later fetching | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | # In your jifty config.yml under the framework section: | ||
| 20 | |||
| 21 | Plugins: | ||
| 22 | - TestServerWarnings: | ||
| 23 | clear_screen: 1 | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This plugin add a new appender L<Jifty::Plugin::TestServerWarnings::Appender> | ||
| 28 | on the first request it sees, which stores away all messages it receives. | ||
| 29 | It also removes the default "Screen" appender unless clear_screen in | ||
| 30 | config.yml is set to be false or env TEST_VERBOSE is true. | ||
| 31 | |||
| 32 | The warnings can be retrieved by a client-side process by calling | ||
| 33 | L</decoded_warnings> with a base URI to the server. | ||
| 34 | |||
| 35 | This plugin is automatically added for all jifty tests. | ||
| 36 | |||
| 37 | =head1 METHODS | ||
| 38 | |||
| 39 | =head2 init | ||
| 40 | |||
| 41 | set clear_screen to 1 if the clear_screen in config.yml is set to be true, | ||
| 42 | if it's not set at all, set it to 1 if TEST_VERBOSE is set to be true. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub init { | ||
| 47 | my $self = shift; | ||
| 48 | my %opt = @_; | ||
| 49 | if ( defined $opt{clear_screen} ) { | ||
| 50 | $self->clear_screen( 1 ) if $opt{clear_screen}; | ||
| 51 | } | ||
| 52 | elsif ( ! $ENV{TEST_VERBOSE} ) { | ||
| 53 | $self->clear_screen( 1 ); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | =head2 new_request | ||
| 58 | |||
| 59 | On the first call to new_request, the plugin adjusts the appenders. | ||
| 60 | This causes it to only have effect if it is run in a forked server | ||
| 61 | process, not in a test process. If C<TEST_VERBOSE> is set, it does | ||
| 62 | not remove the Screen appender. | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub new_request { | ||
| 67 | my $self = shift; | ||
| 68 | return if $self->{init}++; | ||
| 69 | |||
| 70 | Log::Log4perl->eradicate_appender("Screen") if $self->clear_screen; | ||
| 71 | |||
| 72 | my $a = Jifty::Plugin::TestServerWarnings::Appender->new(name => "TestServerAppender"); | ||
| 73 | Log::Log4perl->get_logger("")->add_appender($a); | ||
| 74 | Log::Log4perl->get_logger("")->level($WARN); | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 add_warnings WARN, WARN, .. | ||
| 78 | |||
| 79 | Takes the given warnings, and stores them away for later reporting. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub add_warnings { | ||
| 84 | my $self = shift; | ||
| 85 | push @{ $self->{'stashed_warnings'} }, @_; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 stashed_warnings | ||
| 89 | |||
| 90 | Returns the stored warnings, as a list. This does not clear the list, | ||
| 91 | unlike L</encoded_warnings> or L</decoded_warnings>. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | |||
| 96 | sub stashed_warnings { | ||
| 97 | my $self = shift; | ||
| 98 | return @{ $self->{'stashed_warnings'} || [] }; | ||
| 99 | } | ||
| 100 | |||
| 101 | =head2 encoded_warnings | ||
| 102 | |||
| 103 | Returns the stored warnings, encoded using L<Storable>. This also | ||
| 104 | clears the list of stashed warnings. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub encoded_warnings { | ||
| 109 | my $self = shift; | ||
| 110 | my @warnings = splice @{ $self->{'stashed_warnings'} }; | ||
| 111 | |||
| 112 | return Storable::nfreeze(\@warnings); | ||
| 113 | } | ||
| 114 | |||
| 115 | =head2 decoded_warnings URI | ||
| 116 | |||
| 117 | Given the URI to a jifty server with this plugin enabled, retrieves | ||
| 118 | and decodes the stored warnings, returning them. This will also clear | ||
| 119 | the server's stored list of warnings. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub decoded_warnings { | ||
| 124 | my $self = shift; | ||
| 125 | my $base = shift; | ||
| 126 | |||
| 127 | my $Test = Jifty::Test->builder; | ||
| 128 | |||
| 129 | if ($Jifty::SERVER && $Jifty::SERVER->isa('Jifty::TestServer::Inline')) { | ||
| 130 | return splice @{ $self->{'stashed_warnings'} }; | ||
| 131 | } | ||
| 132 | |||
| 133 | my $uri = URI->new_abs( "/__jifty/test_warnings", $base ); | ||
| 134 | my $text = LWP::Simple::get($uri); | ||
| 135 | |||
| 136 | return @{ Storable::thaw($text) || [] }; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; |