# `Untangle`
[🔗](https://github.com/bonfire-networks/untangle/blob/main/lib/untangle.ex#L1)

Logging/inspecting data, and timing functions, with code location information.

## Logging/inspecting
`Untangle` provides alternatives for `IO.inspect` and the macros in Elixir's `Logger` to output code location information. It also provides a polyfill for `dbg` which was introduce in Elixir 1.14

![screenshot](priv/screenshot.png "Screenshot showing sample logs (shown when running the library tests)")

The first argument is `inspect`ed and the second argument (if provided) is used as a label:

```
> import Untangle
Untangle
> debug(:no, "the answer is") # log at debug
11:19:09.915 [debug] [iex:2] the answer is: :no
:no
> dump(%{a: :map}, "it") # inspect something on stdout
[iex:3] it: %{a: :map}
%{a: :map}
```

When used in a code file, the location information becomes more useful, e.g.:

```
[lib/test_untangle.ex:15@Test.Untangle.example/2] Here's an empty list: []
```

You may also notice from the iex output that it returns its first argument. This makes it ideal for inserting into a pipeline for debugging purposes:

```elixir
do_something()
|> debug("output of do_something/0")
```

When you are done debugging something, the location of the debug statement is already in the output so you know where to remove it, comment it out, or simply change `warn` or `info` for `debug` if you only need it during development :-)

## Timing functions
You can decorate functions to measure and log their execution time:

```
use Untangle

@decorate time()
def fun(), do: :stuff
```

will output something like `[info] Time to run MyModule.fun/0: 1 ms`

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed by adding `untangle` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:untangle, "~> 0.5"}
  ]
end
```

and adding some config:

```elixir
config :untangle, 
  env: config_env()
```

# Configure as default `dbg/2` handler

## In config/config.exs
`config :elixir, :dbg_callback, {Untangle, :custom_dbg, []}`

# Docs

The docs can be found at [https://hexdocs.pm/untangle](https://hexdocs.pm/untangle).

## Copyright and License

Copyright (c) 2022 Bonfire contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

# `custom_dbg`

Custom backend for `Kernel.dbg/2`.
This function provides a backend for `Kernel.dbg/2`.
This function:
  * may log or print information about the given `env`
  * may log or print information about `code` and its returned value (using `opts` to inspect terms)
  * returns the value returned by evaluating `code`

# `debug`
*macro* 

Like `dump`, but for logging at debug level

# `dump`
*macro* 

IO.inspect but outputs to Logger with position information, an optional label and configured not to truncate output too much.

# `err`

Logs or raises errors based on environment.

This function handles errors differently depending on the environment:
- In test: raises an exception
- In dev: prints a warning
- In production: logs a warning

## Examples

    # With just a message
    iex> # When in dev/prod (not test), prints a warning and does not raise
    iex> # Note: Only testing return value here, not side effects
    iex> Process.put([:bonfire, :env], :dev)
    iex> err("error message")
    # Prints: [warning] error message
    nil

    # With just data
    iex> Process.put([:bonfire, :env], :dev)
    iex> err(%{key: "value"})
    # Prints: [warning] An error occurred: %{key: "value"}
    %{key: "value"}

    # With both data and message
    iex> Process.put([:bonfire, :env], :dev)
    iex> err(%{key: "value"}, "Custom error message")
    # Prints: [warning] Custom error message: %{key: "value"}
    %{key: "value"}

In test environment, it raises an exception:

    iex> Process.put([:bonfire, :env], :test)
    iex> err("test error")
    ** (RuntimeError) test error

# `err`

# `error`
*macro* 

Similar to `dump`, but for logging at error level, and returns an error tuple:
- an error tuple with the label, if any
- an error tuple with the passed value otherwise

  iex> error(:value)
  ### [error] :value
  {:error, :value}

  iex> error({:error, :value})
  ### [error] :value
  {:error, :value}

  iex> error(:value, "with label")
  ### [error] with label: :value
  {:error, "with label"}

  iex> error({:error, :value}, "with label")
  ### [error] with label: :value
  {:error, "with label"}

# `flood`

Like `IO.inspect/2`, but accepts a string as second argument and does not truncate the output

# `flood`

# `format_application_location`

# `format_location`

# `format_stacktrace`

```elixir
@spec format_stacktrace(Exception.stacktrace() | nil) :: String.t()
```

Formats the stacktrace.

A stacktrace must be given as an argument. If not, the stacktrace
is retrieved from `Process.info/2`.

# `format_stacktrace_entry`

```elixir
@spec format_stacktrace_entry(Exception.stacktrace_entry()) :: String.t()
```

Receives a stacktrace entry and formats it into a string.

# `format_stacktrace_label`

# `format_stacktrace_sliced`

# `get_current_stacktrace`

# `info`
*macro* 

Like `dump`, but for logging at info level

# `io_inspect`

# `io_inspect`

# `log_enabled?`

Whether a message at `level` would actually be logged (vs discarded by the current minimum level). Use to skip expensive work that only feeds a log line.

# `log_or_flood`

# `maybe_dbg`
*macro* 

Like `debug`, but will do nothing unless the `:debug` option is truthy

# `maybe_info`
*macro* 

Like `maybe_dbg`, but requires the `:verbose` option to be set. Intended for large outputs.

# `module_name`

# `smart`
*macro* 

Tries to 'do what i mean'. Requires the `debug` option to be set regardless. If `verbose` is also
set, will inspect else will attempt to print some (hopefully smaller) type-dependent summary of
the data (list length, map keys).

# `to_io?`

# `warn`
*macro* 

Like `dump`, but for logging at warn level

# `warner`

# `warner`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
