59 lines
1.5 KiB
Lua
59 lines
1.5 KiB
Lua
--------------------------------------------------------------------
|
|
-- lib/journal.lua : journal de vol (console + fichier rotatif)
|
|
--------------------------------------------------------------------
|
|
local Journal = {}
|
|
|
|
local FICHIER, MAX_TAILLE = "drone.log", 64 * 1024
|
|
local recents, MAX_RECENTS = {}, 60
|
|
|
|
local function memoriser(ligne)
|
|
table.insert(recents, ligne)
|
|
if #recents > MAX_RECENTS then table.remove(recents, 1) end
|
|
end
|
|
|
|
-- dernieres lignes (les plus recentes en dernier)
|
|
function Journal.recents(n)
|
|
local depart = math.max(1, #recents - (n or MAX_RECENTS) + 1)
|
|
local lignes = {}
|
|
for i = depart, #recents do table.insert(lignes, recents[i]) end
|
|
return lignes
|
|
end
|
|
|
|
local function horodater(texte)
|
|
return ("[%s] %s"):format(textutils.formatTime(os.time("local"), true), texte)
|
|
end
|
|
|
|
local function ecrireFichier(ligne)
|
|
if fs.exists(FICHIER) and fs.getSize(FICHIER) > MAX_TAILLE then
|
|
fs.delete(FICHIER .. ".old")
|
|
fs.move(FICHIER, FICHIER .. ".old")
|
|
end
|
|
local f = fs.open(FICHIER, "a")
|
|
if f then
|
|
f.writeLine(ligne)
|
|
f.close()
|
|
end
|
|
end
|
|
|
|
function Journal.info(texte)
|
|
local ligne = horodater(texte)
|
|
print(ligne)
|
|
ecrireFichier(ligne)
|
|
memoriser(ligne)
|
|
end
|
|
|
|
function Journal.erreur(texte)
|
|
local ligne = horodater("ERREUR " .. texte)
|
|
printError(ligne)
|
|
ecrireFichier(ligne)
|
|
memoriser(ligne)
|
|
end
|
|
|
|
function Journal.alerte(texte)
|
|
local ligne = horodater("ALERTE " .. texte)
|
|
printError(ligne)
|
|
ecrireFichier(ligne)
|
|
memoriser(ligne)
|
|
end
|
|
|
|
return Journal
|