From 28f6549465fe7bc25194217c82f1c79513bfc7b9 Mon Sep 17 00:00:00 2001 From: Jesse Wertheim Date: Wed, 10 Jan 2024 15:57:11 -0500 Subject: [PATCH 1/2] add stateful container for named loggers State manager for loggers, by id. The primary usecase is for users to override the log level of a given logger id using a central location to store them by ID. Table is weak-valued so loggers can be cleaned up when they have no remaining references outside of the container. Invoking the table as a function ala `(logger "logger-id" ?some-level)` allows users to fetch a logger with a given ID if it's already been instantiated, otherwise creating and storing a new one by passing arguments to `hs.logger.new` --- lib/utils.fnl | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/utils.fnl b/lib/utils.fnl index 92cd6e0..5d683a0 100644 --- a/lib/utils.fnl +++ b/lib/utils.fnl @@ -1,3 +1,5 @@ +(local fennel (require :fennel)) + (fn global-filter [] " @@ -6,4 +8,20 @@ (let [filter (hs.window.filter.new)] (: filter :setAppFilter :Emacs {:allowRoles [:AXUnknown :AXStandardWindow :AXDialog :AXSystemDialog]}))) -{:global-filter global-filter} +(fn get-or-add-logger [loggers id ?level] + "If (. loggers id) exists, returns it; otherwise instaniates & stores a new one. +If ?level is provided, sets it on the new or existing hs.logger instance. +`loggers` is expected to be a weak-valued table." + (case (. loggers id) + log (do (when ?level (log.setLogLevel ?level)) + log) + _ (let [log (hs.logger.new id ?level)] + (tset loggers id log) + log))) + +;; Weak-valued table to store instantiated loggers by ID. Can be called as a +;; function to create & store a new instance, optioanlly with provided log level +(local logger (setmetatable {} {:__mode :v :__call get-or-add-logger})) + +{:global-filter global-filter + : logger} From 19fee88b4fa73ffe29ddbcc303ed3698ca7edfd5 Mon Sep 17 00:00:00 2001 From: Jesse Wertheim Date: Wed, 10 Jan 2024 16:04:12 -0500 Subject: [PATCH 2/2] feature: Make log levels for named loggers overridable in config Uses the logger helper in lib/utils.fnl, iterating k-v pairs found in `config.log-levels` of the form `{logger-id log-level}` to set them. --- core.fnl | 6 +++++- lib/apps.fnl | 3 ++- lib/bind.fnl | 3 ++- lib/lifecycle.fnl | 3 ++- lib/modal.fnl | 3 ++- lib/statemachine.fnl | 4 ++-- vim.fnl | 4 +++- 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/core.fnl b/core.fnl index f588700..3219012 100644 --- a/core.fnl +++ b/core.fnl @@ -9,6 +9,7 @@ :reduce reduce :split split :some some} (require :lib.functional)) +(local {: logger} (require :lib.utils)) (local atom (require :lib.atom)) (require-macros :lib.macros) (require-macros :lib.advice.macros) @@ -24,7 +25,7 @@ (local customdir (.. homedir "/.spacehammer")) (tset fennel :path (.. customdir "/?.fnl;" fennel.path)) -(local log (hs.logger.new "\tcore.fnl\t" "debug")) +(local log (logger "\tcore.fnl\t" "debug")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; defaults @@ -236,3 +237,6 @@ Returns nil. This function causes side-effects. {path (module.init config)}))) (reduce #(merge $1 $2) {}))) +;; override log level for named loggers in config +(each [logger-id level (pairs (or config.log-levels {}))] + (logger logger-id level)) diff --git a/lib/apps.fnl b/lib/apps.fnl index 5ee3af6..eba661f 100644 --- a/lib/apps.fnl +++ b/lib/apps.fnl @@ -20,9 +20,10 @@ This module works mechanically similar to lib/modal.fnl. :bind-keys bind-keys} (require :lib.bind)) (local lifecycle (require :lib.lifecycle)) +(local {: logger} (require :lib.utils)) -(local log (hs.logger.new "apps.fnl" "debug")) +(local log (logger "apps.fnl" "debug")) (local actions (atom.new nil)) ;; Create a dynamic var to hold an accessible instance of our finite state diff --git a/lib/bind.fnl b/lib/bind.fnl index 16dcbb3..52a8355 100644 --- a/lib/bind.fnl +++ b/lib/bind.fnl @@ -3,8 +3,9 @@ : map : split} (require :lib.functional)) +(local {: logger} (require :lib.utils)) -(local log (hs.logger.new "bind.fnl" "debug")) +(local log (logger "bind.fnl" "debug")) (fn do-action [action args] diff --git a/lib/lifecycle.fnl b/lib/lifecycle.fnl index 180b83b..7b534cc 100644 --- a/lib/lifecycle.fnl +++ b/lib/lifecycle.fnl @@ -1,5 +1,6 @@ (local {: do-action} (require :lib.bind)) -(local log (hs.logger.new "lifecycle.fnl" "debug")) +(local {: logger} (require :lib.utils)) +(local log (logger "lifecycle.fnl" "debug")) " diff --git a/lib/modal.fnl b/lib/modal.fnl index 0e36044..ac9e171 100644 --- a/lib/modal.fnl +++ b/lib/modal.fnl @@ -24,6 +24,7 @@ switching menus in one place which is then powered by config.fnl. : map : merge} (require :lib.functional)) +(local {: logger} (require :lib.utils)) (local {:align-columns align-columns} (require :lib.text)) (local {:action->fn action->fn @@ -31,7 +32,7 @@ switching menus in one place which is then powered by config.fnl. (require :lib.bind)) (local lifecycle (require :lib.lifecycle)) -(local log (hs.logger.new "modal.fnl" "debug")) +(local log (logger "modal.fnl" "debug")) (var fsm nil) (local default-style {:textFont "Menlo" :textSize 16 diff --git a/lib/statemachine.fnl b/lib/statemachine.fnl index e5a971e..3d52fdf 100644 --- a/lib/statemachine.fnl +++ b/lib/statemachine.fnl @@ -47,7 +47,7 @@ the next transition. : last : merge : slice} (require :lib.functional)) - +(local {: logger} (require :lib.utils)) (fn update-state [fsm state] @@ -124,7 +124,7 @@ the next transition. (let [fsm {:state (atom.new {:current-state template.state.current-state :context template.state.context}) :states template.states :subscribers (atom.new {}) - :log (if template.log (hs.logger.new template.log "info"))}] + :log (if template.log (logger template.log "info"))}] ; Add methods (tset fsm :get-state (partial get-state fsm)) (tset fsm :send (partial send fsm)) diff --git a/vim.fnl b/vim.fnl index 4caa5db..1d470ac 100644 --- a/vim.fnl +++ b/vim.fnl @@ -9,9 +9,11 @@ : map : noop : some} (require :lib.functional)) +(local {: logger} (require :lib.utils)) + (local statemachine (require :lib.statemachine)) (local {:bind-keys bind-keys} (require :lib.bind)) -(local log (hs.logger.new "vim.fnl" "debug")) +(local log (logger "vim.fnl" "debug")) " Create a vim mode for any text editor!