From 3cfbf812046625c33537af1317c276122d285b9c Mon Sep 17 00:00:00 2001 From: Gordon Hecker Date: Wed, 26 Aug 2009 13:41:33 +0200 Subject: [PATCH] implement logfile rotation, maintain sets of rotated logfiles for debug log and build logs Signed-off-by: Gordon Hecker --- generic/e2lib.lua | 80 ++++++++++++++++++++++++++++++++++++++++++----- global/e2.conf.in | 3 ++ local/e2tool.lua | 4 ++- 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index ebe4036..8737b38 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -87,6 +87,7 @@ e2lib = { template_path = string.format("%s/templates", buildconfig.SYSCONFDIR), extension_config = ".e2/extensions", lock = nil, + logrotate = 5, -- configurable via config.log.logrotate } -- Interrupt handling @@ -398,6 +399,60 @@ function e2lib.log(level, msg) return nil end +function e2lib.rotate_log(file) + local e = new_error("rotating logfile: %s", file) + local rc, re + local logdir = e2lib.dirname(file) + local logfile = e2lib.basename(file) + local dir = e2util.directory(logdir, false) + if not dir then + return false, e:cat(string.format("%s: can't read directory", dir)) + end + local files = {} + for _,f in ipairs(dir) do + local match = f:match(string.format("%s.[0-9]+", logfile)) + if match then + table.insert(files, 1, match) + end + end + -- sort in reverse order + local function comp(a, b) + local na = a:match(string.format("%s.([0-9]+)", logfile)) + local nb = b:match(string.format("%s.([0-9]+)", logfile)) + return tonumber(na) > tonumber(nb) + end + table.sort(files, comp) + for _,f in ipairs(files) do + local n = f:match(string.format("%s.([0-9]+)", logfile)) + if n then + n = tonumber(n) + if n >= e2lib.logrotate - 1 then + local del = string.format("%s/%s.%d", logdir, logfile, n) + rc, re = e2lib.rm(del) + if not rc then + return false, e:cat(re) + end + else + local src = string.format("%s/%s.%d", logdir, logfile, n) + local dst = string.format("%s/%s.%d", logdir, logfile, n + 1) + rc, re = e2lib.mv(src, dst) + if not rc then + return false, e:cat(re) + end + end + end + end + local src = file + local dst = string.format("%s/%s.0", logdir, logfile) + if e2lib.isfile(src) then + rc, re = e2lib.mv(src, dst) + if not rc then + return false, e:cat(re) + end + end + return true, nil +end + local buildidlogfile = false function e2lib.buildidlog(info, text) @@ -846,6 +901,16 @@ end -- this function always succeeds or aborts -- @return nothing function e2lib.use_global_config() + + -- check if type(x) == t, and abort if not. + local function assert_type(x, d, t1) + local t2 = type(x) + if t1 ~= t2 then + e2lib.abort( + string.format("configuration error: %s (expected %s got %s)", d, t1, t2)) + end + end + local config = e2lib.global_config if not config then e2lib.abort("global config not available") @@ -855,19 +920,18 @@ function e2lib.use_global_config() e2lib.log(3, string.format( "e2lib.enable_invocation_log=%s", tostring(config.log.enable))) end + if config.log then + assert_type(config.log, "config.log", "table") + if config.log.logrotate then + assert_type(config.log.logrotate, "config.log.logrotate", "number") + e2lib.logrotate = config.log.logrotate + end + end if config.site and config.site.buildnumber_server_url ~= nil then e2lib.buildnumber_server_url = config.site.buildnumber_server_url e2lib.log(3, string.format("e2lib.buildnumber_server_url=%s", tostring(config.site.buildnumber_server_url))) end - -- check if type(x) == t, and abort if not. - local function assert_type(x, d, t1) - local t2 = type(x) - if t1 ~= t2 then - e2lib.abort( - string.format("configuration error: %s (expected %s got %s)", d, t1, t2)) - end - end assert_type(config.site, "config.site", "table") assert_type(config.site.e2_branch, "config.site.e2_branch", "string") assert_type(config.site.e2_tag, "config.site.e2_tag", "string") diff --git a/global/e2.conf.in b/global/e2.conf.in index 8dc0957..968338e 100644 --- a/global/e2.conf.in +++ b/global/e2.conf.in @@ -4,6 +4,9 @@ e2factory, the emlix embedded build system /etc/e2/e2.conf sample configuration file ]] config { + log = { + logrotate = 5, + }, site = { e2_server = "projects", e2_location = "e2factory.git", diff --git a/local/e2tool.lua b/local/e2tool.lua index 75a9b8f..53da7a0 100644 --- a/local/e2tool.lua +++ b/local/e2tool.lua @@ -165,7 +165,9 @@ function e2tool.opendebuglogfile(info) local e = new_error("error making log directory") return false, e:cat(re) end - local debuglogfile, re = luafile.open(info.root .. "/log/debug.log", "w") + local logfile = info.root .. "/log/debug.log" + local rc, re = e2lib.rotate_log(logfile) + local debuglogfile, re = luafile.open(logfile, "w") if not debuglogfile then local e = new_error("error opening debug logfile") return false, e:cat(re) -- 2.39.5