Skip to content

Commit 1fc8417

Browse files
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 <[email protected]>
1 parent 84f67fc commit 1fc8417

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

configs/core.cfg

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
*/
2222
"LogMode" "daily"
2323

24+
/**
25+
* This option determines the time format SourceMod logging should use.
26+
*
27+
* "default" - Uses SourceMod's default time format. (%m/%d/%Y - %H:%M:%S)
28+
* You can specify any time format you want. See https://cplusplus.com/reference/ctime/strftime/ for a list of format parameters.
29+
* Example: "%d/%m/%Y - %H:%M:%S"
30+
*/
31+
"LogTimeFormat" "default"
32+
2433
/**
2534
* Language that multilingual enabled plugins and extensions will use to print messages.
2635
* Only languages listed in languages.cfg are valid.

core/logic/Logger.cpp

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Version: $Id$
3030
*/
3131

32+
#include <string_view>
3233
#include <time.h>
3334
#include <cstdarg>
3435
#include "Logger.h"
@@ -81,6 +82,19 @@ ConfigResult Logger::OnSourceModConfigChanged(const char *key,
8182
return ConfigResult_Reject;
8283
}
8384

85+
return ConfigResult_Accept;
86+
} else if (strcasecmp(key, "LogTimeFormat") == 0) {
87+
if (strcasecmp(value, "default") == 0)
88+
{
89+
m_isUsingDefaultTimeFormat = true;
90+
m_UserTimeFormat.clear();
91+
}
92+
else {
93+
// value is the time format string
94+
m_isUsingDefaultTimeFormat = false;
95+
m_UserTimeFormat.assign(value);
96+
}
97+
8498
return ConfigResult_Accept;
8599
}
86100

@@ -152,11 +166,7 @@ void Logger::LogToOpenFileEx(FILE *fp, const char *msg, va_list ap)
152166
char buffer[3072];
153167
ke::SafeVsprintf(buffer, sizeof(buffer), msg, ap);
154168

155-
char date[32];
156-
time_t t = g_pSM->GetAdjustedTime();
157-
tm *curtime = localtime(&t);
158-
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
159-
169+
const char* date = GetFormattedDate();
160170
fprintf(fp, "L %s: %s\n", date, buffer);
161171

162172
if (!sv_logecho || bridge->GetCvarBool(sv_logecho))
@@ -174,10 +184,7 @@ void Logger::LogToFileOnlyEx(FILE *fp, const char *msg, va_list ap)
174184
char buffer[3072];
175185
ke::SafeVsprintf(buffer, sizeof(buffer), msg, ap);
176186

177-
char date[32];
178-
time_t t = g_pSM->GetAdjustedTime();
179-
tm *curtime = localtime(&t);
180-
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
187+
const char* date = GetFormattedDate();
181188
fprintf(fp, "L %s: %s\n", date, buffer);
182189

183190
fflush(fp);
@@ -378,11 +385,7 @@ FILE *Logger::_OpenNormal()
378385

379386
if (!m_DamagedNormalFile)
380387
{
381-
time_t t = g_pSM->GetAdjustedTime();
382-
tm *curtime = localtime(&t);
383-
char date[32];
384-
385-
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
388+
const char* date = GetFormattedDate();
386389
fprintf(pFile, "L %s: SourceMod log file session started (file \"%s\") (Version \"%s\")\n", date, m_NormalFileName.c_str(), SOURCEMOD_VERSION);
387390
m_DamagedNormalFile = true;
388391
}
@@ -403,11 +406,7 @@ FILE *Logger::_OpenError()
403406

404407
if (!m_DamagedErrorFile)
405408
{
406-
time_t t = g_pSM->GetAdjustedTime();
407-
tm *curtime = localtime(&t);
408-
409-
char date[32];
410-
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
409+
const char* date = GetFormattedDate();
411410
fprintf(pFile, "L %s: SourceMod error session started\n", date);
412411
fprintf(pFile, "L %s: Info (map \"%s\") (file \"%s\")\n", date, m_CurrentMapName.c_str(), m_ErrorFileName.c_str());
413412
m_DamagedErrorFile = true;
@@ -452,3 +451,25 @@ void Logger::_CloseError()
452451
void Logger::_CloseFatal()
453452
{
454453
}
454+
455+
const char* Logger::GetFormattedDate() const
456+
{
457+
static char date[256];
458+
constexpr std::string_view DEFAULT_TIME_FORMAT{ "%m/%d/%Y - %H:%M:%S" };
459+
460+
time_t t = g_pSM->GetAdjustedTime();
461+
tm *curtime = localtime(&t);
462+
463+
if (m_isUsingDefaultTimeFormat)
464+
{
465+
strftime(date, sizeof(date), DEFAULT_TIME_FORMAT.data(), curtime);
466+
}
467+
else
468+
{
469+
strftime(date, sizeof(date), m_UserTimeFormat.c_str(), curtime);
470+
}
471+
472+
return date;
473+
474+
}
475+

core/logic/Logger.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ enum LoggingMode
5353
class Logger : public SMGlobalClass, public ILogger
5454
{
5555
public:
56-
Logger() : m_Day(-1), m_Mode(LoggingMode_Daily), m_Active(true), m_DamagedNormalFile(false), m_DamagedErrorFile(false)
56+
Logger() : m_Day(-1), m_Mode(LoggingMode_Daily), m_Active(true), m_DamagedNormalFile(false), m_DamagedErrorFile(false), m_isUsingDefaultTimeFormat(true)
5757
{
5858
}
5959
public: //SMGlobalClass
@@ -95,17 +95,20 @@ class Logger : public SMGlobalClass, public ILogger
9595
void _LogFatalOpen(std::string &str);
9696
void _PrintToGameLog(const char *fmt, va_list ap);
9797
void _UpdateFiles(bool bLevelChange = false);
98+
const char* GetFormattedDate() const;
9899
private:
99100
std::string m_NormalFileName;
100101
std::string m_ErrorFileName;
101102
std::string m_CurrentMapName;
103+
std::string m_UserTimeFormat;
102104

103105
int m_Day;
104106

105107
LoggingMode m_Mode;
106108
bool m_Active;
107109
bool m_DamagedNormalFile;
108110
bool m_DamagedErrorFile;
111+
bool m_isUsingDefaultTimeFormat;
109112
};
110113

111114
extern Logger g_Logger;

0 commit comments

Comments
 (0)