|
| 1 | +/- |
| 2 | +Copyright (c) 2025 Lean FRO. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +
|
| 5 | +Author: Marc Huisinga |
| 6 | +-/ |
| 7 | +module |
| 8 | + |
| 9 | +prelude |
| 10 | +public import Lean.Data.JsonRpc |
| 11 | +import Std.Time |
| 12 | +import Lean.Data.Lsp.Extra |
| 13 | +public import Lean.Data.Lsp.InitShutdown |
| 14 | + |
| 15 | +namespace Lean.Server.Logging |
| 16 | + |
| 17 | +public structure LogConfig where |
| 18 | + isEnabled : Bool |
| 19 | + logFile : System.FilePath |
| 20 | + allowedMethods? : Option (Std.HashSet String) |
| 21 | + disallowedMethods? : Option (Std.HashSet String) |
| 22 | + |
| 23 | +public def LogConfig.ofLspLogConfig (lspCfg? : Option Lsp.LogConfig) : IO LogConfig := do |
| 24 | + let time ← Std.Time.ZonedDateTime.now |
| 25 | + let time := time.format "yyyy-MM-dd-HH-mm-ss-SSSSXX" |
| 26 | + let logFileName := s!"LSP_{time}.log" |
| 27 | + let defaultLogFile : System.FilePath := System.FilePath.join "." logFileName |
| 28 | + let some lspCfg := lspCfg? |
| 29 | + | return { |
| 30 | + isEnabled := false |
| 31 | + logFile := defaultLogFile |
| 32 | + allowedMethods? := none |
| 33 | + disallowedMethods? := none |
| 34 | + } |
| 35 | + return { |
| 36 | + isEnabled := true |
| 37 | + logFile := lspCfg.logDir?.map (System.FilePath.join · logFileName) |>.getD defaultLogFile |
| 38 | + allowedMethods? := lspCfg.allowedMethods? |
| 39 | + disallowedMethods? := lspCfg.disallowedMethods? |
| 40 | + } |
| 41 | + |
| 42 | +open Lean |
| 43 | + |
| 44 | +public inductive MessageMethod where |
| 45 | + | request (method : String) |
| 46 | + | rpcRequest (method : String) |
| 47 | + | notification (method : String) |
| 48 | + deriving Inhabited |
| 49 | + |
| 50 | +def MessageMethod.all : MessageMethod → Array String |
| 51 | + | .request method |
| 52 | + | .notification method => |
| 53 | + #[method] |
| 54 | + | .rpcRequest method => |
| 55 | + #["$/lean/rpc/call", method] |
| 56 | + |
| 57 | +public def messageMethod? : JsonRpc.Message → Option MessageMethod |
| 58 | + | .request _ method params? => do |
| 59 | + if method == "$/lean/rpc/call" then |
| 60 | + let params := toJson params? |
| 61 | + if let .ok (callParams : Lsp.RpcCallParams) := fromJson? params then |
| 62 | + return .rpcRequest callParams.method.toString |
| 63 | + return .request method |
| 64 | + | .notification method _ => |
| 65 | + some <| .notification method |
| 66 | + | _ => |
| 67 | + none |
| 68 | + |
| 69 | +def messageId? : JsonRpc.Message → Option JsonRpc.RequestID |
| 70 | + | .request id .. => some id |
| 71 | + | .response id .. => some id |
| 72 | + | .responseError id .. => some id |
| 73 | + | _ => none |
| 74 | + |
| 75 | +def isMsgAllowed (cfg : LogConfig) |
| 76 | + (pending : Std.HashMap JsonRpc.RequestID MessageMethod) |
| 77 | + (msg : JsonRpc.Message) : Bool := Id.run do |
| 78 | + if ! cfg.isEnabled then |
| 79 | + return false |
| 80 | + let some method := method? |
| 81 | + | return false |
| 82 | + let allMethods := method.all |
| 83 | + if let some allowedMethods := cfg.allowedMethods? then |
| 84 | + if allMethods.any (! allowedMethods.contains ·) then |
| 85 | + return false |
| 86 | + if let some disallowedMethods := cfg.disallowedMethods? then |
| 87 | + if allMethods.any (disallowedMethods.contains ·) then |
| 88 | + return false |
| 89 | + return true |
| 90 | +where |
| 91 | + method? : Option MessageMethod := |
| 92 | + messageMethod? msg <|> (do pending.get? (← messageId? msg)) |
| 93 | + |
| 94 | +local instance : ToJson Std.Time.ZonedDateTime where |
| 95 | + toJson dt := dt.toISO8601String |
| 96 | + |
| 97 | +local instance : FromJson Std.Time.ZonedDateTime where |
| 98 | + fromJson? |
| 99 | + | .str s => Std.Time.ZonedDateTime.fromISO8601String s |
| 100 | + | _ => throw "Expected string when converting JSON to Std.Time.ZonedDateTime" |
| 101 | + |
| 102 | +structure LogEntry where |
| 103 | + time : Std.Time.ZonedDateTime |
| 104 | + direction : JsonRpc.MessageDirection |
| 105 | + kind : JsonRpc.MessageKind |
| 106 | + msg : JsonRpc.Message |
| 107 | + deriving FromJson, ToJson |
| 108 | + |
| 109 | +public def writeLogEntry (cfg : LogConfig) (pending : Std.HashMap JsonRpc.RequestID MessageMethod) |
| 110 | + (log : IO.FS.Handle) (direction : JsonRpc.MessageDirection) (msg : JsonRpc.Message) : IO Unit := do |
| 111 | + if ! isMsgAllowed cfg pending msg then |
| 112 | + return |
| 113 | + let entry : LogEntry := { |
| 114 | + time := ← Std.Time.ZonedDateTime.now |
| 115 | + direction |
| 116 | + kind := .ofMessage msg |
| 117 | + msg |
| 118 | + } |
| 119 | + let entry := toJson entry |>.compress |
| 120 | + log.putStrLn entry |
| 121 | + log.flush |
0 commit comments