Skip to content

Commit

Permalink
feat(makeParsedLogger): make a parsed logger or a raw logger
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Jul 11, 2024
1 parent 7c1753e commit f74fb74
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
36 changes: 18 additions & 18 deletions src/Billet.mad
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Comparison } from "Compare"
import type { Maybe } from "Maybe"

import { EQ, GT, LT } from "Compare"
import { equals, when } from "Function"
import { always, equals, when } from "Function"
import IO from "IO"
import List from "List"
import { Just, Nothing, fromMaybe, isJust } from "Maybe"
Expand Down Expand Up @@ -121,16 +121,15 @@ export serializeTag = (t) => {
return name ++ (equals("", scope) ? "" : ":" ++ scope)
}

// TODO: support negated tags next
checkTagScopes :: List Tag -> Tag -> Boolean
export checkTagScopes = (tags, t) => pipe(
List.filter(
pipe(
getId,
pipe(
getId,
equals,
)(t),
),
(u) => {
uId = getId(u)
tId = getId(t)
return tId == "*" || uId == tId
},
),
map(getScope),
checkScopes($, t),
Expand All @@ -140,7 +139,7 @@ export checkTagScopes = (tags, t) => pipe(
firstAsString :: List String -> String
firstAsString = pipe(
List.first,
fromMaybe(""),
fromMaybe("*"),
)

parseTag :: String -> Tag
Expand All @@ -153,6 +152,7 @@ parseTag = (part) => {
segments = List.length(list) == 1 ? [] : List.tail(list)
return pipe(
firstAsString,
IO.pTrace("parsed tag..."),
Tag(negated, segments),
)(list)
},
Expand All @@ -173,18 +173,18 @@ export parseTags = pipe(
)


// log :: Show a => List (Maybe String) -> Tag -> a -> {}
export makeScopedLogger = (tags, t, thing) => {
makeRawLogger :: Show a => List Tag -> Tag -> a -> Maybe a
export makeRawLogger = (tags, t, thing) => {
return if (checkTagScopes(tags, t)) do {
IO.pTrace(serializeTag(t), thing)
return thing
} else do {
IO.putLine(`<<<< SKIPPED: ${serializeTag(t)} >>>>`)
return thing
return Just(thing)
} else {
Nothing
}
}

export makeLogger = (tags, t, thing) => pipe(
// instead of the verbosity of the raw logger, express things as parsed tags
makeParsedLogger :: Show a => List Tag -> String -> a -> Maybe a
export makeParsedLogger = (tags, t, thing) => pipe(
parseTag,
makeScopedLogger(tags, $, thing),
makeRawLogger(tags, $, thing),
)(t)
36 changes: 36 additions & 0 deletions src/Billet.spec.mad
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
getScope,
hasScope,
isScoped,
makeParsedLogger,
makeRawLogger,
parseTags,
scopeEqual,
scopeWithinScope,
Expand Down Expand Up @@ -119,3 +121,37 @@ testCases(
#[scopedTag([], "a"), "a"],
]),
)

SCOPES = [
scopedTag(["deep"], "info"),
tag("warn"),
tag("log"),
scopedTag(["deeply", "nested"], "issue"),
]
test(
"makeRawLogger",
() => do {
alog = makeRawLogger(SCOPES)
_ <- assertEquals(alog(Tag(true, ["deep"], "info"), "[deep] info!"), Just("[deep] info!"))
_ <- assertEquals(alog(Tag(true, [], "info"), "info info!"), Just("info info!"))
_ <- assertEquals(alog(Tag(true, [], "unmatched"), "info info!"), Nothing)
_ <- assertEquals(alog(Tag(true, ["deeply"], "issue"), "skip me!"), Nothing)
return assertEquals(
alog(Tag(true, ["deep", "nested"], "info"), "info info!"),
Just("info info!"),
)
},
)

test(
"makeParsed",
() => do {
alog = makeParsedLogger(SCOPES)
_ <- assertEquals(alog("info:deep", "[deep] info!"), Just("[deep] info!"))
_ <- assertEquals(alog("info", "info info!"), Just("info info!"))
_ <- assertEquals(alog("unmatched", "info info!"), Nothing)
_ <- assertEquals(alog("issue:deeply", "skip me!"), Nothing)
_ <- assertEquals(alog("info:deep:nested", "info info!"), Just("info info!"))
return assertEquals(alog("*", "info info!"), Just("info info!"))
},
)
10 changes: 5 additions & 5 deletions src/Main.mad
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import Billet from "@/Billet"
main = () => {
// a = Billet.scopedTag(["deeply", "nested", "value"], "a")
// IO.pTrace("scoped tag!", #[a, Billet.checkScopes([])])
alog = Billet.makeScopedLogger([
Billet.scopedTag(["deep"], "info"),
Billet.tag("warn"),
Billet.tag("log"),
])
SCOPES = [Billet.scopedTag(["deep"], "info"), Billet.tag("warn"), Billet.tag("log")]
alog = Billet.makeRawLogger(SCOPES)
alog(Billet.Tag(true, ["deep"], "info"), "[deep] info!")
alog(Billet.Tag(true, [], "info"), "info info")
alog(Billet.Tag(true, ["deeply", "nested"], "info"), "skipping deeply.nested")
alog(Billet.Tag(true, [], "log"), "log")
alog(Billet.Tag(true, [], "warning"), "this is skipped")
blog = Billet.makeParsedLogger(SCOPES)
blog("*", "k fun star star!")
blog("info:deep", "info:deep with parsies!")
}

0 comments on commit f74fb74

Please sign in to comment.