Raised This Month: $ Target: $400
 0% 

[Any] Logging for SourcePawn


Post New Thread Reply   
 
Thread Tools Display Modes
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
F1F88
Junior Member
Join Date: Nov 2022
Old 08-07-2024 , 09:07   Re: [Any] Logging for SourcePawn
Reply With Quote #2

Benchmarks

Test platform: Windows 11 + VMware + Ubuntu 24.04 LTS + SM 1.11

Host configuration: AMD Ryzen 7 6800H + 32GB Memory

VMware configuration: 1 CPU + 8 kernel + 4GB Memory

Test case: benchmark-log4sp.sp

Single thread (Synchronous)

Code:
[benchmark] base-file-st      | Iters 1000000 | Elapsed  0.268 secs   3719518/sec
[benchmark] daily-file-st     | Iters 1000000 | Elapsed  0.278 secs   3589439/sec
[benchmark] rotating-file-st  | Iters 1000000 | Elapsed  0.279 secs   3578598/sec
[benchmark] server-console-st | Iters 1000000 | Elapsed  5.609 secs    178255/sec
Multi thread (Asynchronous)

Code:
# Queue size: 8192      Thread count: 1
[benchmark] base-file-block        | Iters 1000000 | Elapsed  0.479 secs   2084762/sec
[benchmark] daily-file-block       | Iters 1000000 | Elapsed  0.488 secs   2046592/sec
[benchmark] rotating-file-block    | Iters 1000000 | Elapsed  0.462 secs   2162868/sec
[benchmark] server-console-block   | Iters 1000000 | Elapsed  8.422 secs    118725/sec

[benchmark] base-file-overrun      | Iters 1000000 | Elapsed  0.442 secs   2259856/sec
[benchmark] daily-file-overrun     | Iters 1000000 | Elapsed  0.438 secs   2280891/sec
[benchmark] rotating-file-overrun  | Iters 1000000 | Elapsed  0.442 secs   2260684/sec
[benchmark] server-console-overrun | Iters 1000000 | Elapsed  0.379 secs   2632167/sec


# Queue size: 8192      Thread count: 4
[benchmark] base-file-block        | Iters 1000000 | Elapsed  1.049 secs    952753/sec
[benchmark] daily-file-block       | Iters 1000000 | Elapsed  1.086 secs    920584/sec
[benchmark] rotating-file-block    | Iters 1000000 | Elapsed  1.034 secs    967049/sec
[benchmark] server-console-block   | Iters 1000000 | Elapsed 15.784 secs     63354/sec

[benchmark] base-file-overrun      | Iters 1000000 | Elapsed  0.439 secs   2273952/sec
[benchmark] daily-file-overrun     | Iters 1000000 | Elapsed  0.451 secs   2212609/sec
[benchmark] rotating-file-overrun  | Iters 1000000 | Elapsed  0.453 secs   2204658/sec
[benchmark] server-console-overrun | Iters 1000000 | Elapsed  0.372 secs   2684282/sec


# Queue size: 131072    Thread count: 4
[benchmark] base-file-block        | Iters 1000000 | Elapsed   0.998 secs   1001216/sec
[benchmark] daily-file-block       | Iters 1000000 | Elapsed   0.973 secs   1027070/sec
[benchmark] rotating-file-block    | Iters 1000000 | Elapsed   0.956 secs   1045255/sec
[benchmark] server-console-block   | Iters 1000000 | Elapsed  13.952 secs     71671/sec

[benchmark] base-file-overrun      | Iters 1000000 | Elapsed   0.472 secs   2116635/sec
[benchmark] daily-file-overrun     | Iters 1000000 | Elapsed   0.441 secs   2264892/sec
[benchmark] rotating-file-overrun  | Iters 1000000 | Elapsed   0.478 secs   2091503/sec
[benchmark] server-console-overrun | Iters 1000000 | Elapsed   0.385 secs   2592245/sec


# Queue size: 8192      Thread count: 8
[benchmark] base-file-block        | Iters 1000000 | Elapsed  1.135 secs    881010/sec
[benchmark] daily-file-block       | Iters 1000000 | Elapsed  1.183 secs    845069/sec
[benchmark] rotating-file-block    | Iters 1000000 | Elapsed  1.193 secs    838199/sec
[benchmark] server-console-block   | Iters 1000000 | Elapsed 14.925 secs     67000/sec

[benchmark] base-file-overrun      | Iters 1000000 | Elapsed  0.533 secs   1875363/sec
[benchmark] daily-file-overrun     | Iters 1000000 | Elapsed  0.569 secs   1754767/sec
[benchmark] rotating-file-overrun  | Iters 1000000 | Elapsed  0.508 secs   1967969/sec
[benchmark] server-console-overrun | Iters 1000000 | Elapsed  0.394 secs   2532556/sec

Last edited by F1F88; 08-19-2024 at 15:24. Reason: v1.3.0
F1F88 is offline
Reply


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 00:25.


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