Skip to content

Commit 9cb0a6e

Browse files
authored
Merge pull request #36 from CFiggers:dev
v0.0.10
2 parents 78471ef + d499073 commit 9cb0a6e

File tree

6 files changed

+111
-28
lines changed

6 files changed

+111
-28
lines changed

CHANGELOG.md

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22
All notable changes to this project will be documented in this file.
33
Format for entires is <version-string> - release date.
44

5+
## 0.0.10 - 2024-12-22
6+
7+
- Logging
8+
- Rotate log files and overwrite eventually to avoid indefinite log file size
9+
- Adjusted some log levels
10+
- New methods
11+
- `enableDebug` and `disableDebug` - Allow clients to set `(dyn :debug)` while running
12+
- `setLogLevel` and `setLogToFileLevel` - Allow clients to change debug level to console and file
13+
14+
## 0.0.9 - 2024-12-07
15+
16+
- Bugfixes
17+
- Decode percent encoding in URIs before saving to or lookup from `state`
18+
- Typo: ":documnts" rather than ":documents", causing redundant keys in `state` when diagnostics are pull (vs push)
19+
- Don't exit loop when handle-message returns an `:error` result, instead report it and reenter loop gracefully
20+
- Misc
21+
- Formatting tweaks
22+
- New "janet/tellJoke" method (testing for future custom LSP RPC calls)
23+
24+
## 0.0.8 - 2024-11-24
25+
26+
- Bug Fixes
27+
- Additional jpm defs (by @strangepete)
28+
- New `eval-env`s should set `*out*` to `stderr`
29+
530
## 0.0.7 - 2024-08-11
631

732
- Core loop

project.janet

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(declare-project
22
:name "janet-lsp"
33
:description "A Language Server (LSP) for the Janet Programming Language"
4-
:version "0.0.6"
4+
:version "0.0.10"
55
:dependencies ["https://github.com/janet-lang/spork.git"
66
"https://github.com/CFiggers/jayson.git"
77
"https://github.com/ianthehenry/judge.git"

src/doc.janet

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@
126126
"Get the documentation for a symbol in a given environment."
127127
[sym env]
128128
(assert env "my-doc*: env is nil")
129-
(logging/info (string/format "env is: %m" env) [:hover] 1)
130-
(logging/info (string/format "my-doc* tried: %m" (env sym)) [:hover] 1)
129+
(logging/info (string/format "env is: %m" env) [:hover] 3)
130+
(logging/info (string/format "my-doc* tried: %m" (env sym)) [:hover] 3)
131131
(if-let [x (env sym)]
132132
(make-module-entry x)
133133
(if (has-value? '[break def do fn if quasiquote quote

src/eval.janet

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
(defn eval-buffer [str &opt filename]
5656
(logging/info (string/format "`eval-buffer` received filename: `%s`" (or filename "none")) [:evaluation] 1)
57-
(logging/info (string/format "`eval-buffer` received str: `%s`" str) [:evaluation] 2)
57+
(logging/info (string/format "`eval-buffer` received str: `%s`" str) [:evaluation] 3)
5858

5959
(default filename "eval.janet")
6060
(var state (string str))
@@ -92,7 +92,7 @@
9292
:location [0 0]})))
9393
returnval) :e fresh-env))
9494
(def eval-fiber-return (resume eval-fiber))
95-
(logging/info (string/format "`eval-buffer` is returning: %m" eval-fiber-return) [:evaluation] 2)
95+
(logging/info (string/format "`eval-buffer` is returning: %m" eval-fiber-return) [:evaluation] 3)
9696
[eval-fiber-return fresh-env])
9797

9898
# tests

src/logging.janet

+38-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
(import spork/rpc)
44

