Skip to content

Commit

Permalink
feat(logging): add lualogging comaptible logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Sep 12, 2024
1 parent 180ebd0 commit 78628bc
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions rockspecs/pegasus-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ build = {
['pegasus.request'] = 'src/pegasus/request.lua',
['pegasus.response'] = 'src/pegasus/response.lua',
['pegasus.compress'] = 'src/pegasus/compress.lua',
['pegasus.log'] = 'src/pegasus/log.lua',
['pegasus.plugins.compress'] = 'src/pegasus/plugins/compress.lua',
['pegasus.plugins.downloads'] = 'src/pegasus/plugins/downloads.lua',
['pegasus.plugins.files'] = 'src/pegasus/plugins/files.lua',
Expand Down
8 changes: 6 additions & 2 deletions src/pegasus/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ local Files = require 'pegasus.plugins.files'
local Handler = {}
Handler.__index = Handler

function Handler:new(callback, location, plugins)
function Handler:new(callback, location, plugins, logger)
local handler = {}
handler.log = logger or require('pegasus.log')
handler.callback = callback
handler.plugins = plugins or {}

Expand All @@ -15,6 +16,9 @@ function Handler:new(callback, location, plugins)
location = location,
default = "/index.html",
}
handler.log:debug('Handler created, location: %s', location)
else
handler.log:debug('Handler created, without location')
end

local result = setmetatable(handler, self)
Expand All @@ -37,7 +41,7 @@ end
function Handler:pluginsNewConnection(client)
for _, plugin in ipairs(self.plugins) do
if plugin.newConnection then
client = plugin:newConnection(client)
client = plugin:newConnection(client, self)
if not client then
return false
end
Expand Down
9 changes: 6 additions & 3 deletions src/pegasus/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ function Pegasus:new(params)
server.location = params.location or ''
server.plugins = params.plugins or {}
server.timeout = params.timeout or 1
server.log = params.log or require('pegasus.log')

return setmetatable(server, self)
end

function Pegasus:start(callback)
local handler = Handler:new(callback, self.location, self.plugins)
local handler = Handler:new(callback, self.location, self.plugins, self.log)
local server = assert(socket.bind(self.host, self.port))
local ip, port = server:getsockname()
print('Pegasus is up on ' .. ip .. ":".. port)

print('Pegasus is up on ' .. ip .. ":".. port) -- needed in case no LuaLogging is available
handler.log:info('Pegasus is up on %s:%s', ip, port)

while 1 do
local client, errmsg = server:accept()
Expand All @@ -30,7 +33,7 @@ function Pegasus:start(callback)
client:settimeout(self.timeout, 'b')
handler:processRequest(self.port, client, server)
else
io.stderr:write('Failed to accept connection:' .. errmsg .. '\n')
handler.log:error('Failed to accept connection: %s', errmsg)
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions src/pegasus/log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- logging

-- returns a LuaLogging compatible logger object.
-- If LuaLogging was already loaded, it returns the defaultlogger,
-- otherwise returns a stub. The stub has only no-op functions.

local ll = package.loaded.logging
if ll and type(ll) == "table" and ll.defaultLogger and
tostring(ll._VERSION):find("LuaLogging") then
-- default LuaLogging logger is available
return ll.defaultLogger()
else
-- just use a stub logger with only no-op functions
local nop = function() end
return setmetatable({}, {
__index = function(self, key) self[key] = nop return nop end
})
end
7 changes: 4 additions & 3 deletions src/pegasus/plugins/tls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function TLS:new(sslparams)
}, TLS)
end

function TLS:newConnection(client)
function TLS:newConnection(client, handler)
local params = self.sslparams

-- wrap the client socket and replace it
Expand All @@ -42,8 +42,9 @@ function TLS:newConnection(client)
assert(client:sni(params.sni.names, params.sni.strict))
end

if not client:dohandshake() then
print"tls handshake failed"
local ok, err = client:dohandshake()
if not ok then
handler.log:error("tls handshake failed: %s", err)
return false
end

Expand Down
3 changes: 3 additions & 0 deletions src/pegasus/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function Request:new(port, client, server, handler)
local obj = {}
obj.client = client
obj.server = server
obj.log = handler.log
obj.port = port
obj.ip = (client.getpeername or function() end)(client) -- luasec doesn't support this method
obj.querystring = {}
Expand Down Expand Up @@ -81,6 +82,8 @@ function Request:parseFirstLine()
end
self.response:skipBody(method == "HEAD")

self.log:info('Request for: %s %s', method, path)

local filename = ''
local querystring = ''

Expand Down
1 change: 1 addition & 0 deletions src/pegasus/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Response.__index = Response

function Response:new(client, writeHandler)
local newObj = {}
newObj.log = writeHandler.log
newObj._headersSended = false
newObj._templateFirstLine = 'HTTP/1.1 {{ STATUS_CODE }} {{ STATUS_TEXT }}\r\n'
newObj._headFirstLine = ''
Expand Down

0 comments on commit 78628bc

Please sign in to comment.