66
77--- @type Validator
88local Validator = assert (SMODS .load_file (" src/lua/core/validator.lua" ))()
9+ --- @type BB_LOGGER
10+ local BB_LOGGER = assert (SMODS .load_file (" src/lua/utils/logger.lua" ))()
11+ local socket = require (" socket" )
912
1013--- @type table<integer , string> ?
1114local STATE_NAME_CACHE = nil
@@ -162,11 +165,15 @@ function BB_DISPATCHER.dispatch(request)
162165 BB_DISPATCHER .send_error (" Unknown method: " .. request .method , BB_ERROR_NAMES .BAD_REQUEST )
163166 return
164167 end
165- sendDebugMessage (" Dispatching: " .. request .method , " BB.DISPATCHER" )
168+
169+ -- Log incoming request with params
170+ local start_time = socket .gettime ()
171+ sendDebugMessage (request .method .. BB_LOGGER .serialize_params (params ), " BB.REQUEST" )
166172
167173 -- TIER 2: Schema Validation
168174 local valid , err_msg , err_code = Validator .validate (params , endpoint .schema )
169175 if not valid then
176+ sendWarnMessage (request .method .. " : " .. (err_msg or " Validation failed" ), " BB.VALIDATION" )
170177 BB_DISPATCHER .send_error (err_msg or " Validation failed" , err_code or BB_ERROR_NAMES .BAD_REQUEST )
171178 return
172179 end
@@ -186,6 +193,11 @@ function BB_DISPATCHER.dispatch(request)
186193 for _ , state in ipairs (endpoint .requires_state ) do
187194 table.insert (state_names , get_state_name (state ))
188195 end
196+ local current_state_name = get_state_name (current_state )
197+ sendWarnMessage (
198+ string.format (" %s: requires %s, current=%s" , request .method , table.concat (state_names , " |" ), current_state_name ),
199+ " BB.STATE"
200+ )
189201 BB_DISPATCHER .send_error (
190202 " Method '" .. request .method .. " ' requires one of these states: " .. table.concat (state_names , " , " ),
191203 BB_ERROR_NAMES .INVALID_STATE
@@ -197,6 +209,14 @@ function BB_DISPATCHER.dispatch(request)
197209 -- TIER 4: Execute Endpoint
198210 local function send_response (response )
199211 if BB_DISPATCHER .Server then
212+ -- Log response with timing
213+ local duration_ms = (socket .gettime () - start_time ) * 1000
214+ local is_error = response .message ~= nil
215+ if is_error then
216+ sendDebugMessage (string.format (" %s ERR (%.0fms)" , request .method , duration_ms ), " BB.RESPONSE" )
217+ else
218+ sendDebugMessage (string.format (" %s OK (%.0fms)" , request .method , duration_ms ), " BB.RESPONSE" )
219+ end
200220 BB_DISPATCHER .Server .send_response (response )
201221 else
202222 sendDebugMessage (" Cannot send response - Server not initialized" , " BB.DISPATCHER" )
@@ -206,6 +226,7 @@ function BB_DISPATCHER.dispatch(request)
206226 endpoint .execute (params , send_response )
207227 end )
208228 if not exec_success then
229+ sendErrorMessage (request .method .. " : " .. tostring (exec_error ), " BB.EXEC" )
209230 BB_DISPATCHER .send_error (tostring (exec_error ), BB_ERROR_NAMES .INTERNAL_ERROR )
210231 end
211232end
0 commit comments