Skip to content

Commit 14eed93

Browse files
committed
Added functionality to delete old logs if timestamps are older than confiugred to be deleted.
1 parent 701eca1 commit 14eed93

File tree

3 files changed

+148
-16
lines changed

3 files changed

+148
-16
lines changed

squid/squid_config.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
---@field days_to_delete_logs integer
1212
---@field min_log_level integer 1|2|3|4|5
1313
---@field unsaved_logs_buffer integer
14-
---@field max_log_length integer
14+
---@field max_data_length integer
1515
---@field max_data_depth integer
1616
---@field is_printing_crashes boolean
1717
---@field is_saving_crashes boolean
@@ -32,7 +32,7 @@ local SquidConfig = {
3232
days_to_delete_logs = sys.get_config_int("squid.days_to_delete_logs", 7),
3333
min_log_level = sys.get_config_int("squid.min_log_level", 2),
3434
unsaved_logs_buffer = sys.get_config_int("squid.unsaved_logs_buffer", 30),
35-
max_log_length = sys.get_config_int("squid.max_log_length", 500),
35+
max_data_length = sys.get_config_int("squid.max_data_length", 500),
3636
max_data_depth = sys.get_config_int("squid.max_data_depth", 5),
3737
is_printing_crashes = sys.get_config_int("squid.is_printing_crashes", 1) == 1 and true or false,
3838
is_saving_crashes = sys.get_config_int("squid.is_saving_crashes", 1) == 1 and true or false,

squid/squid_impl.lua

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,35 @@ function SquidImpl.init()
6464
sys.set_error_handler(SquidImpl.error_handler)
6565
crash.set_file_path(FILEPATH)
6666
end
67+
68+
-- Delete old logs:
69+
local files, error_message = SystemHelper.get_all_files_in_catalog(SquidConfig.app_catalog)
70+
if error_message ~= "OK" or not files then
71+
SquidImpl.log(error_message, WARN, nil, "squid")
72+
return
73+
end
74+
local base_file_path = sys.get_save_file(SquidConfig.app_catalog, "")
75+
local log_file_directory = base_file_path:match("^(.*)[/\\]")
76+
SystemHelper.remove_old_files_by_filename(files, log_file_directory, SquidConfig.days_to_delete_logs)
77+
78+
-- Initialize new log file:
6779
local engine_info = sys.get_engine_info()
6880
local sys_info = sys.get_sys_info()
6981
local init_log = "Squid Initialized."
70-
.. "\n Engine version: "..engine_info.version
71-
.. "\n Engine SHA1: "..engine_info.version_sha1
72-
.. "\n Engine is debug?: "..(engine_info.is_debug and "true" or "false")
73-
.. "\n Device model: "..sys_info.device_model
74-
.. "\n Device manufacturer: "..sys_info.manufacturer
75-
.. "\n Device language: "..sys_info.device_language
76-
.. "\n Device identity: "..sys_info.device_ident
77-
.. "\n System name: "..sys_info.system_name
78-
.. "\n System version: "..sys_info.system_version
79-
.. "\n System API version: "..sys_info.api_version
80-
.. "\n System language: "..sys_info.language
81-
.. "\n Territory: "..sys_info.territory
82-
.. "\n GMT offset: "..sys_info.gmt_offset
83-
.. "\n HTTP User Agent: "..sys_info.user_agent
82+
.. "\n Engine version: "..engine_info.version
83+
.. "\n Engine SHA1: "..engine_info.version_sha1
84+
.. "\n Engine is debug?: "..(engine_info.is_debug and "true" or "false")
85+
.. "\n Device model: "..sys_info.device_model
86+
.. "\n Device manufacturer: "..sys_info.manufacturer
87+
.. "\n Device language: "..sys_info.device_language
88+
.. "\n Device identity: "..sys_info.device_ident
89+
.. "\n System name: "..sys_info.system_name
90+
.. "\n System version: "..sys_info.system_version
91+
.. "\n System API version: "..sys_info.api_version
92+
.. "\n System language: "..sys_info.language
93+
.. "\n Territory: "..sys_info.territory
94+
.. "\n GMT offset: "..sys_info.gmt_offset
95+
.. "\n HTTP User Agent: "..sys_info.user_agent
8496
IS_PRINTING = false
8597
SquidImpl.log(init_log, INFO, nil, "squid")
8698
IS_PRINTING = SquidConfig.is_printing

squid/system_helper.lua

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local SystemHelper = {}
77
local system_name = sys.get_sys_info().system_name
88

99
SystemHelper.is_linux = system_name == "Linux"
10+
SystemHelper.is_windows = system_name == "Windows"
11+
SystemHelper.is_linux_or_mac = SystemHelper.is_linux or system_name == "Darwin"
1012
SystemHelper.is_mobile = ( (system_name == "iPhone OS") or (system_name == "Android") )
1113
SystemHelper.is_debug = sys.get_engine_info().is_debug
1214

@@ -17,6 +19,22 @@ function SystemHelper.get_timestamp(format)
1719
return os.date(format or '%H:%M:%S', os.time()) --[[@as string]]
1820
end
1921

22+
---Get correct system dependent filepath for given catalog
23+
---@param app_catalog string Catalog/folder name
24+
---@return string
25+
function SystemHelper.directory(app_catalog)
26+
if SystemHelper.is_linux then
27+
-- For Linux we modify the default path to make Linux users happy
28+
local config_dir = "config/" .. app_catalog
29+
return sys.get_save_file(config_dir, "")
30+
elseif html5 then
31+
-- For HTML5 there's no need to get the full path
32+
return app_catalog
33+
end
34+
35+
return sys.get_save_file(app_catalog, "")
36+
end
37+
2038
---Get correct system dependent filepath for given file
2139
---@param app_catalog string Catalog/folder name
2240
---@param file_name string File name
@@ -44,4 +62,106 @@ function SystemHelper.filepath(app_catalog, file_name, extension, add_timestamp)
4462
return sys.get_save_file(app_catalog, file_name)
4563
end
4664

65+
---Get a list of all saved files in the specified catalog
66+
---@param app_catalog string Catalog/folder name
67+
---@return table List of saved file paths
68+
---@return string Error message if somethings goes wrong, OK string otherwise
69+
function SystemHelper.get_all_files_in_catalog(app_catalog)
70+
-- Get the base path for the app catalog
71+
local base_file_path
72+
if SystemHelper.is_linux then
73+
local config_dir = "config/" .. app_catalog
74+
base_file_path = sys.get_save_file(config_dir, "")
75+
elseif html5 then
76+
return {}, "HTML5 does not support directory operations."
77+
else
78+
base_file_path = sys.get_save_file(app_catalog, "")
79+
end
80+
81+
-- Extract directory from the file path
82+
local directory = base_file_path:match("^(.*)[/\\]") -- Handles both / and \ separators
83+
if not directory then
84+
return {}, "Unable to determine directory path from ".. base_file_path
85+
end
86+
87+
-- Platform-specific file listing command
88+
local list_files_cmd
89+
if SystemHelper.is_linux_or_mac then
90+
-- Linux/MacOS specific: using `ls` command
91+
list_files_cmd = "ls -1 \"" .. directory .. "\""
92+
elseif SystemHelper.is_windows then
93+
-- Windows specific: using `dir` command
94+
list_files_cmd = "dir /b \"" .. directory .. "\""
95+
else
96+
return {}, "Unsupported platform for directory operations."
97+
end
98+
99+
-- Capture the output of the command
100+
local files = {}
101+
local handle = io.popen(list_files_cmd)
102+
if handle then
103+
for file in handle:lines() do
104+
if file ~= "." and file ~= ".." then
105+
table.insert(files, file)
106+
end
107+
end
108+
handle:close()
109+
else
110+
return {}, "Error: Unable to execute directory listing command."
111+
end
112+
113+
if #files == 0 then
114+
return files, "No files found in directory."
115+
end
116+
117+
return files, "OK"
118+
end
119+
120+
121+
---Extract timestamp from a filename with the format "squid_log_file_YYYY-MM-DD_HH_MM.txt"
122+
---@param file_name string Filename to extract the timestamp from
123+
---@return number|nil The Unix timestamp of the file, or nil if parsing fails
124+
local function extract_timestamp_from_filename(file_name)
125+
local year, month, day, hour, min = file_name:match("_(%d%d%d%d)%-(%d%d)%-(%d%d)_(%d%d)_(%d%d)%.")
126+
if year and month and day and hour and min then
127+
return os.time({ year = tonumber(year), month = tonumber(month), day = tonumber(day),
128+
hour = tonumber(hour), min = tonumber(min), sec = 0 })
129+
end
130+
return nil
131+
end
132+
133+
---Remove files older than a specified number of days based on their filename timestamp
134+
---@param files_table table Table containing file names
135+
---@param directory string Directory where the files are located
136+
---@param days_threshold number Number of days before current date to consider files as old
137+
function SystemHelper.remove_old_files_by_filename(files_table, directory, days_threshold)
138+
-- Get current time
139+
local current_time = os.time()
140+
141+
for _, file_name in ipairs(files_table) do
142+
local file_path = directory .. "/" .. file_name -- Adjust path separator if needed
143+
144+
-- Extract the timestamp from the filename
145+
local file_timestamp = extract_timestamp_from_filename(file_name)
146+
if file_timestamp then
147+
-- Calculate file age in days
148+
local file_age_days = os.difftime(current_time, file_timestamp) / (60 * 60 * 24)
149+
if file_age_days > days_threshold then
150+
-- Remove the file if it's older than the threshold
151+
--local success, err =
152+
os.remove(file_path)
153+
--[[if success then
154+
print("Removed old file:", file_path)
155+
else
156+
print("Error removing file:", file_path, err)
157+
end]]
158+
--else
159+
--print("File is not old enough to remove:", file_path)
160+
end
161+
--else
162+
--print("Skipping file due to invalid timestamp:", file_name)
163+
end
164+
end
165+
end
166+
47167
return SystemHelper

0 commit comments

Comments
 (0)