This is a Sourcemod extension that wraps the
spdlog library to enhance SourcePawn logging and debugging.
See
Github readme for more details. [
中文文档 readme-chi ]
Features
- Very fast, much faster than LogMessage
- Each Logger and Sink can customize the log level
- Each Logger and Sink can customize the log message pattern
- Each Logger can customize the flush policy
- Each Logger can have multiple Sink
- For example: A Logger that has both ServerConsoleSink and DailyFileSink is similar to LogMessage
- Each Logger can dynamic change the log level and pattern
- see server command "sm log4sp"
- Supports asynchronous Logger
- Supports format parameters with variable numbers
- Supports backtrace
- When enabled, Trace and Debug level log message are stored in a circular buffer and only output explicitly after calling DumpBacktrace()
- Supports various log targets
Usage Examples
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <log4sp>
Logger myLogger;
public void OnPluginStart()
{
// Default LogLevel: LogLevel_Info
// Default Pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
myLogger = Logger.CreateServerConsoleLogger("logger-example-1");
RegConsoleCmd("sm_log4sp_example1", CommandCallback);
// Debug is lower than the default LogLevel, so this line of code won't output log message
myLogger.Debug("===== Example 1 code initialization is complete! =====");
}
/**
* Get client aiming entity info.
*/
Action CommandCallback(int client, int args)
{
if (client <= 0 || client > MaxClients || !IsClientInGame(client))
{
// [2024-08-01 12:34:56.789] [logger-example-1] [info] Command is in-game only.
myLogger.Info("Command is in-game only.");
return Plugin_Handled;
}
int entity = GetClientAimTarget(client, false);
if (entity == -2)
{
// [2024-08-01 12:34:56.789] [logger-example-1] [fatal] The GetClientAimTarget() function is not supported.
myLogger.Fatal("The GetClientAimTarget() function is not supported.");
return Plugin_Handled;
}
if (entity == -1)
{
// [2024-08-01 12:34:56.789] [logger-example-1] [warn] client (1) is not aiming at entity.
myLogger.WarnAmxTpl("client (%d) is not aiming at entity.", client);
return Plugin_Handled;
}
if (!IsValidEntity(entity))
{
// [2024-08-01 12:34:56.789] [logger-example-1] [error] entity (444) is invalid.
myLogger.ErrorAmxTpl("entity (%d) is invalid.", entity);
return Plugin_Handled;
}
char classname[64];
GetEntityClassname(entity, classname, sizeof(classname));
// [2024-08-01 12:34:56.789] [logger-example-1] [info] client (1) is aiming a (403 - prop_door_breakable) entity.
myLogger.InfoAmxTpl("client (%d) is aiming a (%d - %s) entity.", client, entity, classname);
return Plugin_Handled;
}
Supported Games
In general, log4sp.ext should work with all games on both Linux and Windows.
But for now, you need to compile the project yourself before using it on Windows.
Github: sm-ext-log4sp
Download: releases
Natives: log4sp.inc
Documentation: wiki pages
Credits
- gabime spdlog library implements most of the functionality, log4sp.ext wraps the spdlog API for SourcePawn to use
- Fyren, nosoop, Deathreus provides solution for managing the Sink Handle
If I missed anyone, please contact me.