Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created June 26, 2022 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfldex/b9a0c2292ad6464fbcbff3e27c8d8bac to your computer and use it in GitHub Desktop.
Save gfldex/b9a0c2292ad6464fbcbff3e27c8d8bac to your computer and use it in GitHub Desktop.
use Log;
dd ERROR('foo') ~~ LogLevel::ERROR;
dd ERROR('foo') ~~ LogLevel::DEBUG;
say ERROR('foo').&{ .file, .line };
ERROR 'foo';
VERBOSE 'Detailed, very much so indeed.';
my $*LOGLEVEL = 2;
VERBOSE 'Detailed, very much so indeed.';
use v6.d;
my role Functor {
has $.message;
my $.level;
has $.captured-loglevel;
has $.file;
has $.line;
method CALL-ME(*@message) {
my ($line, $file) = callframe(1).&{ .line, .file };
self.new: message => "$file:$line " ~ @message.join($?NL), :captured-loglevel($*LOGLEVEL // 0), :$line, :$file;
}
method sink { $*ERR.put(self.message) if ($!captured-loglevel // 0) ≥ $.level }
method Str(::?CLASS:D:) { self.message }
method gist(::?CLASS:D:) { self.^shortname ~ ' ' ~ self.message }
}
my $VERBOSE;
my $DEBUG;
my class ERROR {...}
class LogLevel {
constant ERROR = ERROR;
constant VERBOSE := $VERBOSE;
constant DEBUG := $DEBUG;
}
my class ERROR is LogLevel does Callable does Functor { my $.level = 1; }
$VERBOSE = class :: is LogLevel does Callable does Functor { my $.level = 2; }
$DEBUG = class :: is LogLevel does Callable does Functor { my $.level = 3; }
sub EXPORT {
Map.new: '&ERROR' => ERROR,
'&VERBOSE' => $VERBOSE,
'&DEBUG' => $DEBUG,
'LogLevel' => LogLevel,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment