About
The API for SourceMod plugin developers.
The main goal of SM Logger is to make debugging plugins easier and don't choke with unnecessary information.
SM Logger split debug messages to channels, each channel has its own name (tag e.g. [MY_TAG_NAME]) and bit.
Any number of channels can be output at the same time (turn it on/off) by adding or removing bits via convar.
Features:- Output data where you need (File, server console, chat).
- Output data you need (Switch debugging channels).
- Output data at the speed of light! (Faster than LogMessage() approximately in 5 times)
Note
A lot I/O operation (e.g. log spamming) can lead to server lags and bad gaming experience.
SM Logger keeps a log file always open, so it reduce I/O operation and lead to more speed and may solve the lag problem.
Naming
The log file has the name equivalent to plugin name but lower case letters (spaces will be replaced by underscores).
Let's call that name as
SMLOGGER_NAME. The log file will be created in sourcemod/logs folder.
Convars
SMLOGGER_NAME_log_channel - Outputs bit channel flags (add numbers together):
bits defined by developer
SMLOGGER_NAME_log_output - Outputs bit flags (add numbers together): 0=Disable, 1=Logs to file, 2=Prints to server, 4=Prints to chat, 7=all
API
PHP Code:
// Output log bits
enum SMLOutFlags
{
SML_NONE = (0<<0), // Disable
SML_FILE = (1<<0), // Logs to file
SML_SERVER = (1<<1), // Prints to server
SML_CHAT = (1<<2), // Prints to chat
SML_ALL = SML_FILE|SML_SERVER|SML_CHAT // Spam!
}
/**
* Initializes internal structure necessary for sm_logger.inc functions.
* @remark IT NECESSARY TO RUN THIS FUNCTION OnPluginStart()!
*
* @param tags 2D string array, contain a log tags.
* @param size Number of string (first dimension size of tags string).
* @param channelFlags Default channel bits.
* @param outFlags Default output bits.
* @param truncate True to delete the content of existing file, false otherwise.
*
* @noreturn
*/
stock void SMLoggerInit(const char[][] tags, int size, int channelFlags = 0, SMLOutFlags outFlags = SML_NONE, bool truncate = false)
// Logging without tagging. Respect cvar flags value
stock void SMLog(const char[] format, any ...)
// Logging without tagging. Overwrite outputs cvar flags value
stock void SMLogEx(SMLOutFlags outFlags, const char[] format, any ...)
// Respects logger convars. Converts channelFlags to tag.
stock void SMLogTag(int channelFlags, const char[] format, any ...)
// Uses SMLog behavior but overwrite outputs cvar flags value
stock void SMLogTagEx(int channelFlags, SMLOutFlags outFlags, const char[] format, any ...)
// Uses SMLog behavior but prints to specific client instead of all chat
stock void SMLogClient(int channelFlags, int client, const char[] format, any ...)
// Indents control
stock void SMLoggerTabChar(char c)
stock char SMLoggerGetTab(int tabsize)
stock void SMLoggerSetTab(int tabsize)
Setup
PHP Code:
#include <sm_logger>
char LOG_TAGS[][] = {"CORE", "WARNING"}; // <- adds new tag here (channel names)
// Bitwise values definitions
enum (<<= 1)
{
SML_CORE = 1,
SML_WARN,
// <- adds new bit here
}
public void OnPluginStart()
{
SMLoggerInit(LOG_TAGS, sizeof(LOG_TAGS), SML_CORE|SML_WARN, SML_FILE); // setup logger
}
Example #1
PHP Code:
public void OnMapStart()
{
SMLogTag(SML_CORE, "OnMapStart");
}
public void OnLibraryRemoved(const char[] name)
{
SMLogTag(SML_WARN, "%s lib is removed!", name);
}
HTML Code:
06/29/2019 - 21:23:30 [CORE] OnMapStart
06/29/2019 - 21:23:30 [WARNING] l4d2 lib is removed!
Example #2
PHP Code:
SMLogEx(SML_FILE, "deep 1");
SMLoggerSetTab(1);
int x;
for (int i; i < 3; i++){
SMLogEx(SML_FILE, "index=%d", i);
SMLogEx(SML_FILE, "tick=%d", GetGameTickCount());
SMLogEx(SML_FILE, "gametime=%f", GetGameTime());
SMLogEx(SML_FILE, "deep 2");
SMLoggerSetTab(2);
for (x = 0; x < 5; x++){
SMLogEx(SML_FILE, "index2=%d", x);
SMLogEx(SML_FILE, "sum=%d", i+x);
}
SMLoggerSetTab(1);
}
SMLoggerSetTab(0);
HTML Code:
06/29/2019 - 21:15:25 deep 1
06/29/2019 - 21:15:25 index=0
06/29/2019 - 21:15:25 tick=30
06/29/2019 - 21:15:25 gametime=1.000000
06/29/2019 - 21:15:25 deep 2
06/29/2019 - 21:15:25 index2=0
06/29/2019 - 21:15:25 sum=0
06/29/2019 - 21:15:25 index2=1
06/29/2019 - 21:15:25 sum=1
06/29/2019 - 21:15:25 index2=2
06/29/2019 - 21:15:25 sum=2
06/29/2019 - 21:15:25 index2=3
06/29/2019 - 21:15:25 sum=3
06/29/2019 - 21:15:25 index2=4
06/29/2019 - 21:15:25 sum=4
06/29/2019 - 21:15:25 index=1
06/29/2019 - 21:15:25 tick=30
06/29/2019 - 21:15:25 gametime=1.000000
06/29/2019 - 21:15:25 deep 2
06/29/2019 - 21:15:25 index2=0
06/29/2019 - 21:15:25 sum=1
06/29/2019 - 21:15:25 index2=1
06/29/2019 - 21:15:25 sum=2
06/29/2019 - 21:15:25 index2=2
06/29/2019 - 21:15:25 sum=3
06/29/2019 - 21:15:25 index2=3
06/29/2019 - 21:15:25 sum=4
06/29/2019 - 21:15:25 index2=4
06/29/2019 - 21:15:25 sum=5
06/29/2019 - 21:15:25 index=2
06/29/2019 - 21:15:25 tick=30
06/29/2019 - 21:15:25 gametime=1.000000
06/29/2019 - 21:15:25 deep 2
06/29/2019 - 21:15:25 index2=0
06/29/2019 - 21:15:25 sum=2
06/29/2019 - 21:15:25 index2=1
06/29/2019 - 21:15:25 sum=3
06/29/2019 - 21:15:25 index2=2
06/29/2019 - 21:15:25 sum=4
06/29/2019 - 21:15:25 index2=3
06/29/2019 - 21:15:25 sum=5
06/29/2019 - 21:15:25 index2=4
06/29/2019 - 21:15:25 sum=6
For more examples see
logger_test plugin and
logger_test.log
__________________