AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   logdebug.inc - Simple debug logging (https://forums.alliedmods.net/showthread.php?t=258855)

Dr. McKay 02-26-2015 00:52

logdebug.inc - Simple debug logging
 
1 Attachment(s)
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.

TnTSCS 02-26-2015 10:49

Re: logdebug.inc - Simple debug logging
 
Awesome work McKay - I will incorporate this into my plugins from now on since I usually add debug options anyways.

Thank you!

Chdata 02-26-2015 12:06

Re: logdebug.inc - Simple debug logging
 
Can you make it not compile altogether without if defined wrappers around everything?

Wliu 02-26-2015 13:04

Re: logdebug.inc - Simple debug logging
 
Nice, thanks. I'll probably get rid of FF2's debug functions for this.

Dr. McKay 02-27-2015 01:54

Re: logdebug.inc - Simple debug logging
 
Quote:

Originally Posted by Chdata (Post 2266818)
Can you make it not compile altogether without if defined wrappers around everything?

I don't understand the question.

ddhoward 02-27-2015 04:51

Re: logdebug.inc - Simple debug logging
 
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. :)

Chdata 02-27-2015 09:57

Re: logdebug.inc - Simple debug logging
 
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.

ddhoward 02-27-2015 10:39

Re: logdebug.inc - Simple debug logging
 
Quote:

Originally Posted by Chdata (Post 2267240)
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.

Dr. McKay 02-27-2015 12:34

Re: logdebug.inc - Simple debug logging
 
Quote:

Originally Posted by ddhoward (Post 2267125)
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 (Post 2267240)
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.

Powerlord 02-27-2015 13:37

Re: logdebug.inc - Simple debug logging
 
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.


All times are GMT -4. The time now is 04:06.

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