From 1fc841713eb9990055523b3775db467a125d45a3 Mon Sep 17 00:00:00 2001 From: caxanga334 <10157643+caxanga334@users.noreply.github.com> Date: Sat, 8 Nov 2025 14:08:32 -0300 Subject: [PATCH] Allow the time format used by SM log files to be configured by server ops. (#2372) * Log Time Format Config * Remove extra space * Fix formatting inconsistency --------- Co-authored-by: Nicholas Hastings --- configs/core.cfg | 9 +++++++ core/logic/Logger.cpp | 59 +++++++++++++++++++++++++++++-------------- core/logic/Logger.h | 5 +++- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/configs/core.cfg b/configs/core.cfg index 652ed537..a73f3a4c 100644 --- a/configs/core.cfg +++ b/configs/core.cfg @@ -21,6 +21,15 @@ */ "LogMode" "daily" + /** + * This option determines the time format SourceMod logging should use. + * + * "default" - Uses SourceMod's default time format. (%m/%d/%Y - %H:%M:%S) + * You can specify any time format you want. See https://cplusplus.com/reference/ctime/strftime/ for a list of format parameters. + * Example: "%d/%m/%Y - %H:%M:%S" + */ + "LogTimeFormat" "default" + /** * Language that multilingual enabled plugins and extensions will use to print messages. * Only languages listed in languages.cfg are valid. diff --git a/core/logic/Logger.cpp b/core/logic/Logger.cpp index f582b07b..dd6c94ee 100644 --- a/core/logic/Logger.cpp +++ b/core/logic/Logger.cpp @@ -29,6 +29,7 @@ * Version: $Id$ */ +#include #include #include #include "Logger.h" @@ -81,6 +82,19 @@ ConfigResult Logger::OnSourceModConfigChanged(const char *key, return ConfigResult_Reject; } + return ConfigResult_Accept; + } else if (strcasecmp(key, "LogTimeFormat") == 0) { + if (strcasecmp(value, "default") == 0) + { + m_isUsingDefaultTimeFormat = true; + m_UserTimeFormat.clear(); + } + else { + // value is the time format string + m_isUsingDefaultTimeFormat = false; + m_UserTimeFormat.assign(value); + } + return ConfigResult_Accept; } @@ -152,11 +166,7 @@ void Logger::LogToOpenFileEx(FILE *fp, const char *msg, va_list ap) char buffer[3072]; ke::SafeVsprintf(buffer, sizeof(buffer), msg, ap); - char date[32]; - time_t t = g_pSM->GetAdjustedTime(); - tm *curtime = localtime(&t); - strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime); - + const char* date = GetFormattedDate(); fprintf(fp, "L %s: %s\n", date, buffer); if (!sv_logecho || bridge->GetCvarBool(sv_logecho)) @@ -174,10 +184,7 @@ void Logger::LogToFileOnlyEx(FILE *fp, const char *msg, va_list ap) char buffer[3072]; ke::SafeVsprintf(buffer, sizeof(buffer), msg, ap); - char date[32]; - time_t t = g_pSM->GetAdjustedTime(); - tm *curtime = localtime(&t); - strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime); + const char* date = GetFormattedDate(); fprintf(fp, "L %s: %s\n", date, buffer); fflush(fp); @@ -378,11 +385,7 @@ FILE *Logger::_OpenNormal() if (!m_DamagedNormalFile) { - time_t t = g_pSM->GetAdjustedTime(); - tm *curtime = localtime(&t); - char date[32]; - - strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime); + const char* date = GetFormattedDate(); fprintf(pFile, "L %s: SourceMod log file session started (file \"%s\") (Version \"%s\")\n", date, m_NormalFileName.c_str(), SOURCEMOD_VERSION); m_DamagedNormalFile = true; } @@ -403,11 +406,7 @@ FILE *Logger::_OpenError() if (!m_DamagedErrorFile) { - time_t t = g_pSM->GetAdjustedTime(); - tm *curtime = localtime(&t); - - char date[32]; - strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime); + const char* date = GetFormattedDate(); fprintf(pFile, "L %s: SourceMod error session started\n", date); fprintf(pFile, "L %s: Info (map \"%s\") (file \"%s\")\n", date, m_CurrentMapName.c_str(), m_ErrorFileName.c_str()); m_DamagedErrorFile = true; @@ -452,3 +451,25 @@ void Logger::_CloseError() void Logger::_CloseFatal() { } + +const char* Logger::GetFormattedDate() const +{ + static char date[256]; + constexpr std::string_view DEFAULT_TIME_FORMAT{ "%m/%d/%Y - %H:%M:%S" }; + + time_t t = g_pSM->GetAdjustedTime(); + tm *curtime = localtime(&t); + + if (m_isUsingDefaultTimeFormat) + { + strftime(date, sizeof(date), DEFAULT_TIME_FORMAT.data(), curtime); + } + else + { + strftime(date, sizeof(date), m_UserTimeFormat.c_str(), curtime); + } + + return date; + +} + diff --git a/core/logic/Logger.h b/core/logic/Logger.h index 641474ef..3c6959fd 100644 --- a/core/logic/Logger.h +++ b/core/logic/Logger.h @@ -53,7 +53,7 @@ enum LoggingMode class Logger : public SMGlobalClass, public ILogger { public: - Logger() : m_Day(-1), m_Mode(LoggingMode_Daily), m_Active(true), m_DamagedNormalFile(false), m_DamagedErrorFile(false) + Logger() : m_Day(-1), m_Mode(LoggingMode_Daily), m_Active(true), m_DamagedNormalFile(false), m_DamagedErrorFile(false), m_isUsingDefaultTimeFormat(true) { } public: //SMGlobalClass @@ -95,10 +95,12 @@ private: void _LogFatalOpen(std::string &str); void _PrintToGameLog(const char *fmt, va_list ap); void _UpdateFiles(bool bLevelChange = false); + const char* GetFormattedDate() const; private: std::string m_NormalFileName; std::string m_ErrorFileName; std::string m_CurrentMapName; + std::string m_UserTimeFormat; int m_Day; @@ -106,6 +108,7 @@ private: bool m_Active; bool m_DamagedNormalFile; bool m_DamagedErrorFile; + bool m_isUsingDefaultTimeFormat; }; extern Logger g_Logger;