1.1 --- a/core/smn_core.cpp Sun Aug 26 20:12:30 2007 +0000
1.2 +++ b/core/smn_core.cpp Mon Aug 27 02:00:37 2007 +0000
1.3 @@ -39,6 +39,8 @@
1.4 #include "HandleSys.h"
1.5 #include "LibrarySys.h"
1.6 #include "TimerSys.h"
1.7 +#include "ForwardSys.h"
1.8 +#include "Logger.h"
1.9
1.10 #if defined PLATFORM_WINDOWS
1.11 #include <windows.h>
1.12 @@ -50,6 +52,7 @@
1.13
1.14 HandleType_t g_PlIter;
1.15 ConVar sm_datetime_format("sm_datetime_format", "%m/%d/%Y - %H:%M:%S", 0, "Default formatting time rules");
1.16 +IForward *g_OnLogAction = NULL;
1.17
1.18 class CoreNativeHelpers :
1.19 public SMGlobalClass,
1.20 @@ -63,6 +66,16 @@
1.21 hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
1.22
1.23 g_PlIter = g_HandleSys.CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.24 +
1.25 + g_OnLogAction = g_Forwards.CreateForward("OnLogAction",
1.26 + ET_Hook,
1.27 + 5,
1.28 + NULL,
1.29 + Param_Cell,
1.30 + Param_Cell,
1.31 + Param_Cell,
1.32 + Param_Cell,
1.33 + Param_String);
1.34 }
1.35 void OnHandleDestroy(HandleType_t type, void *object)
1.36 {
1.37 @@ -71,10 +84,44 @@
1.38 }
1.39 void OnSourceModShutdown()
1.40 {
1.41 + g_Forwards.ReleaseForward(g_OnLogAction);
1.42 g_HandleSys.RemoveType(g_PlIter, g_pCoreIdent);
1.43 }
1.44 } g_CoreNativeHelpers;
1.45
1.46 +void LogAction(Handle_t hndl, int type, int client, int target, const char *message)
1.47 +{
1.48 + if (!g_OnLogAction->GetFunctionCount())
1.49 + {
1.50 + return;
1.51 + }
1.52 +
1.53 + cell_t result = 0;
1.54 + g_OnLogAction->PushCell(hndl);
1.55 + g_OnLogAction->PushCell(type);
1.56 + g_OnLogAction->PushCell(client);
1.57 + g_OnLogAction->PushCell(target);
1.58 + g_OnLogAction->PushString(message);
1.59 + g_OnLogAction->Execute(&result);
1.60 +
1.61 + if (result >= (ResultType)Pl_Handled)
1.62 + {
1.63 + return;
1.64 + }
1.65 +
1.66 + const char *logtag = "SM";
1.67 + if (type == 2)
1.68 + {
1.69 + HandleError err;
1.70 + IPlugin *pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
1.71 + if (pPlugin)
1.72 + {
1.73 + logtag = pPlugin->GetFilename();
1.74 + }
1.75 + }
1.76 +
1.77 + g_Logger.LogMessage("[%s] %s", logtag, message);
1.78 +}
1.79
1.80 static cell_t ThrowError(IPluginContext *pContext, const cell_t *params)
1.81 {
1.82 @@ -420,6 +467,88 @@
1.83 return g_PluginSys.LibraryExists(str) ? 1 : 0;
1.84 }
1.85
1.86 +static cell_t sm_LogAction(IPluginContext *pContext, const cell_t *params)
1.87 +{
1.88 + char buffer[2048];
1.89 + g_SourceMod.SetGlobalTarget(LANG_SERVER);
1.90 + g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 3);
1.91 +
1.92 + if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.93 + {
1.94 + return 0;
1.95 + }
1.96 +
1.97 + CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.98 +
1.99 + LogAction(pPlugin->GetMyHandle(), 2, params[1], params[2], buffer);
1.100 +
1.101 + return 1;
1.102 +}
1.103 +
1.104 +static cell_t LogToFile(IPluginContext *pContext, const cell_t *params)
1.105 +{
1.106 + char *file;
1.107 + pContext->LocalToString(params[1], &file);
1.108 +
1.109 + char path[PLATFORM_MAX_PATH];
1.110 + g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
1.111 +
1.112 + FILE *fp = fopen(path, "at");
1.113 + if (!fp)
1.114 + {
1.115 + return pContext->ThrowNativeError("Could not open file \"%s\"", path);
1.116 + }
1.117 +
1.118 + char buffer[2048];
1.119 + g_SourceMod.SetGlobalTarget(LANG_SERVER);
1.120 + g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1.121 +
1.122 + if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.123 + {
1.124 + fclose(fp);
1.125 + return 0;
1.126 + }
1.127 +
1.128 + CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.129 +
1.130 + g_Logger.LogToOpenFile(fp, "[%s] %s", pPlugin->GetFilename(), buffer);
1.131 +
1.132 + fclose(fp);
1.133 +
1.134 + return 1;
1.135 +}
1.136 +
1.137 +static cell_t LogToFileEx(IPluginContext *pContext, const cell_t *params)
1.138 +{
1.139 + char *file;
1.140 + pContext->LocalToString(params[1], &file);
1.141 +
1.142 + char path[PLATFORM_MAX_PATH];
1.143 + g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
1.144 +
1.145 + FILE *fp = fopen(path, "at");
1.146 + if (!fp)
1.147 + {
1.148 + return pContext->ThrowNativeError("Could not open file \"%s\"", path);
1.149 + }
1.150 +
1.151 + char buffer[2048];
1.152 + g_SourceMod.SetGlobalTarget(LANG_SERVER);
1.153 + g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1.154 +
1.155 + if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.156 + {
1.157 + fclose(fp);
1.158 + return 0;
1.159 + }
1.160 +
1.161 + g_Logger.LogToOpenFile(fp, "%s", buffer);
1.162 +
1.163 + fclose(fp);
1.164 +
1.165 + return 1;
1.166 +}
1.167 +
1.168 REGISTER_NATIVES(coreNatives)
1.169 {
1.170 {"AutoExecConfig", AutoExecConfig},
1.171 @@ -438,5 +567,8 @@
1.172 {"MarkNativeAsOptional", MarkNativeAsOptional},
1.173 {"RegPluginLibrary", RegPluginLibrary},
1.174 {"LibraryExists", LibraryExists},
1.175 + {"LogAction", sm_LogAction},
1.176 + {"LogToFile", LogToFile},
1.177 + {"LogToFileEx", LogToFileEx},
1.178 {NULL, NULL},
1.179 };