1 package Test2::EventFacet::Trace;
5 our $VERSION = '1.302101';
7 BEGIN { require Test2::EventFacet; our @ISA = qw(Test2::EventFacet) }
9 use Test2::Util qw/get_tid pkg_to_file/;
12 use Test2::Util::HashBase qw{^frame ^pid ^tid ^cid -hid -nested details -buffered};
18 *set_detail = \&set_details;
22 confess "The 'frame' attribute is required"
23 unless $_[0]->{+FRAME};
25 $_[0]->{+DETAILS} = delete $_[0]->{detail} if $_[0]->{detail};
27 $_[0]->{+PID} = $$ unless defined $_[0]->{+PID};
28 $_[0]->{+TID} = get_tid() unless defined $_[0]->{+TID};
32 my ($orig, @override) = @_;
33 bless {%$orig, @override}, __PACKAGE__;
39 # Signature is only valid if all of these fields are defined, there is no
40 # signature if any is missing. '0' is ok, but '' is not.
41 return join ':' => map { (defined($_) && length($_)) ? $_ : return undef } (
52 return $self->{+DETAILS} if $self->{+DETAILS};
53 my ($pkg, $file, $line) = $self->call;
54 return "at $file line $line";
60 warn $msg . ' ' . $self->debug . ".\n";
66 die $msg . ' ' . $self->debug . ".\n";
69 sub call { @{$_[0]->{+FRAME}} }
71 sub package { $_[0]->{+FRAME}->[0] }
72 sub file { $_[0]->{+FRAME}->[1] }
73 sub line { $_[0]->{+FRAME}->[2] }
74 sub subname { $_[0]->{+FRAME}->[3] }
86 Test2::EventFacet::Trace - Debug information for events
90 The L<Test2::API::Context> object, as well as all L<Test2::Event> types need to
91 have access to information about where they were created. This object
92 represents that information.
96 use Test2::EventFacet::Trace;
98 my $trace = Test2::EventFacet::Trace->new(
99 frame => [$package, $file, $line, $subname],
106 =item $string = $trace->{details}
108 =item $string = $trace->details()
110 Used as a custom trace message that will be used INSTEAD of
111 C<< at <FILE> line <LINE> >> when calling C<< $trace->debug >>.
113 =item $frame = $trace->{frame}
115 =item $frame = $trace->frame()
117 Get the call frame arrayref.
119 =item $int = $trace->{pid}
121 =item $int = $trace->pid()
123 The process ID in which the event was generated.
125 =item $int = $trace->{tid}
127 =item $int = $trace->tid()
129 The thread ID in which the event was generated.
131 =item $id = $trace->{cid}
133 =item $id = $trace->cid()
135 The ID of the context that was used to create the event.
137 =item $hid = $trace->{hid}
139 =item $hid = $trace->hid()
141 The ID of the hub that was current when the event was created.
143 =item $int = $trace->{nested}
145 =item $int = $trace->nested()
147 How deeply nested the event is.
149 =item $bool = $trace->{buffered}
151 =item $bool = $trace->buffered()
153 True if the event was buffered and not sent to the formatter independent of a
154 parent (This should never be set when nested is C<0> or C<undef>).
160 B<Note:> All facet frames are also methods.
164 =item $trace->set_detail($msg)
166 =item $msg = $trace->detail
168 Used to get/set a custom trace message that will be used INSTEAD of
169 C<< at <FILE> line <LINE> >> when calling C<< $trace->debug >>.
171 C<detail()> is an alias to the C<details> facet field for backwards
174 =item $str = $trace->debug
176 Typically returns the string C<< at <FILE> line <LINE> >>. If C<detail> is set
177 then its value will be returned instead.
179 =item $trace->alert($MESSAGE)
181 This issues a warning at the frame (filename and line number where
182 errors should be reported).
184 =item $trace->throw($MESSAGE)
186 This throws an exception at the frame (filename and line number where
187 errors should be reported).
189 =item ($package, $file, $line, $subname) = $trace->call()
191 Get the caller details for the debug-info. This is where errors should be
194 =item $pkg = $trace->package
196 Get the debug-info package.
198 =item $file = $trace->file
200 Get the debug-info filename.
202 =item $line = $trace->line
204 Get the debug-info line number.
206 =item $subname = $trace->subname
208 Get the debug-info subroutine name.
210 =item $sig = trace->signature
212 Get a signature string that identifies this trace. This is used to check if
213 multiple events are related. The Trace includes pid, tid, file, line number,
214 and the cid which is C<'C\d+'> for traces created by a context, or C<'T\d+'>
215 for traces created by C<new()>.
221 The source code repository for Test2 can be found at
222 F<http://github.com/Test-More/test-more/>.
228 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
236 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
242 Copyright 2016 Chad Granum E<lt>exodist@cpan.orgE<gt>.
244 This program is free software; you can redistribute it and/or
245 modify it under the same terms as Perl itself.
247 See F<http://dev.perl.org/licenses/>