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]>
This commit is contained in:
co-authored by
Nicholas Hastings
parent
84f67fcd2d
commit
1fc841713e
@@ -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.
|
||||
|
||||
+40
-19
@@ -29,6 +29,7 @@
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#include <string_view>
|
||||
#include <time.h>
|
||||
#include <cstdarg>
|
||||
#include "Logger.h"
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
@@ -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);
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user