@@ -12,6 +12,7 @@ local PathFinder = require("eca.path_finder")
1212--- @field private _server_capabilities ? table Server capabilities
1313--- @field private _chat_id ? string Current chat ID
1414--- @field private _path_finder eca.PathFinder Server path finder
15+ --- @field private _logs table Array of log entries
1516local M = {}
1617M .__index = M
1718
@@ -32,6 +33,7 @@ function M:new(opts)
3233 instance ._on_started = opts .on_started
3334 instance ._on_status_changed = opts .on_status_changed
3435 instance ._path_finder = PathFinder :new ()
36+ instance ._logs = {}
3537 return instance
3638end
3739
@@ -53,6 +55,29 @@ function M:is_running()
5355 return self ._status == ServerStatus .Running
5456end
5557
58+ --- @param level string Log level (INFO , WARN , ERROR , DEBUG )
59+ --- @param message string Log message
60+ function M :_add_log (level , message )
61+ local timestamp = os.date (" %Y-%m-%d %H:%M:%S" )
62+ local log_entry = {
63+ timestamp = timestamp ,
64+ level = level ,
65+ message = message
66+ }
67+
68+ table.insert (self ._logs , log_entry )
69+
70+ -- Keep only last 1000 log entries to prevent memory issues
71+ if # self ._logs > 1000 then
72+ table.remove (self ._logs , 1 )
73+ end
74+ end
75+
76+ --- @return table Array of log entries
77+ function M :get_logs ()
78+ return vim .deepcopy (self ._logs )
79+ end
80+
5681--- @return table ?
5782function M :connection ()
5883 return self ._rpc
@@ -84,11 +109,14 @@ function M:start()
84109
85110 if not ok or not server_path then
86111 self :_change_status (ServerStatus .Failed )
87- Utils .error (" Could not find or download ECA server: " .. tostring (err ))
112+ local error_msg = " Could not find or download ECA server: " .. tostring (err )
113+ Utils .error (error_msg )
114+ self :_add_log (" ERROR" , error_msg )
88115 return
89116 end
90117
91118 Utils .debug (" Starting ECA server: " .. server_path )
119+ self :_add_log (" INFO" , " Starting ECA server: " .. server_path )
92120
93121 local args = { server_path , " server" }
94122 if Config .server_args and Config .server_args ~= " " then
@@ -109,9 +137,20 @@ function M:start()
109137 end ,
110138 on_stderr = function (_ , data , _ )
111139 if data then
112- local error_output = table.concat (data , " \n " )
113- if error_output and error_output ~= " " and error_output ~= " \n " then
140+ -- Filter out empty lines and join meaningful content
141+ local meaningful_lines = {}
142+ for _ , line in ipairs (data ) do
143+ local trimmed = line :match (" ^%s*(.-)%s*$" ) -- Trim whitespace
144+ if trimmed and trimmed ~= " " then
145+ table.insert (meaningful_lines , trimmed )
146+ end
147+ end
148+
149+ if # meaningful_lines > 0 then
150+ local error_output = table.concat (meaningful_lines , " \n " )
114151 Utils .debug (" ECA server stderr: " .. error_output )
152+ -- Capture logs for the logs buffer
153+ self :_add_log (" SERVER" , error_output )
115154 end
116155 end
117156 end ,
@@ -134,11 +173,14 @@ function M:start()
134173
135174 if self ._proc <= 0 then
136175 self :_change_status (ServerStatus .Failed )
137- Utils .error (" Failed to start ECA server process. Job ID: " .. tostring (self ._proc ))
176+ local error_msg = " Failed to start ECA server process. Job ID: " .. tostring (self ._proc )
177+ Utils .error (error_msg )
178+ self :_add_log (" ERROR" , error_msg )
138179 return
139180 end
140181
141182 Utils .debug (" ECA server started with job ID: " .. self ._proc )
183+ self :_add_log (" INFO" , " ECA server process started successfully (Job ID: " .. self ._proc .. " )" )
142184
143185 -- Create RPC connection with the job ID
144186 self ._rpc = RPC :new (self ._proc )
@@ -200,18 +242,22 @@ function M:_initialize_server()
200242 }, function (err , result )
201243 if err then
202244 self :_change_status (ServerStatus .Failed )
203- Utils .error (" Failed to initialize ECA server: " .. tostring (err ))
245+ local error_msg = " Failed to initialize ECA server: " .. tostring (err )
246+ Utils .error (error_msg )
247+ self :_add_log (" ERROR" , error_msg )
204248 return
205249 end
206250
207251 -- Store server capabilities
208252 if result then
209253 self ._server_capabilities = result
210254 Utils .debug (" Server capabilities: " .. vim .inspect (result ))
255+ self :_add_log (" INFO" , " Server capabilities received and stored" )
211256 end
212257
213258 self :_change_status (ServerStatus .Running )
214259 Utils .info (" ECA server started successfully" )
260+ self :_add_log (" INFO" , " ECA server initialized and running successfully" )
215261
216262 -- Send initialized notification
217263 self ._rpc :send_notification (" initialized" , {})
@@ -227,6 +273,8 @@ function M:stop()
227273 return
228274 end
229275
276+ self :_add_log (" INFO" , " Stopping ECA server..." )
277+
230278 if self ._rpc then
231279 self ._rpc :send_request (" shutdown" , {}, function ()
232280 self ._rpc :send_notification (" exit" , {})
@@ -252,6 +300,7 @@ function M:stop()
252300 self ._chat_id = nil
253301 self :_change_status (ServerStatus .Stopped )
254302 Utils .info (" ECA server stopped" )
303+ self :_add_log (" INFO" , " ECA server stopped successfully" )
255304end
256305
257306--- @param method string
0 commit comments