Skip to content

Commit

Permalink
docs: Write a whole bunch of doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Micha-ohne-el committed Feb 11, 2024
1 parent 288dbab commit dd06a99
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/commonMain/kotlin/moe/micha/logbook/Logbook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ import moe.micha.logbook.pretty.Colorable
import moe.micha.logbook.pretty.formatWithSimplePattern
import moe.micha.logbook.pretty.local

/**
* Base class for all logbooks.
*
* ## Usage
* To create your own logbook, create a subclass of [Logbook].
* You can make it an object, but it is recommended to use the following pattern:
* ```kt
* abstract class MyLog : Logbook() {
* companion object : MyLog()
* }
* ```
* This way, you can create another logger that inherits from your base logger:
* ```kt
* abstract class MySpecificLog : MyLog() {
* companion object : MySpecificLog()
* }
* ```
*
* ## Note
* The [Logbook] class itself comes with the most minimal configuration possible, **probably too minimal for you**.
* For this reason, you can choose to use [Logbook.WithDefaults] instead, which comes preconfigured with four [LogLevel]s –
* each set up with an [AnsiConsoleOutlet] – a nice format, and a randomized name color so you can tell different loggers apart.
* See [Logbook.WithDefaults] for more info.
*/
abstract class Logbook : Colorable, CanFormat {
open val name: String = this::class.simpleName ?: throw Error("Anonymous Logbooks must provide a name explicitly.")

Expand Down Expand Up @@ -59,6 +83,28 @@ abstract class Logbook : Colorable, CanFormat {
level(name, placeBefore = null, outlets = outlets, config)


/**
* Convenience class that provides a lot of useful features that most logbooks will want.
* You can override anything you don't like, of course.
*
* ## Features
* ### Four preconfigured [LogLevel]s
* * [debug] – green text, no background
* * [info] – yellow text, no background
* * [warning] – red text, no background
* * [error] – white text, bright red background
*
* **The `minimumLogLevel` is set to [info].**
*
* ### There is a default format set
* ```
* [DD.MM.YYYY@hh:mm:ss.fff] LogbookName : LogLevelName – data
* ```
* `LogbookName` has a randomly assigned color per logbook. This helps to distinguish logbooks from one another.
* `LogLevelName` has a specific color per log level, as specified above.
*
* ### All levels output to an [AnsiConsoleOutlet].
*/
abstract class WithDefaults(random: Random = Random.Default) : Logbook() {
override fun format(entry: LogEntry) =
listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package moe.micha.logbook.outlets
import moe.micha.logbook.pretty.Color
import moe.micha.logbook.pretty.ColorInfo

/**
* A very commonly used outlet that will color log messages correctly for most modern terminals.
*
* For info on how the colorization is achieved, see:
* [Wikipedia: ANSI escape codes > Select Graphics Rendition](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR)
*/
open class AnsiConsoleOutlet : ColoredConsoleOutlet() {
override fun colorize(text: String, colorInfo: ColorInfo): String {
if (colorInfo.foreground == null && colorInfo.background == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package moe.micha.logbook.outlets
import moe.micha.logbook.pretty.Chunk
import moe.micha.logbook.pretty.ColorInfo

/**
* Base class for console outlets who wish to color their output.
*/
abstract class ColoredConsoleOutlet : ConsoleOutlet() {
override fun print(chunk: Chunk) = print(colorizeIfNeeded(chunk))

Expand Down
17 changes: 17 additions & 0 deletions src/commonMain/kotlin/moe/micha/logbook/outlets/ConsoleOutlet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import moe.micha.logbook.LogEntry
import moe.micha.logbook.LogOutlet
import moe.micha.logbook.pretty.Chunk

/**
* Base class for all console outlets.
*
* Prints all chunks received using consecutive calls to [print], then calls [flush].
* Both of those can be overridden.
*/
open class ConsoleOutlet : LogOutlet {
override fun send(chunks: Iterable<Chunk>) {
for (chunk in chunks) {
Expand All @@ -16,6 +22,17 @@ open class ConsoleOutlet : LogOutlet {
override var formatter: ((LogEntry) -> Iterable<Chunk>)? = null


/**
* Prints the [chunk] to the console.
*
* This should not print a trailing newline!
*
* By default, this only prints [Chunk.text], ignoring any other property [Chunk] may have.
*/
protected open fun print(chunk: Chunk) = print(chunk.text)

/**
* Ends the log entry (by default by printing a newline).
*/
protected open fun flush() = println()
}

0 comments on commit dd06a99

Please sign in to comment.