mirror of
https://github.com/ShadowKatStudios/OC-Minitel.git
synced 2024-11-23 02:28:05 +11:00
initial work on #9
This commit is contained in:
parent
d726ddbfb6
commit
284c67994e
92
syslog/OpenOS/etc/rc.d/syslogd.lua
Normal file
92
syslog/OpenOS/etc/rc.d/syslogd.lua
Normal file
@ -0,0 +1,92 @@
|
||||
local event = require "event"
|
||||
local serial = require "serialization"
|
||||
local computer = require "computer"
|
||||
local havenet, net = pcall(require,"net")
|
||||
|
||||
local hostname = os.getenv("HOSTNAME") or computer.address:sub(1,8)
|
||||
local cfg = {}
|
||||
cfg.port = 514
|
||||
cfg.relay = false
|
||||
cfg.relayhost = ""
|
||||
cfg.receive = false
|
||||
cfg.write = true
|
||||
cfg.destination = "/dev/null"
|
||||
cfg.minlevel = 6
|
||||
cfg.filter = {} -- todo
|
||||
|
||||
local listeners = {}
|
||||
local timers = {}
|
||||
|
||||
local function filter(msg,level,service)
|
||||
if level >= cfg.minlevel then
|
||||
return cfg.write, (cfg.relay and havenet)
|
||||
end
|
||||
return false, false
|
||||
end
|
||||
|
||||
function reload()
|
||||
local f = io.open("/etc/syslogd.cfg","rb")
|
||||
print(f)
|
||||
if f then
|
||||
local newcfg = serial.unserialize(f:read("*a"))
|
||||
f:close()
|
||||
for k,v in pairs(newcfg) do
|
||||
cfg[k] = v
|
||||
end
|
||||
else
|
||||
local f = io.open("/etc/syslogd.cfg","wb")
|
||||
if f then
|
||||
f:write(serial.serialize(cfg))
|
||||
f:close()
|
||||
end
|
||||
end
|
||||
hostname = os.getenv("HOSTNAME") or computer.address:sub(1,8)
|
||||
end
|
||||
|
||||
local function wentry(_,msg,level,service,host)
|
||||
host = host or hostname
|
||||
local ut = computer.uptime()
|
||||
local msg, service = tostring(msg), tostring(service)
|
||||
local entry = string.format("%s\t%i\t%s\n",service,level,msg)
|
||||
local dwrite, drelay = filter(msg,level,service)
|
||||
if dwrite then
|
||||
local f = io.open(cfg.destination, "ab")
|
||||
if f then
|
||||
f:write(string.format("%.2f\t%s\t",ut,host)..entry)
|
||||
f:close()
|
||||
end
|
||||
end
|
||||
if drelay then
|
||||
net.rsend(cfg.relayhost, cfg.port, entry)
|
||||
end
|
||||
print(msg,level,service,host)
|
||||
end
|
||||
|
||||
local function remote_listener(_,from,port,data)
|
||||
if port ~= cfg.port then return false end
|
||||
local ut = computer.uptime()
|
||||
local service, level, msg = data:match("(.-)\t(%d)\t(.+)")
|
||||
if not service and not level and not msg then return false end
|
||||
wentry(nil,msg,level,service,from)
|
||||
print(msg,level,service)
|
||||
end
|
||||
|
||||
function start()
|
||||
reload()
|
||||
if #listeners > 0 then return end
|
||||
event.listen("syslog",wentry)
|
||||
listeners[#listeners+1] = {"syslog",local_listener}
|
||||
if havenet and cfg.receive then
|
||||
event.listen("net_msg",remote_listener)
|
||||
listeners[#listeners+1] = {"net_msg",remote_listener}
|
||||
end
|
||||
end
|
||||
|
||||
function stop()
|
||||
for k,v in pairs(listeners) do
|
||||
event.ignore(v[1],v[2])
|
||||
end
|
||||
for k,v in pairs(timers) do
|
||||
event.cancel(v)
|
||||
end
|
||||
end
|
19
syslog/OpenOS/usr/lib/syslog.lua
Normal file
19
syslog/OpenOS/usr/lib/syslog.lua
Normal file
@ -0,0 +1,19 @@
|
||||
local process = require "process"
|
||||
local computer = require "computer"
|
||||
|
||||
local syslog = {}
|
||||
syslog.emergency = 0
|
||||
syslog.alert = 1
|
||||
syslog.critical = 2
|
||||
syslog.error = 3
|
||||
syslog.warning = 4
|
||||
syslog.notice = 5
|
||||
syslog.info = 6
|
||||
syslog.debug = 7
|
||||
|
||||
setmetatable(syslog,{__call = function(_,msg, level, service)
|
||||
level, service = level or syslog.info, service or process.info().path
|
||||
computer.pushSignal("syslog",msg, level, service)
|
||||
end})
|
||||
|
||||
return syslog
|
Loading…
Reference in New Issue
Block a user