| 1 | package Test2::Util::Trace; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '1.302073'; | ||
| 6 | |||
| 7 | |||
| 8 | use Test2::Util qw/get_tid pkg_to_file/; | ||
| 9 | |||
| 10 | use Carp qw/confess/; | ||
| 11 | |||
| 12 | use Test2::Util::HashBase qw{frame detail pid tid}; | ||
| 13 | |||
| 14 | sub init { | ||
| 15 | confess "The 'frame' attribute is required" | ||
| 16 | unless $_[0]->{+FRAME}; | ||
| 17 | |||
| 18 | $_[0]->{+PID} = $$ unless defined $_[0]->{+PID}; | ||
| 19 | $_[0]->{+TID} = get_tid() unless defined $_[0]->{+TID}; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub snapshot { bless {%{$_[0]}}, __PACKAGE__ }; | ||
| 23 | |||
| 24 | sub debug { | ||
| 25 | my $self = shift; | ||
| 26 | return $self->{+DETAIL} if $self->{+DETAIL}; | ||
| 27 | my ($pkg, $file, $line) = $self->call; | ||
| 28 | return "at $file line $line"; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub alert { | ||
| 32 | my $self = shift; | ||
| 33 | my ($msg) = @_; | ||
| 34 | warn $msg . ' ' . $self->debug . ".\n"; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub throw { | ||
| 38 | my $self = shift; | ||
| 39 | my ($msg) = @_; | ||
| 40 | die $msg . ' ' . $self->debug . ".\n"; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub call { @{$_[0]->{+FRAME}} } | ||
| 44 | |||
| 45 | sub package { $_[0]->{+FRAME}->[0] } | ||
| 46 | sub file { $_[0]->{+FRAME}->[1] } | ||
| 47 | sub line { $_[0]->{+FRAME}->[2] } | ||
| 48 | sub subname { $_[0]->{+FRAME}->[3] } | ||
| 49 | |||
| 50 | sub from_json { | ||
| 51 | my $class = shift; | ||
| 52 | my %p = @_; | ||
| 53 | |||
| 54 | my $trace_pkg = delete $p{__PACKAGE__}; | ||
| 55 | require(pkg_to_file($trace_pkg)); | ||
| 56 | |||
| 57 | return $trace_pkg->new(%p); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub TO_JSON { | ||
| 61 | my $self = shift; | ||
| 62 | return {%$self, __PACKAGE__ => ref $self}; | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | |||
| 67 | __END__ |