55
(defn log [output categories &opt level]
6-
76
(unless (dyn :debug) (break))
87

98
(unless (dyn :client)
@@ -18,15 +17,41 @@
1817
(print (:print (dyn :client) output)))
1918

2019
(when (dyn :debug)
21-
# Always log to file
22-
(try (spit "janetlsp.log.txt" (string output "\n") :a)
23-
([_]))
20+
21+
# Ensure log file exists
22+
(unless (os/stat "janetlsp.log")
23+
(spit "janetlsp.log" ""))
24+
25+
# Log to file, all categories but only if this log's level is <= the specified level
26+
(when (or (nil? level) # There is no level specified on this log
27+
(<= level (dyn :log-to-file-level)))
28+
(try
29+
(do
30+
(def logfiles (filter |(string/has-prefix? "janetlsp.log" $) (os/dir ".")))
31+
(when (> (get (os/stat "janetlsp.log") :size) 5000000)
32+
(when (and (has-value? logfiles "janetlsp.log") (has-value? logfiles "janetlsp.log.1"))
33+
(when (and (has-value? logfiles "janetlsp.log.1") (has-value? logfiles "janetlsp.log.2"))
34+
(when (and (has-value? logfiles "janetlsp.log.2") (has-value? logfiles "janetlsp.log.3"))
35+
(when (and (has-value? logfiles "janetlsp.log.3") (has-value? logfiles "janetlsp.log.4"))
36+
(when (and (has-value? logfiles "janetlsp.log.4") (has-value? logfiles "janetlsp.log.5"))
37+
(os/rm "janetlsp.log.5"))
38+
(os/rename "janetlsp.log.4" "janetlsp.log.5"))
39+
(os/rename "janetlsp.log.3" "janetlsp.log.4"))
40+
(os/rename "janetlsp.log.2" "janetlsp.log.3"))
41+
(os/rename "janetlsp.log.1" "janetlsp.log.2"))
42+
(os/rename "janetlsp.log" "janetlsp.log.1")
43+
(spit "janetlsp.log" ""))
44+
(spit "janetlsp.log" (string output "\n") :a))
45+
([e]
46+
(file/write stderr (string/format "error while trying to write to log file: %q\n" e)))))
47+
48+
# Log to console, only specified categories and if this log's level is >= the specified level
2449
(when (and
25-
# Category Match
50+
# Category Match
2651
(or (empty? (dyn :log-categories)) # No log categories are specified
2752
(empty? categories) # OR, this log doesn't specify a categories (default to sending it)
2853
(any? (map |(has-value? (dyn :log-categories) $) categories))) # Any of this log's categories is in the target categories
29-
# Level Match
54+
# Level Match
3055
(or (nil? level) # There is no level specified on this log
3156
(<= level (dyn :log-level)))) # OR, this log's level is <= the specified level
3257

@@ -40,7 +65,6 @@
4065
(nil? categories)
4166
(has-value? (dyn :log-categories) categories)))
4267

43-
4468
(eprintf "is level nil? %m" (nil? level))
4569
(eprintf "is level high enough? %m" (<= level (dyn :log-level)))
4670
(eprintf "second condition %m" (or (nil? level)
@@ -51,12 +75,12 @@
5175

5276
(defmacro info [output categories &opt level id]
5377
(with-syms [$output $categories $level $id]
54-
~(let [,$output (case (type ,output) :string ,output (string/format "%m" ,output))
55-
,$categories ,categories
56-
,$level ,level
57-
,$id ,id]
58-
(,log (string/format "[INFO%s:%s] %s" (if ,$id (string ":" ,$id) "") (first ,$categories) ,$output)
59-
,$categories ,$level))))
78+
~(let [,$output (case (type ,output) :string ,output (string/format "%m" ,output))
79+
,$categories ,categories
80+
,$level ,level
81+
,$id ,id]
82+
(,log (string/format "[INFO%s:%s] %s" (if ,$id (string ":" ,$id) "") (first ,$categories) ,$output)
83+
,$categories ,$level))))
6084

6185
(defmacro message [output categories &opt level id]
6286
(with-syms [$output $categories $level $id]
@@ -65,7 +89,7 @@
6589
,$level ,level
6690
,$id ,id]
6791
(,log (string/format "[MESSAGE%s:%s] %s" (if ,$id (string ":" ,$id) "") (first ,$categories) ,$output)
68-
,$categories ,$level))))
92+
,$categories ,$level))))
6993

7094
(defmacro err [output categories &opt level id]
7195
(with-syms [$output $categories $level $id]

src/main.janet

+43-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
(use judge)
1515

16-
(def version "0.0.9")
16+
(def version "0.0.10")
1717
(def commit
1818
(with [proc (os/spawn ["git" "rev-parse" "--short" "HEAD"] :xp {:out :pipe})]
1919
(let [[out] (ev/gather
@@ -38,7 +38,7 @@
3838
(if (string/has-prefix? "file:" uri)
3939
(string/slice uri 5) uri)))]
4040

41-
(logging/info (string/format "`eval-buffer` returned: %m" diagnostics) [:evaluation])
41+
(logging/info (string/format "`eval-buffer` returned: %m" diagnostics) [:evaluation] 3)
4242

4343
(each res diagnostics
4444
(match res
@@ -49,8 +49,8 @@
4949
:end {:line (max 0 (dec line)) :character col}}
5050
:message message})))
5151

52-
(logging/info (string/format "`run-diagnostics` is returning these errors: %m" items) [:evaluation])
53-
(logging/info (string/format "`run-diagnostics` is returning this eval-context: %m" env) [:evaluation] 1)
52+
(logging/info (string/format "`run-diagnostics` is returning these errors: %m" items) [:evaluation] 2)
53+
(logging/info (string/format "`run-diagnostics` is returning this eval-context: %m" env) [:evaluation] 3)
5454
[items env]))
5555

