|
30 | 30 | (let [response
|
31 | 31 | (case message-type
|
32 | 32 | (:parse-error :invalid-request)
|
33 |
| - (protocols.endpoint/log client :red "Error reading message" message) |
| 33 | + (protocols.endpoint/log client :error "Error reading message" message) |
34 | 34 | :request
|
35 | 35 | (protocols.endpoint/receive-request client context message)
|
36 | 36 | (:response.result :response.error)
|
|
41 | 41 | (when (identical? :request message-type)
|
42 | 42 | response))
|
43 | 43 | (catch Throwable e
|
44 |
| - (protocols.endpoint/log client :red "Error receiving:" e) |
| 44 | + (protocols.endpoint/log client :error "Error receiving:" e) |
45 | 45 | (throw e)))))
|
46 | 46 |
|
47 | 47 | (defrecord Client [client-id
|
48 | 48 | input-ch
|
49 | 49 | output-ch
|
50 | 50 | join
|
51 | 51 | request-id
|
52 |
| - sent-requests] |
| 52 | + sent-requests |
| 53 | + trace-level] |
53 | 54 | protocols.endpoint/IEndpoint
|
54 | 55 | (start [this context]
|
55 |
| - (protocols.endpoint/log this :white "lifecycle:" "starting") |
| 56 | + (protocols.endpoint/log this :verbose "lifecycle:" "starting") |
56 | 57 | (let [pipeline (async/pipeline-blocking
|
57 | 58 | 1 ;; no parallelism preserves server message order
|
58 | 59 | output-ch
|
|
68 | 69 | ;; shut down
|
69 | 70 | join)
|
70 | 71 | (shutdown [this]
|
71 |
| - (protocols.endpoint/log this :white "lifecycle:" "shutting down") |
| 72 | + (protocols.endpoint/log this :verbose "lifecycle:" "shutting down") |
72 | 73 | ;; closing input will drain pipeline, then close output, then close
|
73 | 74 | ;; pipeline
|
74 | 75 | (async/close! input-ch)
|
75 | 76 | (if (= :done (deref join 10e3 :timeout))
|
76 |
| - (protocols.endpoint/log this :white "lifecycle:" "shutdown") |
77 |
| - (protocols.endpoint/log this :red "lifecycle:" "shutdown timed out"))) |
| 77 | + (protocols.endpoint/log this :verbose "lifecycle:" "shutdown") |
| 78 | + (protocols.endpoint/log this :verbose "lifecycle:" "shutdown timed out"))) |
78 | 79 | (log [this msg params]
|
79 |
| - (protocols.endpoint/log this :white msg params)) |
80 |
| - (log [_this _color msg params] |
81 |
| - ;; TODO apply color |
82 |
| - (logger/info (string/join " " [msg params]))) |
| 80 | + (protocols.endpoint/log this :verbose msg params)) |
| 81 | + (log [_this level msg params] |
| 82 | + (when (or (identical? trace-level level) |
| 83 | + (identical? trace-level :verbose)) |
| 84 | + ;; TODO apply color |
| 85 | + (logger/info (string/join " " [msg params])))) |
83 | 86 | (send-request [this method body]
|
84 | 87 | (let [req (lsp.requests/request (swap! request-id inc) method body)
|
85 | 88 | p (promise)
|
86 | 89 | start-ns (System/nanoTime)]
|
87 |
| - (protocols.endpoint/log this :cyan "sending request:" req) |
| 90 | + (protocols.endpoint/log this :messages "sending request:" req) |
88 | 91 | ;; Important: record request before sending it, so it is sure to be
|
89 | 92 | ;; available during receive-response.
|
90 | 93 | (swap! sent-requests assoc (:id req) {:request p
|
|
93 | 96 | p))
|
94 | 97 | (send-notification [this method body]
|
95 | 98 | (let [notif (lsp.requests/notification method body)]
|
96 |
| - (protocols.endpoint/log this :blue "sending notification:" notif) |
| 99 | + (protocols.endpoint/log this :messages "sending notification:" notif) |
97 | 100 | (async/>!! output-ch notif)))
|
98 | 101 | (receive-response [this {:keys [id] :as resp}]
|
99 | 102 | (if-let [{:keys [request start-ns]} (get @sent-requests id)]
|
100 | 103 | (let [ms (float (/ (- (System/nanoTime) start-ns) 1000000))]
|
101 |
| - (protocols.endpoint/log this :green (format "received response (%.0fms):" ms) resp) |
| 104 | + (protocols.endpoint/log this :messages (format "received response (%.0fms):" ms) resp) |
102 | 105 | (swap! sent-requests dissoc id)
|
103 | 106 | (deliver request (if (:error resp)
|
104 | 107 | resp
|
105 | 108 | (:result resp))))
|
106 |
| - (protocols.endpoint/log this :red "received response for unmatched request:" resp))) |
| 109 | + (protocols.endpoint/log this :error "received response for unmatched request:" resp))) |
107 | 110 | (receive-request [this context {:keys [id method params] :as req}]
|
108 |
| - (protocols.endpoint/log this :magenta "received request:" req) |
| 111 | + (protocols.endpoint/log this :messages "received request:" req) |
109 | 112 | (when-let [response-body (case method
|
110 | 113 | "window/showMessageRequest" (show-message-request params)
|
111 | 114 | "window/showDocument" (show-document context params)
|
112 | 115 | "workspace/applyEdit" (workspace-apply-edit context params)
|
113 | 116 | (logger/warn "Unknown LSP request method" method))]
|
114 | 117 | (let [resp (lsp.responses/response id response-body)]
|
115 |
| - (protocols.endpoint/log this :magenta "sending response:" resp) |
| 118 | + (protocols.endpoint/log this :messages "sending response:" resp) |
116 | 119 | resp)))
|
117 | 120 | (receive-notification [this context {:keys [method params] :as notif}]
|
118 |
| - (protocols.endpoint/log this :blue "received notification:" notif) |
| 121 | + (protocols.endpoint/log this :messages "received notification:" notif) |
119 | 122 | (case method
|
120 | 123 | "window/showMessage" (show-message context params)
|
121 | 124 | "$/progress" (progress context params)
|
122 | 125 | "textDocument/publishDiagnostics" (publish-diagnostics context params)
|
123 | 126 |
|
124 | 127 | (logger/warn "Unknown LSP notification method" method))))
|
125 | 128 |
|
126 |
| -(defn client [in out] |
| 129 | +(defn client [in out trace-level] |
127 | 130 | (map->Client
|
128 | 131 | {:client-id 1
|
129 | 132 | :input-ch (io-chan/input-stream->input-chan out)
|
130 | 133 | :output-ch (io-chan/output-stream->output-chan in)
|
131 | 134 | :join (promise)
|
132 | 135 | :sent-requests (atom {})
|
133 |
| - :request-id (atom 0)})) |
| 136 | + :request-id (atom 0) |
| 137 | + :trace-level trace-level})) |
134 | 138 |
|
135 | 139 | (defn start-client! [client context]
|
136 | 140 | (protocols.endpoint/start client context))
|
|
0 commit comments