From 3a9f4383909f2998cebf3a5342b60fed64a9bb1f Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Fri, 15 Nov 2019 15:39:15 -0800 Subject: [PATCH] Integrate fmt into log --- plugin/fmt/init.lua | 17 +++++++++++++++++ plugin/http/init.lua | 6 +++--- plugin/log/init.lua | 10 ++++++---- plugin/src/InstanceMap.lua | 2 +- plugin/src/ServeSession.lua | 36 +++++++++++++++++------------------- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/plugin/fmt/init.lua b/plugin/fmt/init.lua index 8699b2b7..0fd30fd6 100644 --- a/plugin/fmt/init.lua +++ b/plugin/fmt/init.lua @@ -220,7 +220,24 @@ local function fmt(template, ...) return buffer:finish() end +--[[ + Wrap the given object in a type that implements the given function as its + Debug implementation, and forwards __tostring to the type's underlying + tostring implementation. +]] +local function debugify(object, fmtFunc) + return setmetatable({}, { + __fmtDebug = function(_, ...) + return fmtFunc(object, ...) + end, + __tostring = function() + return tostring(object) + end, + }) +end + return { debugOutputBuffer = debugOutputBuffer, fmt = fmt, + debugify = debugify, } \ No newline at end of file diff --git a/plugin/http/init.lua b/plugin/http/init.lua index 395bcc7e..faffa54a 100644 --- a/plugin/http/init.lua +++ b/plugin/http/init.lua @@ -17,7 +17,7 @@ local function performRequest(requestParams) local requestId = lastRequestId + 1 lastRequestId = requestId - Log.trace("%s(%d) %s", requestParams.Method, requestId, requestParams.Url) + Log.trace("HTTP {}({}) {}", requestParams.Method, requestId, requestParams.Url) if requestParams.Body ~= nil then Log.trace(requestParams.Body) @@ -30,10 +30,10 @@ local function performRequest(requestParams) end) if success then - Log.trace("Request %d success: status code %s", requestId, response.StatusCode) + Log.trace("Request {} success: status code {:?}", requestId, response.StatusCode) resolve(HttpResponse.fromRobloxResponse(response)) else - Log.trace("Request %d failure: %s", requestId, response) + Log.trace("Request {} failure: {:?}", requestId, response) reject(HttpError.fromRobloxErrorString(response)) end end)() diff --git a/plugin/log/init.lua b/plugin/log/init.lua index d3defd6d..dae88594 100644 --- a/plugin/log/init.lua +++ b/plugin/log/init.lua @@ -1,3 +1,5 @@ +local Fmt = require(script.Parent.Fmt) + local Level = { Error = 0, Warning = 1, @@ -29,25 +31,25 @@ end function Log.trace(template, ...) if getLogLevel() >= Level.Trace then - print(addTags(TRACE_TAG, string.format(template, ...))) + print(addTags(TRACE_TAG, Fmt.fmt(template, ...))) end end function Log.info(template, ...) if getLogLevel() >= Level.Info then - print(addTags(INFO_TAG, string.format(template, ...))) + print(addTags(INFO_TAG, Fmt.fmt(template, ...))) end end function Log.debug(template, ...) if getLogLevel() >= Level.Debug then - print(addTags(DEBUG_TAG, string.format(template, ...))) + print(addTags(DEBUG_TAG, Fmt.fmt(template, ...))) end end function Log.warn(template, ...) if getLogLevel() >= Level.Warning then - warn(addTags(WARN_TAG, string.format(template, ...))) + warn(addTags(WARN_TAG, Fmt.fmt(template, ...))) end end diff --git a/plugin/src/InstanceMap.lua b/plugin/src/InstanceMap.lua index e04f918d..43476fb3 100644 --- a/plugin/src/InstanceMap.lua +++ b/plugin/src/InstanceMap.lua @@ -119,7 +119,7 @@ function InstanceMap:__connectSignals(instance) end function InstanceMap:__maybeFireInstanceChanged(instance, propertyName) - Log.trace("%s.%s changed", instance:GetFullName(), propertyName) + Log.trace("{}.{} changed", instance:GetFullName(), propertyName) if self.onInstanceChanged ~= nil then self.onInstanceChanged(instance, propertyName) diff --git a/plugin/src/ServeSession.lua b/plugin/src/ServeSession.lua index 0b533893..4bfb3b65 100644 --- a/plugin/src/ServeSession.lua +++ b/plugin/src/ServeSession.lua @@ -11,28 +11,26 @@ local Status = strict("Session.Status", { Disconnected = "Disconnected", }) -local function fmtPatch(patch) - local output = Fmt.debugOutputBuffer() +local function debugPatch(patch) + return Fmt.debugify(patch, function(patch, output) + output:writeLine("Patch {{") + output:indent() - output:writeLine("Patch {{") - output:indent() + for removed in ipairs(patch.removed) do + output:writeLine("Remove ID {}", removed) + end - for removed in ipairs(patch.removed) do - output:writeLine("Remove ID {}", removed) - end + for id, added in pairs(patch.added) do + output:writeLine("Add ID {} {:#?}", id, added) + end - for id, added in pairs(patch.added) do - output:writeLine("Add ID {} {:#?}", id, added) - end + for _, updated in ipairs(patch.updated) do + output:writeLine("Update ID {} {:#?}", updated.id, updated) + end - for _, updated in ipairs(patch.updated) do - output:writeLine("Update ID {} {:#?}", updated.id, updated) - end - - output:unindent() - output:writeLine("}") - - return output:finish() + output:unindent() + output:write("}") + end) end local ServeSession = {} @@ -104,7 +102,7 @@ function ServeSession:__initialSync(rootInstanceId) game ) - Log.trace("Computed hydration patch: %s", fmtPatch(hydratePatch)) + Log.trace("Computed hydration patch: {:#?}", debugPatch(hydratePatch)) -- TODO: Prompt user to notify them of this patch, since it's -- effectively a conflict between the Rojo server and the client.