Documentation
¶
Overview ¶
Package debug provides comprehensive debugging utilities including conditional logging, stacktrace output, and self-destruct functionality for development and testing.
The package supports multiple debugging modes:
- Debug mode: Enables debug output with optional custom titles
- Stacktrace mode: Includes filtered stack traces in debug output
- Suicide mode: Allows processes to self-terminate after a timeout
Configuration can be done via command-line flags or environment variables:
- -debug or DEBUG=true: Enable debug output
- -stacktrace or STACKTRACE=true: Enable stacktrace output
- -suicide or SUICIDE=true: Enable suicide mode
Example usage:
debug.SetDebug(true) debug.SetTitle("MYAPP") debug.Print("This is a debug message") debug.Suicide(30) // Self-destruct after 30 seconds if suicide mode enabled
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Title is the current debug title prefix. Title string = defaultTitle )
Functions ¶
func GetDebug ¶
func GetDebug() bool
GetDebug returns true if debug mode is currently enabled, either through flags, environment variables, or SetDebug calls.
func GetStacktrace ¶
func GetStacktrace() bool
GetStacktrace returns true if stacktrace output is currently enabled for debug messages, either through flags, environment variables, or SetStacktrace calls.
func GetTitle ¶
func GetTitle() string
GetTitle returns the currently set debug message title prefix. Returns an empty string if no custom title has been set.
func Print ¶
func Print(message ...any)
Print outputs the given message to standard output if debug mode is enabled. Messages are prefixed with [DEBUG] and optionally include a custom title if set. If stacktrace mode is also enabled, a filtered stack trace is included that excludes internal debug package frames for cleaner output.
func Println ¶
func Println(message ...any)
Println prints the given message to the standard output followed by a newline character, but only if debug mode is enabled. This is a convenience wrapper around Print that automatically adds a newline. The message is prefixed with [DEBUG] and optional title.
func ResetTitle ¶
func ResetTitle()
ResetTitle resets the debug message title prefix to the default empty value. This should be called to clean up after using SetTitle, typically with defer.
func SetDebug ¶
func SetDebug(enabled bool)
SetDebug programmatically enables or disables debug mode, overriding any command-line flag or environment variable settings.
func SetStacktrace ¶
func SetStacktrace(enabled bool)
SetStacktrace programmatically enables or disables stacktrace output in debug messages, overriding any command-line flag or environment variable settings.
func SetTitle ¶
func SetTitle(title string)
SetTitle sets the title of the debug message. Note: this is set globally and will affect all debug messages. It is recommended to use this function at the start of a function followed by defer debug.ResetTitle().
func Suicide ¶
func Suicide(timeout int)
Suicide enables a self-destruct mechanism that will terminate the process after the specified timeout in seconds, but only if suicide mode is enabled via flag or environment variable. This is primarily used for testing and development to prevent runaway processes. The function is non-blocking and starts a goroutine to handle the timeout.
Types ¶
This section is empty.