Skip to content

F1F88/sm-ext-log4sp

Repository files navigation

English | 中文

Log for SourcePawn

Log4sp is a powerful SourceMod extension that provides SourceMod plugin developers with a high-performance, feature-rich, and easy-to-use logging API.

With log4sp, plugin developers no longer need to write complex logging code, so they can focus more on the core function development of the plugin.

Features

  1. Very fast, much faster than SourceMod API - Logging

  2. Support custom log filtering.

    • For test, using lower log level (e.g., trace, debug) can increase log messages and help find problems.

    • For release, using higher log level (e.g., warn, error) can reduce log messages and thus improve performance.

  3. Support custom log message pattern.

    • Custom information can be appended to log messages. (such as time, log level, source code location, etc.)

    • Specify the style of the log message and append more information (such as time, log level, source code location, etc.)

    • Default log message pattern:

      [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v

      [2024-08-01 12:34:56:789] [log4sp] [info] [example.sp:123] message

    • All log message pattern flags: spdlog wiki

  4. Support custom log message flush level.

    • By default, the log4sp extension flush the log buffer when it sees fit in order to achieve good performance.

    • Logger.Flush() can be manually flushed.

    • Logger.FlushOn() can set the minimum log level that will trigger automatic flush.

  5. Support "backtrace" log messages.

    • store trace and debug messages in a ring buffer and display them later on demand.
  6. Support server console commands.

    • The server console command "sm log4sp" can dynamically modify the log level, flush level, log pattern, backtrace, etc.
  7. Support for "unlimited size" logging messages.

    • For Logger.LogAmxTpl() method, the maximum length of the log message is 2048 characters, and the excess will be truncated.

    • For Logger.Log() and Logger.LogEx() methods, the length of the log message is not limited (theoretically subject to available memory)

  8. Supports logging to multiple sinks at once.

    • Each Logger can have multiple Sinks.

    • Each Sink can customize different log level and log pattern.

      For example: When the Logger has ServerConsoleSink and DailyFileSink, it is equivalent to LogMessage when sv_logecho 1.

  9. Various log targets

    • BaseFileSink (Similar to LogToFile

    • CallbackSink

    • ClientChatSink(Similar to PrintToChat

    • ClientConsoleSink(Similar to PrintToConsole

    • DailyFileSink(Similar to LogMessage, Rotate log files based on date.)

    • RotatingFileSink(Similar to LogMessage, Rotate log files based on file size.)

    • ServerConsoleSink(Similar to PrintToServer

Documentation

Documentation can be found in the wiki pages.

Supported Games

The log4sp extension should work for all games on Linux and Windows.

Benchmarks

Test platform: Windows 11 + VMware + Ubuntu 24.04 LTS + SourceMod 1.13.0.7178

Host configuration: AMD Ryzen 7 7840HS + 32 GB Memory

VM Ubuntu configuration: 1 CPU + 8 kernel + 8 GB Memory

Test case: benchmark-log4sp.sp

[benchmark] base-file         | Iters 1000000 | Elapsed  0.465 secs   2150463/sec
[benchmark] daily-file        | Iters 1000000 | Elapsed  0.471 secs   2118729/sec
[benchmark] rotating-file     | Iters 1000000 | Elapsed  0.482 secs   2073553/sec
[benchmark] server-console    | Iters 1000000 | Elapsed  4.847 secs    206288/sec

Sourcemod logging

As a reference, sourcemod logging API was also tested

Test case: benchmark-sm-logging.sp

[benchmark] LogMessage    | Iters 1000000 | Elapsed  9.657 secs    103548/sec
[benchmark] LogToFile     | Iters 1000000 | Elapsed  8.070 secs    123903/sec
[benchmark] LogToFileEx   | Iters 1000000 | Elapsed  7.959 secs    125637/sec
[benchmark] PrintToServer | Iters 1000000 | Elapsed  4.718 secs    211920/sec

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

  • blueblur0730, Digby helped improve the traversal operation of all loggers

If I missed anyone, please contact me.