aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2020-04-20 13:23:11 +0200
committerPatrick Spek <p.spek@tyil.nl>2020-04-20 13:23:11 +0200
commit73ca4195d3d19499342b61265bd162adb8481214 (patch)
tree471eefc7e574e169105e749ad30d58d13e82c5d9
Initial commit
-rw-r--r--LICENSE.txt165
-rw-r--r--META6.json20
-rw-r--r--README.rakudoc42
-rw-r--r--lib/Log/Colored.rakumod305
4 files changed, 532 insertions, 0 deletions
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..0a04128
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,165 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+ This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+ 0. Additional Definitions.
+
+ As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+ "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+ An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+ A "Combined Work" is a work produced by combining or linking an
+Application with the Library. The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+ The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+ The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+ 1. Exception to Section 3 of the GNU GPL.
+
+ You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+ 2. Conveying Modified Versions.
+
+ If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+ a) under this License, provided that you make a good faith effort to
+ ensure that, in the event an Application does not supply the
+ function or data, the facility still operates, and performs
+ whatever part of its purpose remains meaningful, or
+
+ b) under the GNU GPL, with none of the additional permissions of
+ this License applicable to that copy.
+
+ 3. Object Code Incorporating Material from Library Header Files.
+
+ The object code form of an Application may incorporate material from
+a header file that is part of the Library. You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+ a) Give prominent notice with each copy of the object code that the
+ Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the object code with a copy of the GNU GPL and this license
+ document.
+
+ 4. Combined Works.
+
+ You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+ a) Give prominent notice with each copy of the Combined Work that
+ the Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
+ document.
+
+ c) For a Combined Work that displays copyright notices during
+ execution, include the copyright notice for the Library among
+ these notices, as well as a reference directing the user to the
+ copies of the GNU GPL and this license document.
+
+ d) Do one of the following:
+
+ 0) Convey the Minimal Corresponding Source under the terms of this
+ License, and the Corresponding Application Code in a form
+ suitable for, and under terms that permit, the user to
+ recombine or relink the Application with a modified version of
+ the Linked Version to produce a modified Combined Work, in the
+ manner specified by section 6 of the GNU GPL for conveying
+ Corresponding Source.
+
+ 1) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (a) uses at run time
+ a copy of the Library already present on the user's computer
+ system, and (b) will operate properly with a modified version
+ of the Library that is interface-compatible with the Linked
+ Version.
+
+ e) Provide Installation Information, but only if you would otherwise
+ be required to provide such information under section 6 of the
+ GNU GPL, and only to the extent that such information is
+ necessary to install and execute a modified version of the
+ Combined Work produced by recombining or relinking the
+ Application with a modified version of the Linked Version. (If
+ you use option 4d0, the Installation Information must accompany
+ the Minimal Corresponding Source and Corresponding Application
+ Code. If you use option 4d1, you must provide the Installation
+ Information in the manner specified by section 6 of the GNU GPL
+ for conveying Corresponding Source.)
+
+ 5. Combined Libraries.
+
+ You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+ a) Accompany the combined library with a copy of the same work based
+ on the Library, uncombined with any other library facilities,
+ conveyed under the terms of this License.
+
+ b) Give prominent notice with the combined library that part of it
+ is a work based on the Library, and explaining where to find the
+ accompanying uncombined form of the same work.
+
+ 6. Revised Versions of the GNU Lesser General Public License.
+
+ The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+ If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
diff --git a/META6.json b/META6.json
new file mode 100644
index 0000000..ff0cd85
--- /dev/null
+++ b/META6.json
@@ -0,0 +1,20 @@
+{
+ "api": "0",
+ "auth": "",
+ "authors": [
+ "Patrick Spek <p.spek@tyil.nl>"
+ ],
+ "depends": [
+ "Log",
+ "Terminal::ANSIColor"
+ ],
+ "description": "A Log implementation with colored output",
+ "license": "LGPL-3.0-only",
+ "name": "Log::Colored",
+ "perl": "6.d",
+ "provides": {
+ "Log::Colored": "lib/Log/Colored.rakumod"
+ },
+ "source-url": "",
+ "version": "0.0.0"
+}
diff --git a/README.rakudoc b/README.rakudoc
new file mode 100644
index 0000000..b54ccc5
--- /dev/null
+++ b/README.rakudoc
@@ -0,0 +1,42 @@
+=begin pod
+
+=NAME Log::Colored
+=VERSION 0.0.0
+=AUTHOR Patrick Spek <p.spek@tyil.nl>
+
+=head1 Description
+
+A Log implementation with colored output. This is based upon C<Log::Simple>,
+with some changes to no longer show the level name in the log messages, and
+instead use colors to denote severity.
+
+=head1 Installation
+
+Install this module through L<zef|https://github.com/ugexe/zef>:
+
+=begin code :lang<sh>
+zef install Log::Colored
+=end code
+
+=head1 Documentation
+
+Documentation is written as L<Pod6|https://docs.raku.org/language/pod>
+documents, and can be read with the
+L<C<p6doc>|https://modules.raku.org/dist/p6doc:github:perl6> module.
+
+=begin input
+p6doc Log::Colored
+=end input
+
+At your option, you can also use prettier readers, such as
+L<C<App::Rakuman>|https://modules.raku.org/dist/App::Rakuman:cpan:TYIL>.
+
+=begin input
+rakuman Log::Colored
+=end input
+
+=head1 License
+
+This module is distributed under the terms of the LGPL-3.0-only.
+
+=end pod
diff --git a/lib/Log/Colored.rakumod b/lib/Log/Colored.rakumod
new file mode 100644
index 0000000..239157e
--- /dev/null
+++ b/lib/Log/Colored.rakumod
@@ -0,0 +1,305 @@
+#! /usr/bin/env false
+
+use v6.d;
+
+use Log;
+use Log::Level;
+use Terminal::ANSIColor;
+
+#| An implementation of Log with colors.
+unit class Log::Colored is Log;
+
+#| An array of hashes. Each has must have a handle key, with an IO::Handle to
+#| print messages to. A level key with an integer to signify a Log::Level is
+#| also required.
+has @!outputs;
+
+#| A supplier to emit messages to.
+has Supplier $!messages;
+
+#| Send an emergency message.
+multi method emergency (
+ #| The message to print.
+ Str:D $message,
+) {
+ $!messages.emit({
+ message => self!message($message, 'bold underline red'),
+ level => 0,
+ })
+}
+
+#| Send a formatted emergency message.
+multi method emergency (
+ #| A printf-style format string.
+ Str:D $format,
+
+ #| Arguments to substitute into the format string placeholders.
+ *@args,
+) {
+ $!messages.emit({
+ message => self!message($format.sprintf(|@args), 'bold underline red'),
+ level => 0,
+ })
+}
+
+#| Send an alert message.
+multi method alert (
+ #| The message to print.
+ Str:D $message,
+) {
+ $!messages.emit({
+ message => self!message($message, 'bold red'),
+ level => 1,
+ })
+}
+
+#| Send a formatted alert message.
+multi method alert (
+ #| A printf-style format string.
+ Str:D $format,
+
+ #| Arguments to substitute into the format string placeholders.
+ *@args,
+) {
+ $!messages.emit({
+ message => self!message($format.sprintf(|@args), 'bold red'),
+ level => 1,
+ })
+}
+
+#| Send a critical message.
+multi method critical (
+ #| The message to print.
+ Str:D $message,
+) {
+ $!messages.emit({
+ message => self!message($message, 'red'),
+ level => 2,
+ })
+}
+
+#| Send a formatted critical message.
+multi method critical (
+ #| A printf-style format string.
+ Str:D $format,
+
+ #| Arguments to substitute into the format string placeholders.
+ *@args,
+) {
+ $!messages.emit({
+ message => self!message($format.sprintf(|@args), 'red'),
+ level => 2,
+ })
+}
+
+#| Send an error message.
+multi method error (
+ #| The message to print.
+ Str:D $message,
+) {
+ $!messages.emit({
+ message => self!message($message, 'bold yellow'),
+ level => 3,
+ })
+}
+
+#| Send a formatted error message.
+multi method error (
+ #| A printf-style format string.
+ Str:D $format,
+
+ #| Arguments to substitute into the format string placeholders.
+ *@args,
+) {
+ $!messages.emit({
+ message => self!message($format.sprintf(|@args), 'bold yellow'),
+ level => 3,
+ })
+}
+
+#| Send a warning message.
+multi method warning (
+ #| The message to print.
+ Str:D $message,
+) {
+ $!messages.emit({
+ message => self!message($message, 'yellow'),
+ level => 4,
+ })
+}
+
+#| Send a formatted warning message.
+multi method warning (
+ #| A printf-style format string.
+ Str:D $format,
+
+ #| Arguments to substitute into the format string placeholders.
+ *@args,
+) {
+ $!messages.emit({
+ message => self!message($format.sprintf(|@args), 'yellow'),
+ level => 4,
+ })
+}
+
+#| Send a notice message. This is the "normal" level to publish log entries on.
+multi method notice (
+ #| The message to print.
+ Str:D $message
+) {
+ $!messages.emit({
+ message => self!message($message, 'default'),
+ level => 5,
+ })
+}
+
+#| Send a formatted notice message. This is the "normal" level to publish log
+#| entries on.
+multi method notice (
+ #| A printf-style format string.
+ Str:D $format,
+
+ #| Arguments to substitute into the format string placeholders.
+ *@args,
+) {
+ $!messages.emit({
+ message => self!message($format.sprintf(|@args), 'default'),
+ level => 5,
+ })
+}
+
+#| Send an informational message.
+multi method info (
+ #| The message to print.
+ Str:D $message
+) {
+ $!messages.emit({
+ message => self!message($message, 'cyan'),
+ level => 6,
+ })
+}
+
+#| Send a formatted informational message.
+multi method info (
+ #| The printf-style format string.
+ Str:D $format,
+
+ #| Arguments to substitute into the format string placeholders.
+ *@args,
+) {
+ $!messages.emit({
+ message => self!message($format.sprintf(|@args), 'cyan'),
+ level => 6,
+ })
+}
+
+#| Send a debug message.
+multi method debug (
+ #| The message to print.
+ Str:D $message,
+) {
+ $!messages.emit({
+ message => self!message($message, 'black'),
+ level => 7,
+ })
+}
+
+#| Send a formatted debug message.
+multi method debug (
+ #| A printf-style format string.
+ Str:D $format,
+
+ #| Arguments to substitute into the format string placeholders.
+ *@args,
+) {
+ $!messages.emit({
+ message => self!message($format.sprintf(|@args), 'black'),
+ level => 7,
+ })
+}
+
+#| Add an output to the logger.
+multi method add-output (
+ #| The IO::Handle to write log messages to.
+ IO::Handle:D $handle,
+
+ #| The log level for this particular stream.
+ Int() $level where Log::Level::Emergency ≤ * ≤ Log::Level::Debug,
+
+ #| A filter function to apply to any messages sent to the output.
+ Callable :$filter?,
+) {
+ @!outputs.push({
+ :$handle,
+ :$level,
+ :$filter,
+ });
+}
+
+#| Convenience method to get the callee name. This is the package name if one
+#| exists, or the source file name.
+method !callee (
+ #| The depth of which to get the callframe.
+ Int:D $depth = 4,
+
+ --> Str
+) {
+ my $file = callframe($depth).annotations<file>;
+
+ return $file unless $file.ends-with(')');
+
+ $file.words.tail.substr(1, *-1);
+}
+
+#| Convenience method for formatting the message as desired.
+method !message (
+ #| The original message to be logged.
+ Str:D $message,
+
+ #| A human readable identifier for the level.
+ Str:D $color,
+
+ --> Str
+) {
+ colored("{DateTime.now.utc} {self!callee}: $message", $color);
+}
+
+submethod TWEAK
+{
+ # Set up the Supply for messages.
+ $!messages .= new;
+ $!messages.Supply.tap(sub (%payload) {
+ @!outputs
+ .race
+ .grep(%payload<level> ≤ *<level>)
+ .grep({ $_<filter> ?? $_<filter>(%payload).so !! True })
+ .map(*<handle>.say(%payload<message>))
+ ;
+ });
+}
+
+=begin pod
+
+=NAME Log::Colored
+=VERSION 0.0.0
+=AUTHOR Patrick Spek <p.spek@tyil.nl>
+
+=begin LICENSE
+Copyright © 2020
+
+This program is free software: you can redistribute it and/or modify it under
+the terms of the GNU Lesser General Public License as published by the Free
+Software Foundation, version 3.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program. If not, see http://www.gnu.org/licenses/.
+=end LICENSE
+
+=end pod
+
+# vim: ft=raku noet sw=8 ts=8