Skip to content

Commit 3a7dc2b

Browse files
Core: Implemented log_to_file_ex & fixes (#262)
* Implemented log_to_file_ex() Fixing #249 * A little optimization * Mod name and amxx version stings in static vars * Fixed logs path * Update cstrike/addons/amxmodx/scripting/ChatAdditions_Core.sma * Code design update and added a new type of log * Fix #207 * Update ChatAdditions_Core.sma * correct CVar desc * Codestyle changes Co-authored-by: Sergey Shorokhov <[email protected]>
1 parent c413024 commit 3a7dc2b

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

cstrike/addons/amxmodx/configs/plugins/ChatAdditions/ChatAdditions_core.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
// Log file type
66
// 0 = log to common amxx log file (logs/L*.log)
77
// 1 = log to plugins folder (logs/ChatAdditions/[plugin name]/*.log)
8+
// 2 = silent log to plugins folder (logs/%s/[plugin name]/L*.log)
89
// -
910
// Default: "1"
1011
// Minimum: "0.000000"
11-
// Maximum: "1.000000"
12+
// Maximum: "2.000000"
1213
ca_log_type "1"
1314

1415
// Log level

cstrike/addons/amxmodx/scripting/ChatAdditions_Core.sma

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
enum logType_s {
1414
_Default,
15-
_LogToDir
15+
_LogToDir,
16+
_LogToDirSilent
1617
}
1718

1819
new logType_s: ca_log_type,
@@ -92,9 +93,12 @@ public _OnConfigsExecuted() {
9293

9394
Register_CVars() {
9495
bind_pcvar_num(create_cvar("ca_log_type", "1",
95-
.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)", LOG_FOLDER),
96+
.description = fmt("Log file type\n \
97+
0 = log to common amxx log file (logs/L*.log)\n \
98+
1 = log to plugins folder (logs/%s/[plugin name]/L*.log)\n \
99+
2 = silent log to plugins folder (logs/%s/[plugin name]/L*.log)", LOG_FOLDER),
96100
.has_min = true, .min_val = 0.0,
97-
.has_max = true, .max_val = float(_LogToDir)
101+
.has_max = true, .max_val = float(_LogToDirSilent)
98102
),
99103
ca_log_type
100104
)
@@ -209,30 +213,35 @@ public bool: native_CA_Log(const plugin_id, const argc) {
209213
return false
210214
}
211215

212-
new msg[2048];
216+
new msg[2048]
213217
vdformat(msg, charsmax(msg), arg_msg, arg_format)
214218

215-
new pluginName[32]
216-
get_plugin(plugin_id, pluginName, charsmax(pluginName))
219+
new logsFile[PLATFORM_MAX_PATH]
217220

218-
replace(pluginName, charsmax(pluginName), ".amxx", "")
221+
if(ca_log_type > _Default)
222+
{
223+
new pluginName[32]
224+
get_plugin(plugin_id, pluginName, charsmax(pluginName))
219225

220-
new logsPath[PLATFORM_MAX_PATH]
221-
formatex(logsPath, charsmax(logsPath), "%s/%s", g_logsPath, pluginName)
226+
replace(pluginName, charsmax(pluginName), ".amxx", "")
222227

223-
if(!dir_exists(logsPath)) {
224-
mkdir(logsPath)
225-
}
228+
new logsPath[PLATFORM_MAX_PATH]
229+
formatex(logsPath, charsmax(logsPath), "%s/%s", g_logsPath, pluginName)
230+
231+
if(!dir_exists(logsPath)) {
232+
mkdir(logsPath)
233+
}
226234

227-
new year, month, day
228-
date(year, month, day)
235+
new year, month, day
236+
date(year, month, day)
229237

230-
new logsFile[PLATFORM_MAX_PATH];
231-
formatex(logsFile, charsmax(logsFile), "%s/%s__%i-%02i-%02i.log", logsPath, pluginName, year, month, day)
238+
formatex(logsFile, charsmax(logsFile), "%s/%s__%i-%02i-%02i.log", logsPath, pluginName, year, month, day)
239+
}
232240

233241
switch(ca_log_type) {
234-
case _LogToDir: log_to_file(logsFile, msg)
235-
case _Default: log_amx(msg)
242+
case _LogToDir: log_to_file(logsFile, msg)
243+
case _Default: log_amx(msg)
244+
case _LogToDirSilent: log_to_file_ex(logsFile, msg)
236245
}
237246

238247
return true
@@ -251,7 +260,6 @@ public bool: native_CA_PlayerHasBlockedPlayer(const plugin_id, const argc) {
251260
return false
252261
}
253262

254-
255263
static GetLogsFilePath(buffer[], len = PLATFORM_MAX_PATH, const dir[] = "ChatAdditions") {
256264
get_localinfo("amxx_logs", buffer, len)
257265
strcat(buffer, fmt("/%s", dir), len)
@@ -366,3 +374,40 @@ static stock CmpVersions(const a[], const b[]) {
366374

367375
return countA - countB
368376
}
377+
378+
stock log_to_file_ex(const filePath[], message[]) {
379+
new file
380+
new bool:firstTime = true
381+
new date[32]
382+
383+
format_time(date, charsmax(date), "%m/%d/%Y - %H:%M:%S")
384+
static modName[15], amxVersion[15]
385+
386+
if(!modName[0]) {
387+
get_modname(modName, charsmax(modName))
388+
}
389+
390+
if(!amxVersion[0]) {
391+
get_amxx_verstring(amxVersion, charsmax(amxVersion))
392+
}
393+
394+
if((file = fopen(filePath, "r"))) {
395+
firstTime = false
396+
fclose(file)
397+
}
398+
399+
if(!(file = fopen(filePath, "at"))) {
400+
log_error(AMX_ERR_GENERAL, "Can't open \"%s\" file for writing.", filePath)
401+
return PLUGIN_CONTINUE
402+
}
403+
404+
if(firstTime) {
405+
fprintf(file, "L %s: Log file started (file \"%s\") (game \"%s\") (amx \"%s\")\n", date, filePath, modName, amxVersion)
406+
}
407+
408+
fprintf(file, "L %s: %s\n", date, message)
409+
410+
fclose(file)
411+
412+
return PLUGIN_HANDLED
413+
}

0 commit comments

Comments
 (0)