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()
452451void 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+
0 commit comments