summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bs.lua9
-rw-r--r--config.lua1
-rw-r--r--discord/pylon.lua13
-rw-r--r--log.lua42
-rw-r--r--main.lua19
-rw-r--r--pylon.lua7
-rw-r--r--wilson.ini.example3
7 files changed, 66 insertions, 28 deletions
diff --git a/bs.lua b/bs.lua
deleted file mode 100644
index bb284cc..0000000
--- a/bs.lua
+++ /dev/null
@@ -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
-
diff --git a/config.lua b/config.lua
index cba0d0c..c4e78d3 100644
--- a/config.lua
+++ b/config.lua
@@ -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
diff --git a/log.lua b/log.lua
new file mode 100644
index 0000000..3d04bfc
--- /dev/null
+++ b/log.lua
@@ -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
+
diff --git a/main.lua b/main.lua
index a500284..aebc001 100644
--- a/main.lua
+++ b/main.lua
@@ -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"))
diff --git a/pylon.lua b/pylon.lua
index 7841f11..45aa8ab 100644
--- a/pylon.lua
+++ b/pylon.lua
@@ -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