Skip to main content

src/version_bump@logging.erl

-module(version_bump@logging).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/version_bump/logging.gleam").
-export([format/2, log/2, info/1, warn/1, error/1, success/1]).
-export_type([level/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(
    " A tiny leveled logger for the release pipeline.\n"
    "\n"
    " Mirrors semantic-release's use of a prefixed, leveled signale/console\n"
    " logger. Each message is printed to stdout with a `[version_bump]` prefix and a\n"
    " colorized level tag. Logging is a side effect, so every function returns\n"
    " `Nil`.\n"
).

-type level() :: info | warn | err | success | debug.

-file("src/version_bump/logging.gleam", 54).
-spec tag(level()) -> binary().
tag(Level) ->
    case Level of
        info ->
            gleam_community@ansi:cyan(<<"info"/utf8>>);

        warn ->
            gleam_community@ansi:yellow(<<"warn"/utf8>>);

        err ->
            gleam_community@ansi:red(<<"error"/utf8>>);

        success ->
            gleam_community@ansi:green(<<"success"/utf8>>);

        debug ->
            gleam_community@ansi:dim(<<"debug"/utf8>>)
    end.

-file("src/version_bump/logging.gleam", 50).
?DOC(
    " Render a level + message into a single colorized, prefixed line. Pure so it\n"
    " can be unit-tested without producing side effects.\n"
).
-spec format(level(), binary()) -> binary().
format(Level, Msg) ->
    <<<<<<<<"[version_bump]"/utf8, " "/utf8>>/binary, (tag(Level))/binary>>/binary,
            " "/utf8>>/binary,
        Msg/binary>>.

-file("src/version_bump/logging.gleam", 24).
?DOC(
    " Log a message at the given level. This is the single primitive that the\n"
    " convenience helpers below delegate to.\n"
).
-spec log(level(), binary()) -> nil.
log(Level, Msg) ->
    gleam_stdlib:println(format(Level, Msg)).

-file("src/version_bump/logging.gleam", 29).
?DOC(" Log an informational message.\n").
-spec info(binary()) -> nil.
info(Msg) ->
    log(info, Msg).

-file("src/version_bump/logging.gleam", 34).
?DOC(" Log a warning.\n").
-spec warn(binary()) -> nil.
warn(Msg) ->
    log(warn, Msg).

-file("src/version_bump/logging.gleam", 39).
?DOC(" Log an error.\n").
-spec error(binary()) -> nil.
error(Msg) ->
    log(err, Msg).

-file("src/version_bump/logging.gleam", 44).
?DOC(" Log a success message.\n").
-spec success(binary()) -> nil.
success(Msg) ->
    log(success, Msg).