Skip to content

Commit

Permalink
Core: Implement auto delete logs (#263)
Browse files Browse the repository at this point in the history
* `Core`: add logs auto delete by creation date

Fixing: #193

* Removed unixtime inc

* Removed useless cvar

Co-authored-by: Sergey Shorokhov <[email protected]>
  • Loading branch information
ShadowsAdi and wopox1337 committed Mar 11, 2022
1 parent b6bda49 commit b1ff8be
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ ca_log_level "1"
// Minimum: "0.000000"
// Maximum: "1.000000"
ca_update_notify "1"

// The time in days after which the log files should be deleted.
// 0 - The logs won't be deleted.
// > 0 - The logs will be deleted at the time inserted.
// -
// Default: "7"
// Minimum: "0.000000"
ca_log_autodelete_time "7"
74 changes: 72 additions & 2 deletions cstrike/addons/amxmodx/scripting/ChatAdditions_Core.sma
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ enum logType_s {
new logType_s: ca_log_type,
logLevel_s: ca_log_level = logLevel_Debug,
g_logsPath[PLATFORM_MAX_PATH],
bool: ca_update_notify
bool: ca_update_notify,
ca_log_autodelete_time

new const LOG_FOLDER[] = "ChatAdditions"

Expand Down Expand Up @@ -76,6 +77,8 @@ public plugin_init() {
g_fwdClientVoice = CreateMultiForward("CA_Client_Voice", ET_STOP, FP_CELL, FP_CELL)
g_fwdClientChangeName = CreateMultiForward("CA_Client_ChangeName", ET_STOP, FP_CELL, FP_STRING)

CheckAutoDelete()

CA_Log(logLevel_Debug, "Chat Additions: Core initialized!")

set_task_ex(6.274, "_OnConfigsExecuted")
Expand All @@ -91,12 +94,70 @@ public _OnConfigsExecuted() {
CheckUpdate()
}

CheckAutoDelete() {
if(ca_log_autodelete_time > 0) {
if(dir_exists(g_logsPath)) {
new logFile[PLATFORM_MAX_PATH]
new dirHandle
new subDirectory[PLATFORM_MAX_PATH]
new deleteTime

deleteTime = get_systime() - (ca_log_autodelete_time * 60 * 60 * 24)

dirHandle = open_dir(g_logsPath, logFile, charsmax(logFile))

if(dirHandle) {
while(next_file(dirHandle, logFile, charsmax(logFile))) {
if(logFile[0] == '.')
continue

if(containi(logFile, ".log") == -1) {
formatex(subDirectory, charsmax(subDirectory), "%s/%s", g_logsPath, logFile)

ReadFolder(deleteTime, subDirectory)

continue
}
}
close_dir(dirHandle)
}
}
}
}

ReadFolder(deleteTime, logPath[]) {
new logFile[PLATFORM_MAX_PATH]
new dirHandle = open_dir(logPath, logFile, charsmax(logFile))
new fileTime

if(dirHandle) {
do
{
if(logFile[0] == '.') {
continue
}

if(containi(logFile, ".log") != -1) {
fileTime = 0
format(logFile, charsmax(logFile), "%s/%s", logPath, logFile)

fileTime = GetFileTime(logFile, FileTime_Created)
if(fileTime < deleteTime) {
unlink(logFile)
}
}
} while(next_file(dirHandle, logFile, charsmax(logFile)))
}
close_dir(dirHandle)
}

Create_CVars() {

bind_pcvar_num(create_cvar("ca_log_type", "1",
.description = fmt("Log file type\n \
0 = log to common amxx log file (logs/L*.log)\n \
1 = log to plugins folder (logs/%s/[plugin name]/L*.log)\n \
2 = silent log to plugins folder (logs/%s/[plugin name]/L*.log)", LOG_FOLDER),
2 = silent log to plugins folder (logs/%s/[plugin name]/L*.log)", LOG_FOLDER, LOG_FOLDER),
.has_min = true, .min_val = 0.0,
.has_max = true, .max_val = float(_LogToDirSilent)
),
Expand All @@ -118,6 +179,15 @@ Create_CVars() {
),
ca_update_notify
)

bind_pcvar_num(create_cvar("ca_log_autodelete_time", "7",
.description = "The time in days after which the log files should be deleted.\n \
0 - The logs won't be deleted.\n \
> 0 - The logs will be deleted at the time inserted.",
.has_min = true, .min_val = 0.0
),
ca_log_autodelete_time
)
}

public plugin_natives() {
Expand Down

0 comments on commit b1ff8be

Please sign in to comment.