1 package Test2::Util::Trace;
5 our $VERSION = '1.302026';
8 use Test2::Util qw/get_tid/;
12 use Test2::Util::HashBase qw{frame detail pid tid};
15 confess "The 'frame' attribute is required"
16 unless $_[0]->{+FRAME};
18 $_[0]->{+PID} = $$ unless defined $_[0]->{+PID};
19 $_[0]->{+TID} = get_tid() unless defined $_[0]->{+TID};
22 sub snapshot { bless {%{$_[0]}}, __PACKAGE__ };
26 return $self->{+DETAIL} if $self->{+DETAIL};
27 my ($pkg, $file, $line) = $self->call;
28 return "at $file line $line";
34 warn $msg . ' ' . $self->debug . ".\n";
40 die $msg . ' ' . $self->debug . ".\n";
43 sub call { @{$_[0]->{+FRAME}} }
45 sub package { $_[0]->{+FRAME}->[0] }
46 sub file { $_[0]->{+FRAME}->[1] }
47 sub line { $_[0]->{+FRAME}->[2] }
48 sub subname { $_[0]->{+FRAME}->[3] }
60 Test2::Util::Trace - Debug information for events
64 The L<Test2::API::Context> object, as well as all L<Test2::Event> types need to
65 have access to information about where they were created. This object
66 represents that information.
70 use Test2::Util::Trace;
72 my $trace = Test2::Util::Trace->new(
73 frame => [$package, $file, $line, $subname],
80 =item $trace->set_detail($msg)
82 =item $msg = $trace->detail
84 Used to get/set a custom trace message that will be used INSTEAD of
85 C<< at <FILE> line <LINE> >> when calling C<< $trace->debug >>.
87 =item $str = $trace->debug
89 Typically returns the string C<< at <FILE> line <LINE> >>. If C<detail> is set
90 then its value will be returned instead.
92 =item $trace->alert($MESSAGE)
94 This issues a warning at the frame (filename and line number where
95 errors should be reported).
97 =item $trace->throw($MESSAGE)
99 This throws an exception at the frame (filename and line number where
100 errors should be reported).
102 =item $frame = $trace->frame()
104 Get the call frame arrayref.
106 =item ($package, $file, $line, $subname) = $trace->call()
108 Get the caller details for the debug-info. This is where errors should be
111 =item $pkg = $trace->package
113 Get the debug-info package.
115 =item $file = $trace->file
117 Get the debug-info filename.
119 =item $line = $trace->line
121 Get the debug-info line number.
123 =item $subname = $trace->subname
125 Get the debug-info subroutine name.
131 The source code repository for Test2 can be found at
132 F<http://github.com/Test-More/test-more/>.
138 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
146 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
152 Copyright 2016 Chad Granum E<lt>exodist@cpan.orgE<gt>.
154 This program is free software; you can redistribute it and/or
155 modify it under the same terms as Perl itself.
157 See F<http://dev.perl.org/licenses/>