Log Time Format Config

This commit is contained in:
caxanga334 2025-10-31 12:59:39 -03:00
parent 0ccf21ecb6
commit cfff550f1d
No known key found for this signature in database
GPG Key ID: A340D41A4ADCE85F
3 changed files with 52 additions and 20 deletions

View File

@ -21,6 +21,15 @@
*/ */
"LogMode" "daily" "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. * Language that multilingual enabled plugins and extensions will use to print messages.
* Only languages listed in languages.cfg are valid. * Only languages listed in languages.cfg are valid.

View File

@ -29,6 +29,7 @@
* Version: $Id$ * Version: $Id$
*/ */
#include <string_view>
#include <time.h> #include <time.h>
#include <cstdarg> #include <cstdarg>
#include "Logger.h" #include "Logger.h"
@ -83,6 +84,20 @@ ConfigResult Logger::OnSourceModConfigChanged(const char *key,
return ConfigResult_Accept; 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; return ConfigResult_Ignore;
} }
@ -152,11 +167,7 @@ void Logger::LogToOpenFileEx(FILE *fp, const char *msg, va_list ap)
char buffer[3072]; char buffer[3072];
ke::SafeVsprintf(buffer, sizeof(buffer), msg, ap); ke::SafeVsprintf(buffer, sizeof(buffer), msg, ap);
char date[32]; const char* date = GetFormattedDate();
time_t t = g_pSM->GetAdjustedTime();
tm *curtime = localtime(&t);
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
fprintf(fp, "L %s: %s\n", date, buffer); fprintf(fp, "L %s: %s\n", date, buffer);
if (!sv_logecho || bridge->GetCvarBool(sv_logecho)) 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]; char buffer[3072];
ke::SafeVsprintf(buffer, sizeof(buffer), msg, ap); ke::SafeVsprintf(buffer, sizeof(buffer), msg, ap);
char date[32]; const char* date = GetFormattedDate();
time_t t = g_pSM->GetAdjustedTime();
tm *curtime = localtime(&t);
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
fprintf(fp, "L %s: %s\n", date, buffer); fprintf(fp, "L %s: %s\n", date, buffer);
fflush(fp); fflush(fp);
@ -378,11 +386,7 @@ FILE *Logger::_OpenNormal()
if (!m_DamagedNormalFile) if (!m_DamagedNormalFile)
{ {
time_t t = g_pSM->GetAdjustedTime(); const char* date = GetFormattedDate();
tm *curtime = localtime(&t);
char date[32];
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
fprintf(pFile, "L %s: SourceMod log file session started (file \"%s\") (Version \"%s\")\n", date, m_NormalFileName.c_str(), SOURCEMOD_VERSION); fprintf(pFile, "L %s: SourceMod log file session started (file \"%s\") (Version \"%s\")\n", date, m_NormalFileName.c_str(), SOURCEMOD_VERSION);
m_DamagedNormalFile = true; m_DamagedNormalFile = true;
} }
@ -403,11 +407,7 @@ FILE *Logger::_OpenError()
if (!m_DamagedErrorFile) if (!m_DamagedErrorFile)
{ {
time_t t = g_pSM->GetAdjustedTime(); const char* date = GetFormattedDate();
tm *curtime = localtime(&t);
char date[32];
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
fprintf(pFile, "L %s: SourceMod error session started\n", date); 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()); fprintf(pFile, "L %s: Info (map \"%s\") (file \"%s\")\n", date, m_CurrentMapName.c_str(), m_ErrorFileName.c_str());
m_DamagedErrorFile = true; m_DamagedErrorFile = true;
@ -452,3 +452,23 @@ void Logger::_CloseError()
void Logger::_CloseFatal() 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;
}

View File

@ -53,7 +53,7 @@ enum LoggingMode
class Logger : public SMGlobalClass, public ILogger class Logger : public SMGlobalClass, public ILogger
{ {
public: 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 public: //SMGlobalClass
@ -95,10 +95,12 @@ private:
void _LogFatalOpen(std::string &str); void _LogFatalOpen(std::string &str);
void _PrintToGameLog(const char *fmt, va_list ap); void _PrintToGameLog(const char *fmt, va_list ap);
void _UpdateFiles(bool bLevelChange = false); void _UpdateFiles(bool bLevelChange = false);
const char* GetFormattedDate() const;
private: private:
std::string m_NormalFileName; std::string m_NormalFileName;
std::string m_ErrorFileName; std::string m_ErrorFileName;
std::string m_CurrentMapName; std::string m_CurrentMapName;
std::string m_UserTimeFormat;
int m_Day; int m_Day;
@ -106,6 +108,7 @@ private:
bool m_Active; bool m_Active;
bool m_DamagedNormalFile; bool m_DamagedNormalFile;
bool m_DamagedErrorFile; bool m_DamagedErrorFile;
bool m_isUsingDefaultTimeFormat;
}; };
extern Logger g_Logger; extern Logger g_Logger;