- implemented amb855 - LogAction()
- changed base plugins to use LogAction() where appropriate --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401384
This commit is contained in:
+45
-22
@@ -228,6 +228,39 @@ void Logger::CloseLogger()
|
||||
_CloseFile();
|
||||
}
|
||||
|
||||
void Logger::LogToOpenFile(FILE *fp, const char *msg, ...)
|
||||
{
|
||||
if (!m_Active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, msg);
|
||||
LogToOpenFileEx(fp, msg, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void Logger::LogToOpenFileEx(FILE *fp, const char *msg, va_list ap)
|
||||
{
|
||||
if (!m_Active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
char buffer[3072];
|
||||
UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
|
||||
|
||||
char date[32];
|
||||
time_t t;
|
||||
GetAdjustedTime(&t);
|
||||
tm *curtime = localtime(&t);
|
||||
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
|
||||
|
||||
fprintf(fp, "L %s: %s\n", date, buffer);
|
||||
g_SMAPI->ConPrintf("L %s: %s\n", date, buffer);
|
||||
}
|
||||
|
||||
void Logger::LogMessage(const char *vafmt, ...)
|
||||
{
|
||||
if (!m_Active)
|
||||
@@ -250,17 +283,9 @@ void Logger::LogMessage(const char *vafmt, ...)
|
||||
_NewMapFile();
|
||||
}
|
||||
|
||||
char msg[3072];
|
||||
va_list ap;
|
||||
va_start(ap, vafmt);
|
||||
vsnprintf(msg, sizeof(msg), vafmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
char date[32];
|
||||
time_t t;
|
||||
GetAdjustedTime(&t);
|
||||
tm *curtime = localtime(&t);
|
||||
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
|
||||
|
||||
FILE *fp = NULL;
|
||||
if (m_Mode == LoggingMode_PerMap)
|
||||
@@ -291,16 +316,20 @@ void Logger::LogMessage(const char *vafmt, ...)
|
||||
{
|
||||
if (m_DailyPrintHdr)
|
||||
{
|
||||
char date[32];
|
||||
m_DailyPrintHdr = false;
|
||||
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
|
||||
fprintf(fp, "L %s: SourceMod log file session started (file \"L%04d%02d%02d.log\") (Version \"%s\")\n", date, curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday, SVN_FULL_VERSION);
|
||||
}
|
||||
fprintf(fp, "L %s: %s\n", date, msg);
|
||||
va_list ap;
|
||||
va_start(ap, vafmt);
|
||||
LogToOpenFileEx(fp, vafmt, ap);
|
||||
va_end(ap);
|
||||
fclose(fp);
|
||||
} else {
|
||||
goto print_error;
|
||||
}
|
||||
|
||||
g_SMAPI->ConPrintf("L %s: %s\n", date, msg);
|
||||
return;
|
||||
print_error:
|
||||
g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
|
||||
@@ -318,9 +347,6 @@ void Logger::LogError(const char *vafmt, ...)
|
||||
GetAdjustedTime(&t);
|
||||
tm *curtime = localtime(&t);
|
||||
|
||||
char date[32];
|
||||
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
|
||||
|
||||
if (curtime->tm_mday != m_CurDay)
|
||||
{
|
||||
char _filename[256];
|
||||
@@ -330,30 +356,27 @@ void Logger::LogError(const char *vafmt, ...)
|
||||
m_ErrMapStart = false;
|
||||
}
|
||||
|
||||
char msg[3072];
|
||||
va_list ap;
|
||||
va_start(ap, vafmt);
|
||||
vsnprintf(msg, sizeof(msg), vafmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
FILE *fp = fopen(m_ErrFileName.c_str(), "a+");
|
||||
if (fp)
|
||||
{
|
||||
if (!m_ErrMapStart)
|
||||
{
|
||||
char date[32];
|
||||
strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
|
||||
fprintf(fp, "L %s: SourceMod error session started\n", date);
|
||||
fprintf(fp, "L %s: Info (map \"%s\") (file \"errors_%04d%02d%02d.log\")\n", date, m_CurMapName.c_str(), curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday);
|
||||
m_ErrMapStart = true;
|
||||
}
|
||||
fprintf(fp, "L %s: %s\n", date, msg);
|
||||
va_list ap;
|
||||
va_start(ap, vafmt);
|
||||
LogToOpenFileEx(fp, vafmt, ap);
|
||||
va_end(ap);
|
||||
fclose(fp);
|
||||
} else {
|
||||
g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
|
||||
m_Active = false;
|
||||
return;
|
||||
}
|
||||
|
||||
g_SMAPI->ConPrintf("L %s: %s\n", date, msg);
|
||||
}
|
||||
|
||||
void Logger::MapChange(const char *mapname)
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#ifndef _INCLUDE_SOURCEMOD_CLOGGER_H_
|
||||
#define _INCLUDE_SOURCEMOD_CLOGGER_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sh_string.h>
|
||||
#include "sm_globals.h"
|
||||
|
||||
@@ -75,6 +76,8 @@ public:
|
||||
void LogMessage(const char *msg, ...);
|
||||
void LogError(const char *msg, ...);
|
||||
void LogFatal(const char *msg, ...);
|
||||
void LogToOpenFile(FILE *fp, const char *msg, ...);
|
||||
void LogToOpenFileEx(FILE *fp, const char *msg, va_list ap);
|
||||
void MapChange(const char *mapname);
|
||||
const char *GetLogFileName(LogType type) const;
|
||||
LoggingMode GetLoggingMode() const;
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
#include "HandleSys.h"
|
||||
#include "LibrarySys.h"
|
||||
#include "TimerSys.h"
|
||||
#include "ForwardSys.h"
|
||||
#include "Logger.h"
|
||||
|
||||
#if defined PLATFORM_WINDOWS
|
||||
#include <windows.h>
|
||||
@@ -50,6 +52,7 @@
|
||||
|
||||
HandleType_t g_PlIter;
|
||||
ConVar sm_datetime_format("sm_datetime_format", "%m/%d/%Y - %H:%M:%S", 0, "Default formatting time rules");
|
||||
IForward *g_OnLogAction = NULL;
|
||||
|
||||
class CoreNativeHelpers :
|
||||
public SMGlobalClass,
|
||||
@@ -63,6 +66,16 @@ public:
|
||||
hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
|
||||
|
||||
g_PlIter = g_HandleSys.CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
|
||||
|
||||
g_OnLogAction = g_Forwards.CreateForward("OnLogAction",
|
||||
ET_Hook,
|
||||
5,
|
||||
NULL,
|
||||
Param_Cell,
|
||||
Param_Cell,
|
||||
Param_Cell,
|
||||
Param_Cell,
|
||||
Param_String);
|
||||
}
|
||||
void OnHandleDestroy(HandleType_t type, void *object)
|
||||
{
|
||||
@@ -71,10 +84,44 @@ public:
|
||||
}
|
||||
void OnSourceModShutdown()
|
||||
{
|
||||
g_Forwards.ReleaseForward(g_OnLogAction);
|
||||
g_HandleSys.RemoveType(g_PlIter, g_pCoreIdent);
|
||||
}
|
||||
} g_CoreNativeHelpers;
|
||||
|
||||
void LogAction(Handle_t hndl, int type, int client, int target, const char *message)
|
||||
{
|
||||
if (!g_OnLogAction->GetFunctionCount())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
cell_t result = 0;
|
||||
g_OnLogAction->PushCell(hndl);
|
||||
g_OnLogAction->PushCell(type);
|
||||
g_OnLogAction->PushCell(client);
|
||||
g_OnLogAction->PushCell(target);
|
||||
g_OnLogAction->PushString(message);
|
||||
g_OnLogAction->Execute(&result);
|
||||
|
||||
if (result >= (ResultType)Pl_Handled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const char *logtag = "SM";
|
||||
if (type == 2)
|
||||
{
|
||||
HandleError err;
|
||||
IPlugin *pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
|
||||
if (pPlugin)
|
||||
{
|
||||
logtag = pPlugin->GetFilename();
|
||||
}
|
||||
}
|
||||
|
||||
g_Logger.LogMessage("[%s] %s", logtag, message);
|
||||
}
|
||||
|
||||
static cell_t ThrowError(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
@@ -420,6 +467,88 @@ static cell_t LibraryExists(IPluginContext *pContext, const cell_t *params)
|
||||
return g_PluginSys.LibraryExists(str) ? 1 : 0;
|
||||
}
|
||||
|
||||
static cell_t sm_LogAction(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
char buffer[2048];
|
||||
g_SourceMod.SetGlobalTarget(LANG_SERVER);
|
||||
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 3);
|
||||
|
||||
if (pContext->GetContext()->n_err != SP_ERROR_NONE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
|
||||
|
||||
LogAction(pPlugin->GetMyHandle(), 2, params[1], params[2], buffer);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t LogToFile(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
char *file;
|
||||
pContext->LocalToString(params[1], &file);
|
||||
|
||||
char path[PLATFORM_MAX_PATH];
|
||||
g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
|
||||
|
||||
FILE *fp = fopen(path, "at");
|
||||
if (!fp)
|
||||
{
|
||||
return pContext->ThrowNativeError("Could not open file \"%s\"", path);
|
||||
}
|
||||
|
||||
char buffer[2048];
|
||||
g_SourceMod.SetGlobalTarget(LANG_SERVER);
|
||||
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
|
||||
|
||||
if (pContext->GetContext()->n_err != SP_ERROR_NONE)
|
||||
{
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
|
||||
|
||||
g_Logger.LogToOpenFile(fp, "[%s] %s", pPlugin->GetFilename(), buffer);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t LogToFileEx(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
char *file;
|
||||
pContext->LocalToString(params[1], &file);
|
||||
|
||||
char path[PLATFORM_MAX_PATH];
|
||||
g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
|
||||
|
||||
FILE *fp = fopen(path, "at");
|
||||
if (!fp)
|
||||
{
|
||||
return pContext->ThrowNativeError("Could not open file \"%s\"", path);
|
||||
}
|
||||
|
||||
char buffer[2048];
|
||||
g_SourceMod.SetGlobalTarget(LANG_SERVER);
|
||||
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
|
||||
|
||||
if (pContext->GetContext()->n_err != SP_ERROR_NONE)
|
||||
{
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
g_Logger.LogToOpenFile(fp, "%s", buffer);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
REGISTER_NATIVES(coreNatives)
|
||||
{
|
||||
{"AutoExecConfig", AutoExecConfig},
|
||||
@@ -438,5 +567,8 @@ REGISTER_NATIVES(coreNatives)
|
||||
{"MarkNativeAsOptional", MarkNativeAsOptional},
|
||||
{"RegPluginLibrary", RegPluginLibrary},
|
||||
{"LibraryExists", LibraryExists},
|
||||
{"LogAction", sm_LogAction},
|
||||
{"LogToFile", LogToFile},
|
||||
{"LogToFileEx", LogToFileEx},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
+79
-5
@@ -69,8 +69,7 @@ public:
|
||||
g_LibSys.CloseDirectory(pDir);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} s_FileNatives;
|
||||
|
||||
static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
@@ -503,6 +502,11 @@ 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 (pContext->GetContext()->n_err != SP_ERROR_NONE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (len >= sizeof(buffer)-2)
|
||||
{
|
||||
buffer[1022] = '\n';
|
||||
@@ -524,7 +528,12 @@ static cell_t sm_LogMessage(IPluginContext *pContext, const cell_t *params)
|
||||
char buffer[1024];
|
||||
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
|
||||
|
||||
IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
|
||||
if (pContext->GetContext()->n_err != SP_ERROR_NONE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
|
||||
g_Logger.LogMessage("[%s] %s", pPlugin->GetFilename(), buffer);
|
||||
|
||||
return 1;
|
||||
@@ -537,7 +546,12 @@ static cell_t sm_LogError(IPluginContext *pContext, const cell_t *params)
|
||||
char buffer[1024];
|
||||
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
|
||||
|
||||
IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
|
||||
if (pContext->GetContext()->n_err != SP_ERROR_NONE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
|
||||
g_Logger.LogError("[%s] %s", pPlugin->GetFilename(), buffer);
|
||||
|
||||
return 1;
|
||||
@@ -586,7 +600,66 @@ static cell_t sm_GetFileTime(IPluginContext *pContext, const cell_t *params)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static FileNatives s_FileNatives;
|
||||
static cell_t sm_LogToOpenFile(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
FILE *pFile;
|
||||
|
||||
sec.pOwner = NULL;
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
|
||||
if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
char buffer[2048];
|
||||
g_SourceMod.SetGlobalTarget(LANG_SERVER);
|
||||
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
|
||||
|
||||
if (pContext->GetContext()->n_err != SP_ERROR_NONE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
|
||||
g_Logger.LogToOpenFile(pFile, "[%s] %s", pPlugin->GetFilename(), buffer);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell_t sm_LogToOpenFileEx(IPluginContext *pContext, const cell_t *params)
|
||||
{
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
HandleError herr;
|
||||
HandleSecurity sec;
|
||||
FILE *pFile;
|
||||
|
||||
sec.pOwner = NULL;
|
||||
sec.pIdentity = g_pCoreIdent;
|
||||
|
||||
if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
|
||||
!= HandleError_None)
|
||||
{
|
||||
return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
|
||||
}
|
||||
|
||||
char buffer[2048];
|
||||
g_SourceMod.SetGlobalTarget(LANG_SERVER);
|
||||
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
|
||||
|
||||
if (pContext->GetContext()->n_err != SP_ERROR_NONE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
g_Logger.LogToOpenFile(pFile, "%s", buffer);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
REGISTER_NATIVES(filesystem)
|
||||
{
|
||||
@@ -610,5 +683,6 @@ REGISTER_NATIVES(filesystem)
|
||||
{"LogError", sm_LogError},
|
||||
{"FlushFile", sm_FlushFile},
|
||||
{"GetFileTime", sm_GetFileTime},
|
||||
{"LogToOpenFile", sm_LogToOpenFile},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user