diff options
| author | rebecca <ubq323@ubq323.website> | 2026-09-13 14:55:10 +0100 |
|---|---|---|
| committer | rebecca <ubq323@ubq323.website> | 2026-09-13 14:55:10 +0100 |
| commit | dbb98dbb0233f8d9fd6a31849de4e297d838189a (patch) | |
| tree | 2c20dd91c59bf345e9a5cfda50a2031673a7da9b | |
| parent | af5285331ec224b14a3de9eda7e3db648603084b (diff) | |
add Log class
| -rw-r--r-- | bs.lua | 9 | ||||
| -rw-r--r-- | config.lua | 1 | ||||
| -rw-r--r-- | discord/pylon.lua | 13 | ||||
| -rw-r--r-- | log.lua | 42 | ||||
| -rw-r--r-- | main.lua | 19 | ||||
| -rw-r--r-- | pylon.lua | 7 | ||||
| -rw-r--r-- | wilson.ini.example | 3 |
7 files changed, 66 insertions, 28 deletions
@@ -1,9 +0,0 @@ -local M = {} --- handles errors sensibly -function M.loop_correctly(context, cq) - local ok, err, code, thread, pollee, fd = cq:loop() - error('👉'..context..' '..debug.traceback(thread,err)..'👈',0) -end - -return M - @@ -14,6 +14,7 @@ local function tuple_syntax(pattern) return function(block,line) end end local schema = { + top = kv_syntax, pylon = kv_syntax, bus = tuple_syntax"^(%S+)%s+(%S+)$", } diff --git a/discord/pylon.lua b/discord/pylon.lua index 180ec27..0e8c470 100644 --- a/discord/pylon.lua +++ b/discord/pylon.lua @@ -50,7 +50,7 @@ function Discord._connect(self) local me = self:_req"users/@me" self.bot_id = me.id - self.log('logged in as ',me) + self.log('logged in as ',me.username) for busname, bus in pairs(self.wilson.busses) do for _, channel in ipairs(bus) do @@ -65,15 +65,14 @@ function Discord.add_channel(self, channel) local webhooks = self:_req("channels/"..channel.descriptor.."/webhooks") for _,wh in pairs(webhooks) do if wh.application_id == self.bot_id then - -- self.log.debug("Found existing webhook for channel",channel.descriptor) + self.log("Found existing webhook for channel",channel.descriptor) self.channel_to_webhook[channel.descriptor] = wh return end end - print("Making webhook for channel "..channel.descriptor) + self.log("Making webhook for channel",channel.descriptor) local wh = self:_req_post("channels/"..channel.descriptor.."/webhooks", { name=self.name:gsub("discord",""):gsub("clyde","").." wilson hook" }) - pprint(wh) self.channel_to_webhook[channel.descriptor] = wh end @@ -92,9 +91,9 @@ end function Discord.recving(self) for packet in self.ws:each() do local event = json.decode(packet) - print(event.s, event.op, event.t) + self.log:debug(event.s, event.op, event.t) if event.op ~= opcodes.dispatch then - pprint(event.d) + self.log:debug(event.d) end if event.s then self.sequence_number = event.s end @@ -117,7 +116,7 @@ function Discord.handle_dispatch(self, event) sender = '[d]' .. d.author.username, }) elseif event.t == 'READY' then - pprint(event.d) + self.log:debug(event.d) end end @@ -0,0 +1,42 @@ +local pprint = require 'pprint' +local class = require 'r.class' +local qw = require 'r.qw' + +local Log = class() +function Log.make(cls, name, level) + return setmetatable({ + name=name, + level = level or 'info', + },cls) +end +local levels = qw"debug info warn error" +function Log.format(self, level, ...) + local vals = {} for i=1,select('#',...) do + vals[i] = tostring(select(i,...)) end + return (level == 'error' and '\x1b[31m' or '') + ..os.date"%Y-%m-%d %H:%M:%S%z" + .. ' ' .. level .. (" "):rep(5-#level) + .. ' [' .. self.name .. '] ' + .. table.concat(vals,' ') .. '\x1b[0m' .. '\n' +end +function Log.msg(self,level,...) + if levels[level] < levels[self.level] then return end + io.stderr:write(self:format(level, ...)) + io.stderr:flush() +end +for i,v in ipairs(levels) do + levels[v]=i + Log[v] = function(self,...) self:msg(v,...) end +end +Log.__call = Log.info + +-- handle errors correctly +function Log.loop(self, cq) + local ok, err, code, thread, pollee, fd = cq:loop() + local msg = debug.traceback(thread, err) + self:error(err) + error('👉'..self.name..' '..msg,0) +end + +return Log + @@ -2,9 +2,9 @@ local cqueues = require'cqueues' local class = require'r.class' local pprint = require'pprint' -local bs = require 'bs' local config = require'config' local Channel = require'channel' +local Log = require 'log' local pylon_classes = { irc = require'irc.pylon', @@ -15,17 +15,19 @@ local pylon_classes = { local Wilson = class() function Wilson.make(cls, conf) + local tlc = conf.top.level or {} local self = setmetatable({ pylons={}, busses={}, cq = cqueues.new(), + log = Log('(toplevel)',tlc.loglevel or 'info'), }, Wilson) for name, pylon_conf in pairs(conf.pylon) do pylon_conf.name = name local pylon_class = pylon_classes[pylon_conf.type] assert(pylon_class, "no such pylon type "..pylon_conf.type) self.pylons[name] = pylon_class:make(self, pylon_conf) - print("constructed pylon",name,pylon_conf.type) + self.log("constructed pylon",name,pylon_conf.type) end for name, bus_conf in pairs(conf.bus) do local bus = {} @@ -35,24 +37,25 @@ function Wilson.make(cls, conf) assert(pylon,"no such pylon named "..pylon_name) table.insert(bus, Channel(pylon, channel_descriptor)) end - print("constructed bus ",name,'with '..#bus_conf..' channels') + self.log("constructed bus ",name,'with '..#bus_conf..' channels') self.busses[name] = bus end - return self end + return self +end -- find bus containing given channel function Wilson._find_bus(self, channel) for name, bus in pairs(self.busses) do for _, q in ipairs(bus) do if q == channel then return bus end end end - print("warning: unfound bus for",channel) + self.log:warn("unfound bus for",channel) end function Wilson.deliver(self, source_channel, message) local bus = self:_find_bus(source_channel) if bus then for _, dest_channel in ipairs(bus) do if source_channel ~= dest_channel then - pprint(source_channel, "-->", dest_channel, message) + self.log:debug(source_channel, "-->", dest_channel, message) dest_channel.pylon:post(dest_channel, message) end end @@ -61,9 +64,9 @@ end function Wilson.run(self) for pylonname, pylon in pairs(self.pylons) do self.cq:wrap(pylon.run, pylon) - print("now running pylon", pylonname) + self.log("now running pylon", pylonname) end - bs.loop_correctly('(toplevel)',self.cq) + self.log:loop(self.cq) end local config_file = assert(io.open("wilson.ini","r")) @@ -3,8 +3,8 @@ local pprint = require 'pprint' local class = require 'r.class' local qw = require 'r.qw' -local bs = require'bs' local Queue = require 'queue' +local Log = require'log' local BasePylon = class() @@ -12,14 +12,13 @@ function BasePylon.make(cls, wilson, conf) local self = setmetatable(conf, cls) self.wilson = wilson self.inbox = Queue() + self.log = Log(self.name,self.loglevel) self:init() return self end function BasePylon._check_fields(self, fields) for k in qw.i(fields) do assert(self[k]~=nil, self.name..": missing field "..k) end end function BasePylon.post(self, dest_channel, message) self.inbox:enqueue(dest_channel, message) end -function BasePylon.log(self, ...) - if self.debug then print(self.name, ...) end end function BasePylon.run(self) self.cq = cqueues.new() self.cq:wrap(function() @@ -27,6 +26,6 @@ function BasePylon.run(self) self.cq:wrap(self.recving, self) self.cq:wrap(self.sending, self) end) - bs.loop_correctly(self.name, self.cq) + self.log:loop(self.cq) end return BasePylon diff --git a/wilson.ini.example b/wilson.ini.example index ee72645..86031a5 100644 --- a/wilson.ini.example +++ b/wilson.ini.example @@ -1,3 +1,6 @@ +[top level] +loglevel=info + [pylon xmpp-barbaz] type=xmpp ; port 5222 |