5656
(def uri-percent-encoding-peg
@@ -154,7 +154,7 @@
154154
~(let [,$name ,name
155155
,$eval-env ,eval-env
156156
s (get-in ,$eval-env [,$name :value] ,$name)]
157-
(,logging/log (string/format "binding-to-lsp-item: s is %m" s) [:completion] 2)
157+
(,logging/log (string/format "binding-to-lsp-item: s is %m" s) [:completion] 3)
158158
{:label ,$name :kind
159159
(case (type s)
160160
:symbol 12 :boolean 6
@@ -343,12 +343,40 @@
343343
[:noresponse state])
344344

345345
(defn on-janet-tell-joke [state params]
346-
# (eprint "What's brown and sticky? A stick!")
347346
(let [message {:question "What's brown and sticky?"
348347
:answer "A stick!"}]
349348
(logging/message message [:joke])
350349
[:ok state message]))
351350

351+
(defn on-enable-debug [state params]
352+
(let [message {:message "Enabled :debug"}]
353+
(setdyn :debug true)
354+
(try (spit "janetlsp.log" "")
355+
([_] (logging/err "Tried to write to janetlsp.log, but couldn't" [:core])))
356+
(logging/message message [:debug])
357+
[:ok state message]))
358+
359+
(defn on-disable-debug [state params]
360+
(let [message {:message "Disabled :debug"}]
361+
(setdyn :debug false)
362+
(setdyn :log-level 2)
363+
(logging/message message [:debug])
364+
[:ok state message]))
365+
366+
(defn do-set-log-level [state params kind]
367+
(let [new-level-string (params "level")
368+
new-level ({"off" 0 "messages" 1 "verbose" 2 "veryverbose" 3} new-level-string)
369+
message {:message (string/format "Set %s to %s" kind new-level-string)}]
370+
(logging/message message [:loglevel])
371+
(setdyn kind new-level)
372+
[:noresponse state]))
373+
374+
(defmacro on-set-log-level [state params]
375+
~(,do-set-log-level ,state ,params :log-level))
376+
377+
(defmacro on-set-file-log-level [state params]
378+
~(,do-set-log-level ,state ,params :log-to-file-level))
379+
352380
(defn handle-message [message state]
353381
(let [id (get message "id")
354382
method (get message "method")
@@ -370,6 +398,10 @@
370398
"textDocument/definition" (on-document-definition state params)
371399
"janet/serverInfo" (on-janet-serverinfo state params)
372400
"janet/tellJoke" (on-janet-tell-joke state params)
401+
"enableDebug" (on-enable-debug state params)
402+
"disableDebug" (on-disable-debug state params)
403+
"setLogLevel" (on-set-log-level state params)
404+
"setLogToFileLevel" (on-set-file-log-level state params)
373405
"shutdown" (on-shutdown state params)
374406
"exit" (on-exit state params)
375407
"$/setTrace" (on-set-trace state params)
@@ -446,7 +478,7 @@
446478
(defn start-language-server []
447479
(print "Starting LSP " version "-" commit)
448480
(when (dyn :debug)
449-
(try (spit "janetlsp.log.txt" "")
481+
(try (spit "janetlsp.log" "")
450482
([_] (logging/err "Tried to write to janetlsp.log txt, but couldn't" [:core]))))
451483

452484
(merge-module root-env jpm-defs nil true)
@@ -488,7 +520,8 @@
488520
[[--dont-search-jpm-tree -j] (flag) "Whether to search `jpm_tree` for modules."
489521
--stdio (flag) "Use STDIO."
490522
[--debug -d] (flag) "Print debug messages."
491-
[--log-level -l] (optional :int++ 0) "What level of logging to display. Defaults to 0."
523+
[--log-level -l] (optional :int++ 1) "What level of logging to display. Defaults to 1."
524+
[--log-to-file-level -f] (optional :int++ 2) "What level of logging to write to the log file. Defaults to 2."
492525
[--log-category -L] (tuple :string) "Enable logging by category. For multiple categories, repeat the flag."
493526
[--console -c] (flag) "Start a debug console instead of starting the Language Server."
494527
[--debug-port -p] (optional :int++) "What port to start or connect to the debug console on. Defaults to 8037."]
@@ -505,7 +538,8 @@
505538
(setdyn :opts opts)
506539
(when debug (setdyn :debug true)) #(setdyn :debug true)
507540
(setdyn :log-level log-level) #(setdyn :log-level 2)
508-
(setdyn :log-categories @[:core ;(map keyword log-category)]) #(setdyn :log-categories [:core :priority :diagnostics])
541+
(setdyn :log-to-file-level log-to-file-level) #(setdyn :log-level 3)
542+
(setdyn :log-categories @[:core ;(map keyword log-category)]) #(setdyn :log-categories [:core :priority :loglevel])
509543
(setdyn :out stderr)
510544
(put root-env :out stderr)
511545

0 commit comments

Comments
 (0)