debug

package
v0.0.0-...-d2a29b3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2025 License: MIT Imports: 7 Imported by: 0

README

debug

This package provides utilities for debugging with conditional output and stacktraces.

Variables

  • Title string - Current debug title prefix

Functions

  • Print(message ...any) - Prints debug message if debug mode enabled
  • Println(message ...any) - Prints debug message with newline
  • SetTitle(title string) - Sets debug message title prefix
  • GetTitle() string - Gets current debug title
  • ResetTitle() - Resets title to default
  • SetDebug(enabled bool) - Toggles debug mode
  • GetDebug() bool - Gets debug mode status
  • SetStacktrace(enabled bool) - Toggles stacktrace mode
  • GetStacktrace() bool - Gets stacktrace mode status
  • Suicide(timeout int) - Self-destructs after timeout if suicide mode enabled

Flags

  • -debug - Enable debug mode (or set DEBUG=true env var)
  • -stacktrace - Enable stacktraces (or set STACKTRACE=true env var)
  • -suicide - Enable suicide mode (or set SUICIDE=true env var)

Example

import "github.com/Merith-TK/utils/pkg/debug"

debug.SetDebug(true)
debug.SetTitle("MyApp")
debug.Print("Starting application")
debug.Println("Configuration loaded")
defer debug.ResetTitle()

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

View Source
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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL