[TF2] Hooking Server Log

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

[TF2] Hooking Server Log

Postby Zeus » Fri Jun 26, 2020 6:29 am

Basically I need the SP equivalent of this: https://sm.alliedmods.net/new-api/loggi ... ameLogHook

This allows me to take the log output from the server console (via the log cvar) and pipe it into a file which I will later need to upload somewhere when the match ends.

Example log line:

Code: Select all

L 06/26/2020 - 06:11:13: "Moon<2><[U:1:80225322]><Blue>" triggered "shot_fired" (weapon "scattergun")


Capturing all server output would prob have a notable performance penalty and is unnecessary if I can just hook the game log.

Anyway I have no idea ahow to do this; I was unable to find anything on the wiki / source code for it
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: [TF2] Hooking Server Log

Postby Ayuto » Fri Jun 26, 2020 9:04 am

If you want to to hook the complete server output, you can use the OnServerOutput listener:
http://wiki.sourcepython.com/developing ... rveroutput

If you want to get the server output at a specific time (e. g. when executing a command), you can use context manager server_output:
http://wiki.sourcepython.com/developing ... ver_output

If you want to hook the LogPrint function (that's what SM does), you can use this little snippet:

Syntax: Select all

from memory import get_virtual_function
from memory.hooks import PreHook
from engines.server import engine_server

@PreHook(get_virtual_function(engine_server, 'LogPrint'))
def pre_log_print(args):
print(args)



# Just to test the hook
from commands.typed import TypedServerCommand

@TypedServerCommand('logsp')
def on_logsp(info, message):
engine_server.log_print(message)

Output:

Code: Select all

sp plugin load test1
[SP] Loading plugin 'test1'...
Hooking function: type=PRE, addr=2076087872, conv=THISCALL, args=(_memory.DataType.POINTER, _memory.DataType.STRING), rtype=VOID, callback=<function pre_log_print at 0x1ACB5A08>
(<_memory.Pointer object at 0x1ACAFB60>, "- sp.core.command\t-\tMESSAGE\n\t[SP] Successfully loaded plugin 'test1'.\n")
[SP] Successfully loaded plugin 'test1'.



logsp "Hello, world!"
(<_memory.Pointer object at 0x1ACAFB60>, 'Hello, world!')
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: [TF2] Hooking Server Log

Postby Zeus » Fri Jun 26, 2020 7:09 pm

Ayuto wrote:If you want to to hook the complete server output, you can use the OnServerOutput listener:
http://wiki.sourcepython.com/developing ... rveroutput

If you want to get the server output at a specific time (e. g. when executing a command), you can use context manager server_output:
http://wiki.sourcepython.com/developing ... ver_output

If you want to hook the LogPrint function (that's what SM does), you can use this little snippet:

Syntax: Select all

from memory import get_virtual_function
from memory.hooks import PreHook
from engines.server import engine_server

@PreHook(get_virtual_function(engine_server, 'LogPrint'))
def pre_log_print(args):
print(args)



# Just to test the hook
from commands.typed import TypedServerCommand

@TypedServerCommand('logsp')
def on_logsp(info, message):
engine_server.log_print(message)

Output:

Code: Select all

sp plugin load test1
[SP] Loading plugin 'test1'...
Hooking function: type=PRE, addr=2076087872, conv=THISCALL, args=(_memory.DataType.POINTER, _memory.DataType.STRING), rtype=VOID, callback=<function pre_log_print at 0x1ACB5A08>
(<_memory.Pointer object at 0x1ACAFB60>, "- sp.core.command\t-\tMESSAGE\n\t[SP] Successfully loaded plugin 'test1'.\n")
[SP] Successfully loaded plugin 'test1'.



logsp "Hello, world!"
(<_memory.Pointer object at 0x1ACAFB60>, 'Hello, world!')



Awesome, ill give this a shot!

Is there a place I can find all of these available virtual functions to hook? Might be useful to put that up as a reference on the wiki
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [TF2] Hooking Server Log

Postby L'In20Cible » Fri Jun 26, 2020 9:36 pm

Zeus wrote:Is there a place I can find all of these available virtual functions to hook? Might be useful to put that up as a reference on the wiki

You can find them all by searching for FUNCTION_INFO on the repo.
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: [TF2] Hooking Server Log

Postby Ayuto » Fri Jun 26, 2020 10:05 pm

Or simply type "sp dump class_info class_info".
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: [TF2] Hooking Server Log

Postby Zeus » Sat Jun 27, 2020 5:18 am

This works! Thanks for all the help guys!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 36 guests