From f9a5920e5b17651a62ffa627ef7bc178b4076589 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 25 Jan 2007 06:19:17 +0000 Subject: [PATCH] added an easier format string Function changed normal log names to be more consistent added logging natives --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40352 --- core/CLogger.cpp | 14 +++++++---- core/smn_filesystem.cpp | 47 +++++++++++++++++++++++++++++++++++ core/sourcemod.cpp | 12 +++++++++ core/sourcemod.h | 1 + plugins/include/sourcemod.inc | 27 ++++++++++++++++++++ public/ISourceMod.h | 16 ++++++++++++ 6 files changed, 112 insertions(+), 5 deletions(-) diff --git a/core/CLogger.cpp b/core/CLogger.cpp index a420e1c2..3817a419 100644 --- a/core/CLogger.cpp +++ b/core/CLogger.cpp @@ -7,6 +7,10 @@ CLogger g_Logger; +/** + * :TODO: This should be creating the log folder if it doesn't exist + */ + void CLogger::OnSourceModStartup(bool late) { //:TODO: read these options from a file, dont hardcode them @@ -34,7 +38,7 @@ void CLogger::_NewMapFile() while (true) { - g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%02d%02d%03d.log", curtime->tm_mon + 1, curtime->tm_mday, i); + g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d%03d.log", curtime->tm_mon + 1, curtime->tm_mday, i); FILE *fp = fopen(_filename, "r"); if (!fp) { @@ -54,7 +58,7 @@ void CLogger::_NewMapFile() } else { char date[32]; strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime); - fprintf(fp, "L %s: SourceMod log file started (file \"L%02d%02d%03d.log\") (Version \"%s\")\n", date, curtime->tm_mon + 1, curtime->tm_mday, i, SOURCEMOD_VERSION); + fprintf(fp, "L %s: SourceMod log file started (file \"logs_%02d%02d%03d.log\") (Version \"%s\")\n", date, curtime->tm_mon + 1, curtime->tm_mday, i, SOURCEMOD_VERSION); fclose(fp); } } @@ -119,7 +123,7 @@ void CLogger::InitLogger(LoggingMode mode, bool startlogging) } case LoggingMode_Daily: { - g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday); + g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday); m_NrmFileName.assign(_filename); m_DailyPrintHdr = true; break; @@ -183,7 +187,7 @@ void CLogger::LogMessage(const char *vafmt, ...) if (m_CurDay != curtime->tm_mday) { char _filename[256]; - g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday); + g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday); m_NrmFileName.assign(_filename); m_CurDay = curtime->tm_mday; m_DailyPrintHdr = true; @@ -196,7 +200,7 @@ void CLogger::LogMessage(const char *vafmt, ...) if (m_DailyPrintHdr) { m_DailyPrintHdr = false; - fprintf(fp, "L %s: SourceMod log file session started (file \"L%02d%02d.log\") (Version \"%s\")\n", date, curtime->tm_mon + 1, curtime->tm_mday, SOURCEMOD_VERSION); + fprintf(fp, "L %s: SourceMod log file session started (file \"logs_%02d%02d.log\") (Version \"%s\")\n", date, curtime->tm_mon + 1, curtime->tm_mday, SOURCEMOD_VERSION); } fprintf(fp, "L %s: %s\n", date, msg); fclose(fp); diff --git a/core/smn_filesystem.cpp b/core/smn_filesystem.cpp index d7d7fc88..2777de27 100644 --- a/core/smn_filesystem.cpp +++ b/core/smn_filesystem.cpp @@ -3,6 +3,9 @@ #include "HandleSys.h" #include "LibrarySys.h" #include "sm_stringutil.h" +#include "CLogger.h" +#include "PluginSys.h" +#include "sourcemm_api.h" HandleType_t g_FileType; HandleType_t g_DirType; @@ -440,6 +443,47 @@ static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params) return g_SourceMod.BuildPath(Path_SM_Rel, buffer, params[3], "%s", path); } +static cell_t sm_LogToGame(IPluginContext *pContext, const cell_t *params) +{ + char buffer[1024]; + size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); + + if (len >= sizeof(buffer)-2) + { + buffer[1022] = '\n'; + buffer[1023] = '\0'; + } else { + buffer[len++] = '\n'; + buffer[len] = '\0'; + } + + engine->LogPrint(buffer); + + return 1; +} + +static cell_t sm_LogMessage(IPluginContext *pContext, const cell_t *params) +{ + char buffer[1024]; + size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); + + IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext()); + g_Logger.LogMessage("[%s] %s", pPlugin->GetFilename(), buffer); + + return 1; +} + +static cell_t sm_LogError(IPluginContext *pContext, const cell_t *params) +{ + char buffer[1024]; + size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); + + IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext()); + g_Logger.LogError("[%s] %s", pPlugin->GetFilename(), buffer); + + return 1; +} + static FileNatives s_FileNatives; REGISTER_NATIVES(filesystem) @@ -459,5 +503,8 @@ REGISTER_NATIVES(filesystem) {"RemoveDir", sm_RemoveDir}, {"WriteFileLine", sm_WriteFileLine}, {"BuildPath", sm_BuildPath}, + {"LogToGame", sm_LogToGame}, + {"LogMessage", sm_LogMessage}, + {"LogError", sm_LogError}, {NULL, NULL}, }; diff --git a/core/sourcemod.cpp b/core/sourcemod.cpp index 99dc5e26..a1adf21f 100644 --- a/core/sourcemod.cpp +++ b/core/sourcemod.cpp @@ -9,6 +9,7 @@ #include "CLogger.h" #include "ExtensionSys.h" #include "AdminCache.h" +#include "sm_stringutil.h" SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool); SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false); @@ -305,6 +306,17 @@ void SourceModBase::LogError(IExtension *pExt, const char *format, ...) } } +size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param) +{ + char *fmt; + + pContext->LocalToString(params[param], &fmt); + + int lparam = ++param; + + return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam); +} + const char *SourceModBase::GetSourceModPath() { return m_SMBaseDir; diff --git a/core/sourcemod.h b/core/sourcemod.h index 8b61c83b..dcdf4148 100644 --- a/core/sourcemod.h +++ b/core/sourcemod.h @@ -49,6 +49,7 @@ public: //ISourceMod size_t BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...); void LogMessage(IExtension *pExt, const char *format, ...); void LogError(IExtension *pExt, const char *format, ...); + size_t FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param); private: /** * @brief Loading plugins diff --git a/plugins/include/sourcemod.inc b/plugins/include/sourcemod.inc index 8ee58bd6..8095db0a 100644 --- a/plugins/include/sourcemod.inc +++ b/plugins/include/sourcemod.inc @@ -226,3 +226,30 @@ native PrintToServer(const String:format[], {Handle,Float,String,_}:...); * @error If the client is not connected an error will be thrown. */ native PrintToConsole(client, const String:format[], {Handle,Float,String,_}:...); + +/** + * Logs a generic message to the HL2 logs. + * + * @param format String format. + * @param ... Format arguments. + * @noreturn + */ +native LogToGame(const String:format[], {Handle,Float,String,_}:...); + +/** + * Logs a plugin message to the SourceMod logs. + * + * @param format String format. + * @param ... Format arguments. + * @noreturn + */ +native LogMessage(const String:format[], {Handle,Float,String,_}:...); + +/** + * Logs a plugin error message to the SourceMod logs. + * + * @param format String format. + * @param ... Format arguments. + * @noreturn + */ +native LogError(const String:format[], {Handle,Float,String,_}:...); \ No newline at end of file diff --git a/public/ISourceMod.h b/public/ISourceMod.h index 9161ffbb..fe2d2144 100644 --- a/public/ISourceMod.h +++ b/public/ISourceMod.h @@ -69,6 +69,22 @@ namespace SourceMod * @param ... Message format parameters. */ virtual void LogError(IExtension *pExt, const char *format, ...) =0; + + /** + * @brief Formats a string from a native. + * + * @param buffer Buffer to store message. + * @param maxlength Maximum length of buffer (inculding null terminator). + * @param pContext Pointer to the plugin's context. + * @param params Parameter array that was passed to the native. + * @param param Parameter index where format string and variable arguments begin. + * @return Number of bytes written, not including the null terminator. + */ + virtual size_t FormatString(char *buffer, + size_t maxlength, + IPluginContext *pContext, + const cell_t *params, + unsigned int param) =0; }; };