Skip to content

Commit 343ca7f

Browse files
committed
add propper log file support and dev strategy
1 parent 7738ce9 commit 343ca7f

16 files changed

Lines changed: 620 additions & 123 deletions

File tree

.luacheckrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ max_line_length = false
55
globals = {
66
'love',
77
'getVersion',
8-
'getTitle'
8+
'getTitle',
9+
'DEBUGGER'
910
}
1011

1112
exclude_files = {

.luarc.json

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,12 @@
33
"runtime.version": "LuaJIT",
44

55
"diagnostics.disable": [
6-
// LuaCATS ---@module annotation conflicts with luadoc
7-
"luadoc-miss-module-name"
8-
],
9-
"diagnostics.globals": [
10-
"love",
11-
"jit"
6+
// LuaCATS ---@module annotation conflicts with luadoc
7+
"luadoc-miss-module-name"
128
],
9+
"diagnostics.globals": ["love", "jit", "DEBBUGER"],
1310

1411
"workspace.checkThirdParty": false,
15-
"workspace.ignoreDir": [
16-
".vscode",
17-
"builds",
18-
"resources",
19-
"tools"
20-
],
21-
"workspace.library": [
22-
"${3rd}/love2d/library",
23-
"game/lib"
24-
]
12+
"workspace.ignoreDir": [".vscode", "builds", "resources", "tools"],
13+
"workspace.library": ["${3rd}/love2d/library", "game/lib"]
2514
}

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@tanstack/react-query-persist-client": "^5.84.2",
4141
"@tanstack/react-table": "^8.21.3",
4242
"@tauri-apps/api": "2",
43+
"@tauri-apps/plugin-dialog": "^2.6.0",
4344
"@tauri-apps/plugin-fs": "^2.4.5",
4445
"@tauri-apps/plugin-opener": "^2.4.0",
4546
"@tauri-apps/plugin-shell": "^2.3.0",

src-lua/demo/tetris.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ end
215215
function Game.update(dt)
216216
timer = timer + dt
217217

218-
print(timer)
218+
DEBUGGER.featherLogger:log({
219+
type = "output",
220+
str = "test",
221+
}, true)
222+
219223
-- Removed local timerLimit = 0.5
220224
if timer >= timerLimit then
221225
timer = 0

src-lua/feather/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function Feather:init(config)
6868
self.debug = conf.debug or false
6969
self.host = conf.host or "*"
7070
self.baseDir = conf.baseDir or ""
71-
self.outfile = conf.outfile or "feather.log"
71+
self.outfile = conf.outfile or "feather"
7272
self.port = conf.port or 4004
7373
self.wrapPrint = conf.wrapPrint or false
7474
self.whitelist = conf.whitelist or { "127.0.0.1" }

src-lua/feather/plugins/logger.lua

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ local types = {
2020
["feather:finish"] = "info",
2121
["feather:start"] = "info",
2222
}
23+
local LOGS_DIR = "logs"
24+
local SCREENSHOTS_DIR = LOGS_DIR .. "/" .. "screenshots"
2325

