Raised This Month: $12 Target: $400
 3% 

[INC] SM Logger


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
disawar1
AlliedModders Donor
Join Date: Aug 2011
Location: Russian
Old 06-30-2019 , 15:48   [INC] SM Logger
Reply With Quote #1

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[][] tagsint sizeint channelFlags 0SMLOutFlags outFlags SML_NONEbool truncate false)

// Logging without tagging. Respect cvar flags value
stock void SMLog(const char[] formatany ...)

// Logging without tagging. Overwrite outputs cvar flags value
stock void SMLogEx(SMLOutFlags outFlags, const char[] formatany ...)

// Respects logger convars. Converts channelFlags to tag.
stock void SMLogTag(int channelFlags, const char[] formatany ...)

// Uses SMLog behavior but overwrite outputs cvar flags value
stock void SMLogTagEx(int channelFlagsSMLOutFlags outFlags, const char[] formatany ...)

// Uses SMLog behavior but prints to specific client instead of all chat
stock void SMLogClient(int channelFlagsint client, const char[] formatany ...)

// 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_TAGSsizeof(LOG_TAGS), SML_CORE|SML_WARNSML_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 i3i++){
        
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 (
05x++){
            
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

__________________

Last edited by disawar1; 06-30-2019 at 16:02. Reason: added more examples
disawar1 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 13:46.


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