Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions configs/core.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
59 changes: 40 additions & 19 deletions core/logic/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Version: $Id$
*/

#include <string_view>
#include <time.h>
#include <cstdarg>
#include "Logger.h"
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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))
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;

}

5 changes: 4 additions & 1 deletion core/logic/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,17 +95,20 @@ class Logger : public SMGlobalClass, public ILogger
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;

LoggingMode m_Mode;
bool m_Active;
bool m_DamagedNormalFile;
bool m_DamagedErrorFile;
bool m_isUsingDefaultTimeFormat;
};

extern Logger g_Logger;
Expand Down