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 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)()

View File

@@ -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

View File

@@ -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,

View File

@@ -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
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 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

View File

@@ -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