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
This commit is contained in:
David Anderson 2007-01-25 06:19:17 +00:00
parent 68ff0eb328
commit f9a5920e5b
6 changed files with 112 additions and 5 deletions

View File

@ -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);

View File

@ -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},
};

View File

@ -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;

View File

@ -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

View File

@ -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,_}:...);

View File

@ -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;
};
};