Skip to content

Commit 05861fe

Browse files
committed
Better debug logging
1 parent d21253d commit 05861fe

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

src/neovim_client/message.clj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44
(def +response+ 1)
55
(def +notify+ 2)
66

7+
(let [last-id (atom 0)]
8+
(defn next-id
9+
[]
10+
(swap! last-id inc)
11+
@last-id))
12+
713
(defn gen-msg-id
814
"Get a unique message id."
915
[]
10-
(System/nanoTime))
16+
(next-id))
1117

1218
(defn ->request-msg
1319
"Construct a msgpack-rpc request message."
1420
[type args]
15-
[0 (gen-msg-id) type args])
21+
[+request+ (gen-msg-id) type args])
1622

1723
(defn ->response-msg
1824
"Construct a msgpack-rpc response message."
1925
[id result]
20-
[1 id nil result])
26+
[+response+ id nil result])
2127

2228
;; TODO - find better way to get [B type.
2329
(def byte-array-type (type (.getBytes "foo")))

src/neovim_client/rpc.clj

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,39 @@
1919
(let [chan (async/chan 1024)]
2020
(async/thread
2121
(loop []
22+
(log/info "stream[m] --- in-chan[ ] --- plugin")
2223
(when-let [msg (msgpack/unpack input-stream)]
23-
(log/info "stream -> msg -> in chan: " msg)
24+
(log/info "stream[ ] ->m in-chan[ ] --- plugin" msg)
2425
(async/>!! chan msg)
26+
(log/info "stream[ ] --- in-chan[m] --- plugin" (id msg))
2527
(recur))))
2628
chan))
2729

28-
(defn- write-msg!
29-
[packed-msg out-stream]
30-
(doseq [b packed-msg]
31-
(.writeByte out-stream b))
32-
(.flush out-stream))
33-
3430
(defn- create-output-channel
3531
"Make a channel to read messages from, write to output stream."
3632
[output-stream]
3733
(let [chan (async/chan 1024)]
3834
(async/thread
3935
(loop []
36+
(log/info "stream[ ] --- out-chan[m] --- plugin")
4037
(when-let [msg (async/<!! chan)]
41-
(log/info "stream <- msg <- out chan: " msg)
42-
(write-msg! (msgpack/pack msg) output-stream)
38+
(log/info "stream[ ] m<- out-chan[ ] --- plugin" (id msg))
39+
(let [packed (msgpack/pack msg)]
40+
(.write output-stream packed 0 (count packed)))
41+
(.flush output-stream)
42+
(log/info "stream[m] --- out-chan[ ] --- plugin" (id msg))
4343
(recur))))
4444
chan))
4545

4646
;; ***** Public *****
4747

4848
(defn send-message-async!
4949
[{:keys [message-table out-chan]} msg callback-fn]
50-
(if (= msg/+request+ (msg-type msg))
50+
(when (= msg/+request+ (msg-type msg))
5151
(swap! message-table assoc (id msg) {:msg msg :fn callback-fn}))
52-
(async/put! out-chan msg))
52+
(log/info "stream[ ] --- out-chan[ ] m<- plugin" msg)
53+
(async/>!! out-chan msg)
54+
(log/info "stream[ ] --- out-chan[m] --- plugin" (id msg)))
5355

5456
(defn send-message!
5557
[component msg]
@@ -78,7 +80,6 @@
7880
[input-stream output-stream]
7981
(let [in-chan (create-input-channel input-stream)
8082
input-stream (DataInputStream. input-stream)
81-
output-stream (DataOutputStream. output-stream)
8283
message-table (atom {})
8384
method-table (atom {})
8485
component {:input-stream input-stream
@@ -87,25 +88,35 @@
8788
:in-chan in-chan
8889
:message-table message-table
8990
:method-table method-table}]
90-
(future (loop
91-
[]
92-
(when-let [msg (async/<!! in-chan)]
93-
(condp = (msg-type msg)
9491

95-
msg/+response+
96-
(let [f (:fn (get @message-table (id msg)))]
97-
(swap! message-table dissoc (id msg))
98-
(f (value msg)))
92+
(future
93+
(try
94+
(loop []
95+
(when-let [msg (async/<!! in-chan)]
96+
(log/info "stream[ ] --- in-chan[ ] ->m plugin" (id msg))
97+
(condp = (msg-type msg)
98+
99+
msg/+response+
100+
(let [f (:fn (get @message-table (id msg)))]
101+
(swap! message-table dissoc (id msg))
102+
;; Don't block the handler to execute this.
103+
(async/thread (when f (f (value msg)))))
104+
105+
msg/+request+
106+
(let [f (get @method-table (method msg) method-not-found)
107+
;; TODO - add async/thread here, remove from methods.
108+
result (f msg)]
109+
(send-message-async!
110+
component (->response-msg (id msg) result) nil))
99111

100-
msg/+request+
101-
(let [f (get @method-table (method msg) method-not-found)
102-
result (f msg)]
103-
(send-message-async!
104-
component (->response-msg (id msg) result) nil))
112+
msg/+notify+
113+
(let [f (get @method-table (method msg) method-not-found)
114+
;; TODO - see above.
115+
result (f msg)]))
105116

106-
msg/+notify+
107-
(let [f (get @method-table (method msg) method-not-found)
108-
result (f msg)]))
117+
(recur)))
118+
(catch Throwable t (log/info
119+
"Exception in message handler, aborting!"
120+
t))))
109121

110-
(recur))))
111122
component))

0 commit comments

Comments
 (0)