Raised This Month: $ Target: $400
 0% 

[Any] Logging for SourcePawn


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
F1F88
Junior Member
Join Date: Nov 2022
Old 07-29-2024 , 09:18   [Any] Logging for SourcePawn
Reply With Quote #1

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
  1. Very fast, much faster than LogMessage
  2. Each Logger and Sink can customize the log level
  3. Each Logger and Sink can customize the log message pattern
  4. Each Logger can customize the flush policy
  5. Each Logger can have multiple Sink
    • For example: A Logger that has both ServerConsoleSink and DailyFileSink is similar to LogMessage
  6. Each Logger can dynamic change the log level and pattern
    • see server command "sm log4sp"
  7. Supports asynchronous Logger
  8. Supports format parameters with variable numbers
  9. Supports backtrace
    • When enabled, Trace and Debug level log message are stored in a circular buffer and only output explicitly after calling DumpBacktrace()
  10. 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 clientint args)
{
    if (
client <= || 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(clientfalse);
    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(entityclassnamesizeof(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."cliententityclassname);

    return 
Plugin_Handled;


Supported Games

log4sp.ext should work for all games on Linux and 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.

Last edited by F1F88; 09-14-2024 at 07:13. Reason: windows releases are available
F1F88 is offline
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:46.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode