From 061ea0e7a30d3d690645b6429d93318db53a5cf1 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Mon, 3 Dec 2018 16:24:28 -0800 Subject: [PATCH] Unify logging --- plugin/src/Http.lua | 26 ++++++++++--------- plugin/src/HttpError.lua | 10 ++++++-- plugin/src/HttpResponse.lua | 10 -------- plugin/src/Logging.lua | 50 +++++++++++++++++++++++++++++-------- plugin/src/Main.server.lua | 7 ++++-- plugin/src/Session.lua | 9 +++---- 6 files changed, 70 insertions(+), 42 deletions(-) diff --git a/plugin/src/Http.lua b/plugin/src/Http.lua index e8753c0e..eda12b05 100644 --- a/plugin/src/Http.lua +++ b/plugin/src/Http.lua @@ -1,23 +1,21 @@ local HttpService = game:GetService("HttpService") -local HTTP_DEBUG = true - local Promise = require(script.Parent.Parent.Promise) +local Logging = require(script.Parent.Logging) local HttpError = require(script.Parent.HttpError) local HttpResponse = require(script.Parent.HttpResponse) -local function dprint(...) - if HTTP_DEBUG then - print(...) - end -end +local lastRequestId = 0 -- TODO: Factor out into separate library, especially error handling local Http = {} function Http.get(url) - dprint("\nGET", url) + local requestId = lastRequestId + 1 + lastRequestId = requestId + + Logging.trace("GET(%d) %s", requestId, url) return Promise.new(function(resolve, reject) coroutine.wrap(function() @@ -29,9 +27,10 @@ function Http.get(url) end) if success then - dprint("\t", response) + Logging.trace("Request %d success: status code %s", requestId, response.StatusCode) resolve(HttpResponse.fromRobloxResponse(response)) else + Logging.trace("Request %d failure: %s", requestId, response) reject(HttpError.fromErrorString(response)) end end)() @@ -39,8 +38,10 @@ function Http.get(url) end function Http.post(url, body) - dprint("\nPOST", url) - dprint(body); + local requestId = lastRequestId + 1 + lastRequestId = requestId + + Logging.trace("POST(%d) %s\n%s", requestId, url, body) return Promise.new(function(resolve, reject) coroutine.wrap(function() @@ -53,9 +54,10 @@ function Http.post(url, body) end) if success then - dprint("\t", response) + Logging.trace("Request %d success: status code %s", requestId, response.StatusCode) resolve(HttpResponse.fromRobloxResponse(response)) else + Logging.trace("Request %d failure: %s", requestId, response) reject(HttpError.fromErrorString(response)) end end)() diff --git a/plugin/src/HttpError.lua b/plugin/src/HttpError.lua index ed611356..462ed5f1 100644 --- a/plugin/src/HttpError.lua +++ b/plugin/src/HttpError.lua @@ -1,3 +1,8 @@ +local Selection = game:GetService("Selection") +local HttpService = game:GetService("HttpService") + +local Logging = require(script.Parent.Logging) + local HttpError = {} HttpError.__index = HttpError @@ -58,9 +63,10 @@ function HttpError.fromErrorString(err) end function HttpError:report() - warn(self.message) + Logging.warn(self.message) + if self.type == HttpError.Error.HttpNotEnabled then - game:GetService("Selection"):Set{game:GetService("HttpService")} + Selection:Set(HttpService) end end diff --git a/plugin/src/HttpResponse.lua b/plugin/src/HttpResponse.lua index c9a210ca..637f57de 100644 --- a/plugin/src/HttpResponse.lua +++ b/plugin/src/HttpResponse.lua @@ -13,16 +13,6 @@ function HttpResponse:__tostring() return stringTemplate:format(self.code, self.body) end -function HttpResponse.new(body) - local response = { - body = body, - } - - setmetatable(response, HttpResponse) - - return response -end - function HttpResponse.fromRobloxResponse(response) local self = { body = response.Body, diff --git a/plugin/src/Logging.lua b/plugin/src/Logging.lua index 0cedb620..a7f15c55 100644 --- a/plugin/src/Logging.lua +++ b/plugin/src/Logging.lua @@ -1,21 +1,49 @@ -local enabledByTest = false +local testLogLevel = nil +local configValue = game:FindFirstChild("ROJO_LOG") -local function isEnabled() - return _G.ROJO_LOG or enabledByTest +local Level = { + Error = 0, + Warning = 1, + Info = 2, + Trace = 3, +} + +local function getLogLevel() + if testLogLevel ~= nil then + return testLogLevel + end + + if _G.ROJO_LOG ~= nil then + return _G.ROJO_LOG + end + + if configValue ~= nil then + return configValue.Value + end + + return Level.Info end -local Logging = {} +local Log = {} -function Logging.info(template, ...) - if isEnabled() then - print("[Rojo] " .. template:format(...)) +Log.Level = Level + +function Log.trace(template, ...) + if getLogLevel() >= Level.Trace then + print("[Rojo-Trace] " .. string.format(template, ...)) end end -function Logging.warn(template, ...) - if isEnabled() then - warn("[Rojo] " .. template:format(...)) +function Log.info(template, ...) + if getLogLevel() >= Level.Info then + print("[Rojo-Info] " .. string.format(template, ...)) end end -return Logging \ No newline at end of file +function Log.warn(template, ...) + if getLogLevel() >= Level.Warning then + warn("[Rojo-Warn] " .. string.format(template, ...)) + end +end + +return Log \ No newline at end of file diff --git a/plugin/src/Main.server.lua b/plugin/src/Main.server.lua index e2c50be3..844c5e87 100644 --- a/plugin/src/Main.server.lua +++ b/plugin/src/Main.server.lua @@ -5,6 +5,7 @@ end local Session = require(script.Parent.Session) local Config = require(script.Parent.Config) local Version = require(script.Parent.Version) +local Logging = require(script.Parent.Logging) --[[ Check if the user is using a newer version of Rojo than last time. If they @@ -41,6 +42,8 @@ end local function main() local displayedVersion = Config.dev and "DEV" or Version.display(Config.version) + Logging.trace("Rojo %s initialized", displayedVersion) + local toolbar = plugin:CreateToolbar("Rojo " .. displayedVersion) local currentSession @@ -52,11 +55,11 @@ local function main() checkUpgrade() if currentSession ~= nil then - warn("Rojo: A session is already running!") + Logging.warn("A session is already running!") return end - print("Rojo: Started session.") + Logging.info("Started new session.") currentSession = Session.new() end) end diff --git a/plugin/src/Session.lua b/plugin/src/Session.lua index 4eb1d012..ab47170a 100644 --- a/plugin/src/Session.lua +++ b/plugin/src/Session.lua @@ -1,5 +1,6 @@ local Config = require(script.Parent.Config) local ApiContext = require(script.Parent.ApiContext) +local Logging = require(script.Parent.Logging) local REMOTE_URL = ("http://localhost:%d"):format(Config.port) @@ -9,16 +10,14 @@ Session.__index = Session function Session.new() local self = {} - setmetatable(self, Session) - local api api = ApiContext.new(REMOTE_URL, function(message) if message.type == "InstanceChanged" then - print("Instance", message.id, "changed!") + Logging.trace("Instance %s changed!", message.id) -- readAll() else - warn("Unknown message type " .. message.type) + Logging.warn("Unknown message type %s", message.type) end end) @@ -30,7 +29,7 @@ function Session.new() return api:retrieveMessages() end) - return self + return setmetatable(self, Session) end return Session