Unify logging

This commit is contained in:
Lucien Greathouse
2018-12-03 16:24:28 -08:00
parent dd4d542d7e
commit 061ea0e7a3
6 changed files with 70 additions and 42 deletions

View File

@@ -1,23 +1,21 @@
local HttpService = game:GetService("HttpService") local HttpService = game:GetService("HttpService")
local HTTP_DEBUG = true
local Promise = require(script.Parent.Parent.Promise) local Promise = require(script.Parent.Parent.Promise)
local Logging = require(script.Parent.Logging)
local HttpError = require(script.Parent.HttpError) local HttpError = require(script.Parent.HttpError)
local HttpResponse = require(script.Parent.HttpResponse) local HttpResponse = require(script.Parent.HttpResponse)
local function dprint(...) local lastRequestId = 0
if HTTP_DEBUG then
print(...)
end
end
-- TODO: Factor out into separate library, especially error handling -- TODO: Factor out into separate library, especially error handling
local Http = {} local Http = {}
function Http.get(url) 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) return Promise.new(function(resolve, reject)
coroutine.wrap(function() coroutine.wrap(function()
@@ -29,9 +27,10 @@ function Http.get(url)
end) end)
if success then if success then
dprint("\t", response) Logging.trace("Request %d success: status code %s", requestId, response.StatusCode)
resolve(HttpResponse.fromRobloxResponse(response)) resolve(HttpResponse.fromRobloxResponse(response))
else else
Logging.trace("Request %d failure: %s", requestId, response)
reject(HttpError.fromErrorString(response)) reject(HttpError.fromErrorString(response))
end end
end)() end)()
@@ -39,8 +38,10 @@ function Http.get(url)
end end
function Http.post(url, body) function Http.post(url, body)
dprint("\nPOST", url) local requestId = lastRequestId + 1
dprint(body); lastRequestId = requestId
Logging.trace("POST(%d) %s\n%s", requestId, url, body)
return Promise.new(function(resolve, reject) return Promise.new(function(resolve, reject)
coroutine.wrap(function() coroutine.wrap(function()
@@ -53,9 +54,10 @@ function Http.post(url, body)
end) end)
if success then if success then
dprint("\t", response) Logging.trace("Request %d success: status code %s", requestId, response.StatusCode)
resolve(HttpResponse.fromRobloxResponse(response)) resolve(HttpResponse.fromRobloxResponse(response))
else else
Logging.trace("Request %d failure: %s", requestId, response)
reject(HttpError.fromErrorString(response)) reject(HttpError.fromErrorString(response))
end end
end)() end)()

View File

@@ -1,3 +1,8 @@
local Selection = game:GetService("Selection")
local HttpService = game:GetService("HttpService")
local Logging = require(script.Parent.Logging)
local HttpError = {} local HttpError = {}
HttpError.__index = HttpError HttpError.__index = HttpError
@@ -58,9 +63,10 @@ function HttpError.fromErrorString(err)
end end
function HttpError:report() function HttpError:report()
warn(self.message) Logging.warn(self.message)
if self.type == HttpError.Error.HttpNotEnabled then if self.type == HttpError.Error.HttpNotEnabled then
game:GetService("Selection"):Set{game:GetService("HttpService")} Selection:Set(HttpService)
end end
end end

View File

@@ -13,16 +13,6 @@ function HttpResponse:__tostring()
return stringTemplate:format(self.code, self.body) return stringTemplate:format(self.code, self.body)
end end
function HttpResponse.new(body)
local response = {
body = body,
}
setmetatable(response, HttpResponse)
return response
end
function HttpResponse.fromRobloxResponse(response) function HttpResponse.fromRobloxResponse(response)
local self = { local self = {
body = response.Body, body = response.Body,

View File

@@ -1,21 +1,49 @@
local enabledByTest = false local testLogLevel = nil
local configValue = game:FindFirstChild("ROJO_LOG")
local function isEnabled() local Level = {
return _G.ROJO_LOG or enabledByTest 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 end
local Logging = {} local Log = {}
function Logging.info(template, ...) Log.Level = Level
if isEnabled() then
print("[Rojo] " .. template:format(...)) function Log.trace(template, ...)
if getLogLevel() >= Level.Trace then
print("[Rojo-Trace] " .. string.format(template, ...))
end end
end end
function Logging.warn(template, ...) function Log.info(template, ...)
if isEnabled() then if getLogLevel() >= Level.Info then
warn("[Rojo] " .. template:format(...)) print("[Rojo-Info] " .. string.format(template, ...))
end end
end end
return Logging function Log.warn(template, ...)
if getLogLevel() >= Level.Warning then
warn("[Rojo-Warn] " .. string.format(template, ...))
end
end
return Log

View File

@@ -5,6 +5,7 @@ end
local Session = require(script.Parent.Session) local Session = require(script.Parent.Session)
local Config = require(script.Parent.Config) local Config = require(script.Parent.Config)
local Version = require(script.Parent.Version) 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 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 function main()
local displayedVersion = Config.dev and "DEV" or Version.display(Config.version) local displayedVersion = Config.dev and "DEV" or Version.display(Config.version)
Logging.trace("Rojo %s initialized", displayedVersion)
local toolbar = plugin:CreateToolbar("Rojo " .. displayedVersion) local toolbar = plugin:CreateToolbar("Rojo " .. displayedVersion)
local currentSession local currentSession
@@ -52,11 +55,11 @@ local function main()
checkUpgrade() checkUpgrade()
if currentSession ~= nil then if currentSession ~= nil then
warn("Rojo: A session is already running!") Logging.warn("A session is already running!")
return return
end end
print("Rojo: Started session.") Logging.info("Started new session.")
currentSession = Session.new() currentSession = Session.new()
end) end)
end end

View File

@@ -1,5 +1,6 @@
local Config = require(script.Parent.Config) local Config = require(script.Parent.Config)
local ApiContext = require(script.Parent.ApiContext) local ApiContext = require(script.Parent.ApiContext)
local Logging = require(script.Parent.Logging)
local REMOTE_URL = ("http://localhost:%d"):format(Config.port) local REMOTE_URL = ("http://localhost:%d"):format(Config.port)
@@ -9,16 +10,14 @@ Session.__index = Session
function Session.new() function Session.new()
local self = {} local self = {}
setmetatable(self, Session)
local api local api
api = ApiContext.new(REMOTE_URL, function(message) api = ApiContext.new(REMOTE_URL, function(message)
if message.type == "InstanceChanged" then if message.type == "InstanceChanged" then
print("Instance", message.id, "changed!") Logging.trace("Instance %s changed!", message.id)
-- readAll() -- readAll()
else else
warn("Unknown message type " .. message.type) Logging.warn("Unknown message type %s", message.type)
end end
end) end)
@@ -30,7 +29,7 @@ function Session.new()
return api:retrieveMessages() return api:retrieveMessages()
end) end)
return self return setmetatable(self, Session)
end end
return Session return Session