Improved logging via serial.

Created a single log function that prints a standard message that
includes IP and port of client. Remove or commented out other debug
prints.
This commit is contained in:
Marcos Kirsch 2017-02-04 21:50:57 -06:00
parent 6efa4ce3c0
commit 2e25a63807
4 changed files with 31 additions and 15 deletions

View File

@ -25,6 +25,10 @@ function BufferedConnection:new(connection)
return false
end
function newInstance:getpeer()
return self.connection:getpeer()
end
function newInstance:send(payload)
local flushThreshold = 1400
local newSize = self.size + payload:len()

View File

@ -14,7 +14,13 @@ return function (connection, req, args)
return header
end
print("Error " .. args.code .. ": " .. args.errorString)
args.logFunction(connection, "Error " .. args.code .. ": " .. args.errorString)
-- local port, ip = connection:getpeer()
-- print("FIX", ip .. ":" .. port, "Error " .. args.code .. ": " .. args.errorString)
-- port = nil
-- ip = nil
args.headers = args.headers or {}
connection:send(getHeader(connection, args.code, args.errorString, args.headers, "text/html"))
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")

View File

@ -20,7 +20,7 @@ return function (connection, req, args)
chunk = nil
collectgarbage()
end
print("Finished sending: ", args.file)
-- print("Finished sending: ", args.file)
fileHandle:close()
fileHandle = nil
collectgarbage()

View File

@ -16,6 +16,16 @@ return function (port)
local allowStatic = {GET=true, HEAD=true, POST=false, PUT=false, DELETE=false, TRACE=false, OPTIONS=false, CONNECT=false, PATCH=false}
-- Pretty log function.
local function log(connection, msg, optionalMsg)
local port, ip = connection:getpeer()
if(optionalMsg == nil) then
print(ip .. ":" .. port, msg)
else
print(ip .. ":" .. port, msg, optionalMsg)
end
end
local function startServing(fileServeFunction, connection, req, args)
connectionThread = coroutine.create(function(fileServeFunction, bufferedConnection, req, args)
fileServeFunction(bufferedConnection, req, args)
@ -29,9 +39,7 @@ return function (port)
local BufferedConnectionClass = dofile("httpserver-connection.lc")
local bufferedConnection = BufferedConnectionClass:new(connection)
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
if not status then
print("Error: ", err)
end
if not status then log(connection, "Error: "..err) end
end
local function handleRequest(connection, req)
@ -42,7 +50,7 @@ return function (port)
if #(uri.file) > 32 then
-- nodemcu-firmware cannot handle long filenames.
uri.args = {code = 400, errorString = "Bad Request"}
uri.args = {code = 400, errorString = "Bad Request", logFunction = log}
fileServeFunction = dofile("httpserver-error.lc")
else
local fileExists = file.open(uri.file, "r")
@ -61,7 +69,7 @@ return function (port)
end
if not fileExists then
uri.args = {code = 404, errorString = "Not Found"}
uri.args = {code = 404, errorString = "Not Found", logFunction = log}
fileServeFunction = dofile("httpserver-error.lc")
elseif uri.isScript then
fileServeFunction = dofile(uri.file)
@ -70,7 +78,7 @@ return function (port)
uri.args = {file = uri.file, ext = uri.ext, isGzipped = uri.isGzipped}
fileServeFunction = dofile("httpserver-static.lc")
else
uri.args = {code = 405, errorString = "Method not supported"}
uri.args = {code = 405, errorString = "Method not supported", logFunction = log}
fileServeFunction = dofile("httpserver-error.lc")
end
end
@ -102,7 +110,7 @@ return function (port)
-- parse payload and decide what to serve.
local req = dofile("httpserver-request.lc")(payload)
print(req.method .. ": " .. req.request)
log(connection, req.method, req.request)
if conf.auth.enabled then
auth = dofile("httpserver-basicauth.lc")
user = auth.authenticate(payload) -- authenticate returns nil on failed auth
@ -114,11 +122,11 @@ return function (port)
local args = {}
local fileServeFunction = dofile("httpserver-error.lc")
if not user then
args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader()}}
args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader()}, logFunction = log}
elseif req.methodIsValid then
args = {code = 501, errorString = "Not Implemented"}
args = {code = 501, errorString = "Not Implemented", logFunction = log}
else
args = {code = 400, errorString = "Bad Request"}
args = {code = 400, errorString = "Bad Request", logFunction = log}
end
startServing(fileServeFunction, connection, req, args)
end
@ -131,9 +139,7 @@ return function (port)
if connectionThreadStatus == "suspended" then
-- Not finished sending file, resume.
local status, err = coroutine.resume(connectionThread)
if not status then
print(err)
end
if not status then log(getpeer(), "Error: " .. err) end
elseif connectionThreadStatus == "dead" then
-- We're done sending file.
connection:close()