Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

debug.gno

// Package debug provides utilities for logging and displaying debug information
// within Gno realms. It supports conditional rendering of logs and metadata,
// toggleable via query parameters.
//
// Key Features:
// - Log collection and display using Markdown formatting.
// - Metadata display for realm path, address, and height.
// - Collapsible debug section for cleaner presentation.
// - Query-based debug toggle using `?debug=1`.
package debug

import (
	"chain/runtime"
	"chain/runtime/unsafe"
	"time"

	"gno.land/p/nym-moultest123/md/v0"
	"gno.land/p/nym-moultest123/mdtable/v0"
	"gno.land/p/nym-moultest123/realmpath/v0"
	"gno.land/p/nt/ufmt/v0"
)

// Debug encapsulates debug information, including logs and metadata.
type Debug struct {
	Logs         []string
	HideMetadata bool
}

// Log appends a new line of debug information to the Logs slice.
func (d *Debug) Log(line string) {
	d.Logs = append(d.Logs, line)
}

// Render generates the debug content as a collapsible Markdown section.
// It conditionally renders logs and metadata if enabled via the `?debug=1` query parameter.
func (d Debug) Render(path string) string {
	if realmpath.Parse(path).Query.Get("debug") != "1" {
		return ""
	}

	var content string

	if d.Logs != nil {
		content += md.H3("Logs")
		content += md.BulletList(d.Logs)
	}

	if !d.HideMetadata {
		content += md.H3("Metadata")
		table := mdtable.Table{
			Headers: []string{"Key", "Value"},
		}
		table.Append([]string{"`std.CurrentRealm().PkgPath()`", string(unsafe.CurrentRealm().PkgPath())})
		table.Append([]string{"`std.CurrentRealm().Address()`", string(unsafe.CurrentRealm().Address())})
		table.Append([]string{"`std.PreviousRealm().PkgPath()`", string(unsafe.PreviousRealm().PkgPath())})
		table.Append([]string{"`std.PreviousRealm().Address()`", string(unsafe.PreviousRealm().Address())})
		table.Append([]string{"`std.ChainHeight()`", ufmt.Sprintf("%d", runtime.ChainHeight())})
		table.Append([]string{"`time.Now().Format(time.RFC3339)`", time.Now().Format(time.RFC3339)})
		content += table.String()
	}

	if content == "" {
		return ""
	}

	return md.CollapsibleSection("debug", content)
}

// Render displays metadata about the current realm but does not display logs.
// This function uses a default Debug struct with metadata enabled and no logs.
func Render(path string) string {
	return Debug{}.Render(path)
}

// IsEnabled checks if the `?debug=1` query parameter is set in the given path.
// Returns true if debugging is enabled, otherwise false.
func IsEnabled(path string) bool {
	req := realmpath.Parse(path)
	return req.Query.Get("debug") == "1"
}

// ToggleURL modifies the given path's query string to toggle the `?debug=1` parameter.
// If debugging is currently enabled, it removes the parameter.
// If debugging is disabled, it adds the parameter.
func ToggleURL(path string) string {
	req := realmpath.Parse(path)
	if IsEnabled(path) {
		req.Query.Del("debug")
	} else {
		req.Query.Add("debug", "1")
	}
	return req.String()
}

debug_test.gno

package debug

import (
	"strings"
	"testing"

	"gno.land/p/nt/uassert/v0"
)

func TestPackage(t *testing.T) {
	testing.SetRealm(testing.NewUserRealm("g1user"))

	testPackage(t)
}

func testPackage(t *testing.T) {
	testing.SetRealm(testing.NewCodeRealm("gno.land/r/test/test"))

	// no debug
	got := Render("")
	expected := ``
	uassert.Equal(t, expected, got)

	// debug without logs
	got = Render("?debug=1")
	expected = `<details><summary>debug</summary>

### Metadata
| Key | Value |
| --- | --- |
| ±std.CurrentRealm().PkgPath()± | gno.land/r/test/test |
| ±std.CurrentRealm().Address()± | g1z7fga7u94pdmamlvcrtvsfwxgsye0qv3rres7n |
| ±std.PreviousRealm().PkgPath()± |  |
| ±std.PreviousRealm().Address()± | g1user |
| ±std.ChainHeight()± | 123 |
| ±time.Now().Format(time.RFC3339)± | 2009-02-13T23:31:30Z |

</details>
`
	expected = strings.ReplaceAll(expected, "±", "`")

	println("###################")
	println(got)
	println("###################")
	println(expected)
	println("###################")

	uassert.Equal(t, expected, got)

	// debug with logs
	var d Debug
	d.Log("hello world!")
	d.Log("foobar")
	got = d.Render("?debug=1")
	expected = `<details><summary>debug</summary>

### Logs
- hello world!
- foobar
### Metadata
| Key | Value |
| --- | --- |
| ±std.CurrentRealm().PkgPath()± | gno.land/r/test/test |
| ±std.CurrentRealm().Address()± | g1z7fga7u94pdmamlvcrtvsfwxgsye0qv3rres7n |
| ±std.PreviousRealm().PkgPath()± |  |
| ±std.PreviousRealm().Address()± | g1user |
| ±std.ChainHeight()± | 123 |
| ±time.Now().Format(time.RFC3339)± | 2009-02-13T23:31:30Z |

</details>
`
	expected = strings.ReplaceAll(expected, "±", "`")
	uassert.Equal(t, got, expected)
}

gnomod.toml

module = "gno.land/p/nym-moultest123/debug/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186409
  max_deposit = "5000000ugnot"