2426
---@class FeatherLogger: FeatherPlugin
2527
---@field debug boolean
2628
---@field wrapPrint boolean
2729
---@field captureScreenshot boolean
2830
---@field lastScreenshot any
2931
---@field outfile string
32+
---@field last_screenshot_taken_at number
33+
---@field screenshotIndex number
34+
---@field screenshotRate number
35+
---@field screenshotPoolSize number
3036
---@field last_log FeatherLine
3137
---@field maxTempLogs number
3238
---@field log fun(self: FeatherLogger, line: FeatherLine, screenshot?: boolean) Logs a line
@@ -44,14 +50,23 @@ local FeatherLogger = Class({
4450
self.captureScreenshot = config.captureScreenshot
4551
self.lastScreenshot = nil
4652
self.last_log = nil
53+
self.last_screenshot_taken_at = nil
54+
self.screenshotIndex = 1
55+
self.screenshotRate = config.screenshotRate or 1
56+
self.screenshotPoolSize = config.screenshotPoolSize or 60
57+
self.lastId = 0
4758

4859
local cwd = love.filesystem.getSaveDirectory()
4960

50-
love.filesystem.createDirectory("logs")
61+
love.filesystem.createDirectory(LOGS_DIR)
5162

52-
local logdir = cwd .. "/logs"
63+
love.filesystem.createDirectory(SCREENSHOTS_DIR)
5364

54-
local logfile = logdir .. "/" .. os.date("%Y-%m-%d_%H:%M:%S") .. "_" .. config.outfile
65+
local logdir = cwd .. "/" .. LOGS_DIR
66+
67+
local logfile = logdir .. "/" .. os.date("%Y-%m-%d_%H:%M:%S") .. "_" .. config.outfile .. ".featherlog"
68+
69+
log.info("Saving logs to " .. logfile)
5570

5671
log.outfile = logfile
5772
log.usecolor = false
@@ -75,14 +90,22 @@ end
7590

7691
function FeatherLogger:update()
7792
if not self.captureScreenshot then
93+
self.lastScreenshot = nil
7894
return
7995
end
8096

81-
self.lastScreenshot = nil
97+
local now = love.timer.getTime()
8298

83-
love.graphics.captureScreenshot(function(img)
84-
self.lastScreenshot = img
85-
end)
99+
if self.last_screenshot_taken_at and now - self.last_screenshot_taken_at < self.screenshotRate then
100+
return -- short throttle so spammy errors don't melt disk
101+
end
102+
103+
self.last_screenshot_taken_at = now
104+
105+
self.screenshotIndex = (self.screenshotIndex or 0) % self.screenshotPoolSize + 1
106+
self.lastScreenshot = SCREENSHOTS_DIR .. "/debug_" .. self.screenshotIndex .. ".png"
107+
108+
love.graphics.captureScreenshot(self.lastScreenshot)
86109
end
87110

88111
--- Manages the print function internally
@@ -126,25 +149,17 @@ function FeatherLogger:log(line, screenshot)
126149
end
127150

128151
if screenshot and self.captureScreenshot then
129-
local takeScreenshot = function()
130-
if not self.lastScreenshot then
131-
return
132-
end
133-
134-
local fileData = self.lastScreenshot:encode("png")
135-
local pngBytes = fileData:getString()
136-
137-
local b64 = love.data.encode("string", "base64", pngBytes)
138-
line.screenshot = b64
152+
if not self.lastScreenshot then
153+
return
139154
end
140155

141-
local result, err = pcall(takeScreenshot)
142-
if not result then
143-
print("Error capturing screenshot: " .. tostring(err))
144-
end
156+
local cwd = love.filesystem.getSaveDirectory()
157+
158+
line.screenshot = cwd .. "/" .. self.lastScreenshot
145159
end
146160

147-
line.id = tostring(os.time())
161+
self.lastId = self.lastId + 1
162+
line.id = tostring(self.lastId)
148163
line.time = os.time()
149164
line.count = 1
150165
line.trace = debug.traceback()

src-lua/main.lua

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ local function customerrorhandler(msg)
147147
end
148148
end
149149

150-
local debugger = FeatherDebugger({
150+
--- If not set, feather will be added in default identity lovegame, which is fine but want to know identity is respected if set
151+
love.filesystem.setIdentity("feather_dev_demo")
152+
153+
DEBUGGER = FeatherDebugger({
151154
errorHandler = customerrorhandler,
152155
wrappedPrint = true,
153156
wrapPrint = true,
@@ -219,16 +222,21 @@ function love.draw()
219222
end
220223

221224
function love.update(dt)
222-
debugger:update(dt)
225+
DEBUGGER:update(dt)
223226
a = a + dt
224227
counter = counter + dt
225228
time = time + dt
226229

227230
if a > 1 then
228231
a = 0
229-
debugger:observe("awesome variable", a)
232+
DEBUGGER:observe("awesome variable", a)
230233
-- print(a)
231234
print(math.random(1, 2))
235+
236+
local original_file = DEBUGGER.featherLogger.outfile
237+
local new_file = love.filesystem.getWorkingDirectory() .. "/public/example.log"
238+
239+
os.rename(original_file, new_file)
232240
end
233241

234242
if counter < 5 then
@@ -243,6 +251,10 @@ function love.update(dt)
243251
counter = 0
244252
end
245253
Game.update(dt)
254+
255+
-- DEBUGGER.featherLogger.outfile
256+
257+
-- Debug logs to dev log files
246258
end
247259

248260
function love.keypressed(key)
@@ -257,13 +269,13 @@ function love.keypressed(key)
257269
end
258270

259271
if key == "f" then
260-
debugger:toggleScreenshots(not debugger.captureScreenshot)
272+
DEBUGGER:toggleScreenshots(not DEBUGGER.captureScreenshot)
261273
end
262274

263275
if key == "f1" then
264-
debugger:action("screenshots", "screenshot", {})
276+
DEBUGGER:action("screenshots", "screenshot", {})
265277
elseif key == "f2" then
266-
debugger:action("screenshots", "gif", { duration = 3, fps = 60 })
278+
DEBUGGER:action("screenshots", "gif", { duration = 3, fps = 60 })
267279
end
268280
Game.keypressed(key)
269281
end

0 commit comments

Comments
 (0)