From 0319ae28baeded9b9fe39d8fd9f579d60087e647 Mon Sep 17 00:00:00 2001 From: Garett PHG Date: Mon, 14 Aug 2023 13:18:33 +0700 Subject: [PATCH 1/5] [Feature] Log flushing and Love2D integration Implemeted log flusing: it stores logs and writes to logfile on demand. This helps performance because file operations are locking. Using `love.filesystem` if exist instead `io`. --- log.lua | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/log.lua b/log.lua index d7bc2d4..b74c43a 100644 --- a/log.lua +++ b/log.lua @@ -12,7 +12,10 @@ local log = { _version = "0.1.0" } log.usecolor = true log.outfile = nil log.level = "trace" +log.entries = {} +local os, debug, math, table, string, lfs = os, debug, math, table, string, love and love.filesystem +local ipairs, select, type, print, fileopen = ipairs, select, type, print, lfs and lfs.newFile or io.open local modes = { { name = "trace", color = "\27[34m", }, @@ -53,9 +56,10 @@ end for i, x in ipairs(modes) do + ---@cast x { name: string|function, color: string } local nameupper = x.name:upper() log[x.name] = function(...) - + -- Return early if we're below the log level if i < levels[log.level] then return @@ -74,15 +78,28 @@ for i, x in ipairs(modes) do lineinfo, msg)) - -- Output to log file + -- Store to log table if log.outfile then - local fp = io.open(log.outfile, "a") local str = string.format("[%-6s%s] %s: %s\n", nameupper, os.date(), lineinfo, msg) + table.insert(log.entries, str) + end + end +end + +log.flush = function(outfile) + local e, o = log.entries, outfile or log.outfile + + -- Output to log file + if o then + local fp = fileopen(o, 'a') + + for i = 1, #e do + local str = table.remove(e, i) fp:write(str) - fp:close() end + fp:close() end end From 5bf6f11966c18bf68cde709124de87a336d1dc6e Mon Sep 17 00:00:00 2001 From: Garett PHG Date: Mon, 14 Aug 2023 15:10:18 +0700 Subject: [PATCH 2/5] Fixed how log entry not getting remoed by order. For API usage, log functions return string. --- log.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/log.lua b/log.lua index b74c43a..f325700 100644 --- a/log.lua +++ b/log.lua @@ -78,12 +78,15 @@ for i, x in ipairs(modes) do lineinfo, msg)) + local str = string.format("[%-6s%s] %s: %s\n", + nameupper, os.date(), lineinfo, msg) + -- Store to log table if log.outfile then - local str = string.format("[%-6s%s] %s: %s\n", - nameupper, os.date(), lineinfo, msg) table.insert(log.entries, str) end + + return str end end @@ -95,8 +98,8 @@ log.flush = function(outfile) local fp = fileopen(o, 'a') for i = 1, #e do - local str = table.remove(e, i) - fp:write(str) + fp:write(e[i]) + e[i] = nil end fp:close() From b4c72ff40f9ac640b500a67e85cd29bb3645601b Mon Sep 17 00:00:00 2001 From: Garett PHG Date: Mon, 14 Aug 2023 19:47:29 +0700 Subject: [PATCH 3/5] Added debug thread level. --- log.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/log.lua b/log.lua index f325700..16c5519 100644 --- a/log.lua +++ b/log.lua @@ -13,6 +13,7 @@ log.usecolor = true log.outfile = nil log.level = "trace" log.entries = {} +log.debugthread = 2 local os, debug, math, table, string, lfs = os, debug, math, table, string, love and love.filesystem local ipairs, select, type, print, fileopen = ipairs, select, type, print, lfs and lfs.newFile or io.open @@ -66,7 +67,7 @@ for i, x in ipairs(modes) do end local msg = tostring(...) - local info = debug.getinfo(2, "Sl") + local info = debug.getinfo(log.debugthread, "Sl") local lineinfo = info.short_src .. ":" .. info.currentline -- Output to console From 9c3b9366458c95ee8ee4a6424b256dc1995ba8ee Mon Sep 17 00:00:00 2001 From: Garett PHG Date: Tue, 15 Aug 2023 00:33:06 +0700 Subject: [PATCH 4/5] Renamed log.debugthread to log.debuglayer. --- log.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/log.lua b/log.lua index 16c5519..f297c67 100644 --- a/log.lua +++ b/log.lua @@ -13,7 +13,7 @@ log.usecolor = true log.outfile = nil log.level = "trace" log.entries = {} -log.debugthread = 2 +log.debuglayer = 2 local os, debug, math, table, string, lfs = os, debug, math, table, string, love and love.filesystem local ipairs, select, type, print, fileopen = ipairs, select, type, print, lfs and lfs.newFile or io.open @@ -67,7 +67,7 @@ for i, x in ipairs(modes) do end local msg = tostring(...) - local info = debug.getinfo(log.debugthread, "Sl") + local info = debug.getinfo(log.debuglayer, "Sl") local lineinfo = info.short_src .. ":" .. info.currentline -- Output to console From 8782e375f64de08d893238263d662b897c0802da Mon Sep 17 00:00:00 2001 From: Garett PHG Date: Wed, 23 Aug 2023 10:10:38 +0700 Subject: [PATCH 5/5] Log returns multiple values. * It stores string to its entries, even if its `outfile` is not truthy. * Flush emptied out the entries even if `outfile` is not truthy. --- log.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/log.lua b/log.lua index f297c67..30085e4 100644 --- a/log.lua +++ b/log.lua @@ -83,27 +83,30 @@ for i, x in ipairs(modes) do nameupper, os.date(), lineinfo, msg) -- Store to log table - if log.outfile then - table.insert(log.entries, str) - end + table.insert(log.entries, str) - return str + -- Return order: formatted_string, message, level, pathname, lineno, asctime, created_on + return str, msg, nameupper, info.short_src, info.currentline, os.date("!%Y-%m-%d %H:%M:%S"), os.time() end end + log.flush = function(outfile) local e, o = log.entries, outfile or log.outfile + local length, fp = #e, o and fileopen(o, 'a') -- Output to log file - if o then - local fp = fileopen(o, 'a') - - for i = 1, #e do + if fp then + for i = 1, length do fp:write(e[i]) e[i] = nil end fp:close() + else + for i = 1, length do + e[i] = nil + end end end