tracing

moon add Yu-zh/tracing@0.1.0
Download zip
Author
Version
0.1.0
License
Apache-2.0
Last updated
6 months ago
Downloads
20

Dependencies

README

#Yu-zh/tracing

A tracing module for MoonBit that provides logging and Chrome trace event generation.

#Features

  • debug/info logging with optional stdout output
  • Chrome trace format (JSON) for visualization in Perfetto
  • scope function to track function execution as duration events

#Usage

async fn main {
@tracing.setup(stdout=true, chrome=Some("trace.json"))

@tracing.scope("main", fn() {
@tracing.debug("starting")
do_work()
@tracing.info("done")
})

@tracing.close()
}

#API

// Initialize tracing
// - stdout: print log messages to stdout
// - chrome: write Chrome trace events to file
pub fn setup(stdout~ : Bool = false, chrome~ : String? = None) -> Unit

// Log messages (instant events)
pub fn debug(msg : String) -> Unit
pub fn info(msg : String) -> Unit

// Track function execution (duration events)
pub fn[T] scope(name : String, f : () -> T) -> T
pub async fn[T] scope_async(name : String, f : async () -> T) -> T

// Write trace file and cleanup
pub async fn close() -> Unit

#Chrome Trace Format

The generated JSON can be opened in Perfetto:

  • Instant events (ph:"i") for debug/info logs
  • Complete events (ph:"X") for scope durations

Example output:
[ {"pid":0, "name":"debug: starting", "ts":5, "ph":"i", "s":"g"}, {"pid":0, "name":"info: done", "ts":100, "ph":"i", "s":"g"}, {"pid":0, "name":"main", "ts":0, "tid":0, "ph":"X", "dur":105} ]

#Requirements

Native backend only (requires C FFI for monotonic time).

#
close

async fn close() -> Unit

Finalize and write the trace file.

#
debug

fn debug(msg : String) -> Unit

Log a debug message.

#
info

fn info(msg : String) -> Unit

Log an info message.

#
scope

fn[T] scope(name : String, f : () -> T raise?) -> T raise?

Execute a sync function and record its duration as a Chrome trace event.

#
scope_async

async fn[T] scope_async(name : String, f : async () -> T) -> T

Execute an async function and record its duration as a Chrome trace event.

#
setup

fn setup(stdout? : Bool, chrome? : String?) -> Unit

Initialize the tracing system.
  • stdout: if true, log messages are printed to stdout
  • chrome: if Some(path), Chrome trace events are written to the file

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io