| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
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 withconsole.log,traceWarn*withconsole.warn, andtraceError*withconsole.error.*Showvariants accept anyShow-able value instead of a string.*Idvariants return the traced value itself.*Withvariants trace the result of applying a function to the value.*Mvariants trace inside anApplicative(e.g. miso'sEffectmonad, orIO).
See also
- Debug.Trace — the
baseequivalent, on which this API is modeled - Miso.FFI —
consoleLog,consoleWarn,consoleError
Synopsis
- trace :: ToMisoString s => s -> a -> a
- traceId :: ToMisoString s => s -> s
- traceWith :: ToMisoString s => (a -> s) -> a -> a
- traceShow :: Show a => a -> b -> b
- traceShowId :: Show a => a -> a
- traceShowWith :: Show b => (a -> b) -> a -> a
- traceM :: (ToMisoString s, Applicative f) => s -> f ()
- traceShowM :: (Show a, Applicative f) => a -> f ()
- traceError :: ToMisoString s => s -> a -> a
- traceErrorId :: ToMisoString s => s -> s
- traceErrorWith :: ToMisoString s => (a -> s) -> a -> a
- traceErrorShow :: Show a => a -> b -> b
- traceErrorShowId :: Show a => a -> a
- traceErrorShowWith :: Show b => (a -> b) -> a -> a
- traceErrorM :: (ToMisoString s, Applicative f) => s -> f ()
- traceErrorShowM :: (Show a, Applicative f) => a -> f ()
- traceWarn :: ToMisoString s => s -> a -> a
- traceWarnId :: ToMisoString s => s -> s
- traceWarnWith :: ToMisoString s => (a -> s) -> a -> a
- traceWarnShow :: Show a => a -> b -> b
- traceWarnShowId :: Show a => a -> a
- traceWarnShowWith :: Show b => (a -> b) -> a -> a
- traceWarnM :: (ToMisoString s, Applicative f) => s -> f ()
- traceWarnShowM :: (Show a, Applicative f) => a -> f ()
- traceTo :: ToMisoString s => (MisoString -> IO ()) -> s -> a -> a
Logging (console.log)
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 #
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.
Arguments
| :: Show a | |
| => a | Value to log |
| -> b | Value to return |
| -> b |
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.
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 #
Errors (console.error)
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.
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.
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.
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)
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.
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.
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.
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
Arguments
| :: ToMisoString s | |
| => (MisoString -> IO ()) | Console function to log with, e.g. |
| -> 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.