@@ -19,19 +19,26 @@ local customErrorHandler = errorhandler
1919function Feather :init (config )
2020 local conf = config or {}
2121 self .pages = {}
22+ self .debug = conf .debug or false
2223 self .host = conf .host or " *"
2324 self .baseDir = conf .baseDir or " "
2425 self .port = conf .port or 4004
25- self .wrapPrint = conf .wrapPrint or true
26- self .timestamp = conf .timestamp or true
26+ self .wrapPrint = conf .wrapPrint or false
2727 self .whitelist = conf .whitelist or { " 127.0.0.1" }
2828 self .maxTempLogs = conf .maxTempLogs or 200
2929 self .updateInterval = conf .updateInterval or 0.1
30+ self .defaultObservers = conf .defaultObservers or true
3031 --- TODO: find a better way to ensure that the error handler is called, maybe a thread?
31- self .errorWait = conf .errorWait or 10
32+ self .errorWait = conf .errorWait or 3
3233 self .autoRegisterErrorHandler = conf .autoRegisterErrorHandler and true or false
3334 self .plugins = conf .plugins or {}
3435 self .lastDelivery = 0
36+ self .observers = {}
37+
38+ if not self .debug then
39+ return
40+ end
41+
3542 customErrorHandler = conf .errorHandler or errorhandler
3643
3744 local server = assert (socket .bind (self .host , self .port ))
@@ -55,7 +62,7 @@ function Feather:init(config)
5562
5663 local start = love .timer .getTime ()
5764 while not delivered and (love .timer .getTime () - start ) < selfRef .errorWait do
58- selfRef :update ()
65+ selfRef :update (0 )
5966 delivered = isDelivered ()
6067 love .timer .sleep (self .updateInterval )
6168 end
@@ -135,6 +142,33 @@ function Feather:__isInWhitelist(addr)
135142 return false
136143end
137144
145+ function Feather :__defaultObservers ()
146+ self :observe (" Global" , _G )
147+ self :observe (" Lua Version" , _VERSION )
148+ end
149+
150+ --- Tracks the value of a key in the observers table
151+ --- @param key string
152+ --- @param value any
153+ function Feather :observe (key , value )
154+ if not self .debug then
155+ return
156+ end
157+
158+ self .observers = self .observers or {}
159+
160+ local curr = self :__format (value )
161+
162+ for i , observer in ipairs (self .observers ) do
163+ if observer .key == key then
164+ observer .value = curr
165+ return
166+ end
167+ end
168+
169+ table.insert (self .observers , { key = key , value = curr })
170+ end
171+
138172function Feather :__errorTraceback (msg )
139173 local trace = debug.traceback ()
140174
@@ -179,6 +213,10 @@ function Feather:finish()
179213end
180214
181215function Feather :onerror (msg , finish )
216+ if not self .debug then
217+ return
218+ end
219+
182220 local err = self :__errorTraceback (msg )
183221 self :log ({ type = " error" , str = self :__errorTraceback (msg ) })
184222 if self .wrapPrint then
193231
194232--- @param dt number
195233function Feather :update (dt )
234+ if not self .debug then
235+ return
236+ end
237+
196238 local client = self .server :accept ()
197239 if client then
198240 if # logs == 0 then
@@ -233,6 +275,14 @@ function Feather:update(dt)
233275 response = self :__buildResponse (body )
234276 end
235277
278+ if request .path == " /observers" then
279+ if self .defaultObservers then
280+ self :__defaultObservers ()
281+ end
282+ local body = json .encode (self .observers )
283+ response = self :__buildResponse (body )
284+ end
285+
236286 client :send (response )
237287 end
238288
@@ -241,6 +291,10 @@ function Feather:update(dt)
241291end
242292
243293function Feather :trace (...)
294+ if not self .debug then
295+ return
296+ end
297+
244298 local str = " [Feather] " .. self :__format (... )
245299 self .logger (str )
246300 if not self .wrapPrint then
@@ -249,6 +303,10 @@ function Feather:trace(...)
249303end
250304
251305function Feather :log (line )
306+ if not self .debug then
307+ return
308+ end
309+
252310 line .id = tostring (os.time ()) .. " -" .. tostring (# logs + 1 )
253311 line .time = os.time ()
254312 line .count = 1
@@ -263,6 +321,10 @@ function Feather:log(line)
263321end
264322
265323function Feather :print (...)
324+ if not self .debug then
325+ return
326+ end
327+
266328 local str = self :__format (... )
267329 local last = logs [# logs ]
268330 if last and str == last .str then
0 commit comments