Skip to content

Commit

Permalink
Fix callframe placeholder work with wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
atroxaper committed Apr 29, 2019
1 parent 7fc4dd6 commit 09eb9c1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
22 changes: 15 additions & 7 deletions lib/LogP6/Context.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,22 @@ method sync-put($trait, $obj) {
#| Gets callframe of log caller level.
method callframe() {
return $_ with $!callframe;
# start with 3. it is much save and optimal
for 3..* -> $level {

my $first-non-logp6;
my $in-logp6 = True;
my $inverts = 0;

for 0..* -> $level {
with callframe($level) -> $frame {
next unless $frame.code.name
~~ any('trace', 'debug', 'info', 'warn', 'error');
# +1 to caller code and +1 to go from `with` block
$!callframe = callframe($level + 2);
last;
if $in-logp6 ne (so $frame.file ~~ / '(LogP6' /) {
$in-logp6 = !$in-logp6;
given ++$inverts {
when 1 { $first-non-logp6 = $frame }
when 3 { $!callframe = $frame; last }
}
}
} else {
$!callframe = $first-non-logp6; last
}
}
return $!callframe;
Expand Down
2 changes: 1 addition & 1 deletion t/00-config-file.t
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ subtest {
my $wrapper = get-cliche('c2').wrapper;
does-ok $wrapper, LogP6::Wrapper::SyncEach::Wrapper, 'each wrapper parced';
is $wrapper.config-path, './t/resource/00-config-file/log-p6-2.json',
'each wpapper parced config-path';
'each wrapper parced config-path';
}, 'integration defaults';

subtest {
Expand Down
7 changes: 1 addition & 6 deletions t/00-grammar.t
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ is parse-process($level-line), '<stay-for-a-wile-and-listen>',
'level length 0 info';

# %frame*
sub info($frame) {
is parse-process('%framename %framefile %frameline').trim,
$frame.code.name ~ ' ' ~ $frame.file ~ ' ' ~ $frame.line, 'frames';
}
sub foo() { info(callframe) }
foo;
ok parse-process('%framename %framefile %frameline').trim.chars > 4, 'frames';

# %trait
$context.trait-set('LogP6::Writer::Async::Std');
Expand Down
22 changes: 15 additions & 7 deletions t/00-logger.t
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,27 @@ subtest {
}, 'mute logger';

subtest {
plan 1;
plan 2;
use LogP6::Wrapper::SyncEach;

cliche(:name<frame>, :matcher<frame>, grooves => (
cliche(:name<frame1>, :matcher<frame1>, grooves => (
writer(:pattern('%msg %framename %framefile %frameline'), :handle($h1)),
level($info)
));
my $frame = get-logger('frame');
cliche(:name<frame2>, :matcher<frame2>, grooves => (
writer(:pattern('%msg %framename %framefile %frameline'), :handle($h1)),
level($info)
), :wrapper(LogP6::Wrapper::SyncEach::Wrapper.new));
my $frame1 = get-logger('frame1');
my $frame2 = get-logger('frame2');

$frame.info('line 1');
$frame1.info('line 1');
my $l1 = $h1.clean;
$frame.info('line 2');
my $l2 = $h1.clean;
isnt $l1, $l2, 'frames are different';
$frame1.infof('line 2');
$frame2.infof('line 2');
my @l2 = $h1.clean.trim.split("\n");
isnt $l1, @l2[0], 'frames are different';
is @l2[1].substr(0, *-1), @l2[0].substr(0, *-1), 'frames are the same';
}, 'different frame';

subtest {
Expand Down

0 comments on commit 09eb9c1

Please sign in to comment.