Skip to content

Commit

Permalink
add logging module
Browse files Browse the repository at this point in the history
  • Loading branch information
fechan committed May 7, 2024
1 parent 03306c2 commit 28c80a4
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ factory.json
node_modules/
build/
.env
sigils-config.json
sigils-config.json
sigils-log.txt
1 change: 1 addition & 0 deletions computercraft/installer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local files = {
'sigils/controller.lua',
'sigils/factory.lua',
'sigils/filter.lua',
'sigils/logger.lua',
'sigils/machine.lua',
'sigils/pipe.lua',
'sigils/transfer-calculator.lua',
Expand Down
3 changes: 3 additions & 0 deletions computercraft/sigils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local Factory = require('sigils.factory')
local Pipe = require('sigils.pipe')
local WebSocket = require('sigils.websocket')
local Utils = require('sigils.utils')
local Logging = require('sigils.logging')

local DEFAULT_SERVER_URL = 'wss://sigils.fredchan.org'

Expand Down Expand Up @@ -50,6 +51,8 @@ local function init ()
local factory

local config = getConfig()
Logging.LOGGER:setLevel(config.logLevel or 1)
Logging.LOGGER:warn("test")

-- if there's no existing json file, generate a factory from detected peripherals
if factoryJsonFile == nil then
Expand Down
38 changes: 38 additions & 0 deletions computercraft/sigils/logging.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local Utils = require('sigils.utils')

local f = fs.open(Utils.absolutePathTo('sigils-log.txt'), 'w')
f.close()

local LEVELS = {
DEBUG = 4,
INFO = 3,
WARN = 2,
ERROR = 1,
FATAL = 0,
}

local LOGGER = {
level = 1,
}

function LOGGER:writeLogLine(text)
local f = fs.open(Utils.absolutePathTo('sigils-log.txt'), 'a')
f.write(os.epoch('utc') .. " " .. text .. "\n")
f.close()
end

function LOGGER:setLevel (newLevel)
self.level = newLevel
end

function LOGGER:warn (text)
if self.level >= LEVELS.WARN then
print(text)
self:writeLogLine(text)
end
end

return {
LOGGER = LOGGER,
LEVELS = LEVELS,
}
3 changes: 2 additions & 1 deletion computercraft/sigils/pipe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

local TransferCalculator = require('sigils.transfer-calculator')
local Filter = require('sigils.filter')
local LOGGER = require('sigils.logging').LOGGER

local function processPipe (pipe, groupMap)
local filter = Filter.getFilterFn(pipe.filter)
Expand All @@ -28,7 +29,7 @@ local function processPipe (pipe, groupMap)
end
parallel.waitForAll(unpack(coros))
else
print('caught err', transferOrders)
LOGGER:warn("pipe.lua#processPipe() caught error " .. transferOrders)
end
end

Expand Down
2 changes: 1 addition & 1 deletion computercraft/sigils/sigils-config.dist.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"server": "wss://sigils.fredchan.org",
"verbose": false
"logLevel": 1
}

0 comments on commit 28c80a4

Please sign in to comment.