diff --git a/configs/core.cfg b/configs/core.cfg index 652ed5375..a73f3a4ce 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 f582b07bd..4d575f616 100644 --- a/core/logic/Logger.cpp +++ b/core/logic/Logger.cpp @@ -29,6 +29,7 @@ * Version: $Id$ */ + #include #include #include #include "Logger.h" @@ -83,6 +84,20 @@ ConfigResult Logger::OnSourceModConfigChanged(const char *key, 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; + } return ConfigResult_Ignore; } @@ -152,11 +167,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 +185,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 +386,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 +407,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 +452,23 @@ 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; +} \ No newline at end of file diff --git a/core/logic/Logger.h b/core/logic/Logger.h index 641474efb..3c6959fdd 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;