miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Trace

Description

Overview

Miso.Trace provides functions for tracing values to the browser's developer console, in the spirit of Debug.Trace from base. Where Debug.Trace writes to stderr, these functions write to the browser console using console.log, console.warn and console.error, gaining the browser's affordances such as severity filtering and stack traces.

The motivation is debugging pure code: places where IO is unavailable or inconvenient, such as a miso application's view function or pure helpers called from update.

Like Debug.Trace, these functions are implemented with unsafePerformIO and are not referentially transparent: they are meant only as a debugging aid and should not be used in production code. Since Haskell is lazily evaluated, a trace fires when (and only when) the traced expression is forced, so messages can appear out of order, once, or not at all.

Naming conventions

The functions follow the naming conventions of Debug.Trace:

  • trace* functions log with console.log, traceWarn* with console.warn, and traceError* with console.error.
  • *Show variants accept any Show-able value instead of a string.
  • *Id variants return the traced value itself.
  • *With variants trace the result of applying a function to the value.
  • *M variants trace inside an Applicative (e.g. miso's Effect monad, or IO).

See also

Synopsis

Logging (console.log)

trace Source #

Arguments

:: ToMisoString s 
=> s

Message to log

-> a

Value to return

-> a 

Outputs a message to the browser console with console.log when the result is forced, then returns the second argument. The browser analogue of trace.

traceId :: ToMisoString s => s -> s Source #

Like trace, but returns the message itself: traceId x = trace x x.

traceWith Source #

Arguments

:: ToMisoString s 
=> (a -> s)

Function producing the message from the value

-> a

Value to trace and return

-> a 

Traces the result of applying a function to a value, then returns the original value. Useful for logging a projection of a larger structure while leaving the structure untouched.

traceShow Source #

Arguments

:: Show a 
=> a

Value to log

-> b

Value to return

-> b 

Like trace, but accepts any Show-able value as the message. The browser analogue of traceShow.

traceShowId :: Show a => a -> a Source #

Shows and traces a value, then returns it. Convenient to wrap around any sub-expression you want to inspect without restructuring the code.

traceShowWith Source #

Arguments

:: Show b 
=> (a -> b)

Function producing the value to show from the value

-> a

Value to trace and return

-> a 

Traces the show-n result of applying a function to a value, then returns the original value.

traceM :: (ToMisoString s, Applicative f) => s -> f () Source #

Traces a message in an Applicative context, such as miso's Effect monad or IO. The browser analogue of traceM.

traceShowM :: (Show a, Applicative f) => a -> f () Source #

Like traceM, but accepts any Show-able value. Useful for logging every action that flows through an update function.

Errors (console.error)

traceError Source #

Arguments

:: ToMisoString s 
=> s

Message to log

-> a

Value to return

-> a 

Like trace, but logs with console.error, which browsers render prominently (typically in red, with an expandable stack trace).

traceErrorId :: ToMisoString s => s -> s Source #

Like traceId, but logs with console.error.

traceErrorWith Source #

Arguments

:: ToMisoString s 
=> (a -> s)

Function producing the message from the value

-> a

Value to trace and return

-> a 

Like traceWith, but logs with console.error.

traceErrorShow Source #

Arguments

:: Show a 
=> a

Value to log

-> b

Value to return

-> b 

Like traceShow, but logs with console.error.

traceErrorShowId :: Show a => a -> a Source #

Like traceShowId, but logs with console.error.

traceErrorShowWith Source #

Arguments

:: Show b 
=> (a -> b)

Function producing the value to show from the value

-> a

Value to trace and return

-> a 

Like traceShowWith, but logs with console.error.

traceErrorM :: (ToMisoString s, Applicative f) => s -> f () Source #

Like traceM, but logs with console.error.

traceErrorShowM :: (Show a, Applicative f) => a -> f () Source #

Like traceShowM, but logs with console.error.

Warnings (console.warn)

traceWarn Source #

Arguments

:: ToMisoString s 
=> s

Message to log

-> a

Value to return

-> a 

Like trace, but logs with console.warn, which browsers render as a warning (typically in yellow) and can be filtered by severity.

traceWarnId :: ToMisoString s => s -> s Source #

Like traceId, but logs with console.warn.

traceWarnWith Source #

Arguments

:: ToMisoString s 
=> (a -> s)

Function producing the message from the value

-> a

Value to trace and return

-> a 

Like traceWith, but logs with console.warn.

traceWarnShow Source #

Arguments

:: Show a 
=> a

Value to log

-> b

Value to return

-> b 

Like traceShow, but logs with console.warn.

traceWarnShowId :: Show a => a -> a Source #

Like traceShowId, but logs with console.warn.

traceWarnShowWith Source #

Arguments

:: Show b 
=> (a -> b)

Function producing the value to show from the value

-> a

Value to trace and return

-> a 

Like traceShowWith, but logs with console.warn.

traceWarnM :: (ToMisoString s, Applicative f) => s -> f () Source #

Like traceM, but logs with console.warn.

traceWarnShowM :: (Show a, Applicative f) => a -> f () Source #

Like traceShowM, but logs with console.warn.

Generalized tracing

traceTo Source #

Arguments

:: ToMisoString s 
=> (MisoString -> IO ())

Console function to log with, e.g. consoleLog

-> s

Message to log

-> a

Value to return

-> a 

The generalized tracing combinator underlying this module: traces via the given console function from Miso.FFI. Every other function here is defined in terms of it.