Raised This Month: $23 Target: $400
 5% 

logdebug.inc - Simple debug logging


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 02-26-2015 , 00:52   logdebug.inc - Simple debug logging
Reply With Quote #1

This is a simple include file which provides a LogDebug stock intended for use in debugging. Just #include it, call InitDebugLog, and use LogDebug wherever you desire. Requires SM 1.7 or later to compile and run.

PHP Code:
/**
 * Inits debug logging. You must call this in OnPluginStart().
 * 
 * @param convarName        A name to use for the cvar which controls debug log output. Also used as filename for logfile.
 * @param debugTag            Tag to prepend to messages, without []. Max 10 characters.
 * @param adminFlag            One or more admin flagbits which define whether a user is an "admin". If you pass multiple flags, users will need ALL flags.
 * @noreturn
 */
stock void InitDebugLog(const char[] convarName, const char[] debugTagint adminFlags ADMFLAG_GENERIC)

/**
 * Logs a message to all enabled debug output points
 * 
 * @param format        Message text with formatting tokens
 * @param ...            Variable number of format parameters
 * @return                true if message was output to at least one place
 */
stock bool LogDebug(const char[] formatany ...)

/**
 * Returns a bitstring containing bits enabled for each output location (see DEBUG_ constants)
 * 
 * @return                bitstring for enabled outputs
 */
stock int GetDebugOutputs() 
At runtime, you can add up the numbers associated with each output method and set the result in the cvar created with the name you specified.

PHP Code:
#define DEBUG_SERVER_CONSOLE        1     /**< Message will be routed to server console */
#define DEBUG_CLIENT_CONSOLE        2     /**< Message will be routed to all clients' consoles */
#define DEBUG_ADMIN_CONSOLE         4     /**< Message will be routed to consoles of admins with a flag specified by plugin */
#define DEBUG_CLIENT_CHAT           8     /**< Message will be routed to all clients' chat boxes (and consequently consoles) */
#define DEBUG_ADMIN_CHAT            16    /**< Message will be routed to chat boxes of admins with a flag specified by plugin */
#define DEBUG_LOG_FILE              32    /**< Message will be routed to plugin's debug log */ 
The cvar will be created with FCVAR_DONTRECORD so as to not clutter your AutoExecConfig.

If you define NO_DEBUG before #including the file, all functionality will be disabled. No cvar will be created and LogDebug will always return false.
Attached Files
File Type: inc logdebug.inc (4.9 KB, 144 views)
__________________

Last edited by Dr. McKay; 02-27-2015 at 14:11.
Dr. McKay is offline
TnTSCS
AlliedModders Donor
Join Date: Oct 2010
Location: Undisclosed...
Old 02-26-2015 , 10:49   Re: logdebug.inc - Simple debug logging
Reply With Quote #2

Awesome work McKay - I will incorporate this into my plugins from now on since I usually add debug options anyways.

Thank you!
__________________
View my Plugins | Donate
TnTSCS is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 02-26-2015 , 12:06   Re: logdebug.inc - Simple debug logging
Reply With Quote #3

Can you make it not compile altogether without if defined wrappers around everything?
__________________
Chdata is offline
Wliu
Veteran Member
Join Date: Apr 2013
Location: N/A
Old 02-26-2015 , 13:04   Re: logdebug.inc - Simple debug logging
Reply With Quote #4

Nice, thanks. I'll probably get rid of FF2's debug functions for this.
__________________
~Wliu
Wliu is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 02-27-2015 , 01:54   Re: logdebug.inc - Simple debug logging
Reply With Quote #5

Quote:
Originally Posted by Chdata View Post
Can you make it not compile altogether without if defined wrappers around everything?
I don't understand the question.
__________________
Dr. McKay is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-27-2015 , 04:51   Re: logdebug.inc - Simple debug logging
Reply With Quote #6

Haven't tested it, but it doesn't seem like g_DebugAdminFlag is ever set to what's specified in InitDebugLog() ?

Also it would be really awesome if it could also accept an override, falling back to the flag if not present.
__________________

Last edited by ddhoward; 02-27-2015 at 04:53.
ddhoward is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 02-27-2015 , 09:57   Re: logdebug.inc - Simple debug logging
Reply With Quote #7

Code:
#if !defined NO_DEBUG
    stock bool LogDebug(const char[] format, any ...)
#else
    define LogDebug(%1)
#endif
Something that altogether makes the code not be included into the compilation when it compiles.
__________________
Chdata is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-27-2015 , 10:39   Re: logdebug.inc - Simple debug logging
Reply With Quote #8

Quote:
Originally Posted by Chdata View Post
Code:
#if !defined NO_DEBUG
    stock bool LogDebug(const char[] format, any ...)
#else
    define LogDebug(%1)
#endif
Something that altogether makes the code not be included into the compilation when it compiles.
PHP Code:
// define NO_DEBUG before including this file to completely disable all debugging
#if defined NO_DEBUG
 
stock void InitDebugLog(const char[] convarName, const char[] debugTagint adminFlag) { }
 
stock bool LogDebug(const char[] formatany ...) { return false; }
 
#endinput
#endif 
That is already a feature.
__________________
ddhoward is offline
Dr. McKay
Sir Dr. SourceMod Plugin Approver Esq. Ltd. M.D. PhD
Join Date: Aug 2011
Location: Atlantis
Old 02-27-2015 , 12:34   Re: logdebug.inc - Simple debug logging
Reply With Quote #9

Quote:
Originally Posted by ddhoward View Post
Haven't tested it, but it doesn't seem like g_DebugAdminFlag is ever set to what's specified in InitDebugLog() ?

Also it would be really awesome if it could also accept an override, falling back to the flag if not present.
Good catch. Fixed. Also added an override with the same name as the cvar.

Quote:
Originally Posted by Chdata View Post
Code:
#if !defined NO_DEBUG
    stock bool LogDebug(const char[] format, any ...)
#else
    define LogDebug(%1)
#endif
Something that altogether makes the code not be included into the compilation when it compiles.
I don't think that would be possible with LogDebug's variable number of parameters.
__________________
Dr. McKay is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-27-2015 , 13:37   Re: logdebug.inc - Simple debug logging
Reply With Quote #10

A few optional things you might want to do:
  • Make the variables in this static on the off chance that a plugin is already using variables with these names.
  • Check if char works for single characters instead of using an int. (int flagChar = '0'; looks weird to say the least).
  • adminFlag should be optional. After all, if you don't include DEBUG_ADMIN_CONSOLE or DEBUG_ADMIN_CHAT, it isn't used. Perhaps use ADMFLAG_GENERIC as the default value for adminFlag?
  • Adjust InitDebugLog to process multiple admin flags correctly or convert it to use AdminFlag types in its signature instead. Right now, the BitToFlag line looks like it's going to barf if I pass in ADMFLAG_KICK|ADMFLAG_BAN for example. This is perfectly acceptable to CheckCommandAccess and will only match if an admin has both flags.
__________________
Am I back? Well, we'll see.
Powerlord 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 10:17.


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