Debug Package
The internal/debug package provides debug logging functionality for gcon.
Usage
Enable debug logging by passing the --debug flag when running gcon:
./gcon --debug
This creates a gcon-debug.log file in the current working directory. The log file is synchronized after every write, which impacts performance but ensures logs are captured even if the application crashes.
API
EnableDebug() error
Enables debug logging and creates the log file. Returns an error if the file cannot be created.
if err := debug.EnableDebug(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to enable debug: %v\n", err)
}
Close() error
Closes the debug log file and disables logging. Safe to call multiple times.
defer debug.Close()
IsEnabled() bool
Returns true if debug logging is currently enabled.
if debug.IsEnabled() {
// Do expensive debug work
}
Writes a formatted message to the debug log. No-op if debug is disabled.
debug.Log("Processing item: %s (count=%d)", item.Name, item.Count)
LogView(name string, content string)
Logs information about a rendered view, useful for debugging layout issues in Bubble Tea components.
content := myView.View()
debug.LogView("MyView", content)
Debug logging is slow because:
- Every log message is written to disk immediately (no buffering)
- Each write is followed by an explicit
Sync() call to flush OS buffers
- This is intentional to ensure logs are captured during crashes
Only use --debug when actively troubleshooting issues. Do not enable it in production or for normal usage.
Log Location
The log file is created in the current working directory as gcon-debug.log, not in /tmp. This makes it easy to find after the application exits.
Testing
Run tests with:
go test ./internal/debug/... -v
The tests verify:
- Log file creation in current directory
- Logging when enabled/disabled
- Multiple log writes
- View logging functionality
- Proper cleanup with Close()